Lexer actually lexes filenames now, and odats are made out of mapvariants
[henge/webcc.git] / src / apc / irmem.c
index 1e0024f..415399c 100644 (file)
@@ -11,7 +11,7 @@ struct cdat*
 alloc_cdat(void);
 struct odat*
 alloc_odat(void);
-void
+struct vdat*
 alloc_vdat(void);
 struct link*
 alloc_link(void);
@@ -23,65 +23,112 @@ struct odat*
 curr_odat(void);
 struct vdat*
 curr_vdat(void);
-struct ele*
-curr_ele(void);
+struct map*
+curr_map(void);
 struct set*
 curr_set(void);
 struct ref*
-prev_ref(void);
-struct quad
-curr_quad(void);
-struct model
+curr_ref(void);
+struct model*
 curr_model(void);
 void
 inc_posts(void);
 
-#define CURR_CDAT (*cdat_stackp)
-#define CURR_SET set_list[CURR_CDAT->num_sets]
-#define CURR_ELE ele_list[CURR_CDAT->CURR_SET.num_ele]
-#define PREV_REF (ref_buf[num_refs-1])
-#define CURR_REF (ref_buf[num_refs])
-#define PREV_ODAT (odat_buf[num_odats-1])
-#define CURR_ODAT (odat_buf[num_odats])
-#define CURR_VDAT (vdat_buf[num_vdats])
-#define PREV_VDAT (vdat_buf[num_vdats-1])
-#define CURR_MODEL (CURR_VDAT->model_list[CURR_VDAT->num_models])
-#define CURR_LINK (link_buf[num_links])
-#define CURR_POST (post_buf[num_posts])
-#define CURR_QUAD (CURR_ODAT->quad_list[CURR_ODAT->num_quads])
-
+#define PAGES_PER_CHUNK 16
+
+//"type free" chunk stacking
+struct chunk_stack
+{ void* chunks[MAX_CHUNKS];
+  void* *csp;            //chunk stack pointer
+  void* dsp[MAX_CHUNKS]; //dat stack pointer (per chunk)
+  int   chunk_size;      //size of a chunk (including its forfeited page)
+  int   max_dats;        //number of dats per chunk for this stack
+} ocs, vcs, ccs, rcs, lcs, pcs, mcs; //odat, vdat, cdat,map, ref, link, post stacks
+
+//type safety handled by macro expansion (do not call these directly from code, make dependent macros for access to these)
+#define CHUNKS_LEN(STACK)         ((STACK).csp - (STACK).chunks)
+#define CURRENT_CHUNK(STACK)      ((STACK).chunks[CHUNKS_LEN(STACK) - 1])
+#define CHUNKS_FULL(STACK)        ( (STACK).csp >= \
+                                    (STACK).chunks + MAX_CHUNKS * (STACK).chunk_size)
+#define CURRENT_DSP(STACK,TYPE)   ((TYPE*) ((STACK).dsp[CHUNKS_LEN(STACK) - 1]))
+#define DATA_FULL(STACK,TYPE)     ((void*) CURRENT_DSP(STACK,TYPE) >=  \
+                                  (CURRENT_CHUNK(STACK) + (STACK).chunk_size))
+#define CSP_PUSH(STACK)           (*(++(STACK).csp) = malloc((STACK).chunk_size))
+#define CURRENT_DATP(STACK,TYPE)  (((TYPE**)(STACK).dsp)[CHUNKS_LEN(STACK) - 1])
+#define PREVIOUS_DATP(STACK,TYPE) (((TYPE**)(STACK).dsp)[CHUNKS_LEN(STACK) - 2])
+#define ALLOC_DAT(STACK,TYPE)     (++CURRENT_DATP(STACK,TYPE))
+#define INIT_STACK(STACK,TYPE) \
+  { int i; \
+    (STACK).chunk_size = PAGES_PER_CHUNK * pagesize;            \
+    (STACK).max_dats = (STACK).chunk_size / sizeof (TYPE);     \
+    CSP_PUSH(STACK); \
+    for( i = 0; i < MAX_CHUNKS; i++){ \
+      (STACK).dsp[i] += pagesize; \
+    } \
+  }
+//Stack-specific macros (called directly from code (safety enforcement)
+#define INIT_ODAT()     (INIT_STACK(ocs, struct odat))
+#define CURRENT_ODAT()  (CURRENT_DATP(ocs,struct odat))
+#define ODAT_FULL()     (DATA_FULL(ocs,struct odat))
+#define ODAT_ALLOC()    (ALLOC_DAT(ocs,struct odat))
+#define OCS_FULL()      (CHUNKS_FULL(ocs))
+#define INIT_VDAT()     (INIT_STACK(vcs, struct vdat))
+#define CURRENT_VDAT()  (CURRENT_DATP(vcs,struct vdat))
+#define VDAT_FULL()     (DATA_FULL(vcs,struct vdat))
+#define VDAT_ALLOC()    (ALLOC_DAT(vcs,struct vdat))
+#define VCS_FULL()      (CHUNKS_FULL(vcs))
+#define INIT_CDAT()     (INIT_STACK(ccs, struct cdat))
+#define CURRENT_CDAT()  (CURRENT_DATP(ccs,struct cdat))
+#define CDAT_FULL()     (DATA_FULL(ccs, struct cdat))
+#define CDAT_ALLOC()    (ALLOC_DAT(ccs, struct cdat))
+#define CCS_FULL()      (CHUNKS_FULL(ccs))
+#define INIT_MAP()     (INIT_STACK(mcs, struct map))
+#define CURRENT_MAP()  (CURRENT_DATP(mcs, struct map))
+#define MAP_FULL()     (DATA_FULL(mcs, struct map))
+#define MAP_ALLOC()     (ALLOC_DAT(mcs, struct map))
+#define MCS_FULL()      (CHUNKS_FULL(mcs))
+#define INIT_LINK()     (INIT_STACK(lcs, struct link))
+#define CURRENT_LINK()  (CURRENT_DATP(lcs,struct link))
+#define LDAT_FULL()     (DATA_FULL(lcs, struct link))
+#define LDAT_ALLOC()    (ALLOC_DAT(lcs, struct link))
+#define LCS_FULL()      (CHUNKS_FULL(lcs))
+#define INIT_POST()     (INIT_STACK(rcs, struct ref))
+#define CURRENT_POST()  (CURRENT_DATP(pcs,struct ref))
+#define POST_FULL()     (DATA_FULL(pcs,struct ref))
+#define POST_ALLOC()    (ALLOC_DAT(pcs,struct ref))
+#define PCS_FULL()      (CHUNKS_FULL(pcs))
+#define INIT_REF()      (INIT_STACK(rcs, struct ref))
+#define CURRENT_REF()   (CURRENT_DATP(rcs,struct ref))
+#define PREVIOUS_REF()  (PREVIOUS_DATP(rcs, struct ref))
+#define REF_FULL()      (DATA_FULL(rcs,struct ref))
+#define REF_ALLOC()     (ALLOC_DAT(rcs,struct ref))
+#define RCS_FULL()      (CHUNKS_FULL(rcs))
+//Metadata
+#define CURRENT_SET()   (CURRENT_CDAT()->set_list[CURRENT_CDAT()->num_sets])
+//#define CURRENT_QUAD()  (CURRENT_MAP()->quad_list[CURRENT_MAP()->num_quads])
+//#define CURRENT_MODEL() (CURRENT_VDAT()->model_list[CURRENT_VDAT()->num_models])
+
+
+
+long pagesize;
 
 int num_cdats = 0;
-int curr_max_cdats = PTRS_IN_PAGE;
 
-struct cdat* cdat_buf[PTRS_IN_PAGE];
-struct cdat* cdat_stack[PTRS_IN_PAGE];
+struct cdat* cdat_stack[MAX_CLASSES];
 struct cdat** cdat_stackp;
 
+int num_odats = 0;
 
-int num_odats = -1;
-int curr_max_odats = PTRS_IN_PAGE;
-struct odat* odat_buf[PTRS_IN_PAGE];
-
-
-int num_vdats = -1;
-int curr_max_vdats = PTRS_IN_PAGE;
-struct vdat* vdat_buf[PTRS_IN_PAGE];
+int num_vdats = 0;
 
+int num_maps = 0;
 
-int num_refs = -1;
-int curr_max_refs = PTRS_IN_PAGE;
-struct ref* ref_buf[PTRS_IN_PAGE];
-uint64_t ss_ref_id = 0x00FFFFFF; /* system space for ref_ids */
+int num_refs = 0;
+int ss_ref_id = 0x0FFFFFFF; /* system space for ref_ids */
 
-int num_posts = -1;
-int curr_max_posts = PTRS_IN_PAGE;
-struct ref* post_buf[PTRS_IN_PAGE];
+int num_posts = 0;
 
-
-int num_links = -1;
-int curr_max_links = PTRS_IN_PAGE;
-struct link* link_buf[PTRS_IN_PAGE];
+int num_links = 0;
 
 
 /* The initalization function of the IR. */
@@ -89,21 +136,24 @@ int
 ir_init()
 {
 
-  /* Init root cdat and stack */
   char root[4] = "root";
 
-  if( (cdat_buf[num_cdats] = (struct cdat*) malloc(sizeof(struct cdat))) == NULL)
-    {
-      perror("malloc root class failed\n");
-      return -1;
-    }
-  cdat_buf[num_cdats]->idx = num_cdats;
-  memmove(cdat_buf[num_cdats]->name, root, 4);
+  pagesize = sysconf(_SC_PAGESIZE);
+
+  INIT_CDAT();
+  *cdat_stackp = CURRENT_CDAT();
+  
+  memmove((*cdat_stackp)->name, root, 32);
+
+  INIT_ODAT();
+  INIT_VDAT();
+  INIT_MAP();
+  INIT_LINK();
+  INIT_REF();
+  INIT_POST();
 
-  cdat_stackp = cdat_stack;
-  *cdat_stackp++ = cdat_buf[num_cdats++];
 
-    return 0;
+  return 0;
 
 }
 
@@ -112,128 +162,164 @@ ir_quit()
 {
   int i;
 
-  for(i = 0; i <= num_odats ; i++)
+  for(i = 0; i < CHUNKS_LEN(ccs) ; i++)
     {
-      free(odat_buf[i]);
+      free(ccs.chunks[i]);
     }
-  for(i = 0; i <= num_cdats; i++)
+  for(i = 0; i < CHUNKS_LEN(ocs); i++)
     {
-      free(cdat_buf[i]);
+      free(ocs.chunks[i]);
     }
-  for(i = 0; i <= num_vdats; i++)
+  for(i = 0; i < CHUNKS_LEN(vcs) ; i++)
     {
-      free(vdat_buf[i]);
+      free(vcs.chunks[i]);
     }
-  for(i = 0; i <= num_refs; i++)
+  for(i = 0; i < CHUNKS_LEN(rcs); i++)
     {
-      free(ref_buf[i]);
+      free(rcs.chunks[i]);
     }
-  for(i = 0; i<= num_links; i++)
+  for(i = 0; i < CHUNKS_LEN(lcs); i++)
     {
-      free(link_buf[i]);
+      free(lcs.chunks[i]);
+    }
+  for(i = 0; i < CHUNKS_LEN(pcs); i++)
+    {
+      free(pcs.chunks[i]);
     }
 
 }
 
-//TODO: FREE MEMORY!
 struct cdat*
 alloc_cdat()
 {
   num_cdats++;
-  if(curr_max_cdats <= num_cdats)
-    { if( (realloc((void*) cdat_buf, PTRS_IN_PAGE * 4)) == NULL)
-        perror("realloc cdat_buf failed");
-      curr_max_cdats += PTRS_IN_PAGE;
-      if( (realloc( (void*) cdat_stack, PTRS_IN_PAGE * 4)) == NULL) //increase cdat_stack also
-        perror("realloc cdat_stack failed");
+  if(CDAT_FULL())
+    { if(CCS_FULL())
+        { fprintf(stderr, "You have allocated to many (%d) cdats ", num_cdats);
+          exit(EXIT_FAILURE);
+        }
+      else
+        CSP_PUSH(ccs);
     }
-  if( (CURR_CDAT = (struct cdat*) malloc(sizeof (struct cdat)) ) == NULL )
-    perror("malloc cdat failed");
-
-  return CURR_CDAT;
+  else
+    CDAT_ALLOC();
 
+  return CURRENT_CDAT();
 }
+
+//these should probably be inline
 struct odat*
 alloc_odat
 ()
 {
-
-  num_odats++;
-  if(num_odats >= curr_max_odats)
-    { if( (realloc((void*) odat_buf, PTRS_IN_PAGE * 4)) == NULL)
-        perror("realloc odat_buf failed");
-      curr_max_odats += PTRS_IN_PAGE;
+   num_odats++;
+  if(ODAT_FULL())
+    { if(!OCS_FULL())
+        { fprintf(stderr, "You have allocated to many (%d) odats ", num_odats);
+          exit(EXIT_FAILURE);
+        }
+      else
+        CSP_PUSH(ocs);
     }
-  if( (CURR_ODAT = (struct odat*) malloc(sizeof (struct odat))) == NULL)
-    perror("malloc odat failed");
+  else
+    ODAT_ALLOC();
 
-  return CURR_ODAT;
+  return CURRENT_ODAT();
 }
 
-void
+struct vdat*
 alloc_vdat
 ()
-{
-  num_vdats++;
-  if(num_vdats >= curr_max_vdats)
-    { if( (realloc((void*) vdat_buf, PTRS_IN_PAGE * 4)) == NULL)
-        perror("realloc vdat_buf failed");
-      curr_max_vdats += PTRS_IN_PAGE;
+{ num_vdats++;
+  if(VDAT_FULL())
+    { if(!VCS_FULL())
+        { fprintf(stderr, "You have allocated to many (%d) vdats ", num_vdats);
+          exit(EXIT_FAILURE);
+        }
+      else
+        CSP_PUSH(vcs);
+    }
+  else
+    VDAT_ALLOC();
+
+  return CURRENT_VDAT();
+}
+
+struct map* 
+alloc_map
+()
+{ num_maps++;
+  if(MAP_FULL())
+    { if(!MCS_FULL())
+        { fprintf(stderr, "You have allocated to many (%d) maps ", num_maps);
+          exit(EXIT_FAILURE);
+        }
+      else
+        CSP_PUSH(mcs);
     }
-  if((CURR_VDAT = (struct vdat*) malloc(sizeof (struct vdat))) == NULL)
-     perror("malloc vdat failed");
+  else
+    MAP_ALLOC();
 
+  return CURRENT_MAP();
 }
 
+
 struct link*
 alloc_link
 ()
-{
-  num_links++;
-
-  if(num_links >= curr_max_links)
-    { if( (realloc((void*) link_buf, PTRS_IN_PAGE * 4)) == NULL)
-        perror("realloc vdat_buf failed");
-      curr_max_links += PTRS_IN_PAGE;
+{ num_links++;
+  if(LDAT_FULL())
+    { if(!LCS_FULL())
+        { fprintf(stderr, "You have allocated to many (%d) links ", num_links);
+          exit(EXIT_FAILURE);
+        }
+      else
+        CSP_PUSH(lcs);
     }
-  if((CURR_LINK = (struct link*) malloc(sizeof (struct link))) == NULL)
-      perror("malloc link failed");
+  else
+    LDAT_ALLOC();
+
+  return CURRENT_LINK();
 
-  return CURR_LINK;
 }
 
 struct ref*
 alloc_ref
 ()
-{
-  num_refs++;
+{ num_refs++;
+  if(REF_FULL())
+    { if(!RCS_FULL())
+        { fprintf(stderr, "You have allocated to many (%d) refs ", num_refs);
+          exit(EXIT_FAILURE);
+        }
+      else
+        CSP_PUSH(rcs);
+    }
+  else
+    REF_ALLOC();
+
 
   if(num_refs % 16 == 0)
-    { CURR_POST = CURR_REF;
+    { CURRENT_POST() = CURRENT_REF();
       inc_posts();
     }
 
-  if(num_refs >= curr_max_refs)
-    { if( (realloc((void*) ref_buf, PTRS_IN_PAGE * 4)) == NULL)
-        perror("realloc ref_buf failed");
-      curr_max_refs += PTRS_IN_PAGE;
-    }
-  if((CURR_REF = (struct ref*) malloc(sizeof (struct ref))) == NULL)
-    perror("malloc ref failed");
-
-  return CURR_REF;
+  return CURRENT_REF();
 }
 
 void
 inc_posts()
-{
-  if(num_posts >= curr_max_posts)
-    { if( (realloc((void*) ref_buf, PTRS_IN_PAGE * 4)) == NULL)
-        perror("realoc post_buf failed");
-      curr_max_posts += PTRS_IN_PAGE;
+{ num_posts++;
+  if(POST_FULL())
+    { if(!PCS_FULL())
+        { fprintf(stderr, "You have allocated to many (%d) refs ", num_posts);
+          exit(EXIT_FAILURE);
+        }
+      else
+        CSP_PUSH(pcs);
     }
-  if ((CURR_POST = (struct ref*) malloc (sizeof (struct ref))) == NULL)
-    perror("malloc post failed");
+  else
+    POST_ALLOC();
 
 }
 
@@ -241,49 +327,48 @@ struct cdat*
 curr_cdat
 ()
 {
-  return CURR_CDAT;
+  return (*cdat_stackp);
 }
 
 struct odat*
 curr_odat
 ()
 {
-  return CURR_ODAT;
+  return CURRENT_ODAT();
 }
 struct vdat*
 curr_vdat
 ()
 {
-  return CURR_VDAT;
+  return CURRENT_VDAT();
 }
 struct set*
 curr_set
 ()
 {
-  return &CURR_CDAT->CURR_SET;
-}
-struct ele*
-curr_ele
-()
-{
-  return &CURR_CDAT->CURR_SET.CURR_ELE;
+  return &CURRENT_SET();
 }
 struct ref*
-prev_ref
-()
-{
-  return PREV_REF;
-}
-
-struct quad
-curr_quad
+curr_ref
 ()
 {
-  return CURR_QUAD;
+  return CURRENT_REF();
 }
-struct model
-curr_model
+struct map*
+curr_map
 ()
 {
-  return CURR_MODEL;
+  return CURRENT_MAP();
 }
+/* struct quad* */
+/* curr_quad */
+/* () */
+/* { */
+/*   return &CURRENT_QUAD(); */
+/* } */
+/* struct model* */
+/* curr_model */
+/* () */
+/* { */
+/*   return &CURRENT_MODEL(); */
+/* } */