source: git/Singular/mmheap.h @ fdc537

spielwiese
Last change on this file since fdc537 was ed6e65, checked in by Hans Schönemann <hannes@…>, 24 years ago
*hannes: mod2.h fomr .h files removed git-svn-id: file:///usr/local/Singular/svn/trunk@4109 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 5.0 KB
Line 
1#ifndef MM_HEAP_H
2#define MM_HEAP_H
3/****************************************
4*  Computer Algebra System SINGULAR     *
5****************************************/
6/* $Id: mmheap.h,v 1.13 2000-02-01 15:30:25 Singular Exp $ */
7
8#include "structs.h"
9#include <stdlib.h>
10
11
12#ifdef __cplusplus
13extern "C" {
14#endif
15
16/*****************************************************************
17 *
18 * Basic routines, see the beginning of mmheap.c for customizations
19 *
20 *****************************************************************/
21/* Initializes Heap */
22extern void mmInitHeap(memHeap heap, size_t size);
23/* creates and initializes heap */
24extern memHeap mmCreateHeap(size_t size);
25
26#ifndef HEAP_DEBUG
27/* UNCONDITIONALLY frees all pages of heap */
28extern void mmClearHeap(memHeap heap);
29/* UNCONDITIONALLY Clears and destroys heap */
30extern void mmDestroyHeap(memHeap *heap);
31#endif
32
33/* if all cunks of one page are in free list, then removes these
34   chunks from freelist
35   if strict == 0, does it only if current free pointer is different
36   from what it was the last time we did a GC
37*/
38extern void mmGarbageCollectHeap(memHeap heap, int strict);
39
40/* Merges what is free in Heap "what" into free list of heap "into" */
41void mmMergeHeap(memHeap into, memHeap what);
42
43/* Removes addr from freelist of heap, provided it finds it there */
44void mmRemoveFromCurrentHeap(memHeap heap, void* addr);
45
46
47/*****************************************************************
48 *
49 * Alloc, Free and Check for Heaps
50 *
51 *****************************************************************/
52#ifndef HEAP_DEBUG
53
54#define mmAllocHeap(res, heap)  _mmAllocHeap(res, heap)
55#define mmFreeHeap(addr, heap) _mmFreeHeap(addr, heap)
56
57#define mmCheckHeap(heap)           1
58#define mmCheckHeapAddr(addr, heap) 1
59
60#else
61/*
62 * define HEAP_DEBUG  and/or set mm_HEAP_DEBUG to
63 * 0 to enable basic heap addr checks (at least on each alloc/free)
64 * 1 for addtl. containment checks in free/alloc list of heap
65 * 2 for addtl. check of entire  heap at each heap addr check
66 * NOTE: For HEAP_DEBUG > 1 on, it gets very slow
67 */
68extern int mm_HEAP_DEBUG;
69
70#define mmAllocHeap(res, heap)\
71  (res) = mmDebugAllocHeap(heap, __FILE__, __LINE__)
72void * mmDebugAllocHeap(memHeap heap, const char*, int );
73
74#define mmFreeHeap(addr, heap)\
75  mmDebugFreeHeap(addr, heap, __FILE__, __LINE__)
76void   mmDebugFreeHeap(void* addr, memHeap heap, const char*, int );
77
78#define mmCheckHeap(heap)\
79  mmDebugCheckHeap(heap, __FILE__, __LINE__)
80int mmDebugCheckHeap(memHeap heap, const char* fn, int line);
81
82#define mmCheckHeapAddr(addr, heap) \
83  mmDebugCheckHeapAdr(addr, heap, MM_HEAP_ADDR_USED_FLAG, __FILE__, __LINE__)
84int mmDebugCheckHeapAddr(void* addr, memHeap heap, int flag,
85                         const char* fn, int l);
86
87
88#define mmClearHeap(h)   mmDebugClearHeap(h, __FILE__, __LINE__)
89#define mmDestroyHeap(h) mmDebugDestroyHeap(h, __FILE__, __LINE__)
90void mmDebugClearHeap(memHeap heap, const char* fn, int line);
91void mmDebugDestroyHeap(memHeap *heap, const char* fn, int line);
92#endif
93
94/* use this for unknown heaps */
95#define MM_UNKNOWN_HEAP ((memHeap) 1)
96
97/*****************************************************************
98 *
99 * Low-level allocation/ routines and declarations
100 *
101 *****************************************************************/
102
103struct sip_memHeapPage
104{
105  memHeapPage next;
106  long counter;
107};
108
109/* Change this appropriately, if you change sip_memHeapPage           */
110/* However, make sure that sizeof(sip_memHeapPage) is a multiple of 8 */
111#define SIZE_OF_HEAP_PAGE_HEADER (SIZEOF_VOIDP + SIZEOF_LONG)
112#define SIZE_OF_HEAP_PAGE (SIZE_OF_PAGE - SIZE_OF_HEAP_PAGE_HEADER)
113
114struct sip_memHeap
115{
116  void*         current; /* Freelist pointer */
117  memHeapPage   pages;   /* Pointer to linked list of pages */
118  void*         last_gc; /* current pointer after last gc */
119  int           size;    /* Size of heap chunks */
120};
121
122
123extern void mmAllocNewHeapPage(memHeap heap);
124
125/* Allocates memory block from a heap */
126#define _mmAllocHeap(what, heap)                            \
127do                                                          \
128{                                                           \
129  register memHeap _heap = heap;                            \
130  if ((_heap)->current == NULL) mmAllocNewHeapPage(_heap);  \
131  (void*) (what) = (void *)((_heap)->current);              \
132  (_heap)->current =  *((void**)(_heap)->current);          \
133}                                                           \
134while (0)
135
136/* Frees addr into heap, assumes  addr was previously allocated from heap */
137#define _mmFreeHeap(addr, heap)                \
138do                                              \
139{                                               \
140  register memHeap _heap = heap;                \
141  *((void**) addr) = (_heap)->current;          \
142  (_heap)->current = (void*) addr;              \
143}                                               \
144while (0)
145
146#define MM_HEAP_ADDR_UNKNOWN_FLAG 0
147#define MM_HEAP_ADDR_USED_FLAG   1
148#define MM_HEAP_ADDR_FREE_FLAG   2
149
150#ifdef __cplusplus
151}
152#endif
153
154#endif /* MM_HEAP_H */
155
Note: See TracBrowser for help on using the repository browser.