Merge "Move simple functions to on device lib." into jb-mr2-dev
diff --git a/driver/rsdCore.cpp b/driver/rsdCore.cpp
index 0f637b6..7b41a47 100644
--- a/driver/rsdCore.cpp
+++ b/driver/rsdCore.cpp
@@ -55,6 +55,8 @@
     Shutdown,
     NULL,
     SetPriority,
+    rsdAllocRuntimeMem,
+    rsdFreeRuntimeMem,
     {
         rsdScriptInit,
         rsdInitIntrinsic,
@@ -202,3 +204,11 @@
     rsc->mHal.drv = NULL;
 }
 
+void* rsdAllocRuntimeMem(size_t size, uint32_t flags) {
+    void* buffer = calloc(size, sizeof(char));
+    return buffer;
+}
+
+void rsdFreeRuntimeMem(void* ptr) {
+    free(ptr);
+}
diff --git a/driver/rsdCore.h b/driver/rsdCore.h
index 8b6ebf7..d873e3e 100644
--- a/driver/rsdCore.h
+++ b/driver/rsdCore.h
@@ -52,6 +52,8 @@
 } RsdHal;
 
 void rsdLaunchThreads(android::renderscript::Context *rsc, WorkerCallback_t cbk, void *data);
+void* rsdAllocRuntimeMem(size_t size, uint32_t flags);
+void rsdFreeRuntimeMem(void* ptr);
 
 #endif
 
diff --git a/rsAllocation.cpp b/rsAllocation.cpp
index 9833ab3..7c7bc40 100644
--- a/rsAllocation.cpp
+++ b/rsAllocation.cpp
@@ -39,9 +39,24 @@
     updateCache();
 }
 
+void Allocation::operator delete(void* ptr) {
+    if (ptr) {
+        Allocation *a = (Allocation*) ptr;
+        a->getContext()->mHal.funcs.freeRuntimeMem(ptr);
+    }
+}
+
 Allocation * Allocation::createAllocation(Context *rsc, const Type *type, uint32_t usages,
                               RsAllocationMipmapControl mc, void * ptr) {
-    Allocation *a = new Allocation(rsc, type, usages, mc, ptr);
+    // Allocation objects must use allocator specified by the driver
+    void* allocMem = rsc->mHal.funcs.allocRuntimeMem(sizeof(Allocation), 0);
+
+    if (!allocMem) {
+        rsc->setError(RS_ERROR_FATAL_DRIVER, "Couldn't allocate memory for Allocation");
+        return NULL;
+    }
+
+    Allocation *a = new (allocMem) Allocation(rsc, type, usages, mc, ptr);
 
     if (!rsc->mHal.funcs.allocation.init(rsc, a, type->getElement()->getHasReferences())) {
         rsc->setError(RS_ERROR_FATAL_DRIVER, "Allocation::Allocation, alloc failure");
diff --git a/rsAllocation.h b/rsAllocation.h
index 2938f65..087d1cd 100644
--- a/rsAllocation.h
+++ b/rsAllocation.h
@@ -80,6 +80,8 @@
     };
     Hal mHal;
 
+    void operator delete(void* ptr);
+
     static Allocation * createAllocation(Context *rsc, const Type *, uint32_t usages,
                                          RsAllocationMipmapControl mc = RS_ALLOCATION_MIPMAP_NONE,
                                          void *ptr = 0);
diff --git a/rs_hal.h b/rs_hal.h
index 4e246c4..6a71930 100644
--- a/rs_hal.h
+++ b/rs_hal.h
@@ -87,7 +87,8 @@
     void (*getVersion)(unsigned int *major, unsigned int *minor);
     void (*setPriority)(const Context *, int32_t priority);
 
-
+    void* (*allocRuntimeMem)(size_t size, uint32_t flags);
+    void (*freeRuntimeMem)(void* ptr);
 
     struct {
         bool (*init)(const Context *rsc, ScriptC *s,