mirror of
https://github.com/drasko/codezero.git
synced 2026-01-13 11:23:16 +01:00
Initial commit
This commit is contained in:
33
libs/c/src/realloc.c
Normal file
33
libs/c/src/realloc.c
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* K&R Malloc
|
||||
*
|
||||
* System specifc code should implement `more_core'
|
||||
*/
|
||||
|
||||
#include "k_r_malloc.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
void *
|
||||
realloc(void *ptr, size_t size)
|
||||
{
|
||||
Header *bp;
|
||||
void *new_ptr;
|
||||
size_t old_size;
|
||||
|
||||
if (ptr == NULL)
|
||||
return malloc(size);
|
||||
bp = (Header *) ptr - 1; /* point to block header */
|
||||
old_size = sizeof(Header) * bp->s.size;
|
||||
new_ptr = malloc(size);
|
||||
if (new_ptr == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
if (old_size <= size) {
|
||||
memcpy(new_ptr, ptr, old_size);
|
||||
} else {
|
||||
memcpy(new_ptr, ptr, size);
|
||||
}
|
||||
free(ptr);
|
||||
return new_ptr;
|
||||
}
|
||||
Reference in New Issue
Block a user