1. paintcan
  2. amt

Commits

Sebastian Hubbard  committed 27b6260 Draft

combined redundant code into a macro def

  • Participants
  • Parent commits 3044964
  • Branches default

Comments (0)

Files changed (1)

File src/amt.c

View file
  • Ignore whitespace
 
 #define AMT_ALIGNMENT 4 /* set this to enforce alignment requirements on nodes */
 
-struct t_amtlist *amtlist_new_chunk(int nodesize, int nodecount) {
-  size_t node_and_list = sizeof(struct t_amtlist) + amt_node_size(nodesize);
-  int alloc_count = nodecount;
-  if (AMT_ALIGNMENT > 4) {
-    node_and_list += AMT_ALIGNMENT - (node_and_list % AMT_ALIGNMENT);
-    alloc_count++;
-  }
-  
-  void *mem = AMT_SBRK(node_and_list * alloc_count);
-  struct t_amtlist *links;
-  int i;
+#define LIST_ALLOCATOR(name, size_function) struct t_amtlist * name ## _new_chunk(int size, int nodecount) { \
+    size_t node_and_list = sizeof(struct t_amtlist) + size_function(size); \
+    int alloc_count = nodecount;					\
+									\
+    if (AMT_ALIGNMENT > 4) {						\
+      node_and_list += AMT_ALIGNMENT - (node_and_list % AMT_ALIGNMENT);	\
+      alloc_count++;							\
+    }									\
+									\
+    void *mem = AMT_SBRK(node_and_list * alloc_count);			\
+    struct t_amtlist *links;						\
+    int i;								\
+									\
+    if (AMT_ALIGNMENT > 4) {						\
+      void *alignme = mem + sizeof(struct t_amtlist);			\
+      alignme += (AMT_ALIGNMENT - ((unsigned long) alignme % AMT_ALIGNMENT)); \
+      mem = alignme - sizeof(struct t_amtlist);				\
+    } links = mem;							\
+									\
+    for (i = 1; i < nodecount; i++) {					\
+      links->next = AMT_MARSHAL((void *) links + node_and_list);	\
+      links = AMT_UNMARSHAL(links->next);				\
+    } links->next = 0;							\
+									\
+    return mem;								\
+  }									\
 
-  if (AMT_ALIGNMENT > 4) {    
-    void *alignme = mem + sizeof(struct t_amtlist);
-    alignme += (AMT_ALIGNMENT - ((unsigned long) alignme % AMT_ALIGNMENT));
-    mem = alignme - sizeof(struct t_amtlist);
-  } links = mem;
-
-  for (i = 1; i < nodecount; i++) {
-    links->next = AMT_MARSHAL((void *) links + node_and_list);
-    links = AMT_UNMARSHAL(links->next);
-  } links->next = 0;
-
-  return mem;
-}
-
-struct t_amtlist *camtlist_new_chunk(int bucket, int nodecount) {
-  size_t node_and_list = sizeof(struct t_amtlist) + camt_node_size(bucket);
-  int alloc_count = nodecount;
-
-  if (AMT_ALIGNMENT > 4) {
-    node_and_list += AMT_ALIGNMENT - (node_and_list % AMT_ALIGNMENT);
-    alloc_count++;
-  }
-
-  void *mem = AMT_SBRK(node_and_list * alloc_count);
-  struct t_amtlist *links;
-  int i;
-
-  if (AMT_ALIGNMENT > 4) {    
-    void *alignme = mem + sizeof(struct t_amtlist);
-    alignme += (AMT_ALIGNMENT - ((unsigned long) alignme % AMT_ALIGNMENT));
-    mem = alignme - sizeof(struct t_amtlist);
-  } links = mem;
-
-  for (i = 1; i < nodecount; i++) {
-    links->next = AMT_MARSHAL((void *) links + node_and_list);
-    links = AMT_UNMARSHAL(links->next);
-  } links->next = 0;
-
-  return mem;
-}
+LIST_ALLOCATOR(amtlist, amt_node_size);
+LIST_ALLOCATOR(camtlist, camt_node_size);
 
 /* the amt structure */
 struct t_amt {
 }
 
 /* if there are two single-child nodes in a row rooted at orig, this will 
- * pack them into a single camt node */
+ * pack them into a single camt node (i've never actually tested this though) */
 static amtnode *_amt_compress(amt *t, amtnode *orig) {
   if (!orig)
     return 0;