cutil/structures.h

Go to the documentation of this file.
00001 
00020 #ifndef STRUCTURES_H
00021 #define STRUCTURES_H
00022 
00023 /*----------------------------------------------------------------------
00024               I n c l u d e s
00025 ----------------------------------------------------------------------*/
00026 #include "tessclas.h"
00027 #include "oldlist.h"
00028 #include "freelist.h"
00029 #include "danerror.h"
00030 
00032 #define NUM_DATA_TYPES 20
00033 
00035 extern int max_data_types;
00037 extern void_void memory_print_functions[NUM_DATA_TYPES];
00038 
00039 /*----------------------------------------------------------------------
00040               M a c r o s
00041 ----------------------------------------------------------------------*/
00048 #define makestructure(new,old,print,type,nextfree,blocksize,typestring,usecount) \
00049    \
00050 static type                *nextfree;                                   \
00051 static int                 usecount=0;                                  \
00052                                                       \
00053                                                       \
00054 void print()                                                            \
00055 {                                                                       \
00056    cprintf("%6d %s elements in use\n", usecount, typestring);          \
00057 }                                                                       \
00058                                                       \
00059                                                       \
00060 type *new()                                                             \
00061 {                                                                       \
00062    static   int            first_time = 1;                             \
00063    register int            index;                                      \
00064    register type           *element;                                   \
00065    type                    *returnelement;   /*return next ptr*/        \
00066    char                    *newblock;                                 \
00067    extern int              structblockcount;                          \
00068                                                       \
00069    if (first_time)                                                     \
00070    memory_print_functions [max_data_types++] = print;                  \
00071    first_time = FALSE;                                                 \
00072                                                       \
00073    returnelement=nextfree;                                             \
00074    if (nextfree==NULL)                                                 \
00075    {                                                                   \
00076       if ((sizeof(type) & 7)==0 && sizeof(type)>=16)                  \
00077       {                                                               \
00078          newblock=(char *)memalloc_p(sizeof(type)*blocksize+4);      \
00079          if (newblock!=NULL)                                         \
00080          {                                                           \
00081             newblock=(char *)(((int)newblock +4) & (-8));           \
00082          }                                                           \
00083          element=(type *)newblock;                                   \
00084       }                                                               \
00085       else                                                            \
00086       {                                                               \
00087          element=(type *)memalloc_p(sizeof(type)*blocksize);         \
00088       }                                                               \
00089       structblockcount++;                                             \
00090       if (!element) DoError (0, "Could not allocate block of cells"); \
00091       returnelement=element;        /*going to return first*/       \
00092       for (index=0;index<blocksize-1;index++)                         \
00093       {  ((type**)element)[0]=element+1;  /*make links*/              \
00094          element++;                                                  \
00095       }                                                               \
00096       ((type**)element)[0]=NULL;       /*with end*/                \
00097    }                                                                   \
00098    nextfree=((type**)returnelement)[0];                                \
00099    usecount++;                                                         \
00100    return (returnelement);                                             \
00101 }                                                                       \
00102                                                       \
00103                                                       \
00104                                                       \
00105 void old(type* deadelement)                                             \
00106 {                                                                       \
00107    if (!deadelement) DoError (0, "Deallocated a NULL pointer");        \
00108    ((type**)deadelement)[0]=nextfree;                                  \
00109    nextfree=deadelement;                                               \
00110    usecount--;                                                         \
00111 }                                                                       \
00112 
00113 
00117 #define newstructure(name,type,nextfree,blocksize,errorstring,usecount)\
00118 static type                *nextfree;     /*head of freelist*/\
00119 static int                 usecount=0;    /*no of used objects*/\
00120 \
00121 type *name()                        /*returns a new type*/\
00122 {\
00123    register int            index;         /*index to block*/\
00124    register type           *element;      /*current element*/\
00125    type                    *returnelement;   /*return value*/\
00126    char                    *newblock;     /*new block of mem*/\
00127    extern int              structblockcount;/*no of memallocs done*/\
00128 \
00129 \
00130    returnelement=nextfree;\
00131    if (nextfree==NULL)\
00132    {\
00133       if ((sizeof(type) & 7)==0 && sizeof(type)>=16)\
00134       {\
00135          newblock=(char *)memalloc_p(sizeof(type)*blocksize+4);\
00136          if (newblock!=NULL)\
00137          {\
00138             newblock=(char *)(((int)newblock +4) & (-8));\
00139          }\
00140          element=(type *)newblock;\
00141       }\
00142       else\
00143       {\
00144          element=(type *)memalloc_p(sizeof(type)*blocksize);\
00145          /*get more memory*/\
00146       }\
00147       structblockcount++;\
00148       if (element==NULL)\
00149       {\
00150          cprintf("Error:MEMORY_OUT:%s\n",errorstring);\
00151          abort();\
00152       }\
00153       returnelement=element;        /*going to return first*/\
00154       for (index=0;index<blocksize-1;index++)\
00155       {  ((type**)element)[0]=element+1;  /*make links*/\
00156          element++;\
00157       }\
00158       ((type**)element)[0]=NULL;       /*with end*/\
00159    }\
00160    nextfree=((type**)returnelement)[0];\
00161    usecount++;\
00162    return returnelement;\
00163 }
00164 
00168 #define oldstructure(name,type,nextfree,stringtype,usecount)\
00169 \
00170 type *name(type* deadelement)\
00171 {\
00172    type       *returnelement;       /*return next ptr*/\
00173 \
00174    if (deadelement==NULL)\
00175    {\
00176       cprintf("No of %ss in use=%d\n",stringtype,usecount);\
00177       \
00178       return (type *) 0x80000000;\
00179    }\
00180    returnelement=deadelement->next;             /*return link*/\
00181    ((type**)deadelement)[0]=nextfree;              /*next free blob*/\
00182    nextfree=deadelement;\
00183    usecount--;\
00184    return returnelement;\
00185 }
00186 
00187 /*----------------------------------------------------------------------
00188               F u n c t i o n s
00189 ----------------------------------------------------------------------*/
00190 extern TBLOB *newblob();
00191 extern TBLOB *oldblob(TBLOB *);
00192 
00193 extern TESSLINE *newoutline();
00194 extern void oldoutline(TESSLINE *);
00195 
00196 extern EDGEPT *newedgept();
00197 extern EDGEPT *oldedgept(EDGEPT *);
00198 
00199 extern TWERD *newword();
00200 extern void oldword(TWERD *);
00201 
00202 extern LIST new_cell();
00203 extern void free_cell(LIST);
00204 #endif

Generated on Wed Feb 28 19:49:10 2007 for Tesseract by  doxygen 1.5.1