Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions tinyalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,36 @@ size_t ta_num_fresh() {
bool ta_check() {
return heap_max_blocks == ta_num_free() + ta_num_used() + ta_num_fresh();
}

static size_t ta_getsize(void *ptr)
{
Block *block = heap->used;
while (block != NULL) {
if (ptr == block->addr) {
return block->size;
}
block = block->next;
}
return 0;
}

void *ta_realloc(void *ptr,size_t num) {
size_t c;
size_t ptrsize;
uint8_t* ptrn;
uint8_t* ptro;
ptrsize=ta_getsize(ptr);
if(ptrsize>0){
Block *block = alloc_block(num);
if (block != NULL) {
ptro=(uint8_t*)ptr;
ptrn=(uint8_t*)block->addr;
if(ptrsize>num) ptrsize=num;
for(c=0;c<ptrsize;c++)
*(ptrn+c)=*(ptro+c);
ta_free(ptr);
return block->addr;
}
}
return NULL;
}
1 change: 1 addition & 0 deletions tinyalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ bool ta_init(const void *base, const void *limit, const size_t heap_blocks, cons
void *ta_alloc(size_t num);
void *ta_calloc(size_t num, size_t size);
bool ta_free(void *ptr);
void *ta_realloc(void *ptr,size_t num);

size_t ta_num_free();
size_t ta_num_used();
Expand Down