source: git/omalloc/omLocal.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: 7.1 KB
Line 
1/*******************************************************************
2 *  File:    omLocal.h
3 *  Purpose: declaration of "local" (not visible to the outside)
4 *           routines for omalloc
5 *  Author:  obachman (Olaf Bachmann)
6 *  Created: 11/99
7 *  Version: $Id: omLocal.h,v 1.3 1999-11-26 17:57:53 obachman Exp $
8 *******************************************************************/
9#ifndef OM_LOCAL_H
10#define OM_LOCAL_H
11
12#include "omConfig.h"
13
14/*******************************************************************
15 * 
16 *  Internal defines
17 * 
18 *******************************************************************/
19/* define if you want to keep size of malloc'ed chunks */
20
21#define OM_DEBUG_LEVEL 3
22/*******************************************************************
23 * 
24 *  Misc things
25 * 
26 *******************************************************************/
27extern omSpecBin om_SpecBin;
28
29/*******************************************************************
30 * 
31 *  Working with pages/bins
32 * 
33 *******************************************************************/
34#define omGetTopBinOfPage(page) \
35  ((omBin) ( ((unsigned long) (page->bin_sticky)) & ~(SIZEOF_VOIDP - 1)))
36#define omGetStickyOfPage(page) \
37  (((unsigned long) (page->bin_sticky)) & (SIZEOF_VOIDP-1))
38#define omSetTopBinOfPage(page, bin) \
39  (page)->bin_sticky= (void*)((unsigned long)bin + omGetStickyOfPage(page))
40#define omSetStickyOfPage(page, sticky) \
41  (page)->bin_sticky = (void*)((unsigned long)sticky + \
42                                (unsigned long)omGetTopBinOfPage(page))
43#define omSetTopBinAndStickyOfPage(page, bin, sticky) \
44  (page)->bin_sticky= (void*)((unsigned long)sticky + (unsigned long)bin)
45
46#define omGetTopBinOfAddr(addr) \
47  omGetTopBinOfPage(((omBinPage) omGetPageOfAddr(addr)))
48
49#if defined(OM_INLINE) || defined(OM_ALLOC_C)
50OM_INLINE omBin omGetBinOfPage(omBinPage page)
51{
52  unsigned long sticky = omGetStickyOfPage(page);
53  omBin bin = omGetTopBinOfPage(page);
54 
55  while (bin->sticky != sticky && bin->next != NULL) 
56  {
57    bin = bin->next;
58  }
59  return bin;
60}
61OM_INLINE omBin omGetBinOfAddr(void* addr)
62{
63  return omGetBinOfPage(omGetPageOfAddr(addr));
64}
65#else
66extern omBin omGetBinOfPage(omBinPage page);
67extern omBin omGetBinOfAddr(void* addr);
68#endif /* defined(OM_INLINE) || defined(OM_ALLOC_C) */
69
70/*******************************************************************
71 * 
72 *  Declarations
73 * 
74 *******************************************************************/
75extern int omIsStaticBin(omBin bin);
76
77/*******************************************************************
78 *
79 * om_assume(x) -- a handy macro for assumptions
80 *
81 ******************************************************************/
82#ifndef HAVE_OM_ASSUME
83
84#define omAssume(x) ((void) 0)
85
86#else /* ! HAVE_OM_ASSUME */
87
88#define omAssume(x)                             \
89do                                              \
90{                                               \
91  if (! (x))                                    \
92  {                                             \
93    omReportError("omAssume violation");        \
94  }                                             \
95}                                               \
96while (0)
97
98#endif /* HAVE_OM_ASSUME */
99
100extern const char* omReportError(const char* msg);
101#define omError(msg)  omReportError(msg)
102
103/*******************************************************************
104 *******************************************************************
105 **
106 ** Inlines
107 **
108 ******************************************************************
109 ******************************************************************/
110
111#if defined(OM_INLINE ) || defined(OM_ALLOC_C)
112#include <stdio.h>
113
114/**********************************************************************
115 *
116 * malloc/free routine from/to system
117 *
118 **********************************************************************/
119#ifdef OM_HAVE_STAT
120static size_t om_bytesMalloc = 0;
121static size_t om_bytesValloc = 0;
122#define omAddCounter(c, s)
123do
124{
125  (c) += (s);
126  OM_CHECK_PRINT(om_bytesMalloc, om_bytesValloc);
127}
128while (0)
129#define OM_STAT_REAL_SIZE(size) size = size + SIZEOF_OM_ALIGNMENT
130#define OM_STAT_GET_SIZEOF_ADDR(addr, size)     \
131do                                              \
132{                                               \
133  addr = addr - SIZEOF_OM_ALIGNMENT;            \
134  size = *((size_t*) addr);                     \
135}                                               \
136while (0)
137
138#define OM_STAT_SET_SIZEOF_PTR(ptr, size)       \
139do                                              \
140{                                               \
141  *((size_t*) ptr) = size;                      \
142  ptr = ptr + SIZEOF_OM_ALIGNMENT;              \
143}                                               \
144while (0)
145#define OM_STAT_DECLARE(x) x
146#else
147
148#define omAddCounter(c, s) do {} while(0)
149#define OM_STAT_REAL_SIZE(size) do {} while(0)
150#define OM_STAT_GET_SIZEOF_ADDR(addr, size) do {} while(0)
151#define OM_STAT_SET_SIZEOF_PTR(ptr, size) do {} while(0)
152#define OM_STAT_DECLARE(x) do {} while(0)
153
154#endif /* OM_HAVE_STAT */
155
156OM_INLINE void* omMallocFromSystem(size_t size)
157{
158  void* ptr;
159  OM_STAT_REAL_SIZE(size);
160
161  ptr = OM_MALLOC(size);
162  if (ptr == NULL)
163  {
164    OM_OUT_OF_MEMORY_HOOK();
165    omReleaseFreePages();
166    ptr = OM_MALLOC(size);
167    if (ptr == NULL)
168    {
169      OM_OUT_OF_MEMORY();
170      exit(1);
171    }
172  }
173  omAddCounter(om_bytesMalloc, size);
174  OM_STAT_SET_SIZEOF_PTR(ptr, size);
175  return ptr;
176}
177
178OM_INLINE void* omReallocFromSystem(void* addr, size_t newsize)
179{
180  void* res;
181  OM_STAT_DECLARE(size_t old_size);
182 
183  OM_STAT_REAL_SIZE(newsize);
184  OM_STAT_GET_SIZEOF_ADDR(addr, old_size);
185
186  res = OM_REALLOC(addr, newsize);
187  if (res == NULL)
188  {
189    OM_OUT_OF_MEMORY_HOOK();
190    omReleaseFreePages();
191    /* Can do a realloc again: manpage reads:
192       "If realloc() fails the original block is left untouched -
193       it is not freed or moved." */
194    res = OM_REALLOC(addr, newsize); 
195    if (res == NULL)
196    {
197      OM_OUT_OF_MEMORY();
198      /* should never get here */
199      exit(1);
200    }
201  }
202 
203  omAddCounter(om_bytesMalloc, (int) newsize - (int) new_size);
204  OM_STAT_SET_SIZEOF_PTR(res, new_size);
205 return res;
206}
207 
208OM_INLINE void omFreeToSystem(void* addr)
209{
210  OM_STAT_DECLARE(size_t size);
211  OM_STAT_GET_SIZEOF_ADDR(addr, size);
212  OM_FREE( addr );
213  omAddCounter(om_bytesMalloc, - size);
214}
215
216OM_INLINE void* omVallocFromSystem(size_t size)
217{
218  void* page = OM_VALLOC(size);
219  if (page == NULL)
220  {
221    OM_OUT_OF_MEMORY_HOOK();
222    omReleaseFreePages();
223    page = OM_VALLOC(size);
224    if (page == NULL)
225    {
226      OM_OUT_OF_MEMORY();
227      /* should never get here */
228      exit(1);
229    }
230  }
231  omAddCounter(om_bytesValloc, size);
232  return page;
233}
234
235OM_INLINE void omVfreeToSystem(void* page, size_t size)
236{
237  OM_VFREE(page);
238  omAddCounter(om_bytesValloc, -size);
239}
240
241#else /* ! defined(OM_INLINE ) || defined(OM_ALLOC_C) */
242
243extern void* omMallocFromSystem(size_t size);
244extern void* omReallocFromSystem(void* addr, size_t new_size);
245extern void  omFreeToSystem(void* addr);
246extern void* omVallocFromSystem(size_t size);
247extern void  omVfreeToSystem(void* addr, size_t size);
248
249#endif /* defined(OM_INLINE ) || defined(OM_ALLOC_C) */
250
251#endif /* OM_LOCAL_H */
Note: See TracBrowser for help on using the repository browser.