[llvm-commits] [poolalloc] r57494 - in /poolalloc/trunk/include/poolalloc_runtime: ./ PoolAllocator.h Support/ Support/SplayTree.h test.ex

Andrew Lenharth alenhar2 at cs.uiuc.edu
Tue Oct 14 09:11:33 PDT 2008


Author: alenhar2
Date: Tue Oct 14 11:11:31 2008
New Revision: 57494

URL: http://llvm.org/viewvc/llvm-project?rev=57494&view=rev
Log:
some common infastructure for safecode runtimes

Added:
    poolalloc/trunk/include/poolalloc_runtime/
    poolalloc/trunk/include/poolalloc_runtime/PoolAllocator.h
    poolalloc/trunk/include/poolalloc_runtime/Support/
    poolalloc/trunk/include/poolalloc_runtime/Support/SplayTree.h
    poolalloc/trunk/include/poolalloc_runtime/test.ex

Added: poolalloc/trunk/include/poolalloc_runtime/PoolAllocator.h
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/include/poolalloc_runtime/PoolAllocator.h?rev=57494&view=auto

==============================================================================
--- poolalloc/trunk/include/poolalloc_runtime/PoolAllocator.h (added)
+++ poolalloc/trunk/include/poolalloc_runtime/PoolAllocator.h Tue Oct 14 11:11:31 2008
@@ -0,0 +1,75 @@
+#include "poolalloc_runtime/Support/SplayTree.h"
+#include <cstdlib>
+
+template<class PageManager, template <typename T> class SlabManager >
+class PoolAllocator : SlabManager<PageManager> {
+  using SlabManager<PageManager>::slab_alloc;
+  using SlabManager<PageManager>::slab_free;
+  using SlabManager<PageManager>::slab_valid;
+  using SlabManager<PageManager>::slab_getbounds;
+ public:
+  PoolAllocator(unsigned objsize, unsigned Alignment)
+    : SlabManager<PageManager>(objsize, Alignment)
+    {}
+    
+    
+    //In-place new operator
+    static void* operator new( std::size_t s, void* p ) throw() {
+      return p;
+    }
+    
+    //Allocate an object of size objsize
+    void* alloc() {
+      return slab_alloc(1);
+    }
+    
+    //Allocate an array with num objects of size objsize
+    void* alloc_array(unsigned num) {
+      return slab_alloc(num);
+    }
+    
+    //Free allocated object
+  void dealloc(void* obj) {
+    slab_free(obj);
+  }
+
+  //Tests if obj is in an allocated object
+  bool isAllocated(void* obj) {
+    return slab_valid(obj);
+  }
+
+  // Returns the start and end of an object, return value is true if found
+  bool getBounds(void* obj, void*& start, void*& end) {
+    return slab_getbounds(obj, start, end);
+  }
+
+};
+
+class mmapPageManager {
+  enum { pageSize = 4096 };
+};
+
+template<class PageManager>
+class MallocSlabManager {
+  SplayRangeSet objs;
+  unsigned objsize;
+ protected:
+  MallocSlabManager(unsigned Osize, unsigned Alignment) : objsize(Osize) {}
+
+  void* slab_alloc(unsigned num) {
+    void* x = malloc(num*objsize);
+    objs.insert(x, (char*)x + num*objsize - 1);
+    return x;
+  }
+  void slab_free(void* obj) {
+    objs.remove(obj);
+    free(obj);
+  }
+  bool slab_valid(void* obj) {
+    return objs.find(obj);
+  }
+  void* slab_getbounds(void* obj, void*& start, void*& end) {
+    return objs.find(obj, start, end);
+  } 
+
+};

Added: poolalloc/trunk/include/poolalloc_runtime/Support/SplayTree.h
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/include/poolalloc_runtime/Support/SplayTree.h?rev=57494&view=auto

==============================================================================
--- poolalloc/trunk/include/poolalloc_runtime/Support/SplayTree.h (added)
+++ poolalloc/trunk/include/poolalloc_runtime/Support/SplayTree.h Tue Oct 14 11:11:31 2008
@@ -0,0 +1,155 @@
+
+class SplayRangeSet {
+  struct tree_node {
+    tree_node* left;
+    tree_node* right;
+    void* start;
+    void* end;
+  };
+
+  tree_node* Tree;
+
+  tree_node* rotate_right(tree_node* p) {
+    tree_node* x = p->left;
+    p->left = x->right;
+    x->right = p;
+    return x;
+  }
+
+  tree_node* rotate_left(tree_node* p) {
+    tree_node* x = p->right;
+    p->right = x->left;
+    x->left = p;
+    return x;
+  }
+
+  bool key_lt(void* _key, tree_node* _t) { return _key < _t->start; };
+
+  bool key_gt(void* _key, tree_node* _t) { return _key > _t->end; };
+
+  /* This function by D. Sleator <sleator at cs.cmu.edu> */
+  tree_node* splay (tree_node * t, void* key) {
+    tree_node N, *l, *r, *y;
+    if (t == 0) return t;
+    N.left = N.right = 0;
+    l = r = &N;
+    
+    while(1) {
+      if (key_lt(key, t)) {
+        if (t->left == 0) break;
+        if (key_lt(key, t->left)) {
+          y = t->left;                           /* rotate right */
+          t->left = y->right;
+          y->right = t;
+          t = y;
+          if (t->left == 0) break;
+        }
+        r->left = t;                               /* link right */
+        r = t;
+        t = t->left;
+      } else if (key_gt(key, t)) {
+        if (t->right == 0) break;
+        if (key_gt(key, t->right)) {
+          y = t->right;                          /* rotate left */
+          t->right = y->left;
+          y->left = t;
+          t = y;
+          if (t->right == 0) break;
+        }
+        l->right = t;                              /* link left */
+        l = t;
+        t = t->right;
+      } else {
+        break;
+      }
+    }
+    l->right = t->left;                                /* assemble */
+    r->left = t->right;
+    t->left = N.right;
+    t->right = N.left;
+    return t;
+  }
+
+  unsigned count_internal(tree_node* t) {
+    if (t)
+      return 1 + count_internal(t->left) + count_internal(t->right);
+    return 0;
+  }
+
+ public:
+
+  SplayRangeSet() : Tree(0) {}
+  ~SplayRangeSet() { clear(); }
+  
+  bool insert(void* start, void* end) {
+    Tree = splay(Tree, start);
+    //If the key is already in, fail the insert
+    if (Tree && !key_lt(start, Tree) && !key_gt(start, Tree))
+      return false;
+    
+    tree_node* n = new tree_node();
+    n->start = start;
+    n->end = end;
+    n->right = n->left = 0;
+    if (Tree) {
+      if (key_lt(start, Tree)) {
+        n->left = Tree->left;
+        n->right = Tree;
+        Tree->left = 0;
+      } else {
+        n->right = Tree->right;
+        n->left = Tree;
+        Tree->right = 0;
+      }
+    }
+    Tree = n;
+  }
+
+  bool remove(void* key) {
+    if (!Tree) return false;
+    Tree = splay(Tree, key);
+    if (!key_lt(key, Tree) && !key_gt(key, Tree)) {
+      tree_node* x = 0;
+      if (!Tree->left)
+        x = Tree->right;
+      else {
+        x = splay(Tree->left, key);
+        x->right = Tree->right;
+      }
+      tree_node* y = Tree;
+      Tree = x;
+      delete y;
+      return true;
+    }
+    return false; /* not there */
+  }
+
+  unsigned count() {
+    return count_internal(Tree);
+  }
+
+  void clear() {
+    while (Tree)
+      remove (Tree->start);
+  }
+
+  bool find(void* key, void*& start, void*& end) {
+    if (!Tree) return false;
+    Tree = splay(Tree, key);
+    if (!key_lt(key, Tree) && !key_gt(key, Tree)) {
+      start = Tree->start;
+      end = Tree->end;
+      return true;
+    }
+    return false;
+  }
+  bool find(void* key) {
+    if (!Tree) return false;
+    Tree = splay(Tree, key);
+    if (!key_lt(key, Tree) && !key_gt(key, Tree)) {
+      return true;
+    }
+    return false;
+  }
+
+};

Added: poolalloc/trunk/include/poolalloc_runtime/test.ex
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/include/poolalloc_runtime/test.ex?rev=57494&view=auto

==============================================================================
--- poolalloc/trunk/include/poolalloc_runtime/test.ex (added)
+++ poolalloc/trunk/include/poolalloc_runtime/test.ex Tue Oct 14 11:11:31 2008
@@ -0,0 +1,13 @@
+#include "PoolAllocator.h"
+#include <iostream>
+
+PoolAllocator<mmapPageManager, MallocSlabManager> a(10, 16);
+
+int main() {
+  void* x = a.alloc();
+  std::cerr << a.isAllocated(x) << " " << a.isAllocated((char*)x + 5) << " " << a.isAllocated((char*)x + 10) << "\n";
+  a.dealloc(x);
+  return 0;
+}
+
+  





More information about the llvm-commits mailing list