1 | /******************************************************************* |
---|
2 | * File: omPrivate.h |
---|
3 | * Purpose: declaration of "private" (but visible to the outside) |
---|
4 | * routines for omalloc |
---|
5 | * Author: obachman (Olaf Bachmann) |
---|
6 | * Created: 11/99 |
---|
7 | * Version: $Id: omPrivate.h,v 1.2 1999-11-22 18:12:59 obachman Exp $ |
---|
8 | *******************************************************************/ |
---|
9 | #ifndef OM_PRIVATE_H |
---|
10 | #define OM_PRIVATE_H |
---|
11 | |
---|
12 | #include <stdlib.h> |
---|
13 | #include <string.h> |
---|
14 | |
---|
15 | /******************************************************************* |
---|
16 | * |
---|
17 | * Misc defines |
---|
18 | * |
---|
19 | *******************************************************************/ |
---|
20 | |
---|
21 | #ifndef NULL |
---|
22 | #define NULL ((void*) 0) |
---|
23 | #endif |
---|
24 | |
---|
25 | #ifdef OM_ALIGN_8 |
---|
26 | #define SIZEOF_OM_ALIGNMENT 8 |
---|
27 | #define SIZEOF_OM_ALIGNMENT_1 7 |
---|
28 | #define LOG_SIZEOF_OM_ALIGNMENT 3 |
---|
29 | #else |
---|
30 | #define SIZEOF_OM_ALIGNMENT 4 |
---|
31 | #define SIZEOF_OM_ALIGNMENT_1 3 |
---|
32 | #define LOG_SIZEOF_OM_ALIGNMENT 2 |
---|
33 | #endif |
---|
34 | |
---|
35 | #define OM_ALIGN_SIZE(size) \ |
---|
36 | ((size + SIZEOF_OM_ALIGNMENT_1) & (~SIZEOF_OM_ALIGNMENT_1)) |
---|
37 | |
---|
38 | /******************************************************************* |
---|
39 | * |
---|
40 | * Definitions of structures we work with |
---|
41 | * |
---|
42 | *******************************************************************/ |
---|
43 | struct omBinPage_s; |
---|
44 | struct omBin_s; |
---|
45 | struct omSpecBin_s; |
---|
46 | typedef struct omBinPage_s omBinPage_t; |
---|
47 | typedef struct omBin_s omBin_t; |
---|
48 | typedef struct omSpecBin_s omSpecBin_t; |
---|
49 | typedef omBinPage_t* omBinPage; |
---|
50 | typedef omBin_t* omBin; |
---|
51 | typedef omSpecBin_t* omSpecBin; |
---|
52 | |
---|
53 | |
---|
54 | /* Need to define it here, has to be known to macros */ |
---|
55 | struct omBinPage_s |
---|
56 | { |
---|
57 | long used_blocks; /* number of used blocks of this page */ |
---|
58 | void* current; /* pointer to current freelist */ |
---|
59 | omBinPage next; /* next/prev pointer of pages */ |
---|
60 | omBinPage prev; |
---|
61 | void* bin_sticky; /* bin this page belongs to with |
---|
62 | sticky tag of page hidden in ptr*/ |
---|
63 | }; |
---|
64 | /* Change this appropriately, if you change omBinPage */ |
---|
65 | /* However, make sure that omBinPage is a multiple of SIZEOF_MAX_TYPE */ |
---|
66 | #define _SIZEOF_OM_BIN_PAGE_HEADER (4*SIZEOF_VOIDP + SIZEOF_LONG + SIZEOF_LONG) |
---|
67 | /* make sure SIZEOF_OM_BIN_PAGE_HEADER is divisible by SIZEOF_OM_ALIGNMENT*/ |
---|
68 | #define SIZEOF_OM_BIN_PAGE_HEADER OM_ALIGN_SIZE(_SIZEOF_OM_BIN_PAGE_HEADER) |
---|
69 | #define SIZEOF_OM_BIN_PAGE (SIZEOF_OM_PAGE - SIZEOF_OM_BIN_PAGE_HEADER) |
---|
70 | |
---|
71 | /* keep all members of omBin_s a sizeof(long) type, |
---|
72 | otherwise list operations will fail */ |
---|
73 | struct omBin_s |
---|
74 | { |
---|
75 | omBinPage current_page; /* page of current freelist */ |
---|
76 | omBinPage last_page; /* pointer to last page of freelist */ |
---|
77 | omBin next; /* sticky bins of the same size */ |
---|
78 | long sizeW; /* size in words */ |
---|
79 | long max_blocks; /* if > 0 # blocks in one page, |
---|
80 | if < 0 # pages for one block */ |
---|
81 | unsigned long sticky; /* sticky tag */ |
---|
82 | }; |
---|
83 | |
---|
84 | struct omSpecBin_s |
---|
85 | { |
---|
86 | omSpecBin next; /* pointer to next bin */ |
---|
87 | omBin bin; /* pointer to bin itself */ |
---|
88 | long max_blocks; /* max_blocks of bin*/ |
---|
89 | long ref; /* ref count */ |
---|
90 | }; |
---|
91 | |
---|
92 | #ifndef OM_GENERATE_INC |
---|
93 | extern omBinPage_t om_ZeroPage[]; |
---|
94 | extern omBinPage_t om_CheckPage[]; |
---|
95 | extern omBinPage_t om_LargePage[]; |
---|
96 | extern omBin_t om_LargeBin[]; |
---|
97 | extern omBin_t om_CheckBin[]; |
---|
98 | extern omBin_t om_StaticBin[]; |
---|
99 | extern omBin om_Size2Bin[]; |
---|
100 | |
---|
101 | #include "omTables.inc" |
---|
102 | |
---|
103 | |
---|
104 | |
---|
105 | /******************************************************************* |
---|
106 | * |
---|
107 | * lowest level alloc/free macros |
---|
108 | * |
---|
109 | *******************************************************************/ |
---|
110 | extern void* omAllocBinFromFullPage(omBin bin); |
---|
111 | extern void omFreeToPageFault(omBinPage page, void* addr); |
---|
112 | |
---|
113 | #include "omMemOps.h" |
---|
114 | #include "omPage.h" |
---|
115 | |
---|
116 | |
---|
117 | /*******************************************************************/ |
---|
118 | /* Page */ |
---|
119 | #define __omTypeAllocFromPage(type, addr, page) \ |
---|
120 | do \ |
---|
121 | { \ |
---|
122 | ((page)->used_blocks)++; \ |
---|
123 | addr = (type)((page)->current); \ |
---|
124 | (page)->current = *((void**) (page)->current); \ |
---|
125 | } \ |
---|
126 | while (0) |
---|
127 | |
---|
128 | #define __omFreeToPage(addr, page) \ |
---|
129 | do \ |
---|
130 | { \ |
---|
131 | if ((page)->used_blocks > 0) \ |
---|
132 | { \ |
---|
133 | *((void**)addr) = (page)->current; \ |
---|
134 | ((page)->used_blocks)--; \ |
---|
135 | (page)->current = (addr); \ |
---|
136 | } \ |
---|
137 | else \ |
---|
138 | { \ |
---|
139 | omFreeToPageFault(page, addr); \ |
---|
140 | } \ |
---|
141 | } \ |
---|
142 | while (0) |
---|
143 | |
---|
144 | |
---|
145 | /*******************************************************************/ |
---|
146 | /* Bin */ |
---|
147 | #define __omTypeAllocBin(type, addr, bin) \ |
---|
148 | do \ |
---|
149 | { \ |
---|
150 | register omBinPage __om_page = (bin)->current_page; \ |
---|
151 | if (__om_page->current != NULL) \ |
---|
152 | __omTypeAllocFromPage(type, addr, __om_page); \ |
---|
153 | else \ |
---|
154 | addr = (type) omAllocBinFromFullPage(bin); \ |
---|
155 | } \ |
---|
156 | while (0) |
---|
157 | |
---|
158 | #define __omTypeAlloc0Bin(type, addr, bin) \ |
---|
159 | do \ |
---|
160 | { \ |
---|
161 | __omTypeAllocBin(type, addr, bin); \ |
---|
162 | omMemsetW(addr, 0, (bin)->sizeW); \ |
---|
163 | } \ |
---|
164 | while (0) |
---|
165 | |
---|
166 | #define __omFreeBin(addr) \ |
---|
167 | do \ |
---|
168 | { \ |
---|
169 | register void* __om_addr = (void*) (addr); \ |
---|
170 | register omBinPage __om_page = omGetPageOfAddr(__om_addr); \ |
---|
171 | __omFreeToPage(__om_addr, __om_page); \ |
---|
172 | } \ |
---|
173 | while (0) |
---|
174 | |
---|
175 | |
---|
176 | /*******************************************************************/ |
---|
177 | /* Block */ |
---|
178 | #define omSmallSize2Bin(size) om_Size2Bin[((size) -1) >> LOG_SIZEOF_OM_ALIGNMENT] |
---|
179 | #define omSize2Bin(size) ((size) <= OM_MAX_BLOCK_SIZE ? omSmallSize2Bin(size) : om_LargeBin) |
---|
180 | |
---|
181 | #define omAllocLargeBlock(size) OM_MALLOC(size) |
---|
182 | #define omFreeLargeBlock(addr, size) OM_FREE(addr) |
---|
183 | |
---|
184 | #define __omTypeAllocBlock(type, addr, size) \ |
---|
185 | do \ |
---|
186 | { \ |
---|
187 | if (size <= OM_MAX_BLOCK_SIZE) \ |
---|
188 | { \ |
---|
189 | omBin __om_bin = omSmallSize2Bin(size); \ |
---|
190 | __omTypeAllocBin(type, addr, __om_bin); \ |
---|
191 | } \ |
---|
192 | else \ |
---|
193 | { \ |
---|
194 | addr = (type) omAllocLargeBlock(size); \ |
---|
195 | } \ |
---|
196 | } \ |
---|
197 | while(0) |
---|
198 | |
---|
199 | #define __omTypeAlloc0Block(type, addr, size) \ |
---|
200 | do \ |
---|
201 | { \ |
---|
202 | if (size <= OM_MAX_BLOCK_SIZE) \ |
---|
203 | { \ |
---|
204 | omBin __om_bin = omSmallSize2Bin(size); \ |
---|
205 | __omTypeAlloc0Bin(type, addr, __om_bin); \ |
---|
206 | } \ |
---|
207 | else \ |
---|
208 | { \ |
---|
209 | addr = (type) omAllocLargeBlock(size); \ |
---|
210 | memset(addr, 0, size); \ |
---|
211 | } \ |
---|
212 | } \ |
---|
213 | while (0) |
---|
214 | |
---|
215 | #define __omFreeBlock(addr, size) \ |
---|
216 | do \ |
---|
217 | { \ |
---|
218 | if (size <= OM_MAX_BLOCK_SIZE) \ |
---|
219 | { \ |
---|
220 | __omFreeBin(addr); \ |
---|
221 | } \ |
---|
222 | else \ |
---|
223 | { \ |
---|
224 | omFreeLargeBlock(addr, size); \ |
---|
225 | } \ |
---|
226 | } \ |
---|
227 | while (0) |
---|
228 | |
---|
229 | /*******************************************************************/ |
---|
230 | /* Chunk */ |
---|
231 | #define omAllocLargeChunk(size) OM_MALLOC(size) |
---|
232 | #define omFreeLargeChunk(addr) OM_FREE(addr) |
---|
233 | |
---|
234 | #define __omTypeAllocChunk(type, addr, size) \ |
---|
235 | do \ |
---|
236 | { \ |
---|
237 | void* __om_addr; \ |
---|
238 | size_t __om_size = (size) + SIZEOF_OM_ALIGNMENT; \ |
---|
239 | if (__om_size <= OM_MAX_BLOCK_SIZE) \ |
---|
240 | { \ |
---|
241 | omBin __om_bin = omSmallSize2Bin(__om_size); \ |
---|
242 | __omTypeAllocBin(void*, __om_addr, __om_bin); \ |
---|
243 | *((void**) __om_addr) = (void*) __om_bin->current_page; \ |
---|
244 | } \ |
---|
245 | else \ |
---|
246 | { \ |
---|
247 | __om_addr = omAllocLargeChunk(__om_size); \ |
---|
248 | *((void**) __om_addr) = om_LargePage; \ |
---|
249 | } \ |
---|
250 | addr = (type) (__om_addr + SIZEOF_OM_ALIGNMENT); \ |
---|
251 | } \ |
---|
252 | while (0) |
---|
253 | |
---|
254 | #define __omTypeAlloc0Chunk(type, addr, size) \ |
---|
255 | do \ |
---|
256 | { \ |
---|
257 | void* __om_addr; \ |
---|
258 | size_t __om_size = (size) + SIZEOF_OM_ALIGNMENT; \ |
---|
259 | if (__om_size <= OM_MAX_BLOCK_SIZE) \ |
---|
260 | { \ |
---|
261 | omBin __om_bin = omSmallSize2Bin(__om_size); \ |
---|
262 | __omTypeAlloc0Bin(void*, __om_addr, __om_bin); \ |
---|
263 | *((void**) __om_addr) = (void*) __om_bin->current_page; \ |
---|
264 | } \ |
---|
265 | else \ |
---|
266 | { \ |
---|
267 | __om_addr = omAllocLargeChunk(__om_size); \ |
---|
268 | memset(__om_addr, 0, __om_size); \ |
---|
269 | *((void**) __om_addr) = om_LargePage; \ |
---|
270 | } \ |
---|
271 | addr = (type) (__om_addr + SIZEOF_OM_ALIGNMENT); \ |
---|
272 | } \ |
---|
273 | while (0) |
---|
274 | |
---|
275 | #define __omFreeChunk(addr) \ |
---|
276 | do \ |
---|
277 | { \ |
---|
278 | void* __addr = ((void*) (addr)) - SIZEOF_OM_ALIGNMENT; \ |
---|
279 | omBinPage __om_page = *((omBinPage*) __addr); \ |
---|
280 | __omFreeToPage(__addr, __om_page); \ |
---|
281 | } \ |
---|
282 | while (0) |
---|
283 | |
---|
284 | /******************************************************************* |
---|
285 | * |
---|
286 | * middle level |
---|
287 | * |
---|
288 | *******************************************************************/ |
---|
289 | |
---|
290 | #if defined(OM_INLINE) |
---|
291 | |
---|
292 | #define OM_ALLOCBIN_FUNC_WRAPPER(func) \ |
---|
293 | OM_INLINE void* _om##func (omBin bin) \ |
---|
294 | { \ |
---|
295 | void* addr; \ |
---|
296 | __omType##func (void*, addr, bin); \ |
---|
297 | return addr; \ |
---|
298 | } |
---|
299 | |
---|
300 | #define OM_ALLOCSIZE_FUNC_WRAPPER(func) \ |
---|
301 | OM_INLINE void* _om##func (size_t size) \ |
---|
302 | { \ |
---|
303 | void* addr; \ |
---|
304 | __omType##func (void*, addr, size); \ |
---|
305 | return addr; \ |
---|
306 | } |
---|
307 | |
---|
308 | #define OM_FREE_FUNC_WRAPPER(func) \ |
---|
309 | OM_INLINE void _om##func (void* addr) \ |
---|
310 | { \ |
---|
311 | __om##func (addr); \ |
---|
312 | } |
---|
313 | |
---|
314 | #define OM_FREEBLOCK_FUNC_WRAPPER(func) \ |
---|
315 | OM_INLINE void _om##func (void* addr, size_t size) \ |
---|
316 | { \ |
---|
317 | __om##func (addr, size); \ |
---|
318 | } |
---|
319 | |
---|
320 | OM_ALLOCBIN_FUNC_WRAPPER(AllocBin) |
---|
321 | OM_ALLOCBIN_FUNC_WRAPPER(Alloc0Bin) |
---|
322 | OM_FREE_FUNC_WRAPPER(FreeBin) |
---|
323 | OM_ALLOCSIZE_FUNC_WRAPPER(AllocBlock) |
---|
324 | OM_ALLOCSIZE_FUNC_WRAPPER(Alloc0Block) |
---|
325 | OM_FREEBLOCK_FUNC_WRAPPER(FreeBlock) |
---|
326 | OM_ALLOCSIZE_FUNC_WRAPPER(AllocChunk) |
---|
327 | OM_ALLOCSIZE_FUNC_WRAPPER(Alloc0Chunk) |
---|
328 | OM_FREE_FUNC_WRAPPER(FreeChunk) |
---|
329 | |
---|
330 | #else /* ! OM_INLINE */ |
---|
331 | |
---|
332 | #define _omAllocBin omFuncAllocBin |
---|
333 | #define _omAlloc0Bin omFuncAlloc0Bin |
---|
334 | #define _omFreeBin omFuncFreeBin |
---|
335 | #define _omAllocBlock omFuncAllocBlock |
---|
336 | #define _omAlloc0Block omFuncAlloc0Block |
---|
337 | #define _omFreeBlock omFuncFreeBlock |
---|
338 | #define _omAlloc omFuncAlloc |
---|
339 | #define _omAlloc0 omFuncAlloc0 |
---|
340 | #define _omFree omFuncFree |
---|
341 | |
---|
342 | #endif /* OM_INLINE */ |
---|
343 | |
---|
344 | omBin omGetSpecBin(size_t size); |
---|
345 | void omUnGetSpecBin(omBin *bin); |
---|
346 | unsigned long omGetNewStickyAllBinTag(); |
---|
347 | void omSetStickyAllBinTag(unsigned long sticky); |
---|
348 | void omUnSetStickyAllBinTag(unsigned long sticky); |
---|
349 | void omDeleteStickyAllBinTag(unsigned long sticky); |
---|
350 | |
---|
351 | #endif /* ! OM_GENERATE_INC */ |
---|
352 | |
---|
353 | #endif /* OM_PRIVATE_H */ |
---|