void setupMemory(int tracelevel, int numInts, int **intarray) { int i; if(numInts<0 || numInts*(int)sizeof(int)<0){ qh_fprintf(stderr, 6303, "qset does not currently support 64-bit ints. Integer overflow\n"); exit(1); } *intarray= qh_malloc(numInts * sizeof(int)); if(!*intarray){ qh_fprintf(stderr, 6304, "Failed to allocate %d bytes of memory\n", numInts * sizeof(int)); exit(1); } for(i= 0; i<numInts; i++){ (*intarray)[i] =i; } qh_meminit(stderr); qh_meminitbuffers(tracelevel, qh_MEMalign, 4 /*sizes*/, qh_MEMbufsize,qh_MEMinitbuf); qh_memsize(10); qh_memsize(20); qh_memsize(30); qh_memsize(40); qh_memsetup(); qh_fprintf(stderr, 8001, "SETelemsize is %d bytes for pointer-to-int\n", SETelemsize); }/*setupMemmory*/
void *qh_memalloc(int insize) { void *object; if (!(object= qh_malloc((size_t)insize))) { qh_fprintf(qhmem.ferr, 6090, "qhull error (qh_memalloc): insufficient memory\n"); qh_errexit(qhmem_ERRmem, NULL, NULL); } qhmem .cntlong++; qhmem .totlong += insize; if (qhmem.maxlong < qhmem.totlong) qhmem.maxlong= qhmem.totlong; if (qhmem.IStracing >= 5) qh_fprintf(qhmem.ferr, 8060, "qh_mem %p n %8d alloc long: %d bytes (tot %d cnt %d)\n", object, qhmem.cntlong+qhmem.freelong, insize, qhmem.totlong, qhmem.cntlong-qhmem.freelong); return object; }
/*-<a href="qh-mem.htm#TOC" >-------------------------------</a><a name="memsetup">-</a> qh_memsetup() set up memory after running memsize() */ void qh_memsetup(void) { int k,i; qsort(qhmem.sizetable, (size_t)qhmem.TABLEsize, sizeof(int), qh_intcompare); qhmem.LASTsize= qhmem.sizetable[qhmem.TABLEsize-1]; if (qhmem .LASTsize >= qhmem .BUFsize || qhmem.LASTsize >= qhmem .BUFinit) { qh_fprintf(qhmem.ferr, 6087, "qhull error (qh_memsetup): largest mem size %d is >= buffer size %d or initial buffer size %d\n", qhmem .LASTsize, qhmem .BUFsize, qhmem .BUFinit); qh_errexit(qhmem_ERRmem, NULL, NULL); } if (!(qhmem.indextable= (int *)qh_malloc((qhmem.LASTsize+1) * sizeof(int)))) { qh_fprintf(qhmem.ferr, 6088, "qhull error (qh_memsetup): insufficient memory\n"); qh_errexit(qhmem_ERRmem, NULL, NULL); } for (k=qhmem.LASTsize+1; k--; ) qhmem.indextable[k]= k; i= 0; for (k=0; k <= qhmem.LASTsize; k++) { if (qhmem.indextable[k] <= qhmem.sizetable[i]) qhmem.indextable[k]= i; else qhmem.indextable[k]= ++i; } } /* memsetup */
/*-<a href="qh-mem.htm#TOC" >--------------------------------</a><a name="memalloc">-</a> qh_memalloc( insize ) returns object of insize bytes qhmem is the global memory structure returns: pointer to allocated memory errors if insufficient memory notes: use explicit type conversion to avoid type warnings on some compilers actual object may be larger than insize use qh_memalloc_() for inline code for quick allocations logs allocations if 'T5' design: if size < qhmem.LASTsize if qhmem.freelists[size] non-empty return first object on freelist else round up request to size of qhmem.freelists[size] allocate new allocation buffer if necessary allocate object from allocation buffer else allocate object with qh_malloc() in user.c */ void *qh_memalloc(int insize) { void **freelistp, *newbuffer; int idx, size, n; int outsize, bufsize; void *object; if (insize<0) { qh_fprintf(qhmem.ferr, 6235, "qhull error (qh_memalloc): negative request size (%d). Did int overflow due to high-D?\n", insize); /* WARN64 */ qh_errexit(qhmem_ERRmem, NULL, NULL); } if (insize>=0 && insize <= qhmem.LASTsize) { idx= qhmem.indextable[insize]; outsize= qhmem.sizetable[idx]; qhmem.totshort += outsize; freelistp= qhmem.freelists+idx; if ((object= *freelistp)) { qhmem.cntquick++; qhmem.totfree -= outsize; *freelistp= *((void **)*freelistp); /* replace freelist with next object */ #ifdef qh_TRACEshort n= qhmem.cntshort+qhmem.cntquick+qhmem.freeshort; if (qhmem.IStracing >= 5) qh_fprintf(qhmem.ferr, 8141, "qh_mem %p n %8d alloc quick: %d bytes (tot %d cnt %d)\n", object, n, outsize, qhmem.totshort, qhmem.cntshort+qhmem.cntquick-qhmem.freeshort); #endif return(object); } else { qhmem.cntshort++; if (outsize > qhmem .freesize) { qhmem .totdropped += qhmem .freesize; if (!qhmem.curbuffer) bufsize= qhmem.BUFinit; else bufsize= qhmem.BUFsize; if (!(newbuffer= qh_malloc((size_t)bufsize))) { qh_fprintf(qhmem.ferr, 6080, "qhull error (qh_memalloc): insufficient memory to allocate short memory buffer (%d bytes)\n", bufsize); qh_errexit(qhmem_ERRmem, NULL, NULL); } *((void **)newbuffer)= qhmem.curbuffer; /* prepend newbuffer to curbuffer list */ qhmem.curbuffer= newbuffer; size= (sizeof(void **) + qhmem.ALIGNmask) & ~qhmem.ALIGNmask; qhmem.freemem= (void *)((char *)newbuffer+size); qhmem.freesize= bufsize - size; qhmem.totbuffer += bufsize - size; /* easier to check */ /* Periodically test totbuffer. It matches at beginning and exit of every call */ n = qhmem.totshort + qhmem.totfree + qhmem.totdropped + qhmem.freesize - outsize; if (qhmem.totbuffer != n) { qh_fprintf(qhmem.ferr, 6212, "qh_memalloc internal error: short totbuffer %d != totshort+totfree... %d\n", qhmem.totbuffer, n); qh_errexit(qhmem_ERRmem, NULL, NULL); } } object= qhmem.freemem; qhmem.freemem= (void *)((char *)qhmem.freemem + outsize); qhmem.freesize -= outsize; qhmem.totunused += outsize - insize; #ifdef qh_TRACEshort n= qhmem.cntshort+qhmem.cntquick+qhmem.freeshort; if (qhmem.IStracing >= 5) qh_fprintf(qhmem.ferr, 8140, "qh_mem %p n %8d alloc short: %d bytes (tot %d cnt %d)\n", object, n, outsize, qhmem.totshort, qhmem.cntshort+qhmem.cntquick-qhmem.freeshort); #endif return object; } } else { /* long allocation */ if (!qhmem.indextable) { qh_fprintf(qhmem.ferr, 6081, "qhull internal error (qh_memalloc): qhmem has not been initialized.\n"); qh_errexit(qhmem_ERRqhull, NULL, NULL); } outsize= insize; qhmem .cntlong++; qhmem .totlong += outsize; if (qhmem.maxlong < qhmem.totlong) qhmem.maxlong= qhmem.totlong; if (!(object= qh_malloc((size_t)outsize))) { qh_fprintf(qhmem.ferr, 6082, "qhull error (qh_memalloc): insufficient memory to allocate %d bytes\n", outsize); qh_errexit(qhmem_ERRmem, NULL, NULL); } if (qhmem.IStracing >= 5) qh_fprintf(qhmem.ferr, 8057, "qh_mem %p n %8d alloc long: %d bytes (tot %d cnt %d)\n", object, qhmem.cntlong+qhmem.freelong, outsize, qhmem.totlong, qhmem.cntlong-qhmem.freelong); } return(object); } /* memalloc */