[llvm-commits] CVS: poolalloc/runtime/FreeListAllocator/PoolAllocator.cpp PoolAllocator.h

John Criswell criswell at cs.uiuc.edu
Mon Nov 10 15:58:01 PST 2003


Changes in directory poolalloc/runtime/FreeListAllocator:

PoolAllocator.cpp updated: 1.1 -> 1.2
PoolAllocator.h updated: 1.1 -> 1.2

---
Log message:

Made the slab header contiguous with the rest of the slab.
Removed reference counts on slabs as they are currently unused.
Modified my funky union pointer cast thing to be worse than before.



---
Diffs of the changes:  (+38 -41)

Index: poolalloc/runtime/FreeListAllocator/PoolAllocator.cpp
diff -u poolalloc/runtime/FreeListAllocator/PoolAllocator.cpp:1.1 poolalloc/runtime/FreeListAllocator/PoolAllocator.cpp:1.2
--- poolalloc/runtime/FreeListAllocator/PoolAllocator.cpp:1.1	Mon Nov 10 13:42:39 2003
+++ poolalloc/runtime/FreeListAllocator/PoolAllocator.cpp	Mon Nov 10 15:57:48 2003
@@ -20,21 +20,12 @@
 #undef assert
 #define assert(X)
 
-typedef union 
+typedef union
 {
-  unsigned char * memory;
-  struct NodeHeader * header;
+  unsigned char * header;
+  unsigned char ** next;
 } NodePointer;
 
-struct NodeHeader
-{
-  // Pointer to the slab which owns me
-  struct SlabHeader * Slab;
-
-  // Pointer to the next node on the free list
-  NodePointer Next;
-};
-
 //===----------------------------------------------------------------------===//
 //
 //  PoolSlab implementation
@@ -55,7 +46,7 @@
   struct SlabHeader * Next;
 
   // Pointer to the list of nodes
-  NodePointer Nodes;
+  unsigned char Data [];
 };
 
 //
@@ -70,20 +61,22 @@
   // Pointer to the new Slab
   struct SlabHeader * NewSlab;
 
-  // Pointer to the new node
-  struct NodeHeader NewNode;
-
   // The number of elements in the slab
   const unsigned int NodesPerSlab = 128;
 
   // Pointers and index for initializing memory
   NodePointer p;
-  unsigned int index;
+
+  //
+  // Determine the size of the slab.
+  //
+  int slab_size = ((sizeof (unsigned char *) + NodeSize) * NodesPerSlab) +
+                   sizeof (struct SlabHeader);
 
   //
   // Allocate a piece of memory for the new slab.
   //
-  NewSlab = (struct SlabHeader *) malloc (sizeof (struct SlabHeader));
+  NewSlab = (struct SlabHeader *) malloc (slab_size);
   assert (NewSlab != NULL);
 
   //
@@ -95,27 +88,28 @@
   NewSlab->Next = NULL;
 
   //
-  // Allocate enough memory for all the nodes.
-  //
-  NewSlab->Nodes.memory = (unsigned char *) malloc ((sizeof (struct NodeHeader) + NodeSize) * NodesPerSlab);
-  assert (NewSlab->Nodes.memory != NULL);
-
-  //
   // Initialize each node in the list.
   //
-  for (p = NewSlab->Nodes, index = 0; index < NodesPerSlab; index++)
+  p.header = &(NewSlab->Data[0]);
+  while (p.header < (&(NewSlab->Data[0]) + slab_size - sizeof (struct SlabHeader)))
   {
-    p.header->Slab = NewSlab;
-    if (index == (NodesPerSlab - 1))
-    {
-      p.header->Next.memory = NULL;
-    }
-    else
-    {
-      p.header->Next.memory = (p.memory += (sizeof (struct NodeHeader) + NodeSize));
-    }
+    //
+    // Calculate the position of the next header and put its address in
+    // this current header.
+    //
+    *(p.next) = p.header + (sizeof (unsigned char *) + NodeSize);
+
+    //
+    // Move on to the next header.
+    //
+    p.header = *(p.next);
   }
 
+  p.header = (&(NewSlab->Data[0]) + slab_size - sizeof (struct SlabHeader)
+                                     - sizeof (unsigned char *)
+                                     - NodeSize);
+  *(p.next) = NULL;
+
   return NewSlab;
 }
 
@@ -216,13 +210,15 @@
     // Take the linked list of nodes inside the slab and add them to the
     // free list.
     //
-    Pool->FreeList = Pool->Slabs->Nodes.header;
+    Pool->FreeList = &(Pool->Slabs->Data[0]);
   }
 
   //
   // Increase the slab's reference count.
   //
+#if 0
   slabAlloc (Pool->FreeList->Slab);
+#endif /* 0 */
 
   //
   // Grab the first element from the free list and return it.
@@ -230,8 +226,8 @@
   NodePointer MemoryBlock;
 
   MemoryBlock.header = Pool->FreeList;
-  Pool->FreeList=Pool->FreeList->Next.header;
-  return (MemoryBlock.memory += sizeof (struct NodeHeader));
+  Pool->FreeList=*(MemoryBlock.next);
+  return (MemoryBlock.header += sizeof (unsigned char *));
 }
 
 void *
@@ -254,17 +250,19 @@
   //
   // Find the header of the memory block.
   //
-  Node.memory = (unsigned char *)(Block) - (sizeof (struct NodeHeader));
+  Node.header = (unsigned char *)(Block) - (sizeof (unsigned char *));
 
+#if 0
   //
   // Decrease the slab's reference count.
   //
   slabFree (Node.header->Slab);
+#endif /* 0 */
 
   //
   // Add the node back to the free list.
   //
-  Node.header->Next.header = Pool->FreeList;
+  *(Node.next) = Pool->FreeList;
   Pool->FreeList = Node.header;
 
   return;


Index: poolalloc/runtime/FreeListAllocator/PoolAllocator.h
diff -u poolalloc/runtime/FreeListAllocator/PoolAllocator.h:1.1 poolalloc/runtime/FreeListAllocator/PoolAllocator.h:1.2
--- poolalloc/runtime/FreeListAllocator/PoolAllocator.h:1.1	Mon Nov 10 13:42:39 2003
+++ poolalloc/runtime/FreeListAllocator/PoolAllocator.h	Mon Nov 10 15:57:48 2003
@@ -16,7 +16,6 @@
 #define POOLALLOCATOR_RUNTIME_H
 
 struct SlabHeader;
-struct NodeHeader;
 
 typedef struct PoolTy {
   // NodeSize - Keep track of the object size tracked by this pool
@@ -26,7 +25,7 @@
   struct SlabHeader * Slabs;
 
   // Pointer to the free list of nodes
-  struct NodeHeader * FreeList;
+  unsigned char * FreeList;
 
   // FreeablePool - Set to false if the memory from this pool cannot be freed
   // before destroy.





More information about the llvm-commits mailing list