38 lines
967 B
C
38 lines
967 B
C
#include "alloc.h"
|
|
#include "string.h"
|
|
#include <stdint.h>
|
|
|
|
// Prototype for kfree and umalloc
|
|
void kfree(void *ptr);
|
|
void *umalloc(uint32_t size);
|
|
|
|
/*
|
|
* ptr: block to resize (undefined behavior if invalid)
|
|
* size: size needed by the user to get vreallocated
|
|
*
|
|
* If we have a size <= to the previous size, we don't have
|
|
* to do anything, we just change sub_size for info purposes
|
|
* and return the same pointer
|
|
* Else, we allocate a new block and copy the content of
|
|
* the previous block in the new one and kfree the old block
|
|
*
|
|
* ptr: returns the aligned pointer of the vreallocated block
|
|
*/
|
|
void *vrealloc(void *ptr, uint32_t size)
|
|
{
|
|
void *new_ptr = NULL;
|
|
if (ptr == NULL)
|
|
return NULL;
|
|
Block *block = (Block *)((uint32_t)ptr - sizeof(Block));
|
|
if (block->size >= size) {
|
|
block->sub_size = size;
|
|
return (ptr);
|
|
}
|
|
new_ptr = umalloc(size);
|
|
if (new_ptr == NULL)
|
|
return NULL;
|
|
memmove(new_ptr, ptr, block->size);
|
|
ufree(ptr);
|
|
return (new_ptr);
|
|
}
|