2 * Copyright (c) 1998,1999,2000
3 * Traakan, Inc., Los Altos, CA
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice unmodified, this list of conditions, and the following
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * The heap is managed like this:
37 * table ------> | entry | <----- heap_top
43 * allo_ent ---> | ..... |
50 * allo_item---> | .... |
57 * +----------------+ <----- heap_end
59 * n_entry = allo_ent - table;
69 ndmfhh_initialize (struct ndmfhheap *fhh)
71 NDMOS_MACRO_ZEROFILL (fhh);
77 ndmfhh_commission (struct ndmfhheap *fhh, void *heap, unsigned size)
79 fhh->heap_base = heap;
80 fhh->heap_size = size;
81 fhh->heap_end = (char*)heap + size;
83 /* Align everything */
84 fhh->heap_top = (void*) (((long)heap + (NDMOS_CONST_ALIGN-1))
85 &~ (NDMOS_CONST_ALIGN-1));
86 fhh->heap_bot = (void*)
87 ((long)((char*)heap+size) &~ (NDMOS_CONST_ALIGN-1));
97 ndmfhh_prepare (struct ndmfhheap *fhh,
98 int fhtype, int entry_size,
99 unsigned n_item, unsigned total_size_of_items)
105 if (fhh->heap_base == 0)
106 return NDMFHH_RET_NO_HEAP;
108 if (fhh->allo_entry == fhh->heap_top) {
109 fhh->fhtype = fhtype;
110 fhh->entry_size = entry_size;
112 if (fhh->fhtype != fhtype)
113 return NDMFHH_RET_TYPE_CHANGE;
115 if (fhh->entry_size != entry_size)
116 return NDMFHH_RET_ENTRY_SIZE_MISMATCH;
119 items_need = total_size_of_items + n_item * NDMOS_CONST_ALIGN + SLOP;
121 pe = (char*)fhh->allo_entry + fhh->entry_size;
122 pi = (char*)fhh->allo_item - items_need;
125 return NDMFHH_RET_OVERFLOW;
128 return NDMFHH_RET_OK;
132 ndmfhh_add_entry (struct ndmfhheap *fhh)
137 if ((char*)fhh->allo_entry + fhh->entry_size < (char*)fhh->allo_item) {
138 fhh->allo_entry = (char *)p + fhh->entry_size;
147 ndmfhh_add_item (struct ndmfhheap *fhh, unsigned size)
151 size += (NDMOS_CONST_ALIGN-1);
152 size &= ~(NDMOS_CONST_ALIGN-1);
154 p = (char*)fhh->allo_item - size;
155 if (p > fhh->allo_entry) {
164 ndmfhh_save_item (struct ndmfhheap *fhh, void *item, unsigned size)
168 p = ndmfhh_add_item (fhh, size);
170 NDMOS_API_BCOPY (item, p, size);
176 ndmfhh_reset (struct ndmfhheap *fhh)
178 fhh->allo_entry = fhh->heap_top;
179 fhh->allo_item = fhh->heap_bot;
181 return NDMFHH_RET_OK;
185 ndmfhh_get_table (struct ndmfhheap *fhh,
186 int *fhtype_p, void **table_p, unsigned *n_entry_p)
190 *fhtype_p = fhh->fhtype;
191 *table_p = fhh->heap_top;
192 n = (char*)fhh->allo_entry - (char*)fhh->heap_top;
194 n /= fhh->entry_size;
198 return NDMFHH_RET_OK;