mem_r.h
Go to the documentation of this file.
1 /*<html><pre> -<a href="qh-mem_r.htm"
2  >-------------------------------</a><a name="TOP">-</a>
3 
4  mem_r.h
5  prototypes for memory management functions
6 
7  see qh-mem_r.htm, mem_r.c and qset_r.h
8 
9  for error handling, writes message and calls
10  qh_errexit(qhT *qh, qhmem_ERRmem, NULL, NULL) if insufficient memory
11  and
12  qh_errexit(qhT *qh, qhmem_ERRqhull, NULL, NULL) otherwise
13 
14  Copyright (c) 1993-2015 The Geometry Center.
15  $Id: //main/2015/qhull/src/libqhull_r/mem_r.h#3 $$Change: 2062 $
16  $DateTime: 2016/01/17 13:13:18 $$Author: bbarber $
17 */
18 
19 #ifndef qhDEFmem
20 #define qhDEFmem 1
21 
22 #include <stdio.h>
23 
24 #ifndef DEFsetT
25 #define DEFsetT 1
26 typedef struct setT setT; /* defined in qset_r.h */
27 #endif
28 
29 #ifndef DEFqhT
30 #define DEFqhT 1
31 typedef struct qhT qhT; /* defined in libqhull_r.h */
32 #endif
33 
34 /*-<a href="qh-mem_r.htm#TOC"
35  >-------------------------------</a><a name="NOmem">-</a>
36 
37  qh_NOmem
38  turn off quick-fit memory allocation
39 
40  notes:
41  mem_r.c implements Quickfit memory allocation for about 20% time
42  savings. If it fails on your machine, try to locate the
43  problem, and send the answer to qhull@qhull.org. If this can
44  not be done, define qh_NOmem to use malloc/free instead.
45 
46  #define qh_NOmem
47 */
48 
49 /*-<a href="qh-mem_r.htm#TOC"
50 >-------------------------------</a><a name="TRACEshort">-</a>
51 
52 qh_TRACEshort
53 Trace short and quick memory allocations at T5
54 
55 */
56 #define qh_TRACEshort
57 
58 /*-------------------------------------------
59  to avoid bus errors, memory allocation must consider alignment requirements.
60  malloc() automatically takes care of alignment. Since mem_r.c manages
61  its own memory, we need to explicitly specify alignment in
62  qh_meminitbuffers().
63 
64  A safe choice is sizeof(double). sizeof(float) may be used if doubles
65  do not occur in data structures and pointers are the same size. Be careful
66  of machines (e.g., DEC Alpha) with large pointers. If gcc is available,
67  use __alignof__(double) or fmax_(__alignof__(float), __alignof__(void *)).
68 
69  see <a href="user.h#MEMalign">qh_MEMalign</a> in user.h for qhull's alignment
70 */
71 
72 #define qhmem_ERRmem 4 /* matches qh_ERRmem in libqhull_r.h */
73 #define qhmem_ERRqhull 5 /* matches qh_ERRqhull in libqhull_r.h */
74 
75 /*-<a href="qh-mem_r.htm#TOC"
76  >--------------------------------</a><a name="ptr_intT">-</a>
77 
78  ptr_intT
79  for casting a void * to an integer-type that holds a pointer
80  Used for integer expressions (e.g., computing qh_gethash() in poly_r.c)
81 
82  notes:
83  WARN64 -- these notes indicate 64-bit issues
84  On 64-bit machines, a pointer may be larger than an 'int'.
85  qh_meminit()/mem_r.c checks that 'ptr_intT' holds a 'void*'
86  ptr_intT is typically a signed value, but not necessarily so
87  size_t is typically unsigned, but should match the parameter type
88  Qhull uses int instead of size_t except for system calls such as malloc, qsort, qh_malloc, etc.
89  This matches Qt convention and is easier to work with.
90 */
91 #if (defined(__MINGW64__)) && defined(_WIN64)
92 typedef long long ptr_intT;
93 #elif (_MSC_VER) && defined(_WIN64)
94 typedef long long ptr_intT;
95 #else
96 typedef long ptr_intT;
97 #endif
98 
99 /*-<a href="qh-mem_r.htm#TOC"
100  >--------------------------------</a><a name="qhmemT">-</a>
101 
102  qhmemT
103  global memory structure for mem_r.c
104 
105  notes:
106  users should ignore qhmem except for writing extensions
107  qhmem is allocated in mem_r.c
108 
109  qhmem could be swapable like qh and qhstat, but then
110  multiple qh's and qhmem's would need to keep in synch.
111  A swapable qhmem would also waste memory buffers. As long
112  as memory operations are atomic, there is no problem with
113  multiple qh structures being active at the same time.
114  If you need separate address spaces, you can swap the
115  contents of qh->qhmem.
116 */
117 typedef struct qhmemT qhmemT;
118 
119 /* Update qhmem in mem_r.c if add or remove fields */
120 struct qhmemT { /* global memory management variables */
121  int BUFsize; /* size of memory allocation buffer */
122  int BUFinit; /* initial size of memory allocation buffer */
123  int TABLEsize; /* actual number of sizes in free list table */
124  int NUMsizes; /* maximum number of sizes in free list table */
125  int LASTsize; /* last size in free list table */
126  int ALIGNmask; /* worst-case alignment, must be 2^n-1 */
127  void **freelists; /* free list table, linked by offset 0 */
128  int *sizetable; /* size of each freelist */
129  int *indextable; /* size->index table */
130  void *curbuffer; /* current buffer, linked by offset 0 */
131  void *freemem; /* free memory in curbuffer */
132  int freesize; /* size of freemem in bytes */
133  setT *tempstack; /* stack of temporary memory, managed by users */
134  FILE *ferr; /* file for reporting errors when 'qh' may be undefined */
135  int IStracing; /* =5 if tracing memory allocations */
136  int cntquick; /* count of quick allocations */
137  /* Note: removing statistics doesn't effect speed */
138  int cntshort; /* count of short allocations */
139  int cntlong; /* count of long allocations */
140  int freeshort; /* count of short memfrees */
141  int freelong; /* count of long memfrees */
142  int totbuffer; /* total short memory buffers minus buffer links */
143  int totdropped; /* total dropped memory at end of short memory buffers (e.g., freesize) */
144  int totfree; /* total size of free, short memory on freelists */
145  int totlong; /* total size of long memory in use */
146  int maxlong; /* maximum totlong */
147  int totshort; /* total size of short memory in use */
148  int totunused; /* total unused short memory (estimated, short size - request size of first allocations) */
149  int cntlarger; /* count of setlarger's */
150  int totlarger; /* total copied by setlarger */
151 };
152 
153 
154 /*==================== -macros ====================*/
155 
156 /*-<a href="qh-mem_r.htm#TOC"
157  >--------------------------------</a><a name="memalloc_">-</a>
158 
159  qh_memalloc_(qh, insize, freelistp, object, type)
160  returns object of size bytes
161  assumes size<=qh->qhmem.LASTsize and void **freelistp is a temp
162 */
163 
164 #if defined qh_NOmem
165 #define qh_memalloc_(qh, insize, freelistp, object, type) {\
166  object= (type*)qh_memalloc(qh, insize); }
167 #elif defined qh_TRACEshort
168 #define qh_memalloc_(qh, insize, freelistp, object, type) {\
169  freelistp= NULL; /* Avoid warnings */ \
170  object= (type*)qh_memalloc(qh, insize); }
171 #else /* !qh_NOmem */
172 
173 #define qh_memalloc_(qh, insize, freelistp, object, type) {\
174  freelistp= qh->qhmem.freelists + qh->qhmem.indextable[insize];\
175  if ((object= (type*)*freelistp)) {\
176  qh->qhmem.totshort += qh->qhmem.sizetable[qh->qhmem.indextable[insize]]; \
177  qh->qhmem.totfree -= qh->qhmem.sizetable[qh->qhmem.indextable[insize]]; \
178  qh->qhmem.cntquick++; \
179  *freelistp= *((void **)*freelistp);\
180  }else object= (type*)qh_memalloc(qh, insize);}
181 #endif
182 
183 /*-<a href="qh-mem_r.htm#TOC"
184  >--------------------------------</a><a name="memfree_">-</a>
185 
186  qh_memfree_(qh, object, insize, freelistp)
187  free up an object
188 
189  notes:
190  object may be NULL
191  assumes size<=qh->qhmem.LASTsize and void **freelistp is a temp
192 */
193 #if defined qh_NOmem
194 #define qh_memfree_(qh, object, insize, freelistp) {\
195  qh_memfree(qh, object, insize); }
196 #elif defined qh_TRACEshort
197 #define qh_memfree_(qh, object, insize, freelistp) {\
198  freelistp= NULL; /* Avoid warnings */ \
199  qh_memfree(qh, object, insize); }
200 #else /* !qh_NOmem */
201 
202 #define qh_memfree_(qh, object, insize, freelistp) {\
203  if (object) { \
204  qh->qhmem.freeshort++;\
205  freelistp= qh->qhmem.freelists + qh->qhmem.indextable[insize];\
206  qh->qhmem.totshort -= qh->qhmem.sizetable[qh->qhmem.indextable[insize]]; \
207  qh->qhmem.totfree += qh->qhmem.sizetable[qh->qhmem.indextable[insize]]; \
208  *((void **)object)= *freelistp;\
209  *freelistp= object;}}
210 #endif
211 
212 /*=============== prototypes in alphabetical order ============*/
213 
214 void *qh_memalloc(qhT *qh, int insize);
215 void qh_memcheck(qhT *qh);
216 void qh_memfree(qhT *qh, void *object, int insize);
217 void qh_memfreeshort(qhT *qh, int *curlong, int *totlong);
218 void qh_meminit(qhT *qh, FILE *ferr);
219 void qh_meminitbuffers(qhT *qh, int tracelevel, int alignment, int numsizes,
220  int bufsize, int bufinit);
221 void qh_memsetup(qhT *qh);
222 void qh_memsize(qhT *qh, int size);
223 void qh_memstatistics(qhT *qh, FILE *fp);
224 void qh_memtotal(qhT *qh, int *totlong, int *curlong, int *totshort, int *curshort, int *maxlong, int *totbuffer);
225 
226 #endif /* qhDEFmem */
ptr_intT
long ptr_intT
Definition: mem_r.h:96
qhmemT::BUFinit
int BUFinit
Definition: mem.h:118
qhmemT::totdropped
int totdropped
Definition: mem.h:139
qhmemT::maxlong
int maxlong
Definition: mem.h:142
qhmemT::freemem
void * freemem
Definition: mem.h:127
qh_memtotal
void qh_memtotal(qhT *qh, int *totlong, int *curlong, int *totshort, int *curshort, int *maxlong, int *totbuffer)
Definition: mem_r.c:554
qhmemT::totlarger
int totlarger
Definition: mem.h:146
qhmemT::tempstack
setT * tempstack
Definition: mem.h:129
qh_meminit
void qh_meminit(qhT *qh, FILE *ferr)
Definition: mem_r.c:303
qh_memstatistics
void qh_memstatistics(qhT *qh, FILE *fp)
Definition: mem_r.c:417
qhmemT::cntlarger
int cntlarger
Definition: mem.h:145
qh_memcheck
void qh_memcheck(qhT *qh)
Definition: mem_r.c:180
qhT
Definition: libqhull.h:465
qhmemT::totlong
int totlong
Definition: mem.h:141
qh_meminitbuffers
void qh_meminitbuffers(qhT *qh, int tracelevel, int alignment, int numsizes, int bufsize, int bufinit)
Definition: mem_r.c:332
qhmemT::ALIGNmask
int ALIGNmask
Definition: mem.h:122
qh_memsetup
void qh_memsetup(qhT *qh)
Definition: mem_r.c:359
qhmemT
Definition: mem.h:116
setT
Definition: qset.h:83
qh_memfreeshort
void qh_memfreeshort(qhT *qh, int *curlong, int *totlong)
Definition: mem_r.c:274
qhmemT::totfree
int totfree
Definition: mem.h:140
qh
#define qh
Definition: libqhull.h:457
qhmemT::cntquick
int cntquick
Definition: mem.h:132
qh_memfree
void qh_memfree(qhT *qh, void *object, int insize)
Definition: mem_r.c:226
qhmemT::totbuffer
int totbuffer
Definition: mem.h:138
qhmemT::freeshort
int freeshort
Definition: mem.h:136
qhmemT::freelong
int freelong
Definition: mem.h:137
qhmemT::indextable
int * indextable
Definition: mem.h:125
qhmemT::freesize
int freesize
Definition: mem.h:128
qhmemT::ferr
FILE * ferr
Definition: mem.h:130
qhmemT::totshort
int totshort
Definition: mem.h:143
qh_memalloc
void * qh_memalloc(qhT *qh, int insize)
Definition: mem_r.c:92
qhmemT::TABLEsize
int TABLEsize
Definition: mem.h:119
qhmemT::cntlong
int cntlong
Definition: mem.h:135
qhmemT::BUFsize
int BUFsize
Definition: mem.h:117
qhmemT::curbuffer
void * curbuffer
Definition: mem.h:126
qhmemT::cntshort
int cntshort
Definition: mem.h:134
qhmemT::LASTsize
int LASTsize
Definition: mem.h:121
qhmemT::freelists
void ** freelists
Definition: mem.h:123
qhmemT::sizetable
int * sizetable
Definition: mem.h:124
qhmemT::totunused
int totunused
Definition: mem.h:144
qhmemT::IStracing
int IStracing
Definition: mem.h:131
qhmemT::NUMsizes
int NUMsizes
Definition: mem.h:120
qh_memsize
void qh_memsize(qhT *qh, int size)
Definition: mem_r.c:390


hpp-fcl
Author(s):
autogenerated on Fri Aug 2 2024 02:45:14