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

Chris Lattner lattner at cs.uiuc.edu
Fri Nov 14 14:21:02 PST 2003


Changes in directory poolalloc/runtime/PoolAllocator:

PageManager.cpp updated: 1.6 -> 1.7
PageManager.h updated: 1.2 -> 1.3
PoolAllocatorBitMask.cpp updated: 1.31 -> 1.32

---
Log message:

Completely rewrite handling of singlearrays


---
Diffs of the changes:  (+50 -51)

Index: poolalloc/runtime/PoolAllocator/PageManager.cpp
diff -u poolalloc/runtime/PoolAllocator/PageManager.cpp:1.6 poolalloc/runtime/PoolAllocator/PageManager.cpp:1.7
--- poolalloc/runtime/PoolAllocator/PageManager.cpp:1.6	Wed Nov 12 16:46:28 2003
+++ poolalloc/runtime/PoolAllocator/PageManager.cpp	Fri Nov 14 14:20:01 2003
@@ -27,11 +27,6 @@
 
 unsigned PageSize = 0;
 
-// Explicitly use the malloc allocator here, to avoid depending on the C++
-// runtime library.
-typedef std::vector<void*, llvm::MallocAllocator<void*> > FreePagesListType;
-static FreePagesListType *FreePages = 0;
-
 void InitializePageManager() {
   if (!PageSize) PageSize = sysconf(_SC_PAGESIZE);
 }
@@ -63,6 +58,21 @@
 }
 #endif
 
+// Explicitly use the malloc allocator here, to avoid depending on the C++
+// runtime library.
+typedef std::vector<void*, llvm::MallocAllocator<void*> > FreePagesListType;
+
+static FreePagesListType &getFreePageList() {
+  static FreePagesListType *FreePages = 0;
+
+  if (!FreePages) {
+    // Avoid using operator new!
+    FreePages = (FreePagesListType*)malloc(sizeof(FreePagesListType));
+    // Use placement new now.
+    new (FreePages) std::vector<void*, llvm::MallocAllocator<void*> >();
+  }
+  return *FreePages;
+}
 
 /// AllocatePage - This function returns a chunk of memory with size and
 /// alignment specified by PageSize.
@@ -72,9 +82,12 @@
   posix_memalign(&Addr, PageSize, PageSize);
   return Addr;
 #else
-  if (FreePages && !FreePages->empty()) {
-    void *Result = FreePages->back();
-    FreePages->pop_back();
+
+  FreePagesListType &FPL = getFreePageList();
+
+  if (!FPL.empty()) {
+    void *Result = FPL.back();
+    FPL.pop_back();
     return Result;
   }
 
@@ -82,18 +95,16 @@
   unsigned NumToAllocate = 8;
   char *Ptr = (char*)GetPages(NumToAllocate);
 
-  if (!FreePages) {
-    // Avoid using operator new!
-    FreePages = (FreePagesListType*)malloc(sizeof(FreePagesListType));
-    // Use placement new now.
-    new (FreePages) std::vector<void*, llvm::MallocAllocator<void*> >();
-  }
   for (unsigned i = 1; i != NumToAllocate; ++i)
-    FreePages->push_back(Ptr+i*PageSize);
+    FPL.push_back(Ptr+i*PageSize);
   return Ptr;
 #endif
 }
 
+void *AllocateNPages(unsigned Num) {
+  if (Num <= 1) return AllocatePage();
+  return GetPages(Num);
+}
 
 /// FreePage - This function returns the specified page to the pagemanager for
 /// future allocation.
@@ -101,8 +112,8 @@
 #if USE_MEMALIGN
   free(Page);
 #else
-  assert(FreePages && "No pages allocated!");
-  FreePages->push_back(Page);
+  FreePagesListType &FPL = getFreePageList();
+  FPL.push_back(Page);
   //munmap(Page, 1);
 #endif
 }


Index: poolalloc/runtime/PoolAllocator/PageManager.h
diff -u poolalloc/runtime/PoolAllocator/PageManager.h:1.2 poolalloc/runtime/PoolAllocator/PageManager.h:1.3
--- poolalloc/runtime/PoolAllocator/PageManager.h:1.2	Fri Nov  7 11:21:24 2003
+++ poolalloc/runtime/PoolAllocator/PageManager.h	Fri Nov 14 14:20:01 2003
@@ -30,6 +30,9 @@
 /// alignment specified by getPageSize().
 void *AllocatePage();
 
+/// AllocateNPages - 
+void *AllocateNPages(unsigned Num);
+
 /// FreePage - This function returns the specified page to the pagemanager for
 /// future allocation.
 void FreePage(void *Page);


Index: poolalloc/runtime/PoolAllocator/PoolAllocatorBitMask.cpp
diff -u poolalloc/runtime/PoolAllocator/PoolAllocatorBitMask.cpp:1.31 poolalloc/runtime/PoolAllocator/PoolAllocatorBitMask.cpp:1.32
--- poolalloc/runtime/PoolAllocator/PoolAllocatorBitMask.cpp:1.31	Fri Nov 14 12:31:08 2003
+++ poolalloc/runtime/PoolAllocator/PoolAllocatorBitMask.cpp	Fri Nov 14 14:20:01 2003
@@ -187,26 +187,26 @@
 }
 
 void *PoolSlab::createSingleArray(PoolTy *Pool, unsigned NumNodes) {
-  if (0) {
-    printf("PoolSlab::createSingleArray has not been tested and is probably broken!");
-    abort();
-  }
+  // FIXME: This wastes memory by allocating space for the NodeFlagsVector
   unsigned NodesPerSlab = getSlabSize(Pool);
   assert(NumNodes > NodesPerSlab && "No need to create a single array!");
-  PoolSlab *PS = (PoolSlab*)malloc(sizeof(PoolSlab) + 4*((NodesPerSlab+15)/16) +
-                                   Pool->NodeSize*NumNodes);
+
+  unsigned NumPages = (NumNodes+NodesPerSlab-1)/NodesPerSlab;
+  PoolSlab *PS = (PoolSlab*)AllocateNPages(NumPages);
   assert(PS && "poolalloc: Could not allocate memory!");
 
-  PS->NumNodesInSlab = NodesPerSlab;  // FIXME: Calculate right.
-  PS->isSingleArray = 1;  // Not a single array!
-  PS->markNodeAllocated(0);
+  PS->addToList((PoolSlab**)&Pool->Ptr2);
 
-  // Add the slab to the list...
-  PS->addToList((PoolSlab**)&Pool->Ptr1);
+  PS->isSingleArray = 1;  // Not a single array!
+  *(unsigned*)&PS->FirstUnused = NumPages;
   return PS->getElementAddress(0, 0);
 }
 
 void PoolSlab::destroy() {
+  if (isSingleArray)
+    for (unsigned NumPages = *(unsigned*)&FirstUnused; NumPages != 1;--NumPages)
+      FreePage((char*)this + (NumPages-1)*PageSize);
+
   FreePage(this);
 }
 
@@ -345,18 +345,11 @@
 void PoolSlab::freeElement(unsigned short ElementIdx) {
   assert(isNodeAllocated(ElementIdx) &&
          "poolfree: Attempt to free node that is already freed\n");
+  assert(!isSingleArray && "Cannot free an element from a single array!");
 
   // Mark this element as being free!
   markNodeFree(ElementIdx);
 
-  // If this slab is a SingleArray, there is nothing else to do.
-  if (isSingleArray) {
-    FirstUnused = UsedEnd = 0;               // This slab is now empty
-    assert(ElementIdx == 0 &&
-           "poolfree: Attempt to free middle of allocated array\n");
-    return;
-  }
-
   // If this slab is not a SingleArray
   assert(isStartOfAllocation(ElementIdx) &&
          "poolfree: Attempt to free middle of allocated array\n");
@@ -675,6 +668,13 @@
     // of the pool.
     assert((PageSize & PageSize-1) == 0 && "Page size is not a power of 2??");
     PS = (PoolSlab*)((long)Node & ~(PageSize-1));
+
+    if (PS->isSingleArray) {
+      PS->unlinkFromList();
+      PS->destroy();
+      return;
+    }
+
     Idx = PS->containsElement(Node, Pool->NodeSize);
     assert((int)Idx != -1 && "Node not contained in slab??");
   }
@@ -705,21 +705,6 @@
   // is already an empty slab at the head of the list.
   //
   if (PS->isEmpty()) {
-    if (PS->isSingleArray)
-      if (Pool->FreeablePool) {
-        // If it is a SingleArray, just free it
-        PS->unlinkFromList();
-        PS->destroy();
-        return;
-      } else {
-        // If this is a non-freeable pool, we might as well use the memory
-        // allocated for normal node allocations.
-        PS->isSingleArray = 0;
-      }
-
-    // No more singlearray objects exist at this point.
-    assert(!PS->isSingleArray);
-
     PS->unlinkFromList();   // Unlink from the list of slabs...
     
     // If we can free this pool, check to see if there are any empty slabs at





More information about the llvm-commits mailing list