-
Notifications
You must be signed in to change notification settings - Fork 0
/
memBlock.cpp
38 lines (28 loc) · 961 Bytes
/
memBlock.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "memBlock.h"
memBlock* allocateMemBlock(const size_t cellSize ,const uint32_t nCell){
memBlock* res=(memBlock*)malloc(sizeof(memBlock));
res->capacity = nCell*cellSize;
res->data = malloc(nCell * cellSize);
return res;
}
memBlock* copyMemBlock(const memBlock*m){
memBlock* res = allocateMemBlock(1, m->capacity);
memcpy(res->data, m->data, m->capacity);
return res;
}
memBlock* reallocateMemBlock(memBlock* m, const size_t cellSize, const uint32_t newN){
m->data = (uint32_t*)realloc(m->data, newN * cellSize);
if(newN > m->capacity)
memset((char *)m->data+ m->capacity*cellSize, 0, cellSize*(newN - m->capacity));
m->capacity = newN;
return m;
}
void freeMemBlock(memBlock* b){
free(b->data);
free(b);
}
void memBlockprintAsString(const memBlock* b){
for(uint32_t i=0;i!=b->capacity-1;i++)
putchar(((char *)b->data)[i]);
putchar(((char *)b->data)[b->capacity-1]);
}