source: git/omalloc/omPrivate.h @ ba6f0c

spielwiese
Last change on this file since ba6f0c was ba6f0c, checked in by Olaf Bachmann <obachman@…>, 24 years ago
as we go along git-svn-id: file:///usr/local/Singular/svn/trunk@3930 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 21.0 KB
Line 
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.4 1999-11-26 17:57:54 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 *******************************************************************/
43struct omBinPage_s;
44typedef struct omBinPage_s omBinPage_t;
45typedef omBinPage_t*       omBinPage;
46
47
48/* Need to define it here, has to be known to macros */
49struct omBinPage_s
50{
51  long          used_blocks;    /* number of used blocks of this page */
52  void*         current;        /* pointer to current freelist */
53  omBinPage     next;           /* next/prev pointer of pages */
54  omBinPage     prev;
55  void*         bin_sticky;     /* bin this page belongs to with
56                                   sticky tag of page hidden in ptr*/
57};
58
59/* Change this appropriately, if you change omBinPage           */
60/* However, make sure that omBinPage is a multiple of SIZEOF_MAX_TYPE */
61#define _SIZEOF_OM_BIN_PAGE_HEADER (4*SIZEOF_VOIDP + SIZEOF_LONG + SIZEOF_LONG)
62/* make sure SIZEOF_OM_BIN_PAGE_HEADER is divisible by SIZEOF_OM_ALIGNMENT*/
63#define SIZEOF_OM_BIN_PAGE_HEADER OM_ALIGN_SIZE(_SIZEOF_OM_BIN_PAGE_HEADER)
64#define SIZEOF_OM_BIN_PAGE (SIZEOF_OM_PAGE - SIZEOF_OM_BIN_PAGE_HEADER)
65
66/* keep all members of omBin_s a sizeof(long) type,
67   otherwise list operations will fail */
68struct omBin_s
69{
70  omBinPage     current_page;   /* page of current freelist */
71  omBinPage     last_page;      /* pointer to last page of freelist */
72  omBin         next;           /* sticky bins of the same size */
73  long          sizeW;          /* size in words */
74  long          max_blocks;     /* if > 0   # blocks in one page,
75                                   if < 0   # pages for one block */
76  unsigned long sticky;         /* sticky tag */
77};
78
79struct omSpecBin_s
80{
81  omSpecBin        next;       /* pointer to next bin */
82  omBin            bin;        /* pointer to bin itself */
83  long             max_blocks; /* max_blocks of bin*/
84  long             ref;        /* ref count */
85};
86
87#ifndef OM_GENERATE_INC
88extern  omBinPage_t om_ZeroPage[];
89extern  omBinPage_t om_CheckPage[];
90extern  omBinPage_t om_LargePage[];
91extern  omBin_t     om_LargeBin[];
92extern  omBin_t     om_CheckBin[];
93extern  omBin_t     om_StaticBin[];
94extern  omBin       om_Size2Bin[];
95
96#include "omTables.inc"
97 
98/*******************************************************************
99 * 
100 *  lowest level alloc/free macros
101 * 
102 *******************************************************************/
103extern void* omAllocBinFromFullPage(omBin bin);
104extern void  omFreeToPageFault(omBinPage page, void* addr);
105
106#include "omMemOps.h"
107#include "omPage.h"
108
109
110/*******************************************************************/
111/* Page                                                            */
112#define __omTypeAllocFromPage(type, addr, page)             \
113do                                                          \
114{                                                           \
115  ((page)->used_blocks)++;                                  \
116  addr = (type)((page)->current);                           \
117  (page)->current =  *((void**) (page)->current);           \
118}                                                           \
119while (0)
120
121#define __omFreeToPage(addr, page)              \
122do                                              \
123{                                               \
124  if ((page)->used_blocks > 0)                  \
125  {                                             \
126    *((void**)addr) = (page)->current;          \
127    ((page)->used_blocks)--;                    \
128    (page)->current = (addr);                   \
129  }                                             \
130  else                                          \
131  {                                             \
132    omFreeToPageFault(page, addr);              \
133  }                                             \
134}                                               \
135while (0)
136
137
138/*******************************************************************/
139/* Bin                                                             */
140#define __omTypeAllocBin(type, addr, bin)               \
141do                                                      \
142{                                                       \
143  register omBinPage __om_page = (bin)->current_page;   \
144  if (__om_page->current != NULL)                       \
145    __omTypeAllocFromPage(type, addr, __om_page);       \
146  else                                                  \
147    addr = (type) omAllocBinFromFullPage(bin);          \
148}                                                       \
149while (0)
150
151#define __omTypeAlloc0Bin(type, addr, bin)      \
152do                                              \
153{                                               \
154  __omTypeAllocBin(type, addr, bin);            \
155  omMemsetW(addr, 0, (bin)->sizeW);             \
156}                                               \
157while (0)
158
159#define __omFreeBin(addr)                                       \
160do                                                              \
161{                                                               \
162  register void* __om_addr = (void*) (addr);                    \
163  register omBinPage __om_page = omGetPageOfAddr(__om_addr);    \
164  __omFreeToPage(__om_addr, __om_page);                         \
165}                                                               \
166while (0)
167
168
169/*******************************************************************/
170/* Block                                                           */
171#define omSmallSize2Bin(size) om_Size2Bin[((size) -1)>>LOG_SIZEOF_OM_ALIGNMENT]
172#define omSize2Bin(size) ((size) <= OM_MAX_BLOCK_SIZE ? \
173                          omSmallSize2Bin(size) : om_LargeBin)
174
175#define omAllocLargeBlock(size)       OM_MALLOC(size)
176#define omFreeLargeBlock(addr, size)  OM_FREE(addr)
177
178#define __omTypeAllocBlock(type, addr, size)    \
179do                                              \
180{                                               \
181  if (size <= OM_MAX_BLOCK_SIZE)                \
182  {                                             \
183    omBin __om_bin = omSmallSize2Bin(size);     \
184    __omTypeAllocBin(type, addr, __om_bin);     \
185  }                                             \
186  else                                          \
187  {                                             \
188    addr = (type) omAllocLargeBlock(size);      \
189  }                                             \
190}                                               \
191while(0)
192
193#define __omTypeAlloc0Block(type, addr, size)   \
194do                                              \
195{                                               \
196  if (size <= OM_MAX_BLOCK_SIZE)                \
197  {                                             \
198    omBin __om_bin = omSmallSize2Bin(size);     \
199    __omTypeAlloc0Bin(type, addr, __om_bin);    \
200  }                                             \
201  else                                          \
202  {                                             \
203    addr = (type) omAllocLargeBlock(size);      \
204    memset(addr, 0, size);                      \
205  }                                             \
206}                                               \
207while (0)
208
209#ifdef OM_ALIGNMENT_NEEDS_WORK
210#define __omTypeAllocAlignedBlock(type, addr, size) \
211do                                                  \
212{                                                   \
213  if (size <= OM_MAX_BLOCK_SIZE)                    \
214  {                                                 \
215    omBin __om_bin = omSmallSize2AlignedBin(size);  \
216    __omTypeAllocBin(type, addr, __om_bin);         \
217  }                                                 \
218  else                                              \
219  {                                                 \
220    addr = (type) omAllocLargeBlock(size);          \
221  }                                                 \
222}                                                   \
223while(0)
224
225#define __omTypeAlloc0AlignedBlock(type, addr, size)    \
226do                                                      \
227{                                                       \
228  if (size <= OM_MAX_BLOCK_SIZE)                        \
229  {                                                     \
230    omBin __om_bin = omSmallSize2AlignedBin(size);      \
231    __omTypeAlloc0Bin(type, addr, __om_bin);            \
232  }                                                     \
233  else                                                  \
234  {                                                     \
235    addr = (type) omAllocLargeBlock(size);              \
236    memset(addr, 0, size);                              \
237  }                                                     \
238}                                                       \
239while (0)
240
241#else /* ! OM_ALIGNMENT_NEEDS_WORK */
242#define __omTypeAllocAlignedBlock  __omTypeAllocBlock
243#define __omTypeAlloc0AlignedBlock __omTypeAlloc0Block
244#endif /* OM_ALIGNMENT_NEEDS_WORK */
245     
246#define __omFreeBlock(addr, size)               \
247do                                              \
248{                                               \
249  if (size <= OM_MAX_BLOCK_SIZE)                \
250  {                                             \
251    __omFreeBin(addr);                          \
252  }                                             \
253  else                                          \
254  {                                             \
255    omFreeLargeBlock(addr, size);               \
256  }                                             \
257}                                               \
258while (0)
259
260/*******************************************************************/
261/* Chunk                                                           */
262#define omAllocLargeChunk(size)  OM_MALLOC(size)
263#define omFreeLargeChunk(addr)   OM_FREE(addr)
264 
265#define __omTypeAllocChunk(type, addr, size)                \
266do                                                          \
267{                                                           \
268  void* __om_addr;                                          \
269  size_t __om_size = (size) + SIZEOF_OM_ALIGNMENT;          \
270  if (__om_size <= OM_MAX_BLOCK_SIZE)                       \
271  {                                                         \
272    omBin __om_bin = omSmallSize2Bin(__om_size);            \
273    __omTypeAllocBin(void*, __om_addr, __om_bin);           \
274    *((void**) __om_addr) = (void*) __om_bin->current_page; \
275  }                                                         \
276  else                                                      \
277  {                                                         \
278    __om_addr = omAllocLargeChunk(__om_size);               \
279    *((void**) __om_addr) = (void*) om_LargePage;           \
280  }                                                         \
281  addr = (type) (__om_addr + SIZEOF_OM_ALIGNMENT);          \
282}                                                           \
283while (0)
284
285#define __omTypeAlloc0Chunk(type, addr, size)               \
286do                                                          \
287{                                                           \
288  void* __om_addr;                                          \
289  size_t __om_size = (size) + SIZEOF_OM_ALIGNMENT;          \
290  if (__om_size <= OM_MAX_BLOCK_SIZE)                       \
291  {                                                         \
292    omBin __om_bin = omSmallSize2Bin(__om_size);            \
293    __omTypeAlloc0Bin(void*, __om_addr, __om_bin);          \
294    *((void**) __om_addr) = (void*) __om_bin->current_page; \
295  }                                                         \
296  else                                                      \
297  {                                                         \
298    __om_addr = omAllocLargeChunk(__om_size);               \
299    memset(__om_addr, 0, __om_size);                        \
300    *((void**) __om_addr) = (void*) om_LargePage;           \
301  }                                                         \
302  addr = (type) (__om_addr + SIZEOF_OM_ALIGNMENT);          \
303}                                                           \
304while (0)
305
306#ifdef OM_ALIGNMENT_NEEDS_WORK
307
308#define __omTypeAllocAlignedChunk(type, addr, size)         \
309do                                                          \
310{                                                           \
311  void* __om_addr;                                          \
312  size_t __om_size = (size) + SIZEOF_OM_CHUNK_ALIGNMENT;    \
313  if (__om_size <= OM_MAX_BLOCK_SIZE)                       \
314  {                                                         \
315    omBin __om_bin = omSmallSize2AlignedBin(__om_size);     \
316    __omTypeAllocBin(void*, __om_addr, __om_bin);           \
317    *((void**) __om_addr) = (void*) __om_bin->current_page; \
318  }                                                         \
319  else                                                      \
320  {                                                         \
321    __om_addr = omAllocLargeChunk(__om_size);               \
322    *((void**) __om_addr) = (void*) om_LargePage;           \
323  }                                                         \
324  addr = (type) (__om_addr + SIZEOF_OM_CHUNK_ALIGNMENT);    \
325}                                                           \
326while (0)
327
328#define __omTypeAlloc0AlignedChunk(type, addr, size)        \
329do                                                          \
330{                                                           \
331  void* __om_addr;                                          \
332  size_t __om_size = (size) + SIZEOF_OM_CHUNK_ALIGNMENT;    \
333  if (__om_size <= OM_MAX_BLOCK_SIZE)                       \
334  {                                                         \
335    omBin __om_bin = omSmallSize2Bin(__om_size);            \
336    __omTypeAlloc0Bin(void*, __om_addr, __om_bin);          \
337    *((void**) __om_addr) = (void*) __om_bin->current_page; \
338  }                                                         \
339  else                                                      \
340  {                                                         \
341    __om_addr = omAllocLargeChunk(__om_size);               \
342    memset(__om_addr, 0, __om_size);                        \
343    *((void**) __om_addr) = (void*) om_LargePage;           \
344  }                                                         \
345  addr = (type) (__om_addr + SIZEOF_OM_CHUNK_ALIGNMENT);    \
346}                                                           \
347while (0)
348
349#define __omFreeAlignedChunk(addr)                              \
350do                                                              \
351{                                                               \
352  void* __addr = ((void*) (addr)) - SIZEOF_OM_CHUNK_ALIGNMENT;  \
353  omBinPage __om_page = *((omBinPage*) __addr);                 \
354  __omFreeToPage(__addr, __om_page);                            \
355}                                                               \
356while (0)
357
358#else /* ! OM_ALIGNMENT_NEEDS_WORK */
359#define __omTypeAllocAlignedChunk  __omTypeAllocChunk
360#define __omTypeAlloc0AlignedChunk __omTypeAlloc0Chunk
361#define __omFreeAlignedChunk       __omFreeChunk
362#endif /* OM_ALIGNMENT_NEEDS_WORK */
363
364#define __omFreeChunk(addr)                                 \
365do                                                          \
366{                                                           \
367  void* __addr = ((void*) (addr)) - SIZEOF_OM_ALIGNMENT;    \
368  omBinPage __om_page = *((omBinPage*) __addr);             \
369  __omFreeToPage(__addr, __om_page);                        \
370}                                                           \
371while (0)
372
373/*******************************************************************
374 * 
375 *  middle level
376 * 
377 *******************************************************************/
378
379#if defined(OM_INLINE)
380
381#define OM_ALLOCBIN_FUNC_WRAPPER(func)          \
382OM_INLINE void* _om##func (omBin bin)           \
383{                                               \
384  void* addr;                                   \
385  __omType##func (void*, addr, bin);            \
386  return addr;                                  \
387}
388
389#define OM_ALLOCSIZE_FUNC_WRAPPER(func)         \
390OM_INLINE void* _om##func (size_t size)         \
391{                                               \
392  void* addr;                                   \
393  __omType##func (void*, addr, size);           \
394  return addr;                                  \
395}
396
397#define OM_FREE_FUNC_WRAPPER(func)              \
398OM_INLINE void _om##func (void* addr)           \
399{                                               \
400  __om##func (addr);                            \
401}
402
403#define OM_FREEBLOCK_FUNC_WRAPPER(func)             \
404OM_INLINE void _om##func (void* addr, size_t size)  \
405{                                                   \
406  __om##func (addr, size);                          \
407}
408
409OM_ALLOCBIN_FUNC_WRAPPER(AllocBin)
410OM_ALLOCBIN_FUNC_WRAPPER(Alloc0Bin)
411OM_FREE_FUNC_WRAPPER(FreeBin)
412OM_ALLOCSIZE_FUNC_WRAPPER(AllocBlock)
413OM_ALLOCSIZE_FUNC_WRAPPER(Alloc0Block)
414OM_FREEBLOCK_FUNC_WRAPPER(FreeBlock)
415OM_ALLOCSIZE_FUNC_WRAPPER(AllocChunk)
416OM_ALLOCSIZE_FUNC_WRAPPER(Alloc0Chunk)
417OM_FREE_FUNC_WRAPPER(FreeChunk)
418
419#ifdef OM_ALIGNMENT_NEEDS_WORK
420OM_ALLOCSIZE_FUNC_WRAPPER(AllocAlignedBlock)
421OM_ALLOCSIZE_FUNC_WRAPPER(Alloc0AlignedBlock)
422OM_ALLOCSIZE_FUNC_WRAPPER(AllocAlignedChunk)
423OM_ALLOCSIZE_FUNC_WRAPPER(Alloc0AlignedChunk)
424OM_FREE_FUNC_WRAPPER(FreeAlignedChunk)
425#else
426#define _omAllocAlignedBlock    _omAllocBlock
427#define _omAlloc0AlignedBlock   _omAlloc0Block
428#define _omAllocAlignedChunk    _omAllocChunk
429#define _omFreeAlignedChunk     _omFreeChunk
430#endif /* OM_ALIGNMENT_NEEDS_WORK */
431
432#else /* ! OM_INLINE */
433
434#define _omAllocBin      omFuncAllocBin
435#define _omAlloc0Bin     omFuncAlloc0Bin
436#define _omFreeBin       omFuncFreeBin
437#define _omAllocBlock    omFuncAllocBlock
438#define _omAlloc0Block   omFuncAlloc0Block
439#define _omFreeBlock     omFuncFreeBlock
440#define _omAlloc         omFuncAlloc
441#define _omAlloc0        omFuncAlloc0
442#define _omFree          omFuncFree
443#define _omAllocAlignedBlock    omFuncAllocAlignedBlock
444#define _omAlloc0AlignedBlock   omFuncAlloc0AlignedBlock
445#define _omAllocAlignedChunk    omFuncAllocAlignedChunk
446#define _omFreeAlignedChunk     omFuncFreeAlignedChunk
447
448#endif /* OM_INLINE */
449
450void* _omReallocBlock(void* old_addr, size_t old_size, size_t new_size);
451void* _omRealloc0Block(void* old_addr, size_t old_size, size_t new_size);
452void* _omReallocChunk(void* old_addr, size_t new_size);
453
454#ifdef OM_ALIGNMENT_NEEDS_WORK
455void* _omReallocAlignedBlock(void* old_addr, size_t old_size, size_t new_size);
456void* _omRealloc0AlignedBlock(void* old_addr, size_t old_size,size_t new_size);
457void* _omReallocAlignedChunk(void* old_addr, size_t new_size);
458#else
459#define _omReallocAlignedBlock  _omRealllocBlock
460#define _omRealloc0AlignedBlock _omReallloc0Block
461#define _omReallocAlignedChunk  _omReallocChunk
462#endif
463
464/*******************************************************************
465 * 
466 *  Declaration of Func
467 * 
468 *******************************************************************/
469void* omFuncAllocBin(omBin bin);
470void* omFuncAlloc0Bin(omBin bin);
471void  omFuncFreeBin(void* addr);
472
473void* omFuncAllocBlock(size_t size);
474void* omFuncAlloc0Block(size_t size);
475void  omFuncFreeBlock(void* addr, size_t size);
476#define omFuncReallocBlock  _omReallocBlock
477#define omFuncRealloc0Block _omRealloc0Block
478
479void* omFuncAllocChunk(size_t size);
480void* omFuncAlloc0Chunk(size_t size);
481void  omFuncFreeChunk(void* addr);
482#define omFuncReallocChunk  _omReallocChunk
483
484#ifdef OM_ALIGNMENT_NEEDS_WORK
485void* omFuncAllocAlignedBlock(size_t size);
486void* omFuncAlloc0AlignedBlock(size_t size);
487#define omFuncReallocAlignedBlock  _omReallocAlignedBlock
488#define omFuncRealloc0AlignedBlock _omRealloc0AlignedBlock
489
490void* omFuncAllocAlignedChunk(size_t size);
491void* omFuncAllocAligned0Chunk(size_t size);
492void  omFuncFreeAlignedChunk(void* addr);
493#define omFuncReallocAlignedChunk  _omReallocAlignedChunk
494#else
495#define omFuncAllocAlignedBlock    omFuncAllocBlock
496#define omFuncAlloc0AlignedBlock   omFuncAlloc0Block
497#define omFuncReallocAlignedBlock  omFuncReallocBlock
498#define omFuncRealloc0AlignedBlock omFuncRealloc0Block
499
500#define omFuncAllocAlignedChunk    omFuncAllocChunk
501#define omFuncAlloc0AlignedChunk   omFuncAlloc0Chunk
502#define omFuncReallocAlignedChunk  omFuncReallocBlock
503#define omFuncFreeAlignedChunk     omFuncFreeChunk
504#endif /* OM_ALIGNMENT_NEEDS_WORK */ 
505
506
507
508#endif /* ! OM_GENERATE_INC */
509
510#endif /* OM_PRIVATE_H */
Note: See TracBrowser for help on using the repository browser.