2 * Implement primitive realloc(3) functionality.
4 * Author: Mark A. Greer <mgreer@mvista.com>
6 * 2006 (c) MontaVista, Software, Inc. This file is licensed under
7 * the terms of the GNU General Public License version 2. This program
8 * is licensed "as is" without any warranty of any kind, whether express
18 #define ENTRY_BEEN_USED 0x01
19 #define ENTRY_IN_USE 0x02
21 static struct alloc_info
{
27 static u32 tbl_entries
;
30 static u32 space_left
;
33 * First time an entry is used, its base and size are set.
34 * An entry can be freed and re-malloc'd but its base & size don't change.
35 * Should be smart enough for needs of bootwrapper.
37 static void *simple_malloc(u32 size
)
40 struct alloc_info
*p
= alloc_tbl
;
45 size
= _ALIGN_UP(size
, alloc_min
);
47 for (i
=0; i
<tbl_entries
; i
++, p
++)
48 if (!(p
->flags
& ENTRY_BEEN_USED
)) { /* never been used */
49 if (size
<= space_left
) {
52 p
->flags
= ENTRY_BEEN_USED
| ENTRY_IN_USE
;
55 return (void *)p
->base
;
57 goto err_out
; /* not enough space left */
59 /* reuse an entry keeping same base & size */
60 else if (!(p
->flags
& ENTRY_IN_USE
) && (size
<= p
->size
)) {
61 p
->flags
|= ENTRY_IN_USE
;
62 return (void *)p
->base
;
68 static struct alloc_info
*simple_find_entry(void *ptr
)
71 struct alloc_info
*p
= alloc_tbl
;
73 for (i
=0; i
<tbl_entries
; i
++,p
++) {
74 if (!(p
->flags
& ENTRY_BEEN_USED
))
76 if ((p
->flags
& ENTRY_IN_USE
) && (p
->base
== (u32
)ptr
))
82 static void simple_free(void *ptr
)
84 struct alloc_info
*p
= simple_find_entry(ptr
);
87 p
->flags
&= ~ENTRY_IN_USE
;
91 * Change size of area pointed to by 'ptr' to 'size'.
92 * If 'ptr' is NULL, then its a malloc(). If 'size' is 0, then its a free().
93 * 'ptr' must be NULL or a pointer to a non-freed area previously returned by
94 * simple_realloc() or simple_malloc().
96 static void *simple_realloc(void *ptr
, unsigned long size
)
107 return simple_malloc(size
);
109 p
= simple_find_entry(ptr
);
110 if (p
== NULL
) /* ptr not from simple_malloc/simple_realloc */
112 if (size
<= p
->size
) /* fits in current block */
115 new = simple_malloc(size
);
116 memcpy(new, ptr
, p
->size
);
122 * Returns addr of first byte after heap so caller can see if it took
123 * too much space. If so, change args & try again.
125 void *simple_alloc_init(char *base
, u32 heap_size
, u32 granularity
,
128 u32 heap_base
, tbl_size
;
130 heap_size
= _ALIGN_UP(heap_size
, granularity
);
131 alloc_min
= granularity
;
132 tbl_entries
= max_allocs
;
134 tbl_size
= tbl_entries
* sizeof(struct alloc_info
);
136 alloc_tbl
= (struct alloc_info
*)_ALIGN_UP((unsigned long)base
, 8);
137 memset(alloc_tbl
, 0, tbl_size
);
139 heap_base
= _ALIGN_UP((u32
)alloc_tbl
+ tbl_size
, alloc_min
);
141 next_base
= heap_base
;
142 space_left
= heap_size
;
144 platform_ops
.malloc
= simple_malloc
;
145 platform_ops
.free
= simple_free
;
146 platform_ops
.realloc
= simple_realloc
;
148 return (void *)(heap_base
+ heap_size
);
This page took 0.036859 seconds and 6 git commands to generate.