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

John Criswell criswell at cs.uiuc.edu
Tue Nov 11 14:14:02 PST 2003


Changes in directory poolalloc/runtime/FreeListAllocator:

PageManager.cpp updated: 1.2 -> 1.3
PageManager.h updated: 1.1 -> 1.2
PoolAllocator.cpp updated: 1.9 -> 1.10

---
Log message:

Added support for array spanning multiple pages.  Current implementation
bypasses some of the PageManager functionality, but this keeps things
simple.
Added some comments to the PageManager code.
Made GetPages() externally accessible.



---
Diffs of the changes:  (+42 -43)

Index: poolalloc/runtime/FreeListAllocator/PageManager.cpp
diff -u poolalloc/runtime/FreeListAllocator/PageManager.cpp:1.2 poolalloc/runtime/FreeListAllocator/PageManager.cpp:1.3
--- poolalloc/runtime/FreeListAllocator/PageManager.cpp:1.2	Mon Nov 10 16:44:57 2003
+++ poolalloc/runtime/FreeListAllocator/PageManager.cpp	Tue Nov 11 14:13:42 2003
@@ -32,12 +32,19 @@
 typedef std::vector<void*, MallocAllocator<void*> > FreePagesListType;
 static FreePagesListType *FreePages = 0;
 
+//
+// Function: InitializePageManager ()
+//
+// Description:
+//  This function initializes the Page Manager code.  It must be called before
+//  any other Page Manager functions are called.
+//
 void InitializePageManager() {
   if (!PageSize) PageSize = sysconf(_SC_PAGESIZE);
 }
 
 #if !USE_MEMALIGN
-static void *GetPages(unsigned NumPages) {
+void *GetPages(unsigned NumPages) {
 #if defined(i386) || defined(__i386__) || defined(__x86__)
   /* Linux and *BSD tend to have these flags named differently. */
 #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
@@ -64,14 +71,21 @@
 #endif
 
 
-/// AllocatePage - This function returns a chunk of memory with size and
-/// alignment specified by PageSize.
+///
+/// Function: AllocatePage ()
+///
+/// Description:
+/// This function returns a chunk of memory with size and alignment specified
+/// by PageSize.
 void *AllocatePage() {
 #if USE_MEMALIGN
   void *Addr;
   posix_memalign(&Addr, PageSize, PageSize);
   return Addr;
 #else
+  //
+  // Try to allocate a page that has already been created.
+  //
   if (FreePages && !FreePages->empty()) {
     void *Result = FreePages->back();
     FreePages->pop_back();


Index: poolalloc/runtime/FreeListAllocator/PageManager.h
diff -u poolalloc/runtime/FreeListAllocator/PageManager.h:1.1 poolalloc/runtime/FreeListAllocator/PageManager.h:1.2
--- poolalloc/runtime/FreeListAllocator/PageManager.h:1.1	Mon Nov 10 16:31:11 2003
+++ poolalloc/runtime/FreeListAllocator/PageManager.h	Tue Nov 11 14:13:42 2003
@@ -34,4 +34,8 @@
 /// future allocation.
 void FreePage(void *Page);
 
+/// GetPages - Just allocate the specified pages on a page boundary.  This is
+///            a hack for large arrays.
+void * GetPages (unsigned NumPages);
+
 #endif


Index: poolalloc/runtime/FreeListAllocator/PoolAllocator.cpp
diff -u poolalloc/runtime/FreeListAllocator/PoolAllocator.cpp:1.9 poolalloc/runtime/FreeListAllocator/PoolAllocator.cpp:1.10
--- poolalloc/runtime/FreeListAllocator/PoolAllocator.cpp:1.9	Tue Nov 11 10:12:14 2003
+++ poolalloc/runtime/FreeListAllocator/PoolAllocator.cpp	Tue Nov 11 14:13:42 2003
@@ -36,54 +36,38 @@
 struct SlabHeader *
 createSlab (unsigned int NodeSize, unsigned int NodesPerSlab = 0)
 {
-  // Maximum number of nodes per slab
-  unsigned int MaxNodesPerSlab;
+  // Maximum number of nodes per page
+  unsigned int MaxNodesPerPage;
 
   // Pointer to the new Slab
   struct SlabHeader * NewSlab;
 
-  // Pointers and index for initializing memory
-  NodePointer p;
-  unsigned int index;
-
   //
-  // Determine how many nodes should exist within a slab.
+  // Determine how many nodes can exist within a regular slab.
   //
-  MaxNodesPerSlab = (PageSize - sizeof (struct SlabHeader)) / (sizeof (unsigned char *) + NodeSize);
-  assert ((MaxNodesPerSlab >= NodesPerSlab) && "Too many nodes requested");
+  MaxNodesPerPage = (PageSize - sizeof (struct SlabHeader)) / (sizeof (NodePointer) + NodeSize);
 
   //
-  // Regardless of what the caller asked for, allocate the maximum number of
-  // nodes available in a page.
-  //
-  // This is the default behavior for regular nodes, and for arrays, we want to
-  // round up to the nearest slab so that we don't waste space when we use the
-  // array memory.
-  //
-  // FIXME:
-  //  Specifying the number of nodes that you want will come into play once
-  //  we allow pool allocations to span multiple memory pages.
-  //
-  NodesPerSlab = MaxNodesPerSlab;
-
+  // Allocate the memory for the slab and initialize its contents.
   //
-  // Determine the size of the slab.
-  //
-  int slab_size = ((sizeof (unsigned char *) + NodeSize) * NodesPerSlab) +
-                   sizeof (struct SlabHeader);
-
-  assert (slab_size <= PageSize);
+  if (NodesPerSlab > MaxNodesPerPage)
+  {
+    NewSlab = (struct SlabHeader *) GetPages ((NodeSize * NodesPerSlab / PageSize) + 1);
+    assert (NewSlab != NULL);
+    NewSlab->IsArray = 1;
+  }
+  else
+  {
+    NewSlab = (struct SlabHeader *) AllocatePage ();
+    assert (NewSlab != NULL);
+    NewSlab->IsArray = 0;
 
-  //
-  // Allocate a piece of memory for the new slab.
-  //
-  NewSlab = (struct SlabHeader *) AllocatePage ();
-  assert (NewSlab != NULL);
+    //
+    // Bump the number of nodes in the slab up to the maximum.
+    //
+    NodesPerSlab = MaxNodesPerPage;
+  }
 
-  //
-  // Initialize the contents of the slab.
-  //
-  NewSlab->IsArray = 0;
   NewSlab->NodesPerSlab = NodesPerSlab;
   NewSlab->NextFreeData = NewSlab->LiveNodes = 0;
   NewSlab->Next = NULL;
@@ -272,9 +256,6 @@
 // Inputs:
 //  Pool - The pool from which to allocate memory.
 //  ArraySize - The size of the array in number of elements (not bytes).
-//
-// FIXME:
-//  This algorithm is not very space efficient.  This needs to be fixed.
 //
 void *
 poolallocarray(PoolTy* Pool, unsigned ArraySize)





More information about the llvm-commits mailing list