Optimize out the classic or moveable allocator when not needed
authorNick Downing <nick.downing@lifx.co>
Thu, 11 Apr 2019 12:41:26 +0000 (22:41 +1000)
committerNick Downing <nick.downing@lifx.co>
Thu, 11 Apr 2019 12:41:26 +0000 (22:41 +1000)
blank.py
gen.sh
pool.c
pool.h

index ba68ce9..823ba15 100755 (executable)
--- a/blank.py
+++ b/blank.py
@@ -13,7 +13,7 @@ for i in sys.stdin:
     blank = True
   else:
     after = i.lstrip()
-    if blank and before[-1:] not in opens and after[:1] not in closes:
+    if blank and len(before) and before[-1:] not in opens and after[:1] not in closes:
       sys.stdout.write('\n')
     sys.stdout.write(i + '\n')
     j = i.find('//')
diff --git a/gen.sh b/gen.sh
index f1c9596..3749c71 100755 (executable)
--- a/gen.sh
+++ b/gen.sh
@@ -1,5 +1,8 @@
 #!/bin/sh -e
 
+sed -e 's/^\/\/#define CLASSIC_POOL 1/#define CLASSIC_POOL 1/' -i pool.h
+sed -e 's/^#define MOVEABLE_POOL 1/\/\/#define MOVEABLE_POOL 1/' -i pool.h
+
 # we will only use non-moveable swap, so it must be preallocated,
 # this is because the swap-move routines are too hard to implement
 sed -e 's/^\/\/#define PREALLOCATE_SWAP 1/#define PREALLOCATE_SWAP 1/' -i swap.h
@@ -23,6 +26,8 @@ sed -e 's/^\/\/#define INODE_SWAP 1/#define INODE_SWAP 1/' -i process.h
 rm indirect_core_inode_swap/swap.[ch]
 cp rassert.h fuzix_fs.[ch] util.[ch] indirect_core_inode_swap
 
+sed -e 's/^\/\/#define MOVEABLE_POOL 1/#define MOVEABLE_POOL 1/' -i pool.h
+
 sed -e 's/^#define PREALLOCATE_CORE 1/\/\/#define PREALLOCATE_CORE 1/' -i core.h
 sed -e 's/^#define INDIRECT_CORE 1/\/\/#define INDIRECT_CORE 1/' -i core.h
 sed -e 's/^\/\/#define MOVEABLE_CORE 1/#define MOVEABLE_CORE 1/' -i core.h
@@ -36,6 +41,8 @@ sed -e 's/^\/\/#define INDIRECT_SWAP 1/#define INDIRECT_SWAP 1/' -i swap.h
 ./pp.sh moveable_core_indirect_swap
 cp rassert.h moveable_core_indirect_swap
 
+sed -e 's/^#define CLASSIC_POOL 1/\/\/#define CLASSIC_POOL 1/' -i pool.h
+
 sed -e 's/^\/\/#define INODE_SWAP 1/#define INODE_SWAP 1/' -i process.h
 ./pp.sh moveable_core_inode_swap
 rm moveable_core_inode_swap/swap.[ch]
diff --git a/pool.c b/pool.c
index 0a55269..e89184e 100644 (file)
--- a/pool.c
+++ b/pool.c
@@ -39,6 +39,7 @@ void pool_init(
  check_invariants(head);
 }
  
+#ifdef CLASSIC_POOL
 bool pool_alloc(
   struct pool_head *head,
   struct pool_item *item,
@@ -248,24 +249,9 @@ resize:
  check_invariants(head);
   return true;
 }
-
-void pool_free(struct pool_head *head, struct pool_item *item) {
-  // item must be already allocated
-  assert(item->prev && item->next);
-
-  // remove from list
-  item->prev->next = item->next;
-  item->next->prev = item->prev;
-#ifndef NDEBUG
-  item->prev = NULL;
-  item->next = NULL;
 #endif
 
-  // keep track of total allocation
-  head->avail += item->limit - item->base;
- check_invariants(head);
-}
-
+#ifdef MOVEABLE_POOL
 bool pool_alloc_moveable(
   struct pool_head *head,
   struct pool_item *item,
@@ -802,19 +788,21 @@ resize:
  check_invariants(head);
   return true;
 }
+#endif
 
-void pool_move_item(struct pool_item *from, struct pool_item *to) {
-  // from-item must be already allocated
-  assert(from->prev && from->next);
-
-  // to-item must not be already allocated
-  assert(to->prev == NULL && to->next == NULL);
+void pool_free(struct pool_head *head, struct pool_item *item) {
+  // item must be already allocated
+  assert(item->prev && item->next);
 
-  *to = *from;
+  // remove from list
+  item->prev->next = item->next;
+  item->next->prev = item->prev;
 #ifndef NDEBUG
-  from->prev = NULL;
-  from->next = NULL;
+  item->prev = NULL;
+  item->next = NULL;
 #endif
-  to->prev->next = to;
-  to->next->prev = to;
+
+  // keep track of total allocation
+  head->avail += item->limit - item->base;
+ check_invariants(head);
 }
diff --git a/pool.h b/pool.h
index 3928cd3..1b4d106 100644 (file)
--- a/pool.h
+++ b/pool.h
@@ -3,6 +3,9 @@
 
 #include <stdbool.h>
 
+//#define CLASSIC_POOL 1
+#define MOVEABLE_POOL 1
+
 struct pool_item {
   struct pool_item *prev;
   struct pool_item *next;
@@ -24,6 +27,7 @@ void pool_init(
   void (*move)(struct pool_item *item, int new_base),
   void (*move_up)(struct pool_item *item, int new_limit)
 );
+#ifdef CLASSIC_POOL
 bool pool_alloc(
   struct pool_head *head,
   struct pool_item *item,
@@ -39,7 +43,8 @@ bool pool_realloc_base(
   struct pool_item *item,
   int size
 );
-void pool_free(struct pool_head *head, struct pool_item *item);
+#endif
+#ifdef MOVEABLE_POOL
 bool pool_alloc_moveable(
   struct pool_head *head,
   struct pool_item *item,
@@ -55,6 +60,7 @@ bool pool_realloc_base_moveable(
   struct pool_item *item,
   int size
 );
-void pool_move_item(struct pool_item *from, struct pool_item *to);
+#endif
+void pool_free(struct pool_head *head, struct pool_item *item);
 
 #endif