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.3 1999-11-23 20:40:13 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 | |
---|
65 | /* Change this appropriately, if you change omBinPage */ |
---|
66 | /* However, make sure that omBinPage is a multiple of SIZEOF_MAX_TYPE */ |
---|
67 | #define _SIZEOF_OM_BIN_PAGE_HEADER (4*SIZEOF_VOIDP + SIZEOF_LONG + SIZEOF_LONG) |
---|
68 | /* make sure SIZEOF_OM_BIN_PAGE_HEADER is divisible by SIZEOF_OM_ALIGNMENT*/ |
---|
69 | #define SIZEOF_OM_BIN_PAGE_HEADER OM_ALIGN_SIZE(_SIZEOF_OM_BIN_PAGE_HEADER) |
---|
70 | #define SIZEOF_OM_BIN_PAGE (SIZEOF_OM_PAGE - SIZEOF_OM_BIN_PAGE_HEADER) |
---|
71 | |
---|
72 | /* keep all members of omBin_s a sizeof(long) type, |
---|
73 | otherwise list operations will fail */ |
---|
74 | struct omBin_s |
---|
75 | { |
---|
76 | omBinPage current_page; /* page of current freelist */ |
---|
77 | omBinPage last_page; /* pointer to last page of freelist */ |
---|
78 | omBin next; /* sticky bins of the same size */ |
---|
79 | long sizeW; /* size in words */ |
---|
80 | long max_blocks; /* if > 0 # blocks in one page, |
---|
81 | if < 0 # pages for one block */ |
---|
82 | unsigned long sticky; /* sticky tag */ |
---|
83 | }; |
---|
84 | |
---|
85 | struct omSpecBin_s |
---|
86 | { |
---|
87 | omSpecBin next; /* pointer to next bin */ |
---|
88 | omBin bin; /* pointer to bin itself */ |
---|
89 | long max_blocks; /* max_blocks of bin*/ |
---|
90 | long ref; /* ref count */ |
---|
91 | }; |
---|
92 | |
---|
93 | #ifndef OM_GENERATE_INC |
---|
94 | extern omBinPage_t om_ZeroPage[]; |
---|
95 | extern omBinPage_t om_CheckPage[]; |
---|
96 | extern omBinPage_t om_LargePage[]; |
---|
97 | extern omBin_t om_LargeBin[]; |
---|
98 | extern omBin_t om_CheckBin[]; |
---|
99 | extern omBin_t om_StaticBin[]; |
---|
100 | extern omBin om_Size2Bin[]; |
---|
101 | |
---|
102 | #include "omTables.inc" |
---|
103 | |
---|
104 | |
---|
105 | |
---|
106 | /******************************************************************* |
---|
107 | * |
---|
108 | * lowest level alloc/free macros |
---|
109 | * |
---|
110 | *******************************************************************/ |
---|
111 | extern void* omAllocBinFromFullPage(omBin bin); |
---|
112 | extern void omFreeToPageFault(omBinPage page, void* addr); |
---|
113 | |
---|
114 | #include "omMemOps.h" |
---|
115 | #include "omPage.h" |
---|
116 | |
---|
117 | |
---|
118 | /*******************************************************************/ |
---|
119 | /* Page */ |
---|
120 | #define __omTypeAllocFromPage(type, addr, page) \ |
---|
121 | do \ |
---|
122 | { \ |
---|
123 | ((page)->used_blocks)++; \ |
---|
124 | addr = (type)((page)->current); \ |
---|
125 | (page)->current = *((void**) (page)->current); \ |
---|
126 | } \ |
---|
127 | while (0) |
---|
128 | |
---|
129 | #define __omFreeToPage(addr, page) \ |
---|
130 | do \ |
---|
131 | { \ |
---|
132 | if ((page)->used_blocks > 0) \ |
---|
133 | { \ |
---|
134 | *((void**)addr) = (page)->current; \ |
---|
135 | ((page)->used_blocks)--; \ |
---|
136 | (page)->current = (addr); \ |
---|
137 | } \ |
---|
138 | else \ |
---|
139 | { \ |
---|
140 | omFreeToPageFault(page, addr); \ |
---|
141 | } \ |
---|
142 | } \ |
---|
143 | while (0) |
---|
144 | |
---|
145 | |
---|
146 | /*******************************************************************/ |
---|
147 | /* Bin */ |
---|
148 | #define __omTypeAllocBin(type, addr, bin) \ |
---|
149 | do \ |
---|
150 | { \ |
---|
151 | register omBinPage __om_page = (bin)->current_page; \ |
---|
152 | if (__om_page->current != NULL) \ |
---|
153 | __omTypeAllocFromPage(type, addr, __om_page); \ |
---|
154 | else \ |
---|
155 | addr = (type) omAllocBinFromFullPage(bin); \ |
---|
156 | } \ |
---|
157 | while (0) |
---|
158 | |
---|
159 | #define __omTypeAlloc0Bin(type, addr, bin) \ |
---|
160 | do \ |
---|
161 | { \ |
---|
162 | __omTypeAllocBin(type, addr, bin); \ |
---|
163 | omMemsetW(addr, 0, (bin)->sizeW); \ |
---|
164 | } \ |
---|
165 | while (0) |
---|
166 | |
---|
167 | #define __omFreeBin(addr) \ |
---|
168 | do \ |
---|
169 | { \ |
---|
170 | register void* __om_addr = (void*) (addr); \ |
---|
171 | register omBinPage __om_page = omGetPageOfAddr(__om_addr); \ |
---|
172 | __omFreeToPage(__om_addr, __om_page); \ |
---|
173 | } \ |
---|
174 | while (0) |
---|
175 | |
---|
176 | |
---|
177 | /*******************************************************************/ |
---|
178 | /* Block */ |
---|
179 | #define omSmallSize2Bin(size) om_Size2Bin[((size) -1)>>LOG_SIZEOF_OM_ALIGNMENT] |
---|
180 | #define omSize2Bin(size) ((size) <= OM_MAX_BLOCK_SIZE ? \ |
---|
181 | omSmallSize2Bin(size) : om_LargeBin) |
---|
182 | |
---|
183 | #define omAllocLargeBlock(size) OM_MALLOC(size) |
---|
184 | #define omFreeLargeBlock(addr, size) OM_FREE(addr) |
---|
185 | |
---|
186 | #define __omTypeAllocBlock(type, addr, size) \ |
---|
187 | do \ |
---|
188 | { \ |
---|
189 | if (size <= OM_MAX_BLOCK_SIZE) \ |
---|
190 | { \ |
---|
191 | omBin __om_bin = omSmallSize2Bin(size); \ |
---|
192 | __omTypeAllocBin(type, addr, __om_bin); \ |
---|
193 | } \ |
---|
194 | else \ |
---|
195 | { \ |
---|
196 | addr = (type) omAllocLargeBlock(size); \ |
---|
197 | } \ |
---|
198 | } \ |
---|
199 | while(0) |
---|
200 | |
---|
201 | #define __omTypeAlloc0Block(type, addr, size) \ |
---|
202 | do \ |
---|
203 | { \ |
---|
204 | if (size <= OM_MAX_BLOCK_SIZE) \ |
---|
205 | { \ |
---|
206 | omBin __om_bin = omSmallSize2Bin(size); \ |
---|
207 | __omTypeAlloc0Bin(type, addr, __om_bin); \ |
---|
208 | } \ |
---|
209 | else \ |
---|
210 | { \ |
---|
211 | addr = (type) omAllocLargeBlock(size); \ |
---|
212 | memset(addr, 0, size); \ |
---|
213 | } \ |
---|
214 | } \ |
---|
215 | while (0) |
---|
216 | |
---|
217 | #ifdef OM_ALIGNMENT_NEEDS_WORK |
---|
218 | #define __omTypeAllocAlignedBlock(type, addr, size) \ |
---|
219 | do \ |
---|
220 | { \ |
---|
221 | if (size <= OM_MAX_BLOCK_SIZE) \ |
---|
222 | { \ |
---|
223 | omBin __om_bin = omSmallSize2AlignedBin(size); \ |
---|
224 | __omTypeAllocBin(type, addr, __om_bin); \ |
---|
225 | } \ |
---|
226 | else \ |
---|
227 | { \ |
---|
228 | addr = (type) omAllocLargeBlock(size); \ |
---|
229 | } \ |
---|
230 | } \ |
---|
231 | while(0) |
---|
232 | |
---|
233 | #define __omTypeAlloc0AlignedBlock(type, addr, size) \ |
---|
234 | do \ |
---|
235 | { \ |
---|
236 | if (size <= OM_MAX_BLOCK_SIZE) \ |
---|
237 | { \ |
---|
238 | omBin __om_bin = omSmallSize2AlignedBin(size); \ |
---|
239 | __omTypeAlloc0Bin(type, addr, __om_bin); \ |
---|
240 | } \ |
---|
241 | else \ |
---|
242 | { \ |
---|
243 | addr = (type) omAllocLargeBlock(size); \ |
---|
244 | memset(addr, 0, size); \ |
---|
245 | } \ |
---|
246 | } \ |
---|
247 | while (0) |
---|
248 | |
---|
249 | #else /* ! OM_ALIGNMENT_NEEDS_WORK */ |
---|
250 | #define __omTypeAllocAlignedBlock __omTypeAllocBlock |
---|
251 | #define __omTypeAlloc0AlignedBlock __omTypeAlloc0Block |
---|
252 | #endif /* OM_ALIGNMENT_NEEDS_WORK */ |
---|
253 | |
---|
254 | #define __omFreeBlock(addr, size) \ |
---|
255 | do \ |
---|
256 | { \ |
---|
257 | if (size <= OM_MAX_BLOCK_SIZE) \ |
---|
258 | { \ |
---|
259 | __omFreeBin(addr); \ |
---|
260 | } \ |
---|
261 | else \ |
---|
262 | { \ |
---|
263 | omFreeLargeBlock(addr, size); \ |
---|
264 | } \ |
---|
265 | } \ |
---|
266 | while (0) |
---|
267 | |
---|
268 | /*******************************************************************/ |
---|
269 | /* Chunk */ |
---|
270 | #define omAllocLargeChunk(size) OM_MALLOC(size) |
---|
271 | #define omFreeLargeChunk(addr) OM_FREE(addr) |
---|
272 | |
---|
273 | #define __omTypeAllocChunk(type, addr, size) \ |
---|
274 | do \ |
---|
275 | { \ |
---|
276 | void* __om_addr; \ |
---|
277 | size_t __om_size = (size) + SIZEOF_OM_ALIGNMENT; \ |
---|
278 | if (__om_size <= OM_MAX_BLOCK_SIZE) \ |
---|
279 | { \ |
---|
280 | omBin __om_bin = omSmallSize2Bin(__om_size); \ |
---|
281 | __omTypeAllocBin(void*, __om_addr, __om_bin); \ |
---|
282 | *((void**) __om_addr) = (void*) __om_bin->current_page; \ |
---|
283 | } \ |
---|
284 | else \ |
---|
285 | { \ |
---|
286 | __om_addr = omAllocLargeChunk(__om_size); \ |
---|
287 | *((void**) __om_addr) = (void*) om_LargePage; \ |
---|
288 | } \ |
---|
289 | addr = (type) (__om_addr + SIZEOF_OM_ALIGNMENT); \ |
---|
290 | } \ |
---|
291 | while (0) |
---|
292 | |
---|
293 | #define __omTypeAlloc0Chunk(type, addr, size) \ |
---|
294 | do \ |
---|
295 | { \ |
---|
296 | void* __om_addr; \ |
---|
297 | size_t __om_size = (size) + SIZEOF_OM_ALIGNMENT; \ |
---|
298 | if (__om_size <= OM_MAX_BLOCK_SIZE) \ |
---|
299 | { \ |
---|
300 | omBin __om_bin = omSmallSize2Bin(__om_size); \ |
---|
301 | __omTypeAlloc0Bin(void*, __om_addr, __om_bin); \ |
---|
302 | *((void**) __om_addr) = (void*) __om_bin->current_page; \ |
---|
303 | } \ |
---|
304 | else \ |
---|
305 | { \ |
---|
306 | __om_addr = omAllocLargeChunk(__om_size); \ |
---|
307 | memset(__om_addr, 0, __om_size); \ |
---|
308 | *((void**) __om_addr) = (void*) om_LargePage; \ |
---|
309 | } \ |
---|
310 | addr = (type) (__om_addr + SIZEOF_OM_ALIGNMENT); \ |
---|
311 | } \ |
---|
312 | while (0) |
---|
313 | |
---|
314 | #ifdef OM_ALIGNMENT_NEEDS_WORK |
---|
315 | |
---|
316 | #define __omTypeAllocAlignedChunk(type, addr, size) \ |
---|
317 | do \ |
---|
318 | { \ |
---|
319 | void* __om_addr; \ |
---|
320 | size_t __om_size = (size) + SIZEOF_OM_CHUNK_ALIGNMENT; \ |
---|
321 | if (__om_size <= OM_MAX_BLOCK_SIZE) \ |
---|
322 | { \ |
---|
323 | omBin __om_bin = omSmallSize2AlignedBin(__om_size); \ |
---|
324 | __omTypeAllocBin(void*, __om_addr, __om_bin); \ |
---|
325 | *((void**) __om_addr) = (void*) __om_bin->current_page; \ |
---|
326 | } \ |
---|
327 | else \ |
---|
328 | { \ |
---|
329 | __om_addr = omAllocLargeChunk(__om_size); \ |
---|
330 | *((void**) __om_addr) = (void*) om_LargePage; \ |
---|
331 | } \ |
---|
332 | addr = (type) (__om_addr + SIZEOF_OM_CHUNK_ALIGNMENT); \ |
---|
333 | } \ |
---|
334 | while (0) |
---|
335 | |
---|
336 | #define __omTypeAlloc0AlignedChunk(type, addr, size) \ |
---|
337 | do \ |
---|
338 | { \ |
---|
339 | void* __om_addr; \ |
---|
340 | size_t __om_size = (size) + SIZEOF_OM_CHUNK_ALIGNMENT; \ |
---|
341 | if (__om_size <= OM_MAX_BLOCK_SIZE) \ |
---|
342 | { \ |
---|
343 | omBin __om_bin = omSmallSize2Bin(__om_size); \ |
---|
344 | __omTypeAlloc0Bin(void*, __om_addr, __om_bin); \ |
---|
345 | *((void**) __om_addr) = (void*) __om_bin->current_page; \ |
---|
346 | } \ |
---|
347 | else \ |
---|
348 | { \ |
---|
349 | __om_addr = omAllocLargeChunk(__om_size); \ |
---|
350 | memset(__om_addr, 0, __om_size); \ |
---|
351 | *((void**) __om_addr) = (void*) om_LargePage; \ |
---|
352 | } \ |
---|
353 | addr = (type) (__om_addr + SIZEOF_OM_CHUNK_ALIGNMENT); \ |
---|
354 | } \ |
---|
355 | while (0) |
---|
356 | |
---|
357 | #define __omFreeAlignedChunk(addr) \ |
---|
358 | do \ |
---|
359 | { \ |
---|
360 | void* __addr = ((void*) (addr)) - SIZEOF_OM_CHUNK_ALIGNMENT; \ |
---|
361 | omBinPage __om_page = *((omBinPage*) __addr); \ |
---|
362 | __omFreeToPage(__addr, __om_page); \ |
---|
363 | } \ |
---|
364 | while (0) |
---|
365 | |
---|
366 | |
---|
367 | #else /* ! OM_ALIGNMENT_NEEDS_WORK */ |
---|
368 | #define __omTypeAllocAlignedChunk __omTypeAllocChunk |
---|
369 | #define __omTypeAlloc0AlignedChunk __omTypeAlloc0Chunk |
---|
370 | #define __omFreeAlignedChunk __omFreeChunk |
---|
371 | #endif /* OM_ALIGNMENT_NEEDS_WORK */ |
---|
372 | |
---|
373 | #define __omFreeChunk(addr) \ |
---|
374 | do \ |
---|
375 | { \ |
---|
376 | void* __addr = ((void*) (addr)) - SIZEOF_OM_ALIGNMENT; \ |
---|
377 | omBinPage __om_page = *((omBinPage*) __addr); \ |
---|
378 | __omFreeToPage(__addr, __om_page); \ |
---|
379 | } \ |
---|
380 | while (0) |
---|
381 | |
---|
382 | /******************************************************************* |
---|
383 | * |
---|
384 | * middle level |
---|
385 | * |
---|
386 | *******************************************************************/ |
---|
387 | |
---|
388 | #if defined(OM_INLINE) |
---|
389 | |
---|
390 | #define OM_ALLOCBIN_FUNC_WRAPPER(func) \ |
---|
391 | OM_INLINE void* _om##func (omBin bin) \ |
---|
392 | { \ |
---|
393 | void* addr; \ |
---|
394 | __omType##func (void*, addr, bin); \ |
---|
395 | return addr; \ |
---|
396 | } |
---|
397 | |
---|
398 | #define OM_ALLOCSIZE_FUNC_WRAPPER(func) \ |
---|
399 | OM_INLINE void* _om##func (size_t size) \ |
---|
400 | { \ |
---|
401 | void* addr; \ |
---|
402 | __omType##func (void*, addr, size); \ |
---|
403 | return addr; \ |
---|
404 | } |
---|
405 | |
---|
406 | #define OM_FREE_FUNC_WRAPPER(func) \ |
---|
407 | OM_INLINE void _om##func (void* addr) \ |
---|
408 | { \ |
---|
409 | __om##func (addr); \ |
---|
410 | } |
---|
411 | |
---|
412 | #define OM_FREEBLOCK_FUNC_WRAPPER(func) \ |
---|
413 | OM_INLINE void _om##func (void* addr, size_t size) \ |
---|
414 | { \ |
---|
415 | __om##func (addr, size); \ |
---|
416 | } |
---|
417 | |
---|
418 | OM_ALLOCBIN_FUNC_WRAPPER(AllocBin) |
---|
419 | OM_ALLOCBIN_FUNC_WRAPPER(Alloc0Bin) |
---|
420 | OM_FREE_FUNC_WRAPPER(FreeBin) |
---|
421 | OM_ALLOCSIZE_FUNC_WRAPPER(AllocBlock) |
---|
422 | OM_ALLOCSIZE_FUNC_WRAPPER(Alloc0Block) |
---|
423 | OM_FREEBLOCK_FUNC_WRAPPER(FreeBlock) |
---|
424 | OM_ALLOCSIZE_FUNC_WRAPPER(AllocChunk) |
---|
425 | OM_ALLOCSIZE_FUNC_WRAPPER(Alloc0Chunk) |
---|
426 | OM_FREE_FUNC_WRAPPER(FreeChunk) |
---|
427 | |
---|
428 | #else /* ! OM_INLINE */ |
---|
429 | |
---|
430 | #define _omAllocBin omFuncAllocBin |
---|
431 | #define _omAlloc0Bin omFuncAlloc0Bin |
---|
432 | #define _omFreeBin omFuncFreeBin |
---|
433 | #define _omAllocBlock omFuncAllocBlock |
---|
434 | #define _omAlloc0Block omFuncAlloc0Block |
---|
435 | #define _omFreeBlock omFuncFreeBlock |
---|
436 | #define _omAlloc omFuncAlloc |
---|
437 | #define _omAlloc0 omFuncAlloc0 |
---|
438 | #define _omFree omFuncFree |
---|
439 | |
---|
440 | #endif /* OM_INLINE */ |
---|
441 | |
---|
442 | omBin omGetSpecBin(size_t size); |
---|
443 | void omUnGetSpecBin(omBin *bin); |
---|
444 | unsigned long omGetNewStickyAllBinTag(); |
---|
445 | void omSetStickyAllBinTag(unsigned long sticky); |
---|
446 | void omUnSetStickyAllBinTag(unsigned long sticky); |
---|
447 | void omDeleteStickyAllBinTag(unsigned long sticky); |
---|
448 | |
---|
449 | #endif /* ! OM_GENERATE_INC */ |
---|
450 | |
---|
451 | #endif /* OM_PRIVATE_H */ |
---|