mirror of
https://github.com/drasko/codezero.git
synced 2026-01-13 03:13:15 +01:00
34 lines
568 B
C
34 lines
568 B
C
/*
|
|
* 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;
|
|
}
|