1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Implement primitive realloc(3) functionality.
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Author: Mark A. Greer <mgreer@mvista.com>
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * 2006 (c) MontaVista, Software, Inc. This file is licensed under
7*4882a593Smuzhiyun * the terms of the GNU General Public License version 2. This program
8*4882a593Smuzhiyun * is licensed "as is" without any warranty of any kind, whether express
9*4882a593Smuzhiyun * or implied.
10*4882a593Smuzhiyun */
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun #include <stddef.h>
13*4882a593Smuzhiyun #include "types.h"
14*4882a593Smuzhiyun #include "page.h"
15*4882a593Smuzhiyun #include "string.h"
16*4882a593Smuzhiyun #include "ops.h"
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun #define ENTRY_BEEN_USED 0x01
19*4882a593Smuzhiyun #define ENTRY_IN_USE 0x02
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun static struct alloc_info {
22*4882a593Smuzhiyun unsigned long flags;
23*4882a593Smuzhiyun unsigned long base;
24*4882a593Smuzhiyun unsigned long size;
25*4882a593Smuzhiyun } *alloc_tbl;
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun static unsigned long tbl_entries;
28*4882a593Smuzhiyun static unsigned long alloc_min;
29*4882a593Smuzhiyun static unsigned long next_base;
30*4882a593Smuzhiyun static unsigned long space_left;
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun /*
33*4882a593Smuzhiyun * First time an entry is used, its base and size are set.
34*4882a593Smuzhiyun * An entry can be freed and re-malloc'd but its base & size don't change.
35*4882a593Smuzhiyun * Should be smart enough for needs of bootwrapper.
36*4882a593Smuzhiyun */
simple_malloc(unsigned long size)37*4882a593Smuzhiyun static void *simple_malloc(unsigned long size)
38*4882a593Smuzhiyun {
39*4882a593Smuzhiyun unsigned long i;
40*4882a593Smuzhiyun struct alloc_info *p = alloc_tbl;
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun if (size == 0)
43*4882a593Smuzhiyun goto err_out;
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun size = _ALIGN_UP(size, alloc_min);
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun for (i=0; i<tbl_entries; i++, p++)
48*4882a593Smuzhiyun if (!(p->flags & ENTRY_BEEN_USED)) { /* never been used */
49*4882a593Smuzhiyun if (size <= space_left) {
50*4882a593Smuzhiyun p->base = next_base;
51*4882a593Smuzhiyun p->size = size;
52*4882a593Smuzhiyun p->flags = ENTRY_BEEN_USED | ENTRY_IN_USE;
53*4882a593Smuzhiyun next_base += size;
54*4882a593Smuzhiyun space_left -= size;
55*4882a593Smuzhiyun return (void *)p->base;
56*4882a593Smuzhiyun }
57*4882a593Smuzhiyun goto err_out; /* not enough space left */
58*4882a593Smuzhiyun }
59*4882a593Smuzhiyun /* reuse an entry keeping same base & size */
60*4882a593Smuzhiyun else if (!(p->flags & ENTRY_IN_USE) && (size <= p->size)) {
61*4882a593Smuzhiyun p->flags |= ENTRY_IN_USE;
62*4882a593Smuzhiyun return (void *)p->base;
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun err_out:
65*4882a593Smuzhiyun return NULL;
66*4882a593Smuzhiyun }
67*4882a593Smuzhiyun
simple_find_entry(void * ptr)68*4882a593Smuzhiyun static struct alloc_info *simple_find_entry(void *ptr)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun unsigned long i;
71*4882a593Smuzhiyun struct alloc_info *p = alloc_tbl;
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun for (i=0; i<tbl_entries; i++,p++) {
74*4882a593Smuzhiyun if (!(p->flags & ENTRY_BEEN_USED))
75*4882a593Smuzhiyun break;
76*4882a593Smuzhiyun if ((p->flags & ENTRY_IN_USE) &&
77*4882a593Smuzhiyun (p->base == (unsigned long)ptr))
78*4882a593Smuzhiyun return p;
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun return NULL;
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun
simple_free(void * ptr)83*4882a593Smuzhiyun static void simple_free(void *ptr)
84*4882a593Smuzhiyun {
85*4882a593Smuzhiyun struct alloc_info *p = simple_find_entry(ptr);
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun if (p != NULL)
88*4882a593Smuzhiyun p->flags &= ~ENTRY_IN_USE;
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun /*
92*4882a593Smuzhiyun * Change size of area pointed to by 'ptr' to 'size'.
93*4882a593Smuzhiyun * If 'ptr' is NULL, then its a malloc(). If 'size' is 0, then its a free().
94*4882a593Smuzhiyun * 'ptr' must be NULL or a pointer to a non-freed area previously returned by
95*4882a593Smuzhiyun * simple_realloc() or simple_malloc().
96*4882a593Smuzhiyun */
simple_realloc(void * ptr,unsigned long size)97*4882a593Smuzhiyun static void *simple_realloc(void *ptr, unsigned long size)
98*4882a593Smuzhiyun {
99*4882a593Smuzhiyun struct alloc_info *p;
100*4882a593Smuzhiyun void *new;
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun if (size == 0) {
103*4882a593Smuzhiyun simple_free(ptr);
104*4882a593Smuzhiyun return NULL;
105*4882a593Smuzhiyun }
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun if (ptr == NULL)
108*4882a593Smuzhiyun return simple_malloc(size);
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun p = simple_find_entry(ptr);
111*4882a593Smuzhiyun if (p == NULL) /* ptr not from simple_malloc/simple_realloc */
112*4882a593Smuzhiyun return NULL;
113*4882a593Smuzhiyun if (size <= p->size) /* fits in current block */
114*4882a593Smuzhiyun return ptr;
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun new = simple_malloc(size);
117*4882a593Smuzhiyun memcpy(new, ptr, p->size);
118*4882a593Smuzhiyun simple_free(ptr);
119*4882a593Smuzhiyun return new;
120*4882a593Smuzhiyun }
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun /*
123*4882a593Smuzhiyun * Returns addr of first byte after heap so caller can see if it took
124*4882a593Smuzhiyun * too much space. If so, change args & try again.
125*4882a593Smuzhiyun */
simple_alloc_init(char * base,unsigned long heap_size,unsigned long granularity,unsigned long max_allocs)126*4882a593Smuzhiyun void *simple_alloc_init(char *base, unsigned long heap_size,
127*4882a593Smuzhiyun unsigned long granularity, unsigned long max_allocs)
128*4882a593Smuzhiyun {
129*4882a593Smuzhiyun unsigned long heap_base, tbl_size;
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun heap_size = _ALIGN_UP(heap_size, granularity);
132*4882a593Smuzhiyun alloc_min = granularity;
133*4882a593Smuzhiyun tbl_entries = max_allocs;
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun tbl_size = tbl_entries * sizeof(struct alloc_info);
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun alloc_tbl = (struct alloc_info *)_ALIGN_UP((unsigned long)base, 8);
138*4882a593Smuzhiyun memset(alloc_tbl, 0, tbl_size);
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun heap_base = _ALIGN_UP((unsigned long)alloc_tbl + tbl_size, alloc_min);
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun next_base = heap_base;
143*4882a593Smuzhiyun space_left = heap_size;
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun platform_ops.malloc = simple_malloc;
146*4882a593Smuzhiyun platform_ops.free = simple_free;
147*4882a593Smuzhiyun platform_ops.realloc = simple_realloc;
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun return (void *)(heap_base + heap_size);
150*4882a593Smuzhiyun }
151