home *** CD-ROM | disk | FTP | other *** search
/ Mega CD-ROM 1 / megacd_rom_1.zip / megacd_rom_1 / IRIT / IRITS.ZIP / ALLOCATE.C < prev    next >
C/C++ Source or Header  |  1990-05-05  |  10KB  |  334 lines

  1. /*****************************************************************************
  2. *   "Irit" - the 3d polygonal solid modeller.                     *
  3. *                                         *
  4. * Written by:  Gershon Elber                Ver 0.2, Mar. 1990   *
  5. ******************************************************************************
  6. *   Dynamic allocation module of "Irit" - the 3d polygonal solid modeller.   *
  7. *****************************************************************************/
  8.  
  9. /* #define DEBUG            Print more messages in free/allocating. */
  10.  
  11. #ifdef __MSDOS__
  12. #include <alloc.h>
  13. #include <time.h>
  14. #include <graphics.h>
  15. #endif /* __MSDOS__ */
  16.  
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include "program.h"
  20. #include "ctrl-brk.h"
  21. #include "graphgng.h"
  22. #include "allocatl.h"
  23. #include "allocatg.h"
  24. #include "windowsg.h"
  25.  
  26. #ifndef __MSDOS__
  27. #include "xgraphic.h"
  28. #endif /* __MSDOS__ */
  29.  
  30. #ifdef __MSDOS__
  31. /* Used to say when to update core left on screen: */
  32. static time_t LastTimeAlloc = 0;
  33. #endif /* __MSDOS__ */
  34.  
  35. /* Used for fast reallocation of most common object types: */
  36. static VertexStruct *VertexFreedList = NULL;
  37. static PolygonStruct *PolygonFreedList = NULL;
  38. static ObjectStruct *ObjectFreedList = NULL;
  39.  
  40. /*****************************************************************************
  41. * My Routine to    allocate dynamic memory. All program requests must call this *
  42. * routine (no direct call to malloc). Dies if no memory.             *
  43. * In order to reduce the overhead of the allocation, basic objects are       *
  44. * allocated in ALLOCATE_NUM number of objects blocks.                 *
  45. *****************************************************************************/
  46. char *MyMalloc(unsigned int Size, int Type)
  47. {
  48.     char *p;
  49.     int i;
  50.  
  51. #   ifdef __MSDOS__
  52.     if (time(NULL) != LastTimeAlloc) {
  53.         if (WasCtrlBrk) FatalError(NULL);           /* Jump to main loop. */
  54.         WndwStatusWindowUpdate();               /* Print free memory. */
  55.         LastTimeAlloc = time(NULL);
  56.         }
  57. #   endif /* __MSDOS__ */
  58.  
  59.     switch (Type) {
  60.     case VERTEX_TYPE:
  61. #        ifdef DEBUG
  62.         fprintf(stderr, "MyMalloc: Allocate vertex type");
  63. #        endif /* DEBUG */
  64.         if (VertexFreedList != NULL) {
  65.         p = (char *) VertexFreedList;
  66.         VertexFreedList = VertexFreedList -> Pnext;
  67.         }
  68.         else {
  69.         VertexStruct *V;
  70.  
  71.         /* Allocate ALLOCATE_NUM objects, returns first one as new   */
  72.         /* and chain together the rest of them into the free list.   */
  73.         p = malloc(Size * ALLOCATE_NUM);
  74.         V = (VertexStruct *) p;
  75.         if (V != NULL) {
  76.             for (i=1; i<ALLOCATE_NUM-1; i++) V[i].Pnext = &V[i+1];
  77.             V[ALLOCATE_NUM-1].Pnext = NULL;
  78.             VertexFreedList = &V[1];
  79.         }
  80.         }
  81.         break;
  82.     case POLYGON_TYPE:
  83. #        ifdef DEBUG
  84.         fprintf(stderr, "MyMalloc: Allocate polygon type");
  85. #        endif /* DEBUG */
  86.         if (PolygonFreedList != NULL) {
  87.         p = (char *) PolygonFreedList;
  88.         PolygonFreedList = PolygonFreedList -> Pnext;
  89.         }
  90.         else {
  91.         PolygonStruct *V;
  92.  
  93.         /* Allocate ALLOCATE_NUM objects, returns first one as new   */
  94.         /* and chain together the rest of them into the free list.   */
  95.         p = malloc(Size * ALLOCATE_NUM);
  96.         V = (PolygonStruct *) p;
  97.         if (V != NULL) {
  98.             for (i=1; i<ALLOCATE_NUM-1; i++) V[i].Pnext = &V[i+1];
  99.             V[ALLOCATE_NUM-1].Pnext = NULL;
  100.             PolygonFreedList = &V[1];
  101.         }
  102.         }
  103.         break;
  104.     case OBJECT_TYPE:
  105. #        ifdef DEBUG
  106.         fprintf(stderr, "MyMalloc: Allocate object type");
  107. #        endif /* DEBUG */
  108.         if (ObjectFreedList != NULL) {
  109.         p = (char *) ObjectFreedList;
  110.         ObjectFreedList = ObjectFreedList -> Pnext;
  111.         }
  112.         else {
  113.         ObjectStruct *V;
  114.  
  115.         /* Allocate ALLOCATE_NUM objects, returns first one as new   */
  116.         /* and chain together the rest of them into the free list.   */
  117.         p = malloc(Size * ALLOCATE_NUM);
  118.         V = (ObjectStruct *) p;
  119.         if (V != NULL) {
  120.             for (i=1; i<ALLOCATE_NUM-1; i++) V[i].Pnext = &V[i+1];
  121.             V[ALLOCATE_NUM-1].Pnext = NULL;
  122.             ObjectFreedList = &V[1];
  123.         }
  124.         }
  125.         break;
  126.     default:
  127. #        ifdef DEBUG
  128.         fprintf(stderr, "MyMalloc: Allocate undefined Type %d", Type);
  129. #        endif /* DEBUG */
  130.         p = malloc(Size);
  131.         break;
  132.     }
  133.  
  134. #   ifdef DEBUG
  135.     fprintf(stderr, " (Size = %d, ptr = %p)\n", Size, p);
  136. #   endif /* DEBUG */
  137.  
  138.     if (p != NULL) return p;
  139.  
  140.     WndwInputWindowPutStr(
  141.     "Not enough memory - program cannt continue", RED);
  142.     GlblFatalError = TRUE;
  143.     /* Long jump to main intraction loop - irit.c module */
  144.     longjmp(LongJumpBuffer, 2);
  145.  
  146.     return NULL;                /* Only makes warnings silent... */
  147. }
  148.  
  149. /**********************************************************************
  150. * Routine to free a given structure, which is not needed any more     *
  151. * Note usually only object will be given directly to MyFree which     *
  152. * recursively free its structure...                      *
  153. * Also, it is perfectly legal to call with NULL to MyFree...          *
  154. **********************************************************************/
  155. void MyFree(char *p, int Type)
  156. {
  157.     int index;
  158.     char Line[LINE_LEN];
  159.  
  160. #   ifdef __MSDOS__
  161.     if (time(NULL) != LastTimeAlloc) {
  162.         if (WasCtrlBrk) FatalError(NULL);           /* Jump to main loop. */
  163.         WndwStatusWindowUpdate();               /* Print free memory. */
  164.         LastTimeAlloc = time(NULL);
  165.         }
  166. #   endif /* __MSDOS__ */
  167.  
  168.     if (p == NULL) return;
  169.  
  170. #   ifdef DEBUG
  171.     /* The following might fail if a record is freed without any usage!  */
  172.     if (*((long *) p) == MAGIC_FREE_NUM)
  173.         FatalError("MyFree:Free the same record twice, dies");
  174.     *((long *) p) = MAGIC_FREE_NUM;          /* And set it for next time... */
  175. #   endif /* DEBUG */
  176.  
  177.     switch (Type) {
  178.     case VERTEX_TYPE:
  179. #        ifdef DEBUG
  180.         fprintf(stderr, "MyFree: free vertex type\n");
  181. #        endif /* DEBUG */
  182.         FreeVertexList((VertexStruct *) p);
  183.         break;
  184.     case POLYGON_TYPE:
  185. #        ifdef DEBUG
  186.         fprintf(stderr, "MyFree: free polygon type\n");
  187. #        endif /* DEBUG */
  188.         FreePolygonList((PolygonStruct *) p);
  189.         break;
  190.     case OBJECT_TYPE:
  191. #        ifdef DEBUG
  192.         fprintf(stderr, "MyFree: free object type\n");
  193. #        endif /* DEBUG */
  194.         switch (((ObjectStruct *) p)->ObjType) {
  195.         case UNDEF_OBJ:
  196.             break;
  197.         case GEOMETRIC_OBJ:     /* The union points on Polygon list. */
  198.             MyFree((char *) (((ObjectStruct *) p)-> U.Pl),
  199.                                 POLYGON_TYPE);
  200.             break;
  201.         case NUMERIC_OBJ:
  202.         case VECTOR_OBJ:
  203.         case MATRIX_OBJ:
  204.         case STRING_OBJ:
  205.             break;
  206.         case OBJ_LIST_OBJ:  /* Need to dereference elements in list. */
  207.             index = 0;
  208.             while (index < MAX_OBJ_LIST &&
  209.                ((ObjectStruct *) p) -> U.PObjList[index] != NULL)
  210.             ((ObjectStruct *) p) -> U.PObjList[index++] -> Count--;
  211.             break;
  212.         default:         /* Kill the program - something is WRONG! */
  213.             sprintf(Line,
  214.             "MyFree: Attempt to free undefined Object type %d",
  215.             ((ObjectStruct *) p)->ObjType);
  216.             FatalError(Line);
  217.             break;
  218.         }
  219.         /* Add it to global freed object list: */
  220.         ((ObjectStruct *) p) -> Pnext = ObjectFreedList;
  221.         ObjectFreedList = (ObjectStruct *) p;
  222.         break;
  223.     default:
  224. #        ifdef DEBUG
  225.         fprintf(stderr, "MyFree: Free undefined Type %d\n", Type);
  226. #        endif /* DEBUG */
  227.         free(p);
  228.         break;
  229.     }
  230. }
  231.  
  232. /**********************************************************************
  233. *   Routine to free a polygon list each consists of circular vertex   *
  234. * list.                                      *
  235. **********************************************************************/
  236. static void FreePolygonList(PolygonStruct *PPoly)
  237. {
  238.     struct PolygonStruct *Ptemp, *PPolyHead = PPoly;
  239.  
  240. #   ifdef DEBUG
  241.     fprintf(stderr, "FreePolygonList: free polygon list\n");
  242. #   endif /* DEBUG */
  243.  
  244.     while (PPoly) {
  245.     FreeVertexList(PPoly -> V);
  246.     Ptemp = PPoly;
  247.     PPoly = PPoly -> Pnext;
  248.     }
  249.  
  250.     /* Now chain this new list to the global freed polygon list: */
  251.     Ptemp -> Pnext = PolygonFreedList;
  252.     PolygonFreedList = PPolyHead;
  253. }
  254.  
  255. /**********************************************************************
  256. *   Routine to free a circular vertex list - one polygon contour.     *
  257. **********************************************************************/
  258. static void FreeVertexList(VertexStruct *VFirst)
  259. {
  260.     struct VertexStruct *V = VFirst, *Vtemp;
  261.  
  262. #   ifdef DEBUG
  263.     fprintf(stderr, "FreeVertexList: free vertex list\n");
  264. #   endif /* DEBUG */
  265.  
  266.     if (VFirst == NULL) return;
  267.  
  268.     do {
  269.     Vtemp = V;
  270.     V = V -> Pnext;
  271.     }
  272.     while (V != NULL && V != VFirst); /* Both - circular or NULL terminated. */
  273.  
  274.     /* Now chain this new list to the global freed vertex list: */
  275.     Vtemp -> Pnext = VertexFreedList;
  276.     VertexFreedList = VFirst;
  277. }
  278.  
  279. /**********************************************************************
  280. * Allocate one Vertex Structure:                      *
  281. **********************************************************************/
  282. struct VertexStruct * AllocVertex(ByteType Count, ByteType Tags,
  283.                 PolygonStruct * PAdj, VertexStruct * Pnext)
  284. {
  285.     struct VertexStruct *p;
  286.  
  287.     p = (VertexStruct *) MyMalloc(sizeof(VertexStruct), VERTEX_TYPE);
  288.  
  289.     p -> Count = Count;
  290.     p -> Tags = Tags;
  291.     p -> Pnext = Pnext;
  292.     p -> PAdj = PAdj;
  293.  
  294.     return p;
  295. }
  296.  
  297. /**********************************************************************
  298. * Allocate one Polygon Structure:                      *
  299. **********************************************************************/
  300. struct PolygonStruct * AllocPolygon(ByteType Count, ByteType Tags,
  301.                 VertexStruct * V, PolygonStruct * Pnext)
  302. {
  303.     struct PolygonStruct *p;
  304.  
  305.     p = (PolygonStruct *) MyMalloc(sizeof(PolygonStruct), POLYGON_TYPE);
  306.  
  307.     p -> Plane[0] = p -> Plane[1] = p -> Plane[2] = p -> Plane[3] = 0.0;
  308.     p -> Count = Count;
  309.     p -> Tags = Tags;
  310.     p -> V = V;
  311.     p -> Pnext = Pnext;
  312.  
  313.     return p;
  314. }
  315.  
  316. /**********************************************************************
  317. * Allocate one Object Structure:                      *
  318. **********************************************************************/
  319. struct ObjectStruct * AllocObject(char *Name, ByteType ObjType,
  320.                          ObjectStruct * Pnext)
  321. {
  322.     struct ObjectStruct *p;
  323.  
  324.     p = (ObjectStruct *) MyMalloc(sizeof(ObjectStruct), OBJECT_TYPE);
  325.  
  326.     strcpy(p -> Name, Name);
  327.     p -> ObjType = ObjType;
  328.     p -> Count = 0;
  329.     p -> Pnext = Pnext;
  330.     p -> U.Pl = NULL;                /* To be on the safe size... */
  331.  
  332.     return p;
  333. }
  334.