Shared allocation of bulk data
authorPhil Miller <mille121@illinois.edu>
Tue, 2 Dec 2008 21:40:19 +0000 (21:40 +0000)
committerPhil Miller <mille121@illinois.edu>
Tue, 2 Dec 2008 21:40:19 +0000 (21:40 +0000)
src/langs/bluegene/shared-alloc.h [new file with mode: 0644]

diff --git a/src/langs/bluegene/shared-alloc.h b/src/langs/bluegene/shared-alloc.h
new file mode 100644 (file)
index 0000000..51001d4
--- /dev/null
@@ -0,0 +1,49 @@
+#include <vector>
+#include <utility>
+#include <stdlib.h>
+
+namespace SharedAlloc {
+
+  typedef std::pair<void*, int> Alloc;
+
+  std::vector<Alloc> allocs;
+
+  void *shalloc(int i, size_t s)
+  {
+    // Make space to record at least i allocations
+    if(allocs.size() <= i)
+      allocs.resize(i+1, Alloc(0,0));
+
+    // Ensure allocation i is initialized
+    if(allocs[i].first == 0)
+      allocs[i].first = malloc(s);
+
+    // Increment its reference count
+    allocs[i].second++;
+
+    // Return the address
+    return allocs[i].first;
+  }
+
+  void shfree(int i, void *p)
+  {
+    // Check that pointer matches
+    //CkAssert(p == allocs[i].first);
+           
+    // Decrement refcount
+    allocs[i].second--;
+
+    // Free if 0
+    if (allocs[i].second == 0)
+      {
+       free(allocs[i].first);
+       allocs[i].first = 0;
+      }
+  }
+
+
+
+};
+
+using SharedAlloc::shalloc;
+using SharedAlloc::shfree;