1 // SPDX-License-Identifier: GPL-2.0
3 * Implement primitive realloc(3) functionality.
5 * Author: Mark A. Greer <mgreer@mvista.com>
7 * 2006 (c) MontaVista, Software, Inc.
16 #define ENTRY_BEEN_USED 0x01
17 #define ENTRY_IN_USE 0x02
19 static struct alloc_info
{
25 static unsigned long tbl_entries
;
26 static unsigned long alloc_min
;
27 static unsigned long next_base
;
28 static unsigned long space_left
;
31 * First time an entry is used, its base and size are set.
32 * An entry can be freed and re-malloc'd but its base & size don't change.
33 * Should be smart enough for needs of bootwrapper.
35 static void *simple_malloc(unsigned long size
)
38 struct alloc_info
*p
= alloc_tbl
;
43 size
= _ALIGN_UP(size
, alloc_min
);
45 for (i
=0; i
<tbl_entries
; i
++, p
++)
46 if (!(p
->flags
& ENTRY_BEEN_USED
)) { /* never been used */
47 if (size
<= space_left
) {
50 p
->flags
= ENTRY_BEEN_USED
| ENTRY_IN_USE
;
53 return (void *)p
->base
;
55 goto err_out
; /* not enough space left */
57 /* reuse an entry keeping same base & size */
58 else if (!(p
->flags
& ENTRY_IN_USE
) && (size
<= p
->size
)) {
59 p
->flags
|= ENTRY_IN_USE
;
60 return (void *)p
->base
;
66 static struct alloc_info
*simple_find_entry(void *ptr
)
69 struct alloc_info
*p
= alloc_tbl
;
71 for (i
=0; i
<tbl_entries
; i
++,p
++) {
72 if (!(p
->flags
& ENTRY_BEEN_USED
))
74 if ((p
->flags
& ENTRY_IN_USE
) &&
75 (p
->base
== (unsigned long)ptr
))
81 static void simple_free(void *ptr
)
83 struct alloc_info
*p
= simple_find_entry(ptr
);
86 p
->flags
&= ~ENTRY_IN_USE
;
90 * Change size of area pointed to by 'ptr' to 'size'.
91 * If 'ptr' is NULL, then its a malloc(). If 'size' is 0, then its a free().
92 * 'ptr' must be NULL or a pointer to a non-freed area previously returned by
93 * simple_realloc() or simple_malloc().
95 static void *simple_realloc(void *ptr
, unsigned long size
)
106 return simple_malloc(size
);
108 p
= simple_find_entry(ptr
);
109 if (p
== NULL
) /* ptr not from simple_malloc/simple_realloc */
111 if (size
<= p
->size
) /* fits in current block */
114 new = simple_malloc(size
);
116 memcpy(new, ptr
, p
->size
);
124 * Returns addr of first byte after heap so caller can see if it took
125 * too much space. If so, change args & try again.
127 void *simple_alloc_init(char *base
, unsigned long heap_size
,
128 unsigned long granularity
, unsigned long max_allocs
)
130 unsigned long heap_base
, tbl_size
;
132 heap_size
= _ALIGN_UP(heap_size
, granularity
);
133 alloc_min
= granularity
;
134 tbl_entries
= max_allocs
;
136 tbl_size
= tbl_entries
* sizeof(struct alloc_info
);
138 alloc_tbl
= (struct alloc_info
*)_ALIGN_UP((unsigned long)base
, 8);
139 memset(alloc_tbl
, 0, tbl_size
);
141 heap_base
= _ALIGN_UP((unsigned long)alloc_tbl
+ tbl_size
, alloc_min
);
143 next_base
= heap_base
;
144 space_left
= heap_size
;
146 platform_ops
.malloc
= simple_malloc
;
147 platform_ops
.free
= simple_free
;
148 platform_ops
.realloc
= simple_realloc
;
150 return (void *)(heap_base
+ heap_size
);