feat(allocator): implemented realloc function
This commit is contained in:
parent
79ea8dda64
commit
6e1533c943
3 changed files with 30 additions and 0 deletions
|
|
@ -32,6 +32,7 @@ void *hallocy_allocate(size_t size, bool zero_memory);
|
|||
|
||||
static inline void *hallocy_malloc(size_t size) { return hallocy_allocate(size, false); }
|
||||
static inline void *hallocy_calloc(size_t size, size_t count) { return hallocy_allocate(size * count, true); }
|
||||
void *hallocy_realloc(void *memory_pointer, size_t size);
|
||||
HallocyError hallocy_free(void *pointer);
|
||||
|
||||
#endif
|
||||
|
|
@ -229,6 +229,28 @@ void *hallocy_allocate(size_t size, bool zero_memory) {
|
|||
return (void*)(memory_pointer + 1);
|
||||
}
|
||||
|
||||
void *hallocy_realloc(void *memory_pointer, size_t size) {
|
||||
if (memory_pointer == NULL) {
|
||||
return hallocy_allocate(size, false);
|
||||
}
|
||||
|
||||
if (size == 0) {
|
||||
hallocy_free(memory_pointer);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
HallocyMemoryHeader *memory_header = ((HallocyMemoryHeader*)memory_pointer) - 1;
|
||||
if (memory_header->size - sizeof(HallocyMemoryHeader) >= size) {
|
||||
return memory_pointer;
|
||||
}
|
||||
|
||||
void *new_memory = hallocy_allocate(size, false);
|
||||
hallocy_copy_memory(new_memory, memory_pointer, memory_header->size);
|
||||
hallocy_free(memory_pointer);
|
||||
|
||||
return new_memory;
|
||||
}
|
||||
|
||||
HallocyError hallocy_free(void *pointer) {
|
||||
if (pointer == NULL) {
|
||||
return HALLOCY_ERROR_INVALID_POINTER;
|
||||
|
|
|
|||
|
|
@ -70,6 +70,13 @@
|
|||
printf("text_copy_copy and text_copy are equal\n");
|
||||
}
|
||||
|
||||
text_copy_copy = (char*)hallocy_realloc(text_copy_copy, 40);
|
||||
|
||||
hallocy_set_memory(text_copy_copy + 35, 'q', 4);
|
||||
text_copy_copy[39] = '\0';
|
||||
|
||||
printf("%s\n", text_copy_copy);
|
||||
|
||||
hallocy_free(text_copy_copy);
|
||||
hallocy_free(text_copy);
|
||||
hallocy_free(text);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue