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

John Criswell criswell at cs.uiuc.edu
Mon Nov 10 16:32:01 PST 2003


Changes in directory poolalloc/runtime/FreeListAllocator:

PageManager.cpp added (r1.1)
PageManager.h added (r1.1)
PoolAllocator.cpp updated: 1.2 -> 1.3

---
Log message:

Initial checkin of PageManager code.
Created a simplistic and non-working version of pool alloc'ed array.



---
Diffs of the changes:  (+178 -5)

Index: poolalloc/runtime/FreeListAllocator/PageManager.cpp
diff -c /dev/null poolalloc/runtime/FreeListAllocator/PageManager.cpp:1.1
*** /dev/null	Mon Nov 10 16:31:21 2003
--- poolalloc/runtime/FreeListAllocator/PageManager.cpp	Mon Nov 10 16:31:11 2003
***************
*** 0 ****
--- 1,108 ----
+ //===- PageManager.cpp - Implementation of the page allocator -------------===//
+ // 
+ //                       The LLVM Compiler Infrastructure
+ //
+ // This file was developed by the LLVM research group and is distributed under
+ // the University of Illinois Open Source License. See LICENSE.TXT for details.
+ // 
+ //===----------------------------------------------------------------------===//
+ //
+ // This file implements the PageManager.h interface.
+ //
+ //===----------------------------------------------------------------------===//
+ 
+ #include "PageManager.h"
+ #ifndef _POSIX_MAPPED_FILES
+ #define _POSIX_MAPPED_FILES
+ #endif
+ #include "Support/MallocAllocator.h"
+ #include "Config/unistd.h"
+ #include "Config/sys/mman.h"
+ #include <cassert>
+ #include <vector>
+ 
+ // Define this if we want to use memalign instead of mmap to get pages.
+ // Empirically, this slows down the pool allocator a LOT.
+ #define USE_MEMALIGN 0
+ 
+ unsigned PageSize = 0;
+ 
+ // Explicitly use the malloc allocator here, to avoid depending on the C++
+ // runtime library.
+ typedef std::vector<void*, MallocAllocator<void*> > FreePagesListType;
+ static FreePagesListType *FreePages = 0;
+ 
+ void InitializePageManager() {
+   if (!PageSize) PageSize = sysconf(_SC_PAGESIZE);
+ }
+ 
+ #if !USE_MEMALIGN
+ static 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)
+ # define MAP_ANONYMOUS MAP_ANON
+ #endif /* defined(MAP_ANON) && !defined(MAP_ANONYMOUS) */
+ #elif defined(sparc) || defined(__sparc__) || defined(__sparcv9)
+   /* nothing */
+ #else
+   std::cerr << "This architecture is not supported by the pool allocator!\n";
+   abort();
+ #endif
+ 
+ #if defined(__linux__)
+ #define fd 0
+ #else
+ #define fd -1
+ #endif
+ 
+   void *pa = mmap(0, NumPages*PageSize, PROT_READ|PROT_WRITE|PROT_EXEC,
+                   MAP_PRIVATE|MAP_ANONYMOUS, fd, 0);
+   assert(pa != MAP_FAILED && "MMAP FAILED!");
+   return pa;
+ }
+ #endif
+ 
+ 
+ /// AllocatePage - 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
+   if (FreePages && !FreePages->empty()) {
+     void *Result = FreePages->back();
+     FreePages->pop_back();
+     return Result;
+   }
+ 
+   // Allocate several pages, and put the extras on the freelist...
+   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*, MallocAllocator<void*> >();
+   }
+   for (unsigned i = 1; i != NumToAllocate; ++i)
+     FreePages->push_back(Ptr+i*PageSize);
+   return Ptr;
+ #endif
+ }
+ 
+ 
+ /// FreePage - This function returns the specified page to the pagemanager for
+ /// future allocation.
+ void FreePage(void *Page) {
+ #if USE_MEMALIGN
+   free(Page);
+ #else
+   assert(FreePages && "No pages allocated!");
+   FreePages->push_back(Page);
+   //munmap(Page, 1);
+ #endif
+ }


Index: poolalloc/runtime/FreeListAllocator/PageManager.h
diff -c /dev/null poolalloc/runtime/FreeListAllocator/PageManager.h:1.1
*** /dev/null	Mon Nov 10 16:31:21 2003
--- poolalloc/runtime/FreeListAllocator/PageManager.h	Mon Nov 10 16:31:11 2003
***************
*** 0 ****
--- 1,37 ----
+ //===- PageManager.h - Allocates memory on page boundaries ------*- C++ -*-===//
+ // 
+ //                     The LLVM Compiler Infrastructure
+ //
+ // This file was developed by the LLVM research group and is distributed under
+ // the University of Illinois Open Source License. See LICENSE.TXT for details.
+ // 
+ //===----------------------------------------------------------------------===//
+ //
+ // This file defines the interface used by the pool allocator to allocate memory
+ // on large alignment boundaries.
+ //
+ //===----------------------------------------------------------------------===//
+ 
+ #ifndef PAGEMANAGER_H
+ #define PAGEMANAGER_H
+ 
+ /// InitializePageManager - This function must be called before any other page
+ /// manager accesses are performed.  It may be called multiple times.
+ /// 
+ void InitializePageManager();
+ 
+ /// PageSize - Contains the size of the unit of memory allocated by
+ /// AllocatePage.  This is a value that is typically several kilobytes in size,
+ /// and is guaranteed to be a power of two.
+ ///
+ extern unsigned PageSize;
+ 
+ /// AllocatePage - This function returns a chunk of memory with size and
+ /// alignment specified by getPageSize().
+ void *AllocatePage();
+ 
+ /// FreePage - This function returns the specified page to the pagemanager for
+ /// future allocation.
+ void FreePage(void *Page);
+ 
+ #endif


Index: poolalloc/runtime/FreeListAllocator/PoolAllocator.cpp
diff -u poolalloc/runtime/FreeListAllocator/PoolAllocator.cpp:1.2 poolalloc/runtime/FreeListAllocator/PoolAllocator.cpp:1.3
--- poolalloc/runtime/FreeListAllocator/PoolAllocator.cpp:1.2	Mon Nov 10 15:57:48 2003
+++ poolalloc/runtime/FreeListAllocator/PoolAllocator.cpp	Mon Nov 10 16:31:11 2003
@@ -56,14 +56,11 @@
 //  Allocate memory for a new slab and initialize the slab.
 //
 struct SlabHeader *
-createSlab (unsigned int NodeSize)
+createSlab (unsigned int NodeSize, unsigned int NodesPerSlab = 128)
 {
   // Pointer to the new Slab
   struct SlabHeader * NewSlab;
 
-  // The number of elements in the slab
-  const unsigned int NodesPerSlab = 128;
-
   // Pointers and index for initializing memory
   NodePointer p;
 
@@ -230,12 +227,43 @@
   return (MemoryBlock.header += sizeof (unsigned char *));
 }
 
+//
+// Function: poolallocarray ()
+//
+// Description:
+//  Allocate an array of contiguous nodes.
+//
+// FIXME:
+//  This algorithm is not very space efficient.  This needs to be fixed.
+//
 void *
 poolallocarray(PoolTy* Pool, unsigned Size)
 {
   assert(Pool && "Null pool pointer passed into poolallocarray!\n");
-  assert (0 && "Not implemented yet")
+  assert (0 && "I don't work yet\n");
+
+#if 0
+  //
+  // Create a new slab and add it to the list.
+  //
+  struct SlabHeader * NewSlab = createSlab (Pool->NodeSize, Size);
+  if (Pool->Slabs == NULL)
+  {
+    Pool->Slabs = NewSlab;
+  }
+  else
+  {
+    NewSlab->Next = Pool->Slabs;
+    Pool->Slabs = NewSlab;
+  }
+
+  //
+  // Return the list of blocks to the caller.
+  //
+  return (&(Pool->Slabs->Data[0]));
+#else /* 0 */
   return NULL;
+#endif /* 0 */
 }
 
 void





More information about the llvm-commits mailing list