58 return(*((
const int *)i) - *((
const int *)j));
93 void **freelistp, *newbuffer;
99 qh_fprintf(
qh,
qh->qhmem.ferr, 6235,
"qhull error (qh_memalloc): negative request size (%d). Did int overflow due to high-D?\n", insize);
103 idx=
qh->qhmem.indextable[insize];
104 outsize=
qh->qhmem.sizetable[idx];
105 qh->qhmem.totshort += outsize;
106 freelistp=
qh->qhmem.freelists+idx;
107 if ((
object= *freelistp)) {
108 qh->qhmem.cntquick++;
109 qh->qhmem.totfree -= outsize;
110 *freelistp= *((
void **)*freelistp);
112 n=
qh->qhmem.cntshort+
qh->qhmem.cntquick+
qh->qhmem.freeshort;
113 if (
qh->qhmem.IStracing >= 5)
114 qh_fprintf(
qh,
qh->qhmem.ferr, 8141,
"qh_mem %p n %8d alloc quick: %d bytes (tot %d cnt %d)\n",
object, n, outsize,
qh->qhmem.totshort,
qh->qhmem.cntshort+
qh->qhmem.cntquick-
qh->qhmem.freeshort);
118 qh->qhmem.cntshort++;
119 if (outsize >
qh->qhmem.freesize) {
120 qh->qhmem.totdropped +=
qh->qhmem.freesize;
121 if (!
qh->qhmem.curbuffer)
122 bufsize=
qh->qhmem.BUFinit;
124 bufsize=
qh->qhmem.BUFsize;
125 if (!(newbuffer=
qh_malloc((
size_t)bufsize))) {
126 qh_fprintf(
qh,
qh->qhmem.ferr, 6080,
"qhull error (qh_memalloc): insufficient memory to allocate short memory buffer (%d bytes)\n", bufsize);
129 *((
void **)newbuffer)=
qh->qhmem.curbuffer;
131 qh->qhmem.curbuffer= newbuffer;
132 size= (
sizeof(
void **) +
qh->qhmem.ALIGNmask) & ~
qh->qhmem.ALIGNmask;
133 qh->qhmem.freemem= (
void *)((
char *)newbuffer+size);
134 qh->qhmem.freesize= bufsize - size;
135 qh->qhmem.totbuffer += bufsize - size;
137 n =
qh->qhmem.totshort +
qh->qhmem.totfree +
qh->qhmem.totdropped +
qh->qhmem.freesize - outsize;
138 if (
qh->qhmem.totbuffer != n) {
139 qh_fprintf(
qh,
qh->qhmem.ferr, 6212,
"qh_memalloc internal error: short totbuffer %d != totshort+totfree... %d\n",
qh->qhmem.totbuffer, n);
143 object=
qh->qhmem.freemem;
144 qh->qhmem.freemem= (
void *)((
char *)
qh->qhmem.freemem + outsize);
145 qh->qhmem.freesize -= outsize;
146 qh->qhmem.totunused += outsize - insize;
148 n=
qh->qhmem.cntshort+
qh->qhmem.cntquick+
qh->qhmem.freeshort;
149 if (
qh->qhmem.IStracing >= 5)
150 qh_fprintf(
qh,
qh->qhmem.ferr, 8140,
"qh_mem %p n %8d alloc short: %d bytes (tot %d cnt %d)\n",
object, n, outsize,
qh->qhmem.totshort,
qh->qhmem.cntshort+
qh->qhmem.cntquick-
qh->qhmem.freeshort);
155 if (!
qh->qhmem.indextable) {
156 qh_fprintf(
qh,
qh->qhmem.ferr, 6081,
"qhull internal error (qh_memalloc): qhmem has not been initialized.\n");
161 qh->qhmem.totlong += outsize;
162 if (
qh->qhmem.maxlong <
qh->qhmem.totlong)
163 qh->qhmem.maxlong=
qh->qhmem.totlong;
164 if (!(
object=
qh_malloc((
size_t)outsize))) {
165 qh_fprintf(
qh,
qh->qhmem.ferr, 6082,
"qhull error (qh_memalloc): insufficient memory to allocate %d bytes\n", outsize);
168 if (
qh->qhmem.IStracing >= 5)
169 qh_fprintf(
qh,
qh->qhmem.ferr, 8057,
"qh_mem %p n %8d alloc long: %d bytes (tot %d cnt %d)\n",
object,
qh->qhmem.cntlong+
qh->qhmem.freelong, outsize,
qh->qhmem.totlong,
qh->qhmem.cntlong-
qh->qhmem.freelong);
181 int i, count, totfree= 0;
185 qh_fprintf_stderr(6243,
"qh_memcheck(qh) error: qh is 0. It does not point to a qhT");
188 if (
qh->qhmem.ferr == 0 ||
qh->qhmem.IStracing < 0 ||
qh->qhmem.IStracing > 10 || (((
qh->qhmem.ALIGNmask+1) &
qh->qhmem.ALIGNmask) != 0)) {
189 qh_fprintf_stderr(6244,
"qh_memcheck error: either qh->qhmem is overwritten or qh->qhmem is not initialized. Call qh_mem_new() or qh_new_qhull() before calling qh_mem routines. ferr 0x%x IsTracing %d ALIGNmask 0x%x",
qh->qhmem.ferr,
qh->qhmem.IStracing,
qh->qhmem.ALIGNmask);
192 if (
qh->qhmem.IStracing != 0)
193 qh_fprintf(
qh,
qh->qhmem.ferr, 8143,
"qh_memcheck: check size of freelists on qh->qhmem\nqh_memcheck: A segmentation fault indicates an overwrite of qh->qhmem\n");
194 for (i=0; i <
qh->qhmem.TABLEsize; i++) {
196 for (
object=
qh->qhmem.freelists[i];
object;
object= *((
void **)
object))
198 totfree +=
qh->qhmem.sizetable[i] * count;
200 if (totfree !=
qh->qhmem.totfree) {
201 qh_fprintf(
qh,
qh->qhmem.ferr, 6211,
"Qhull internal error (qh_memcheck): totfree %d not equal to freelist total %d\n",
qh->qhmem.totfree, totfree);
204 if (
qh->qhmem.IStracing != 0)
205 qh_fprintf(
qh,
qh->qhmem.ferr, 8144,
"qh_memcheck: total size of freelists totfree is the same as qh->qhmem.totfree\n", totfree);
233 qh->qhmem.freeshort++;
234 idx=
qh->qhmem.indextable[insize];
235 outsize=
qh->qhmem.sizetable[idx];
236 qh->qhmem.totfree += outsize;
237 qh->qhmem.totshort -= outsize;
238 freelistp=
qh->qhmem.freelists + idx;
239 *((
void **)
object)= *freelistp;
242 idx=
qh->qhmem.cntshort+
qh->qhmem.cntquick+
qh->qhmem.freeshort;
243 if (
qh->qhmem.IStracing >= 5)
244 qh_fprintf(
qh,
qh->qhmem.ferr, 8142,
"qh_mem %p n %8d free short: %d bytes (tot %d cnt %d)\n",
object, idx, outsize,
qh->qhmem.totshort,
qh->qhmem.cntshort+
qh->qhmem.cntquick-
qh->qhmem.freeshort);
247 qh->qhmem.freelong++;
248 qh->qhmem.totlong -= insize;
249 if (
qh->qhmem.IStracing >= 5)
250 qh_fprintf(
qh,
qh->qhmem.ferr, 8058,
"qh_mem %p n %8d free long: %d bytes (tot %d cnt %d)\n",
object,
qh->qhmem.cntlong+
qh->qhmem.freelong, insize,
qh->qhmem.totlong,
qh->qhmem.cntlong-
qh->qhmem.freelong);
275 void *buffer, *nextbuffer;
278 *curlong=
qh->qhmem.cntlong -
qh->qhmem.freelong;
279 *totlong=
qh->qhmem.totlong;
280 for (buffer=
qh->qhmem.curbuffer; buffer; buffer= nextbuffer) {
281 nextbuffer= *((
void **) buffer);
284 qh->qhmem.curbuffer= NULL;
285 if (
qh->qhmem.LASTsize) {
290 ferr=
qh->qhmem.ferr;
291 memset((
char *)&
qh->qhmem, 0,
sizeof(
qh->qhmem));
292 qh->qhmem.ferr= ferr;
305 memset((
char *)&
qh->qhmem, 0,
sizeof(
qh->qhmem));
307 qh->qhmem.ferr= ferr;
309 qh->qhmem.ferr= stderr;
310 if (
sizeof(
void*) <
sizeof(
int)) {
311 qh_fprintf(
qh,
qh->qhmem.ferr, 6083,
"qhull internal error (qh_meminit): sizeof(void*) %d < sizeof(int) %d. qset.c will not work\n", (
int)
sizeof(
void*), (
int)
sizeof(
int));
314 if (
sizeof(
void*) >
sizeof(
ptr_intT)) {
315 qh_fprintf(
qh,
qh->qhmem.ferr, 6084,
"qhull internal error (qh_meminit): sizeof(void*) %d > sizeof(ptr_intT) %d. Change ptr_intT in mem.h to 'long long'\n", (
int)
sizeof(
void*), (
int)
sizeof(
ptr_intT));
334 qh->qhmem.IStracing= tracelevel;
335 qh->qhmem.NUMsizes= numsizes;
336 qh->qhmem.BUFsize= bufsize;
337 qh->qhmem.BUFinit= bufinit;
338 qh->qhmem.ALIGNmask= alignment-1;
339 if (
qh->qhmem.ALIGNmask & ~
qh->qhmem.ALIGNmask) {
340 qh_fprintf(
qh,
qh->qhmem.ferr, 6085,
"qhull internal error (qh_meminit): memory alignment %d is not a power of 2\n", alignment);
343 qh->qhmem.sizetable= (
int *) calloc((
size_t)numsizes,
sizeof(int));
344 qh->qhmem.freelists= (
void **) calloc((
size_t)numsizes,
sizeof(
void *));
345 if (!
qh->qhmem.sizetable || !
qh->qhmem.freelists) {
346 qh_fprintf(
qh,
qh->qhmem.ferr, 6086,
"qhull error (qh_meminit): insufficient memory\n");
349 if (
qh->qhmem.IStracing >= 1)
350 qh_fprintf(
qh,
qh->qhmem.ferr, 8059,
"qh_meminitbuffers: memory initialized with alignment %d\n", alignment);
362 qsort(
qh->qhmem.sizetable, (
size_t)
qh->qhmem.TABLEsize,
sizeof(
int),
qh_intcompare);
363 qh->qhmem.LASTsize=
qh->qhmem.sizetable[
qh->qhmem.TABLEsize-1];
364 if(
qh->qhmem.LASTsize >=
qh->qhmem.BUFsize ||
qh->qhmem.LASTsize >=
qh->qhmem.BUFinit) {
365 qh_fprintf(
qh,
qh->qhmem.ferr, 6087,
"qhull error (qh_memsetup): largest mem size %d is >= buffer size %d or initial buffer size %d\n",
366 qh->qhmem.LASTsize,
qh->qhmem.BUFsize,
qh->qhmem.BUFinit);
369 if (!(
qh->qhmem.indextable= (
int *)
qh_malloc((
qh->qhmem.LASTsize+1) *
sizeof(
int)))) {
370 qh_fprintf(
qh,
qh->qhmem.ferr, 6088,
"qhull error (qh_memsetup): insufficient memory\n");
373 for (k=
qh->qhmem.LASTsize+1; k--; )
374 qh->qhmem.indextable[k]= k;
376 for (k=0; k <=
qh->qhmem.LASTsize; k++) {
377 if (
qh->qhmem.indextable[k] <=
qh->qhmem.sizetable[i])
378 qh->qhmem.indextable[k]= i;
380 qh->qhmem.indextable[k]= ++i;
393 if(
qh->qhmem.LASTsize) {
394 qh_fprintf(
qh,
qh->qhmem.ferr, 6089,
"qhull error (qh_memsize): called after qhmem_setup\n");
397 size= (size +
qh->qhmem.ALIGNmask) & ~
qh->qhmem.ALIGNmask;
398 for (k=
qh->qhmem.TABLEsize; k--; ) {
399 if (
qh->qhmem.sizetable[k] == size)
402 if (
qh->qhmem.TABLEsize <
qh->qhmem.NUMsizes)
403 qh->qhmem.sizetable[
qh->qhmem.TABLEsize++]= size;
405 qh_fprintf(
qh,
qh->qhmem.ferr, 7060,
"qhull warning (memsize): free list table has room for only %d sizes\n",
qh->qhmem.NUMsizes);
424 %7d quick allocations\n\
425 %7d short allocations\n\
426 %7d long allocations\n\
429 %7d bytes of short memory in use\n\
430 %7d bytes of short memory in freelists\n\
431 %7d bytes of dropped short memory\n\
432 %7d bytes of unused short memory (estimated)\n\
433 %7d bytes of long memory allocated (max, except for input)\n\
434 %7d bytes of long memory in use (in %d pieces)\n\
435 %7d bytes of short memory buffers (minus links)\n\
436 %7d bytes per short memory buffer (initially %d bytes)\n",
437 qh->qhmem.cntquick,
qh->qhmem.cntshort,
qh->qhmem.cntlong,
438 qh->qhmem.freeshort,
qh->qhmem.freelong,
439 qh->qhmem.totshort,
qh->qhmem.totfree,
440 qh->qhmem.totdropped +
qh->qhmem.freesize,
qh->qhmem.totunused,
441 qh->qhmem.maxlong,
qh->qhmem.totlong,
qh->qhmem.cntlong -
qh->qhmem.freelong,
442 qh->qhmem.totbuffer,
qh->qhmem.BUFsize,
qh->qhmem.BUFinit);
443 if (
qh->qhmem.cntlarger) {
444 qh_fprintf(
qh, fp, 9279,
"%7d calls to qh_setlarger\n%7.2g average copy size\n",
445 qh->qhmem.cntlarger, ((
float)
qh->qhmem.totlarger)/(
float)
qh->qhmem.cntlarger);
448 for (i=0; i <
qh->qhmem.TABLEsize; i++) {
450 for (
object=
qh->qhmem.freelists[i];
object;
object= *((
void **)
object))
452 qh_fprintf(
qh, fp, 9281,
" %d->%d",
qh->qhmem.sizetable[i], count);
472 if (!(
object=
qh_malloc((
size_t)insize))) {
473 qh_fprintf(
qh,
qh->qhmem.ferr, 6090,
"qhull error (qh_memalloc): insufficient memory\n");
477 qh->qhmem.totlong += insize;
478 if (
qh->qhmem.maxlong <
qh->qhmem.totlong)
479 qh->qhmem.maxlong=
qh->qhmem.totlong;
480 if (
qh->qhmem.IStracing >= 5)
481 qh_fprintf(
qh,
qh->qhmem.ferr, 8060,
"qh_mem %p n %8d alloc long: %d bytes (tot %d cnt %d)\n",
object,
qh->qhmem.cntlong+
qh->qhmem.freelong, insize,
qh->qhmem.totlong,
qh->qhmem.cntlong-
qh->qhmem.freelong);
490 qh->qhmem.freelong++;
491 qh->qhmem.totlong -= insize;
492 if (
qh->qhmem.IStracing >= 5)
493 qh_fprintf(
qh,
qh->qhmem.ferr, 8061,
"qh_mem %p n %8d free long: %d bytes (tot %d cnt %d)\n",
object,
qh->qhmem.cntlong+
qh->qhmem.freelong, insize,
qh->qhmem.totlong,
qh->qhmem.cntlong-
qh->qhmem.freelong);
497 *totlong=
qh->qhmem.totlong;
498 *curlong=
qh->qhmem.cntlong -
qh->qhmem.freelong;
499 memset((
char *)&
qh->qhmem, 0,
sizeof(
qh->qhmem));
504 memset((
char *)&
qh->qhmem, 0,
sizeof(
qh->qhmem));
506 qh->qhmem.ferr= ferr;
508 qh->qhmem.ferr= stderr;
509 if (
sizeof(
void*) <
sizeof(
int)) {
510 qh_fprintf(
qh,
qh->qhmem.ferr, 6091,
"qhull internal error (qh_meminit): sizeof(void*) %d < sizeof(int) %d. qset.c will not work\n", (
int)
sizeof(
void*), (
int)
sizeof(
int));
517 qh->qhmem.IStracing= tracelevel;
531 %7d long allocations\n\
533 %7d bytes of long memory allocated (max, except for input)\n\
534 %7d bytes of long memory in use (in %d pieces)\n",
537 qh->qhmem.maxlong,
qh->qhmem.totlong,
qh->qhmem.cntlong -
qh->qhmem.freelong);
554 void qh_memtotal(
qhT *
qh,
int *totlong,
int *curlong,
int *totshort,
int *curshort,
int *maxlong,
int *totbuffer) {
555 *totlong=
qh->qhmem.totlong;
556 *curlong=
qh->qhmem.cntlong -
qh->qhmem.freelong;
557 *totshort=
qh->qhmem.totshort;
558 *curshort=
qh->qhmem.cntshort +
qh->qhmem.cntquick -
qh->qhmem.freeshort;
559 *maxlong=
qh->qhmem.maxlong;
560 *totbuffer=
qh->qhmem.totbuffer;