1 | /******************************************************************* |
---|
2 | * File: omAllocPrivate.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: omAllocPrivate.h,v 1.8 2006-05-02 11:39:02 Singular Exp $ |
---|
8 | *******************************************************************/ |
---|
9 | #ifndef OM_ALLOC_PRIVATE_H |
---|
10 | #define OM_ALLOC_PRIVATE_H |
---|
11 | |
---|
12 | /******************************************************************* |
---|
13 | * |
---|
14 | * Definitions of structures we work with |
---|
15 | * |
---|
16 | *******************************************************************/ |
---|
17 | /* Need to define it here, has to be known to macros */ |
---|
18 | struct omBinPage_s |
---|
19 | { |
---|
20 | long used_blocks; /* number of used blocks of this page */ |
---|
21 | void* current; /* pointer to current freelist */ |
---|
22 | omBinPage next; /* next/prev pointer of pages */ |
---|
23 | omBinPage prev; |
---|
24 | void* bin_sticky; /* bin this page belongs to with |
---|
25 | sticky tag of page hidden in ptr */ |
---|
26 | omBinPageRegion region; /* BinPageRegion of this page */ |
---|
27 | }; |
---|
28 | |
---|
29 | /* Change this appropriately, if you change omBinPage */ |
---|
30 | /* However, make sure that omBinPage is a multiple of SIZEOF_MAX_TYPE */ |
---|
31 | #define SIZEOF_OM_BIN_PAGE_HEADER (5*SIZEOF_VOIDP + SIZEOF_LONG) |
---|
32 | #define SIZEOF_OM_BIN_PAGE (SIZEOF_SYSTEM_PAGE - SIZEOF_OM_BIN_PAGE_HEADER) |
---|
33 | |
---|
34 | /* keep all members of omBin_s a sizeof(long) type, |
---|
35 | otherwise list operations will fail */ |
---|
36 | struct omBin_s |
---|
37 | { |
---|
38 | omBinPage current_page; /* page of current freelist */ |
---|
39 | omBinPage last_page; /* pointer to last page of freelist */ |
---|
40 | omBin next; /* sticky bins of the same size */ |
---|
41 | size_t sizeW; /* size in words */ |
---|
42 | long max_blocks; /* if > 0 # blocks in one page, |
---|
43 | if < 0 # pages for one block */ |
---|
44 | unsigned long sticky; /* sticky tag */ |
---|
45 | }; |
---|
46 | |
---|
47 | struct omSpecBin_s |
---|
48 | { |
---|
49 | omSpecBin next; /* pointer to next bin */ |
---|
50 | omBin bin; /* pointer to bin itself */ |
---|
51 | long max_blocks; /* max_blocks of bin*/ |
---|
52 | long ref; /* ref count */ |
---|
53 | }; |
---|
54 | |
---|
55 | extern omSpecBin om_SpecBin; |
---|
56 | extern omBin om_StickyBins; |
---|
57 | extern omBinPage_t om_ZeroPage[]; |
---|
58 | extern omBin om_Size2Bin[]; |
---|
59 | |
---|
60 | /******************************************************************* |
---|
61 | * |
---|
62 | * Working with pages/bins |
---|
63 | * |
---|
64 | *******************************************************************/ |
---|
65 | #define omGetTopBinOfPage(page) \ |
---|
66 | ((omBin) ( ((unsigned long) ((page)->bin_sticky)) & ~((unsigned long)SIZEOF_VOIDP - 1))) |
---|
67 | #define omGetStickyOfPage(page) \ |
---|
68 | (((unsigned long) ((page)->bin_sticky)) & ((unsigned long)SIZEOF_VOIDP-1)) |
---|
69 | #define omSetTopBinOfPage(page, bin) \ |
---|
70 | (page)->bin_sticky= (void*)((unsigned long)bin + omGetStickyOfPage(page)) |
---|
71 | #define omSetStickyOfPage(page, sticky) \ |
---|
72 | (page)->bin_sticky = (void*)(((unsigned long)sticky & ((unsigned long)SIZEOF_VOIDP-1)) + \ |
---|
73 | (unsigned long)omGetTopBinOfPage(page)) |
---|
74 | #define omSetTopBinAndStickyOfPage(page, bin, sticky) \ |
---|
75 | (page)->bin_sticky= (void*)(((unsigned long)sticky & (SIZEOF_VOIDP-1)) \ |
---|
76 | + (unsigned long)bin) |
---|
77 | |
---|
78 | #define omGetTopBinOfAddr(addr) \ |
---|
79 | omGetTopBinOfPage(((omBinPage) omGetPageOfAddr(addr))) |
---|
80 | #define omGetBinOfAddr(addr) omGetBinOfPage(omGetBinPageOfAddr(addr)) |
---|
81 | |
---|
82 | #ifndef OM_GENERATE_INC |
---|
83 | extern omBin_t om_StaticBin[]; |
---|
84 | extern omBin om_Size2Bin[]; |
---|
85 | #ifdef OM_ALIGNMENT_NEEDS_WORK |
---|
86 | extern omBin om_Size2AlignedBin[]; |
---|
87 | #endif |
---|
88 | |
---|
89 | /******************************************************************* |
---|
90 | * |
---|
91 | * SizeOfAddr |
---|
92 | * |
---|
93 | *******************************************************************/ |
---|
94 | #ifdef OM_INTERNAL_DEBUG |
---|
95 | size_t omSizeOfBinAddr(void* addr); |
---|
96 | #else |
---|
97 | #define omSizeOfBinAddr(addr) _omSizeOfBinAddr(addr) |
---|
98 | #endif |
---|
99 | |
---|
100 | #define _omSizeOfBinAddr(addr) ((omSizeWOfBinAddr(addr)) << LOG_SIZEOF_LONG) |
---|
101 | #define omSizeWOfBinAddr(addr) ((omGetTopBinOfAddr(addr))->sizeW) |
---|
102 | |
---|
103 | /******************************************************************* |
---|
104 | * |
---|
105 | * lowest level alloc/free macros |
---|
106 | * |
---|
107 | *******************************************************************/ |
---|
108 | extern void* omAllocBinFromFullPage(omBin bin); |
---|
109 | extern void omFreeToPageFault(omBinPage page, void* addr); |
---|
110 | |
---|
111 | /*******************************************************************/ |
---|
112 | /* Page */ |
---|
113 | #define __omTypeAllocFromNonEmptyPage(type, addr, page) \ |
---|
114 | do \ |
---|
115 | { \ |
---|
116 | ((page)->used_blocks)++; \ |
---|
117 | addr = (type)((page)->current); \ |
---|
118 | (page)->current = *((void**) (page)->current); \ |
---|
119 | } \ |
---|
120 | while (0) |
---|
121 | |
---|
122 | #define __omFreeToPage(addr, page) \ |
---|
123 | do \ |
---|
124 | { \ |
---|
125 | if ((page)->used_blocks > 0L) \ |
---|
126 | { \ |
---|
127 | *((void**) (addr)) = (page)->current; \ |
---|
128 | ((page)->used_blocks)--; \ |
---|
129 | (page)->current = (addr); \ |
---|
130 | } \ |
---|
131 | else \ |
---|
132 | { \ |
---|
133 | omFreeToPageFault(page, addr); \ |
---|
134 | } \ |
---|
135 | } \ |
---|
136 | while (0) |
---|
137 | |
---|
138 | |
---|
139 | /*******************************************************************/ |
---|
140 | /* Bin */ |
---|
141 | #define __omTypeAllocBin(type, addr, bin) \ |
---|
142 | do \ |
---|
143 | { \ |
---|
144 | register omBinPage __om_page = (bin)->current_page; \ |
---|
145 | if (__om_page->current != NULL) \ |
---|
146 | __omTypeAllocFromNonEmptyPage(type, addr, __om_page); \ |
---|
147 | else \ |
---|
148 | addr = (type) omAllocBinFromFullPage(bin); \ |
---|
149 | } \ |
---|
150 | while (0) |
---|
151 | |
---|
152 | #define __omTypeAlloc0Bin(type, addr, bin) \ |
---|
153 | do \ |
---|
154 | { \ |
---|
155 | __omTypeAllocBin(type, addr, bin); \ |
---|
156 | omMemsetW(addr, 0, (bin)->sizeW); \ |
---|
157 | } \ |
---|
158 | while (0) |
---|
159 | |
---|
160 | |
---|
161 | #define __omFreeBinAddr(addr) \ |
---|
162 | do \ |
---|
163 | { \ |
---|
164 | register void* __om_addr = (void*) (addr); \ |
---|
165 | register omBinPage __om_page = omGetBinPageOfAddr(__om_addr); \ |
---|
166 | __omFreeToPage(__om_addr, __om_page); \ |
---|
167 | } \ |
---|
168 | while (0) |
---|
169 | |
---|
170 | #define __omTypeReallocBin(old_addr, old_bin, new_type, new_addr, new_bin) \ |
---|
171 | do \ |
---|
172 | { \ |
---|
173 | if (old_bin != new_bin) \ |
---|
174 | { \ |
---|
175 | size_t old_sizeW = (omIsNormalBinPageAddr(old_addr) ? old_bin->sizeW : omSizeWOfAddr(old_addr)); \ |
---|
176 | __omTypeAllocBin(new_type, new_addr, new_bin); \ |
---|
177 | omMemcpyW(new_addr, old_addr, (new_bin->sizeW > old_sizeW ? old_sizeW : new_bin->sizeW)); \ |
---|
178 | __omFreeBinAddr(old_addr); \ |
---|
179 | } \ |
---|
180 | else \ |
---|
181 | { \ |
---|
182 | new_addr = (new_type) old_addr; \ |
---|
183 | } \ |
---|
184 | } \ |
---|
185 | while (0) |
---|
186 | |
---|
187 | |
---|
188 | #define __omTypeRealloc0Bin(old_addr, old_bin, new_type, new_addr, new_bin) \ |
---|
189 | do \ |
---|
190 | { \ |
---|
191 | if (old_bin != new_bin) \ |
---|
192 | { \ |
---|
193 | size_t old_sizeW = (omIsNormalBinPageAddr(old_addr) ? old_bin->sizeW : omSizeWOfAddr(old_addr)); \ |
---|
194 | __omTypeAllocBin(new_type, new_addr, new_bin); \ |
---|
195 | omMemcpyW(new_addr, old_addr, (new_bin->sizeW > old_sizeW ? old_sizeW : new_bin->sizeW)); \ |
---|
196 | if (new_bin->sizeW > old_sizeW) \ |
---|
197 | omMemsetW((void**)new_addr + old_sizeW, 0, new_bin->sizeW - old_sizeW); \ |
---|
198 | __omFreeBinAddr(old_addr); \ |
---|
199 | } \ |
---|
200 | else \ |
---|
201 | { \ |
---|
202 | new_addr = (new_type) old_addr; \ |
---|
203 | } \ |
---|
204 | } \ |
---|
205 | while (0) |
---|
206 | |
---|
207 | /*******************************************************************/ |
---|
208 | /* Size */ |
---|
209 | #define omSmallSize2Bin(size) om_Size2Bin[((size) -1)>>LOG_SIZEOF_OM_ALIGNMENT] |
---|
210 | |
---|
211 | #define __omTypeAlloc(type, addr, size) \ |
---|
212 | do \ |
---|
213 | { \ |
---|
214 | size_t __size = size; \ |
---|
215 | if (__size <= OM_MAX_BLOCK_SIZE) \ |
---|
216 | { \ |
---|
217 | omBin __om_bin = omSmallSize2Bin(__size); \ |
---|
218 | __omTypeAllocBin(type, addr, __om_bin); \ |
---|
219 | } \ |
---|
220 | else \ |
---|
221 | { \ |
---|
222 | addr = (type) omAllocLarge(__size); \ |
---|
223 | } \ |
---|
224 | } \ |
---|
225 | while(0) |
---|
226 | |
---|
227 | #define __omTypeAlloc0(type, addr, size) \ |
---|
228 | do \ |
---|
229 | { \ |
---|
230 | size_t __size = size; \ |
---|
231 | if (__size <= OM_MAX_BLOCK_SIZE) \ |
---|
232 | { \ |
---|
233 | omBin __om_bin = omSmallSize2Bin(__size); \ |
---|
234 | __omTypeAlloc0Bin(type, addr, __om_bin); \ |
---|
235 | } \ |
---|
236 | else \ |
---|
237 | { \ |
---|
238 | addr = (type) omAlloc0Large(__size); \ |
---|
239 | } \ |
---|
240 | } \ |
---|
241 | while (0) |
---|
242 | |
---|
243 | #ifdef OM_ALIGNMENT_NEEDS_WORK |
---|
244 | #define omSmallSize2AlignedBin(size) om_Size2AlignedBin[((size) -1)>>LOG_SIZEOF_OM_ALIGNMENT] |
---|
245 | |
---|
246 | #define __omTypeAllocAligned(type, addr, size) \ |
---|
247 | do \ |
---|
248 | { \ |
---|
249 | size_t __size = size; \ |
---|
250 | if (__size <= OM_MAX_BLOCK_SIZE) \ |
---|
251 | { \ |
---|
252 | omBin __om_bin = omSmallSize2AlignedBin(__size); \ |
---|
253 | __omTypeAllocBin(type, addr, __om_bin); \ |
---|
254 | } \ |
---|
255 | else \ |
---|
256 | { \ |
---|
257 | addr = (type) omAllocLarge(__size); \ |
---|
258 | } \ |
---|
259 | } \ |
---|
260 | while(0) |
---|
261 | |
---|
262 | #define __omTypeAlloc0Aligned(type, addr, size) \ |
---|
263 | do \ |
---|
264 | { \ |
---|
265 | size_t __size = size; \ |
---|
266 | if (__size <= OM_MAX_BLOCK_SIZE) \ |
---|
267 | { \ |
---|
268 | omBin __om_bin = omSmallSize2AlignedBin(__size); \ |
---|
269 | __omTypeAlloc0Bin(type, addr, __om_bin); \ |
---|
270 | } \ |
---|
271 | else \ |
---|
272 | { \ |
---|
273 | addr = (type) omAlloc0Large(__size); \ |
---|
274 | } \ |
---|
275 | } \ |
---|
276 | while (0) |
---|
277 | #else |
---|
278 | #define __omTypeAllocAligned __omTypeAlloc |
---|
279 | #define __omTypeAlloc0Aligned __omTypeAlloc0 |
---|
280 | #endif /* OM_ALIGNMENT_NEEDS_WORK */ |
---|
281 | |
---|
282 | #define __omFreeSize(addr, size) \ |
---|
283 | do \ |
---|
284 | { \ |
---|
285 | if ((size <= OM_MAX_BLOCK_SIZE) || omIsBinPageAddr(addr)) \ |
---|
286 | { \ |
---|
287 | __omFreeBinAddr(addr); \ |
---|
288 | } \ |
---|
289 | else \ |
---|
290 | { \ |
---|
291 | omFreeLarge(addr); \ |
---|
292 | } \ |
---|
293 | } \ |
---|
294 | while (0) |
---|
295 | |
---|
296 | #define __omFree(addr) \ |
---|
297 | do \ |
---|
298 | { \ |
---|
299 | if (omIsBinPageAddr(addr)) \ |
---|
300 | { \ |
---|
301 | __omFreeBinAddr(addr); \ |
---|
302 | } \ |
---|
303 | else \ |
---|
304 | { \ |
---|
305 | omFreeLarge(addr); \ |
---|
306 | } \ |
---|
307 | } \ |
---|
308 | while (0) |
---|
309 | |
---|
310 | void* omDoRealloc(void* old_addr, size_t new_size, int flags); |
---|
311 | |
---|
312 | #define ___omTypeRealloc(old_addr, new_type, new_addr, new_size, SIZE_2_BIN, REALLOC_BIN, flags) \ |
---|
313 | do \ |
---|
314 | { \ |
---|
315 | size_t __new_size = new_size; \ |
---|
316 | if (__new_size <= OM_MAX_BLOCK_SIZE && omIsBinPageAddr(old_addr)) \ |
---|
317 | { \ |
---|
318 | omBin __old_bin = omGetBinOfAddr(old_addr); \ |
---|
319 | omBin __new_bin = SIZE_2_BIN(__new_size); \ |
---|
320 | REALLOC_BIN(old_addr, __old_bin, new_type, new_addr, __new_bin); \ |
---|
321 | } \ |
---|
322 | else \ |
---|
323 | { \ |
---|
324 | new_addr = (new_type) omDoRealloc(old_addr, __new_size, flags); \ |
---|
325 | } \ |
---|
326 | } \ |
---|
327 | while (0) |
---|
328 | |
---|
329 | #define ___omTypeReallocSize(old_addr, old_size, new_type, new_addr, new_size, SIZE_2_BIN, REALLOC_BIN, flags) \ |
---|
330 | do \ |
---|
331 | { \ |
---|
332 | size_t __new_size = new_size; \ |
---|
333 | if (__new_size <= OM_MAX_BLOCK_SIZE && old_size <= OM_MAX_BLOCK_SIZE) \ |
---|
334 | { \ |
---|
335 | omBin __old_bin = omGetBinOfAddr(old_addr); \ |
---|
336 | omBin __new_bin = SIZE_2_BIN(__new_size); \ |
---|
337 | REALLOC_BIN(old_addr, __old_bin, new_type, new_addr, __new_bin); \ |
---|
338 | } \ |
---|
339 | else \ |
---|
340 | { \ |
---|
341 | new_addr = (new_type) omDoRealloc(old_addr, __new_size, flags); \ |
---|
342 | } \ |
---|
343 | } \ |
---|
344 | while (0) |
---|
345 | |
---|
346 | #define __omTypeRealloc(old_addr, new_type, new_addr, new_size) \ |
---|
347 | ___omTypeRealloc(old_addr, new_type, new_addr, new_size, omSmallSize2Bin, __omTypeReallocBin, 0) |
---|
348 | #define __omTypeRealloc0(old_addr, new_type, new_addr, new_size) \ |
---|
349 | ___omTypeRealloc(old_addr, new_type, new_addr, new_size, omSmallSize2Bin, __omTypeRealloc0Bin, 1) |
---|
350 | #define __omTypeReallocSize(old_addr, old_size, new_type, new_addr, new_size) \ |
---|
351 | ___omTypeReallocSize(old_addr, old_size, new_type, new_addr, new_size, omSmallSize2Bin, __omTypeReallocBin, 0) |
---|
352 | #define __omTypeRealloc0Size(old_addr, old_size, new_type, new_addr, new_size) \ |
---|
353 | ___omTypeReallocSize(old_addr, old_size, new_type, new_addr, new_size, omSmallSize2Bin, __omTypeRealloc0Bin, 1) |
---|
354 | |
---|
355 | #ifdef OM_ALIGNMENT_NEEDS_WORK |
---|
356 | #define __omTypeReallocAligned(old_addr, new_type, new_addr, new_size) \ |
---|
357 | ___omTypeRealloc(old_addr, new_type, new_addr, new_size, omSmallSize2AlignedBin, __omTypeReallocBin, 2) |
---|
358 | #define __omTypeRealloc0Aligned(old_addr, new_type, new_addr, new_size) \ |
---|
359 | ___omTypeRealloc(old_addr, new_type, new_addr, new_size, omSmallSize2AlignedBin, __omTypeRealloc0Bin, 3) |
---|
360 | #define __omTypeReallocAlignedSize(old_addr, old_size, new_type, new_addr, new_size) \ |
---|
361 | ___omTypeReallocSize(old_addr, old_size, new_type, new_addr, new_size, omSmallSize2AlignedBin, __omTypeReallocBin, 2) |
---|
362 | #define __omTypeRealloc0AlignedSize(old_addr, old_size, new_type, new_addr, new_size) \ |
---|
363 | ___omTypeReallocSize(old_addr, old_size, new_type, new_addr, new_size, omSmallSize2AlignedBin, __omTypeRealloc0Bin, 3) |
---|
364 | #else |
---|
365 | #define __omTypeReallocAligned __omTypeRealloc |
---|
366 | #define __omTypeRealloc0Aligned __omTypeRealloc0 |
---|
367 | #define __omTypeReallocAlignedSize __omTypeReallocSize |
---|
368 | #define __omTypeRealloc0AlignedSize __omTypeRealloc0Size |
---|
369 | #endif |
---|
370 | |
---|
371 | #endif /* OM_GENERATE_INC */ |
---|
372 | #endif /* OM_ALLOC_PRIVATE_H */ |
---|