source: git/omalloc/omLocal.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: 7.2 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.2 1999-11-22 18:12:59 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 *******************************************************************/
75#include <stdio.h>
76extern int omIsStaticBin(omBin bin);
77extern void omPrintBinStats(FILE* fd);
78extern void omPrintBinStat(FILE * fd, omBin bin);
79
80/*******************************************************************
81 *
82 * om_assume(x) -- a handy macro for assumptions
83 *
84 ******************************************************************/
85#ifndef HAVE_OM_ASSUME
86
87#define omAssume(x) ((void) 0)
88
89#else /* ! HAVE_OM_ASSUME */
90
91#define omAssume(x)                             \
92do                                              \
93{                                               \
94  if (! (x))                                    \
95  {                                             \
96    omReportError("omAssume violation");        \
97  }                                             \
98}                                               \
99while (0)
100
101#endif /* HAVE_OM_ASSUME */
102
103extern const char* omReportError(const char* msg);
104#define omError(msg)  omReportError(msg)
105
106/*******************************************************************
107 *******************************************************************
108 **
109 ** Inlines
110 **
111 ******************************************************************
112 ******************************************************************/
113
114#if defined(OM_INLINE ) || defined(OM_ALLOC_C)
115#include <stdio.h>
116
117/**********************************************************************
118 *
119 * malloc/free routine from/to system
120 *
121 **********************************************************************/
122#ifdef OM_HAVE_STAT
123static size_t om_bytesMalloc = 0;
124static size_t om_bytesValloc = 0;
125#define omAddCounter(c, s)
126do
127{
128  (c) += (s);
129  OM_CHECK_PRINT(om_bytesMalloc, om_bytesValloc);
130}
131while (0)
132#define OM_STAT_REAL_SIZE(size) size = size + SIZEOF_OM_ALIGNMENT
133#define OM_STAT_GET_SIZEOF_ADDR(addr, size)     \
134do                                              \
135{                                               \
136  addr = addr - SIZEOF_OM_ALIGNMENT;            \
137  size = *((size_t*) addr);                     \
138}                                               \
139while (0)
140
141#define OM_STAT_SET_SIZEOF_PTR(ptr, size)       \
142do                                              \
143{                                               \
144  *((size_t*) ptr) = size;                      \
145  ptr = ptr + SIZEOF_OM_ALIGNMENT;              \
146}                                               \
147while (0)
148#define OM_STAT_DECLARE(x) x
149#else
150
151#define omAddCounter(c, s) do {} while(0)
152#define OM_STAT_REAL_SIZE(size) do {} while(0)
153#define OM_STAT_GET_SIZEOF_ADDR(addr, size) do {} while(0)
154#define OM_STAT_SET_SIZEOF_PTR(ptr, size) do {} while(0)
155#define OM_STAT_DECLARE(x) do {} while(0)
156
157#endif /* OM_HAVE_STAT */
158
159OM_INLINE void* omMallocFromSystem(size_t size)
160{
161  void* ptr;
162  OM_STAT_REAL_SIZE(size);
163
164  ptr = OM_MALLOC(size);
165  if (ptr == NULL)
166  {
167    OM_OUT_OF_MEMORY_HOOK();
168    omReleaseFreePages();
169    ptr = OM_MALLOC(size);
170    if (ptr == NULL)
171    {
172      OM_OUT_OF_MEMORY();
173      exit(1);
174    }
175  }
176  omAddCounter(om_bytesMalloc, size);
177  OM_STAT_SET_SIZEOF_PTR(ptr, size);
178  return ptr;
179}
180
181OM_INLINE void* omReallocFromSystem(void* addr, size_t newsize)
182{
183  void* res;
184  OM_STAT_DECLARE(size_t old_size);
185 
186  OM_STAT_REAL_SIZE(newsize);
187  OM_STAT_GET_SIZEOF_ADDR(addr, old_size);
188
189  res = OM_REALLOC(addr, newsize);
190  if (res == NULL)
191  {
192    OM_OUT_OF_MEMORY_HOOK();
193    omReleaseFreePages();
194    /* Can do a realloc again: manpage reads:
195       "If realloc() fails the original block is left untouched -
196       it is not freed or moved." */
197    res = OM_REALLOC(addr, newsize); 
198    if (res == NULL)
199    {
200      OM_OUT_OF_MEMORY();
201      /* should never get here */
202      exit(1);
203    }
204  }
205 
206  omAddCounter(om_bytesMalloc, (int) newsize - (int) new_size);
207  OM_STAT_SET_SIZEOF_PTR(res, new_size);
208 return res;
209}
210 
211OM_INLINE void omFreeToSystem(void* addr)
212{
213  OM_STAT_DECLARE(size_t size);
214  OM_STAT_GET_SIZEOF_ADDR(addr, size);
215  OM_FREE( addr );
216  omAddCounter(om_bytesMalloc, - size);
217}
218
219OM_INLINE void* omVallocFromSystem(size_t size)
220{
221  void* page = OM_VALLOC(size);
222  if (page == NULL)
223  {
224    OM_OUT_OF_MEMORY_HOOK();
225    omReleaseFreePages();
226    page = OM_VALLOC(size);
227    if (page == NULL)
228    {
229      OM_OUT_OF_MEMORY();
230      /* should never get here */
231      exit(1);
232    }
233  }
234  omAddCounter(om_bytesValloc, size);
235  return page;
236}
237
238OM_INLINE void omVfreeToSystem(void* page, size_t size)
239{
240  OM_VFREE(page);
241  omAddCounter(om_bytesValloc, -size);
242}
243
244#else /* ! defined(OM_INLINE ) || defined(OM_ALLOC_C) */
245
246extern void* omMallocFromSystem(size_t size);
247extern void* omReallocFromSystem(void* addr, size_t new_size);
248extern void  omFreeToSystem(void* addr);
249extern void* omVallocFromSystem(size_t size);
250extern void  omVfreeToSystem(void* addr, size_t size);
251
252#endif /* defined(OM_INLINE ) || defined(OM_ALLOC_C) */
253
254#endif /* OM_LOCAL_H */
Note: See TracBrowser for help on using the repository browser.