Doxygen Source Code Documentation
mem.c File Reference
#include <stdio.h>#include <stdlib.h>#include <string.h>#include "mem.h"Go to the source code of this file.
| Typedefs | |
| typedef ridgeT | ridgeT | 
| typedef facetT | facetT | 
| Functions | |
| void | qh_errexit (int exitcode, facetT *, ridgeT *) | 
| int | qh_intcompare (const void *i, const void *j) | 
| void * | qh_memalloc (int insize) | 
| void | qh_memfree (void *object, int size) | 
| void | qh_memfreeshort (int *curlong, int *totlong) | 
| void | qh_meminit (FILE *ferr) | 
| void | qh_meminitbuffers (int tracelevel, int alignment, int numsizes, int bufsize, int bufinit) | 
| void | qh_memsetup (void) | 
| void | qh_memsize (int size) | 
| void | qh_memstatistics (FILE *fp) | 
| Variables | |
| qhmemT | qhmem = {0} | 
Typedef Documentation
| 
 | 
| 
 Definition at line 42 of file qhulldir/mem.c. | 
| 
 | 
| 
 Definition at line 41 of file qhulldir/mem.c. | 
Function Documentation
| 
 | ||||||||||||||||
| 
 Definition at line 192 of file user.c. 
 00192                                                             {
00193 
00194   if (qh ERREXITcalled) {
00195     fprintf (qh ferr, "\nqhull error while processing previous error.  Exit program\n");
00196     exit(1);
00197   }
00198   qh ERREXITcalled= True;
00199   if (!qh QHULLfinished)
00200     qh hulltime= qh_CPUclock - qh hulltime;
00201   qh_errprint("ERRONEOUS", facet, NULL, ridge, NULL);
00202   fprintf (qh ferr, "\nWhile executing: %s | %s\n", qh rbox_command, qh qhull_command);
00203   fprintf(qh ferr, "Options selected for Qhull %s:\n%s\n", qh_version, qh qhull_options);
00204   if (qh furthest_id >= 0) {
00205     fprintf(qh ferr, "Last point added to hull was p%d.", qh furthest_id);
00206     if (zzval_(Ztotmerge))
00207       fprintf(qh ferr, "  Last merge was #%d.", zzval_(Ztotmerge));
00208     if (qh QHULLfinished)
00209       fprintf(qh ferr, "\nQhull has finished constructing the hull.");
00210     else if (qh POSTmerging)
00211       fprintf(qh ferr, "\nQhull has started post-merging.");
00212     fprintf (qh ferr, "\n");
00213   }
00214   if (qh FORCEoutput && (qh QHULLfinished || (!facet && !ridge)))
00215     qh_produce_output();
00216   else {
00217     if (exitcode != qh_ERRsingular && zzval_(Zsetplane) > qh hull_dim+1) {
00218       fprintf (qh ferr, "\nAt error exit:\n");
00219       qh_printsummary (qh ferr);
00220       if (qh PRINTstatistics) {
00221         qh_collectstatistics();
00222         qh_printstatistics(qh ferr, "at error exit");
00223         qh_memstatistics (qh ferr);
00224       }
00225     }
00226     if (qh PRINTprecision)
00227       qh_printstats (qh ferr, qhstat precision, NULL);
00228   }
00229   if (!exitcode)
00230     exitcode= qh_ERRqhull;
00231   else if (exitcode == qh_ERRsingular)
00232     qh_printhelp_singular(qh ferr);
00233   else if (exitcode == qh_ERRprec && !qh PREmerge)
00234     qh_printhelp_degenerate (qh ferr);
00235   if (qh NOerrexit) {
00236     fprintf (qh ferr, "qhull error while ending program.  Exit program\n");
00237     exit(1);
00238   }
00239   qh NOerrexit= True;
00240   longjmp(qh errexit, exitcode);
00241 } /* errexit */
 | 
| 
 | ||||||||||||
| 
 Definition at line 66 of file qhulldir/mem.c. References i. Referenced by qh_memsetup(). 
 00066                                                        {
00067   return(*((int *)i) - *((int *)j));
00068 } /* intcompare */
 | 
| 
 | 
| 
 Definition at line 99 of file qhulldir/mem.c. References qhmemT::ALIGNmask, qhmemT::BUFinit, qhmemT::BUFsize, qhmemT::cntlong, qhmemT::cntquick, qhmemT::cntshort, qhmemT::curbuffer, qhmemT::curlong, qhmemT::ferr, qhmemT::freelists, qhmemT::freemem, qhmemT::freesize, qhmemT::indextable, qhmemT::IStracing, qhmemT::LASTsize, malloc, qhmemT::maxlong, object, qh_errexit(), qhmem_ERRmem, qhmem_ERRqhull, qhmemT::sizetable, qhmemT::totlong, and qhmemT::totshort. Referenced by qh_findbestsharp(), qh_getcenter(), qh_initqhull_buffers(), qh_newvertex(), qh_printafacet(), qh_projectinput(), qh_setduplicate(), qh_setnew(), and qh_voronoi_center(). 
 00099                               {
00100   void **freelistp, *newbuffer;
00101   int index, size;
00102   int outsize, bufsize;
00103   void *object;
00104 
00105   if ((unsigned) insize <= (unsigned) qhmem.LASTsize) {
00106     index= qhmem.indextable[insize];
00107     freelistp= qhmem.freelists+index;
00108     if ((object= *freelistp)) {
00109       qhmem.cntquick++;  
00110       *freelistp= *((void **)*freelistp);  /* replace freelist with next object */
00111       return (object);
00112     }else {
00113       outsize= qhmem.sizetable[index];
00114       qhmem.cntshort++;
00115       if (outsize > qhmem .freesize) {
00116         if (!qhmem.curbuffer)
00117           bufsize= qhmem.BUFinit;
00118         else
00119           bufsize= qhmem.BUFsize;
00120         qhmem.totshort += bufsize;
00121         if (!(newbuffer= malloc(bufsize))) {
00122           fprintf(qhmem.ferr, "qhull error (qh_memalloc): insufficient memory\n");
00123           qh_errexit(qhmem_ERRmem, NULL, NULL);
00124         } 
00125         *((void **)newbuffer)= qhmem.curbuffer;  /* prepend newbuffer to curbuffer 
00126                                                     list */
00127         qhmem.curbuffer= newbuffer;
00128         size= (sizeof(void **) + qhmem.ALIGNmask) & ~qhmem.ALIGNmask;
00129         qhmem.freemem= (void *)((char *)newbuffer+size);
00130         qhmem.freesize= bufsize - size;
00131       }
00132       object= qhmem.freemem;
00133       qhmem.freemem= (void *)((char *)qhmem.freemem + outsize);
00134       qhmem.freesize -= outsize;
00135       return object;
00136     }
00137   }else {                     /* long allocation */
00138     if (!qhmem.indextable) {
00139       fprintf (qhmem.ferr, "qhull internal error (qh_memalloc): qhmem has not been initialized.\n");
00140       qh_errexit(qhmem_ERRqhull, NULL, NULL);
00141     }
00142     outsize= insize;
00143     qhmem .cntlong++;
00144     qhmem .curlong++;
00145     qhmem .totlong += outsize;
00146     if (qhmem.maxlong < qhmem.totlong)
00147       qhmem.maxlong= qhmem.totlong;
00148     if (!(object= malloc(outsize))) {
00149       fprintf(qhmem.ferr, "qhull error (qh_memalloc): insufficient memory\n");
00150       qh_errexit(qhmem_ERRmem, NULL, NULL);
00151     }
00152     if (qhmem.IStracing >= 5)
00153       fprintf (qhmem.ferr, "qh_memalloc long: %d bytes at %p\n", outsize, object);
00154   }
00155   return (object);
00156 } /* memalloc */
 | 
| 
 | ||||||||||||
| 
 | ||||||||||||
| 
 Definition at line 206 of file qhulldir/mem.c. References qhmemT::cntlong, qhmemT::curbuffer, free, qhmemT::freelong, qhmemT::LASTsize, and qhmemT::totlong. Referenced by main(). 
 00206                                                   {
00207   void *buffer, *nextbuffer;
00208 
00209   *curlong= qhmem .cntlong - qhmem .freelong;
00210   *totlong= qhmem .totlong;
00211   for(buffer= qhmem.curbuffer; buffer; buffer= nextbuffer) {
00212     nextbuffer= *((void **) buffer);
00213     free(buffer);
00214   }
00215   qhmem.curbuffer= NULL;
00216   if (qhmem .LASTsize) {
00217     free (qhmem .indextable);
00218     free (qhmem .freelists);
00219     free (qhmem .sizetable);
00220   }
00221   memset((char *)&qhmem, 0, sizeof qhmem);  /* every field is 0, FALSE, NULL */
00222 } /* memfreeshort */
 | 
| 
 | 
| 
 Definition at line 231 of file qhulldir/mem.c. References qhmemT::ferr. Referenced by qh_init_A(), and qh_new_qhull(). 
 00231                              {
00232   
00233   memset((char *)&qhmem, 0, sizeof qhmem);  /* every field is 0, FALSE, NULL */
00234   qhmem.ferr= ferr;
00235   if (sizeof(void*) < sizeof(int)) {
00236     fprintf (ferr, "qhull internal error (qh_meminit): sizeof(void*) < sizeof(int).  qset.c will not work\n");
00237     exit (1);  /* can not use qh_errexit() */
00238   }
00239 } /* meminit */
 | 
| 
 | ||||||||||||||||||||||||
| 
 Definition at line 252 of file qhulldir/mem.c. References qhmemT::ALIGNmask, qhmemT::BUFinit, qhmemT::BUFsize, calloc, qhmemT::ferr, qhmemT::freelists, qhmemT::IStracing, qhmemT::NUMsizes, qh_errexit(), qhmem_ERRmem, qhmem_ERRqhull, and qhmemT::sizetable. Referenced by qh_initqhull_mem(). 
 00252                                                                                                {
00253 
00254   qhmem.IStracing= tracelevel;
00255   qhmem.NUMsizes= numsizes;
00256   qhmem.BUFsize= bufsize;
00257   qhmem.BUFinit= bufinit;
00258   qhmem.ALIGNmask= alignment-1;
00259   if (qhmem.ALIGNmask & ~qhmem.ALIGNmask) {
00260     fprintf (qhmem.ferr, "qhull internal error (qh_meminit): memory alignment %d is not a power of 2\n", alignment);
00261     qh_errexit (qhmem_ERRqhull, NULL, NULL);
00262   }
00263   qhmem.sizetable= (int *) calloc (numsizes, sizeof(int));
00264   qhmem.freelists= (void **) calloc (numsizes, sizeof(void *));
00265   if (!qhmem.sizetable || !qhmem.freelists) {
00266     fprintf(qhmem.ferr, "qhull error (qh_meminit): insufficient memory\n");
00267     qh_errexit (qhmem_ERRmem, NULL, NULL);
00268   }
00269   if (qhmem.IStracing >= 1)
00270     fprintf (qhmem.ferr, "qh_meminitbuffers: memory initialized with alignment %d\n", alignment);
00271 } /* meminitbuffers */
 | 
| 
 | 
| 
 Definition at line 279 of file qhulldir/mem.c. References qhmemT::BUFinit, qhmemT::BUFsize, qhmemT::ferr, i, qhmemT::indextable, qhmemT::LASTsize, malloc, qh_errexit(), qh_intcompare(), qhmem_ERRmem, qhmemT::sizetable, and qhmemT::TABLEsize. Referenced by qh_initqhull_mem(). 
 00279                         {
00280   int k,i;
00281 
00282   qsort(qhmem.sizetable, qhmem.TABLEsize, sizeof(int), qh_intcompare);
00283   qhmem.LASTsize= qhmem.sizetable[qhmem.TABLEsize-1];
00284   if (qhmem .LASTsize >= qhmem .BUFsize || qhmem.LASTsize >= qhmem .BUFinit) {
00285     fprintf (qhmem.ferr, "qhull error (qh_memsetup): largest mem size %d is >= buffer size %d or initial buffer size %d\n",
00286             qhmem .LASTsize, qhmem .BUFsize, qhmem .BUFinit);
00287     qh_errexit(qhmem_ERRmem, NULL, NULL);
00288   }
00289   if (!(qhmem.indextable= (int *)malloc((qhmem.LASTsize+1) * sizeof(int)))) {
00290     fprintf(qhmem.ferr, "qhull error (qh_memsetup): insufficient memory\n");
00291     qh_errexit(qhmem_ERRmem, NULL, NULL);
00292   }
00293   for(k=qhmem.LASTsize+1; k--; )
00294     qhmem.indextable[k]= k;
00295   i= 0;
00296   for(k= 0; k <= qhmem.LASTsize; k++) {
00297     if (qhmem.indextable[k] <= qhmem.sizetable[i])
00298       qhmem.indextable[k]= i;
00299     else
00300       qhmem.indextable[k]= ++i;
00301   }
00302 } /* memsetup */
 | 
| 
 | 
| 
 Definition at line 310 of file qhulldir/mem.c. References qhmemT::ALIGNmask, qhmemT::ferr, qhmemT::LASTsize, qhmemT::NUMsizes, qh_errexit(), qhmem_ERRqhull, qhmemT::sizetable, and qhmemT::TABLEsize. Referenced by qh_initqhull_mem(). 
 00310                           {
00311   int k;
00312 
00313   if (qhmem .LASTsize) {
00314     fprintf (qhmem .ferr, "qhull error (qh_memsize): called after qhmem_setup\n");
00315     qh_errexit (qhmem_ERRqhull, NULL, NULL);
00316   }
00317   size= (size + qhmem.ALIGNmask) & ~qhmem.ALIGNmask;
00318   for(k= qhmem.TABLEsize; k--; ) {
00319     if (qhmem.sizetable[k] == size)
00320       return;
00321   }
00322   if (qhmem.TABLEsize < qhmem.NUMsizes)
00323     qhmem.sizetable[qhmem.TABLEsize++]= size;
00324   else
00325     fprintf(qhmem.ferr, "qhull warning (memsize): free list table has room for only %d sizes\n", qhmem.NUMsizes);
00326 } /* memsize */
 | 
| 
 | 
| 
 Definition at line 338 of file qhulldir/mem.c. References qhmemT::BUFinit, qhmemT::BUFsize, qhmemT::cntlarger, qhmemT::cntlong, qhmemT::cntshort, qhmemT::freelists, qhmemT::freelong, qhmemT::freeshort, qhmemT::freesize, i, qhmemT::maxlong, object, qhmemT::sizetable, qhmemT::TABLEsize, qhmemT::totlarger, qhmemT::totlong, and qhmemT::totshort. Referenced by qh_errexit(), qh_printallstatistics(), and qh_produce_output(). 
 00338                                  {
00339   int i, count, totfree= 0;
00340   void *object;
00341   
00342   for (i=0; i < qhmem.TABLEsize; i++) {
00343     count=0;
00344     for (object= qhmem .freelists[i]; object; object= *((void **)object))
00345       count++;
00346     totfree += qhmem.sizetable[i] * count;
00347   }
00348   fprintf (fp, "\nmemory statistics:\n\
00349 %7d quick allocations\n\
00350 %7d short allocations\n\
00351 %7d long allocations\n\
00352 %7d short frees\n\
00353 %7d long frees\n\
00354 %7d bytes of short memory in use\n\
00355 %7d bytes of short memory in freelists\n\
00356 %7d bytes of long memory allocated (except for input)\n\
00357 %7d bytes of long memory in use (in %d pieces)\n\
00358 %7d bytes per memory buffer (initially %d bytes)\n",
00359            qhmem .cntquick, qhmem.cntshort, qhmem.cntlong,
00360            qhmem .freeshort, qhmem.freelong, 
00361            qhmem .totshort - qhmem .freesize - totfree,
00362            totfree,
00363            qhmem .maxlong, qhmem .totlong, qhmem .cntlong - qhmem .freelong,
00364            qhmem .BUFsize, qhmem .BUFinit);
00365   if (qhmem.cntlarger) {
00366     fprintf (fp, "%7d calls to qh_setlarger\n%7.2g     average copy size\n",
00367            qhmem.cntlarger, ((float) qhmem.totlarger)/ qhmem.cntlarger);
00368     fprintf (fp, "  freelists (bytes->count):");
00369   }
00370   for (i=0; i < qhmem.TABLEsize; i++) {
00371     count=0;
00372     for (object= qhmem .freelists[i]; object; object= *((void **)object))
00373       count++;
00374     fprintf (fp, " %d->%d", qhmem.sizetable[i], count);
00375   }
00376   fprintf (fp, "\n\n");
00377 } /* memstatistics */
 | 
Variable Documentation
| 
 | 
| 
 Definition at line 50 of file qhulldir/mem.c. | 
 
                             
                             
                             
                             
                             
                             
                             
                             
                             
                             
                             
                             
 
 
 
 
       
	   
	   
	   
	  