summaryrefslogtreecommitdiff
path: root/lib/libc/stdlib/realloc.c
blob: 4722ac5e029dcb0632b04e5f542c4963239d21c1 (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
48
#include "stddef.h" // for NULL

#include <atomic.h> // for LIBC_UNLOCK, LIBC_LOCK
#include <errno.h>  // for EINVAL, errno
#include <libc.h>   // for (anonymous struct)::(anonymous), (anonymous), libc
#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;
}