1 // SPDX-License-Identifier: BSD-3-Clause
2 /*
3  * Copyright (c) 2021-2024, Arm Limited and Contributors. All rights reserved.
4  */
5 
6 #include "compiler.h"
7 #include <errno.h>
8 #include <stddef.h> /* dlmalloc requires size_t */
9 #include <stdint.h>
10 #include <string.h>
11 
12 /* Allocating heap area */
13 #ifndef SP_HEAP_SIZE
14 #error "SP_HEAP_SIZE is undefined, please define it in the build system"
15 #endif
16 
17 static uint8_t sp_heap[SP_HEAP_SIZE] __aligned(16);
18 static uint8_t *program_break = sp_heap;
19 
20 /**
21  * Basic sbrk implementation which increases the program break through the
22  * sp_heap buffer.
23  */
sbrk(ptrdiff_t incr)24 void *sbrk(ptrdiff_t incr)
25 {
26 	uint8_t *previous_break = program_break;
27 	uint8_t *new_break = program_break + incr;
28 
29 	if ((new_break < sp_heap) || (new_break > (sp_heap + sizeof(sp_heap))))
30 		return (void *)(uintptr_t) -1;
31 
32 	program_break += incr;
33 
34 	return (void *) previous_break;
35 }
36 
37 
38 
39 /*
40  * There's no way of including a custom configuration file without modifying
41  * malloc.c. As a workaround this file includes the missing stddef.h, sets the
42  * configuration values of dlmalloc and then includes malloc.c.
43  */
44 
45 /* dlmalloc configuration */
46 #define USE_SPIN_LOCKS	0
47 #define HAVE_MORECORE	1
48 #define HAVE_MMAP	0
49 #define HAVE_MREMAP	0
50 #define MALLOC_FAILURE_ACTION	do {} while (0)
51 #define MMAP_CLEARS	0
52 #define MORECORE_CONTIGUOUS	1
53 #define MORECORE_CANNOT_TRIM	1
54 #define LACKS_SYS_PARAM_H	1
55 #define LACKS_SYS_TYPES_H	1
56 #define LACKS_TIME_H	1
57 #define LACKS_UNISTD_H	1
58 #define NO_MALLINFO	1
59 #define NO_MALLOC_STATS	1
60 #define DEFAULT_GRANULARITY 64
61 
62 #include "malloc.c"
63