Skip to content

Add simple Realloc functions #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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