blob: 78081564e4bdb3b611dc6995fbe9392858b46f68 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
#include "stddef.h" // for NULL
#include <atomic.h> // for LIBC_UNLOCK, LIBC_LOCK
#include <errno.h> // for EINVAL, errno
#include <libc.h> // for libc, libc::(anonymous)
#include <malloc.h> // for page, page::(anonymous), __malloc_pvec
#include <stdint.h> // for uintptr_t
#include <stdlib.h> // for free, malloc, realloc
#include <string.h> // for memcpy, size_t
void *realloc(void *ptr, size_t size)
{
if (ptr == NULL) {
return malloc(size);
}
if (size == 0) {
free(ptr);
return NULL;
}
LIBC_LOCK(__libc.lock.malloc);
struct page *p = __malloc_pvec;
while (p) {
if ((uintptr_t)ptr >= (uintptr_t)p->heap &&
(uintptr_t)ptr < (uintptr_t)(p->heap + (p->block.size * p->block.count))) {
size_t old_size = p->block.size;
LIBC_UNLOCK(__libc.lock.malloc);
if (size <= old_size) {
return ptr;
}
void *new_ptr = malloc(size);
if (new_ptr) {
memcpy(new_ptr, ptr, old_size);
free(ptr);
}
return new_ptr;
}
p = p->next;
}
LIBC_UNLOCK(__libc.lock.malloc);
errno = EINVAL;
return NULL;
}
|