source: git/omalloc/omPrivate.h @ 212fc04

spielwiese
Last change on this file since 212fc04 was 212fc04, checked in by Olaf Bachmann <obachman@…>, 24 years ago
* as we go along git-svn-id: file:///usr/local/Singular/svn/trunk@3878 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 13.9 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.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 *******************************************************************/
43struct omBinPage_s;
44struct omBin_s;
45struct omSpecBin_s;
46typedef struct omBinPage_s omBinPage_t;
47typedef struct omBin_s     omBin_t;
48typedef struct omSpecBin_s omSpecBin_t;
49typedef omBinPage_t*       omBinPage;
50typedef omBin_t*           omBin;
51typedef omSpecBin_t*       omSpecBin;
52
53
54/* Need to define it here, has to be known to macros */
55struct 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 */
73struct 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
84struct 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
93extern  omBinPage_t om_ZeroPage[];
94extern  omBinPage_t om_CheckPage[];
95extern  omBinPage_t om_LargePage[];
96extern  omBin_t     om_LargeBin[];
97extern  omBin_t     om_CheckBin[];
98extern  omBin_t     om_StaticBin[];
99extern  omBin       om_Size2Bin[];
100
101#include "omTables.inc"
102 
103
104
105/*******************************************************************
106 * 
107 *  lowest level alloc/free macros
108 * 
109 *******************************************************************/
110extern void* omAllocBinFromFullPage(omBin bin);
111extern 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)             \
120do                                                          \
121{                                                           \
122  ((page)->used_blocks)++;                                  \
123  addr = (type)((page)->current);                           \
124  (page)->current =  *((void**) (page)->current);           \
125}                                                           \
126while (0)
127
128#define __omFreeToPage(addr, page)              \
129do                                              \
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}                                               \
142while (0)
143
144
145/*******************************************************************/
146/* Bin                                                             */
147#define __omTypeAllocBin(type, addr, bin)               \
148do                                                      \
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}                                                       \
156while (0)
157
158#define __omTypeAlloc0Bin(type, addr, bin)      \
159do                                              \
160{                                               \
161  __omTypeAllocBin(type, addr, bin);            \
162  omMemsetW(addr, 0, (bin)->sizeW);             \
163}                                               \
164while (0)
165
166#define __omFreeBin(addr)                                       \
167do                                                              \
168{                                                               \
169  register void* __om_addr = (void*) (addr);                    \
170  register omBinPage __om_page = omGetPageOfAddr(__om_addr);    \
171  __omFreeToPage(__om_addr, __om_page);                         \
172}                                                               \
173while (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)    \
185do                                              \
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}                                               \
197while(0)
198
199#define __omTypeAlloc0Block(type, addr, size)   \
200do                                              \
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}                                               \
213while (0)
214
215#define __omFreeBlock(addr, size)               \
216do                                              \
217{                                               \
218  if (size <= OM_MAX_BLOCK_SIZE)                \
219  {                                             \
220    __omFreeBin(addr);                          \
221  }                                             \
222  else                                          \
223  {                                             \
224    omFreeLargeBlock(addr, size);               \
225  }                                             \
226}                                               \
227while (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)                \
235do                                                          \
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}                                                           \
252while (0)
253
254#define __omTypeAlloc0Chunk(type, addr, size)               \
255do                                                          \
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}                                                           \
273while (0)
274
275#define __omFreeChunk(addr)                                 \
276do                                                          \
277{                                                           \
278  void* __addr = ((void*) (addr)) - SIZEOF_OM_ALIGNMENT;    \
279  omBinPage __om_page = *((omBinPage*) __addr);             \
280  __omFreeToPage(__addr, __om_page);                        \
281}                                                           \
282while (0)
283
284/*******************************************************************
285 * 
286 *  middle level
287 * 
288 *******************************************************************/
289
290#if defined(OM_INLINE)
291
292#define OM_ALLOCBIN_FUNC_WRAPPER(func)         \
293OM_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)         \
301OM_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)              \
309OM_INLINE void _om##func (void* addr)           \
310{                                               \
311  __om##func (addr);                            \
312}
313
314#define OM_FREEBLOCK_FUNC_WRAPPER(func)             \
315OM_INLINE void _om##func (void* addr, size_t size)  \
316{                                                   \
317  __om##func (addr, size);                          \
318}
319
320OM_ALLOCBIN_FUNC_WRAPPER(AllocBin)
321OM_ALLOCBIN_FUNC_WRAPPER(Alloc0Bin)
322OM_FREE_FUNC_WRAPPER(FreeBin)
323OM_ALLOCSIZE_FUNC_WRAPPER(AllocBlock)
324OM_ALLOCSIZE_FUNC_WRAPPER(Alloc0Block)
325OM_FREEBLOCK_FUNC_WRAPPER(FreeBlock)
326OM_ALLOCSIZE_FUNC_WRAPPER(AllocChunk)
327OM_ALLOCSIZE_FUNC_WRAPPER(Alloc0Chunk)
328OM_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
344omBin omGetSpecBin(size_t size);
345void  omUnGetSpecBin(omBin *bin);
346unsigned long omGetNewStickyAllBinTag();
347void omSetStickyAllBinTag(unsigned long sticky);
348void omUnSetStickyAllBinTag(unsigned long sticky);
349void omDeleteStickyAllBinTag(unsigned long sticky);
350
351#endif /* ! OM_GENERATE_INC */
352
353#endif /* OM_PRIVATE_H */
Note: See TracBrowser for help on using the repository browser.