home *** CD-ROM | disk | FTP | other *** search
/ Virtual Reality Homebrewer's Handbook / vr.iso / vr386 / memory.c < prev    next >
C/C++ Source or Header  |  1996-03-19  |  4KB  |  193 lines

  1. // new MEMLIST memory management
  2. // 1/1/94 by Dave Stampe
  3.  
  4.  
  5. /*
  6.  This code is part of the REND386 project, created by Dave Stampe and
  7.  Bernie Roehl.
  8.  
  9.  Copyright 1992, 1993, 1994 by Dave Stampe and Bernie Roehl.
  10.  
  11.  May be freely used to write software for release into the public domain;
  12.  all commercial endeavours MUST contact BOTH Bernie Roehl and Dave Stampe
  13.  for permission to incorporate any part of this software into their
  14.  products!  Usually there is no charge for under 50-100 items for
  15.  low-cost or shareware, and terms are reasonable.  Any royalties are used
  16.  for development, so equipment is often acceptable payment.
  17.  
  18.  ATTRIBUTION:  If you use any part of this source code or the libraries
  19.  in your projects, you must give attribution to REND386, Dave Stampe,
  20.  and Bernie Roehl in your documentation, source code, and at startup
  21.  of your program.  Let's keep the freeware ball rolling!  No more
  22.  code ripoffs please.
  23.  
  24.  CONTACTS: dstampe@psych.toronto.edu, broehl@sunee.uwaterloo.ca
  25.  See the COPYRITE.H file for more information.
  26. */
  27.  
  28.  
  29. #include <stdio.h>
  30. #include <stdlib.h>  /* for atol(), only for debugging! */
  31. #include <dos.h>
  32. #include <string.h>
  33. #include <alloc.h>
  34.  
  35.  
  36. #ifndef DEFMB
  37.  
  38. struct _mb_ {
  39.           struct _mb_ *prev;    // header of memory
  40.           struct _mb_ *next;
  41.       } ;
  42.  
  43. typedef struct _mb_ MEMBLOCK;
  44.  
  45. typedef struct {                    // head of memory list
  46.          MEMBLOCK *prev;    // USUALLY STATIC MEMORY
  47.          MEMBLOCK *next;
  48.            } MEMLIST;
  49. #endif
  50. #define DEFMB 1
  51.  
  52. #define INIT_MEMLIST { NULL,NULL }    // store in new memlist header
  53. // example:
  54. // MEMLIST env_memlist = INIT_MEMLIST;
  55.  
  56. #define ptr2memblock(p) MK_FP(FP_SEG(p),FP_OFF(p)-(2*sizeof(MEMBLOCK *)))
  57. #define memblock2ptr(p) MK_FP(FP_SEG(p),FP_OFF(p)+(2*sizeof(MEMBLOCK *)))
  58.  
  59.  
  60. #include "vr_api.h"
  61. #include "rend386.h"
  62. #include "intmath.h"
  63.  
  64.  
  65.  
  66. static MEMLIST *current_memory_list = NULL;
  67.  
  68.     // set list which memory will be recorded to
  69.     // returns old list if any
  70. MEMLIST *set_memory_list(MEMLIST *m)
  71. {
  72.   MEMLIST *old = current_memory_list;
  73.   current_memory_list = m;
  74.   return old;
  75. }
  76.  
  77.  
  78. void *listalloc(long size)       // to default list
  79. {
  80.   MEMBLOCK *m;
  81.  
  82.   if(current_memory_list==NULL) return malloc(size);
  83.   m = malloc(size + 2*sizeof(MEMBLOCK *));
  84.   if(!m) return NULL;
  85.   m->next = current_memory_list->next;    // add to list
  86.   m->prev = current_memory_list;
  87.   return memblock2ptr(m);
  88. }
  89.  
  90.  
  91. void *listcalloc(long size, long count)   // to default list
  92. {
  93.   void *p;
  94.  
  95.   if(current_memory_list==NULL) return calloc(size, count);
  96.   p = listalloc(size*count);
  97.   if(!p) return NULL;
  98.   memset(p, 0, size*count);
  99.   return p;
  100. }
  101.  
  102.  
  103. void listfree(void *p)        // any list
  104. {
  105.   MEMBLOCK *m = ptr2memblock(p);
  106.  
  107.   if(!p) return;    // sanity check
  108.  
  109.   if(m->prev)    // can't delete header!
  110.     {
  111.       m->prev->next = m->next;
  112.       if(m->next)
  113.      m->next->prev = m->prev;
  114.       free(m);
  115.     }
  116. }
  117.  
  118.  
  119. void listfreeall(MEMLIST *p)    // any list
  120. {
  121.   if(!p) return;    // sanity check
  122.   while(p->next)
  123.     {
  124.       p->next = p->next->next;
  125.       free(p->next);
  126.     }
  127. }
  128.  
  129.  
  130. void *xlistalloc(long size, MEMLIST *list)    // specific list
  131. {
  132.   MEMBLOCK *m;
  133.  
  134.   if(current_memory_list==NULL) return malloc(size);
  135.   m = malloc(size + 2*sizeof(MEMBLOCK *));
  136.   if(!m) return NULL;
  137.   m->next = current_memory_list->next;    // add to list
  138.   m->prev = current_memory_list;
  139.   return memblock2ptr(m);
  140. }
  141.  
  142.  
  143. void *xlistcalloc(long size, long count, MEMLIST *list)    // specific list
  144. {
  145.   void *p;
  146.  
  147.   if(current_memory_list==NULL) return calloc(size, count);
  148.   p = listalloc(size*count);
  149.   if(!p) return NULL;
  150.   memset(p, 0, size*count);
  151.   return p;
  152. }
  153.  
  154.  
  155.  
  156. ////////////////////////////////////////////////////
  157. /// WORLD MANAGEMENT:
  158. /// allows suspension, resumption, freeing of memory
  159. /// also loading (some)
  160.  
  161.  
  162. /// WORLD STATICS
  163.  
  164.  
  165. //CAMERA *default_camera;
  166. //CAMERA *current_camera;
  167. //POSE initial_body_pose;
  168. //POSE *home_pose;
  169. //POSE *body_pose;
  170.  
  171.  
  172. WORLD *create_world()
  173. {
  174. }
  175.  
  176. void release_world(WORLD *w)
  177. {
  178.  
  179.   free(w);
  180. }
  181.  
  182. void save_world_state(WORLD *w)
  183. {
  184. }
  185.  
  186. void restore_world_state(WORLD *w)
  187. {
  188. }
  189.  
  190. void load_world(FILE *in, WORLD *w)
  191. {
  192. }
  193.