Make all references use smart pointers.

BUG=5972398

Change-Id: I7b8c2930ccf7d64623270cd3d0550aa6a852f2e5
diff --git a/cpp/Allocation.cpp b/cpp/Allocation.cpp
index e37d5de..d6f79b0 100644
--- a/cpp/Allocation.cpp
+++ b/cpp/Allocation.cpp
@@ -24,6 +24,8 @@
 #include "Type.h"
 #include "Allocation.h"
 
+using namespace android;
+using namespace renderscriptCpp;
 
 void * Allocation::getIDSafe() const {
     //if (mAdaptedAllocation != NULL) {
@@ -32,7 +34,7 @@
     return getID();
 }
 
-void Allocation::updateCacheInfo(const Type *t) {
+void Allocation::updateCacheInfo(sp<const Type> t) {
     mCurrentDimX = t->getX();
     mCurrentDimY = t->getY();
     mCurrentDimZ = t->getZ();
@@ -45,7 +47,9 @@
     }
 }
 
-Allocation::Allocation(void *id, RenderScript *rs, const Type *t, uint32_t usage) : BaseObj(id, rs) {
+Allocation::Allocation(void *id, RenderScript *rs, sp<const Type> t, uint32_t usage) :
+        BaseObj(id, rs) {
+
     if ((usage & ~(RS_ALLOCATION_USAGE_SCRIPT |
                    RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE |
                    RS_ALLOCATION_USAGE_GRAPHICS_VERTEX |
@@ -68,7 +72,7 @@
     mType = t;
     mUsage = usage;
 
-    if (t != NULL) {
+    if (t.get() != NULL) {
         updateCacheInfo(t);
     }
 }
@@ -127,12 +131,11 @@
 
     const void *typeID = rsaAllocationGetType(mRS->mContext, getID());
     if(typeID != NULL) {
-        const Type *old = mType;
-        Type *t = new Type((void *)typeID, mRS);
+        sp<const Type> old = mType;
+        sp<Type> t = new Type((void *)typeID, mRS);
         t->updateFromNative();
         updateCacheInfo(t);
         mType = t;
-        delete old;
     }
 }
 
@@ -221,7 +224,9 @@
     rsAllocationGenerateMipmaps(mRS->mContext, getID());
 }
 
-void Allocation::copy1DRangeFromUnchecked(uint32_t off, size_t count, const void *data, size_t dataLen) {
+void Allocation::copy1DRangeFromUnchecked(uint32_t off, size_t count, const void *data,
+        size_t dataLen) {
+
     if(count < 1) {
         ALOGE("Count must be >= 1.");
         return;
@@ -258,7 +263,9 @@
     copy1DRangeFromUnchecked(off, count, d, dataLen);
 }
 
-void Allocation::copy1DRangeFrom(uint32_t off, size_t count, const Allocation *data, uint32_t dataOff) {
+void Allocation::copy1DRangeFrom(uint32_t off, size_t count, const Allocation *data,
+        uint32_t dataOff) {
+
     rsAllocationCopy2DRange(mRS->mContext, getIDSafe(), off, 0,
                             mSelectedLOD, mSelectedFace,
                             count, 1, data->getIDSafe(), dataOff, 0,
@@ -371,7 +378,7 @@
 */
 
 
-Allocation *Allocation::createTyped(RenderScript *rs, const Type *type,
+android::sp<Allocation> Allocation::createTyped(RenderScript *rs, sp<const Type> type,
                         RsAllocationMipmapControl mips, uint32_t usage) {
     void *id = rsAllocationCreateTyped(rs->mContext, type->getID(), mips, usage, 0);
     if (id == 0) {
@@ -381,7 +388,7 @@
     return new Allocation(id, rs, type, usage);
 }
 
-Allocation *Allocation::createTyped(RenderScript *rs, const Type *type,
+android::sp<Allocation> Allocation::createTyped(RenderScript *rs, sp<const Type> type,
                                     RsAllocationMipmapControl mips, uint32_t usage, void *pointer) {
     void *id = rsAllocationCreateTyped(rs->mContext, type->getID(), mips, usage, (uint32_t)pointer);
     if (id == 0) {
@@ -390,16 +397,20 @@
     return new Allocation(id, rs, type, usage);
 }
 
-Allocation *Allocation::createTyped(RenderScript *rs, const Type *type, uint32_t usage) {
+android::sp<Allocation> Allocation::createTyped(RenderScript *rs, sp<const Type> type,
+        uint32_t usage) {
     return createTyped(rs, type, RS_ALLOCATION_MIPMAP_NONE, usage);
 }
 
-Allocation *Allocation::createSized(RenderScript *rs, const Element *e, size_t count, uint32_t usage) {
+android::sp<Allocation> Allocation::createSized(RenderScript *rs, sp<const Element> e,
+        size_t count, uint32_t usage) {
+
     Type::Builder b(rs, e);
     b.setX(count);
-    const Type *t = b.create();
+    sp<const Type> t = b.create();
 
-    void *id = rsAllocationCreateTyped(rs->mContext, t->getID(), RS_ALLOCATION_MIPMAP_NONE, usage, 0);
+    void *id = rsAllocationCreateTyped(rs->mContext, t->getID(),
+        RS_ALLOCATION_MIPMAP_NONE, usage, 0);
     if (id == 0) {
         ALOGE("Allocation creation failed.");
     }
diff --git a/cpp/Allocation.h b/cpp/Allocation.h
index c9e00a4..7a893f8 100644
--- a/cpp/Allocation.h
+++ b/cpp/Allocation.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008-2012 The Android Open Source Project
+ * Copyright (C) 2012 The Android Open Source Project
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -24,11 +24,14 @@
 #include "Type.h"
 #include "Element.h"
 
+namespace android {
+namespace renderscriptCpp {
+
 class Allocation : public BaseObj {
 protected:
-    const Type *mType;
+    android::sp<const Type> mType;
     uint32_t mUsage;
-    Allocation *mAdaptedAllocation;
+    android::sp<Allocation> mAdaptedAllocation;
 
     bool mConstrainedLOD;
     bool mConstrainedFace;
@@ -48,9 +51,9 @@
 
 
     void * getIDSafe() const;
-    void updateCacheInfo(const Type *t);
+    void updateCacheInfo(sp<const Type> t);
 
-    Allocation(void *id, RenderScript *rs, const Type *t, uint32_t usage);
+    Allocation(void *id, RenderScript *rs, sp<const Type> t, uint32_t usage);
 
     void validateIsInt32();
     void validateIsInt16();
@@ -63,7 +66,7 @@
     void validate2DRange(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h);
 
 public:
-    const Type * getType() {
+    android::sp<const Type> getType() {
         return mType;
     }
 
@@ -109,18 +112,21 @@
     void resize(int dimX);
     void resize(int dimX, int dimY);
 
-    static Allocation *createTyped(RenderScript *rs, const Type *type,
+    static sp<Allocation> createTyped(RenderScript *rs, sp<const Type> type,
                                    RsAllocationMipmapControl mips, uint32_t usage);
-    static Allocation *createTyped(RenderScript *rs, const Type *type,
+    static sp<Allocation> createTyped(RenderScript *rs, sp<const Type> type,
                                    RsAllocationMipmapControl mips, uint32_t usage, void * pointer);
 
-    static Allocation *createTyped(RenderScript *rs, const Type *type,
+    static sp<Allocation> createTyped(RenderScript *rs, sp<const Type> type,
                                    uint32_t usage = RS_ALLOCATION_USAGE_SCRIPT);
-    static Allocation *createSized(RenderScript *rs, const Element *e, size_t count,
+    static sp<Allocation> createSized(RenderScript *rs, sp<const Element> e, size_t count,
                                    uint32_t usage = RS_ALLOCATION_USAGE_SCRIPT);
     //SurfaceTexture *getSurfaceTexture();
     //void setSurfaceTexture(SurfaceTexture *sur);
 
 };
 
+}
+}
+
 #endif
diff --git a/cpp/BaseObj.cpp b/cpp/BaseObj.cpp
index 82e51e7..a370e8d 100644
--- a/cpp/BaseObj.cpp
+++ b/cpp/BaseObj.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008-2012 The Android Open Source Project
+ * Copyright (C) 2012 The Android Open Source Project
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -21,6 +21,9 @@
 #include "RenderScript.h"
 #include "BaseObj.h"
 
+using namespace android;
+using namespace renderscriptCpp;
+
 void * BaseObj::getID() const {
     if (mID == NULL) {
         ALOGE("Internal error: Object id 0.");
@@ -28,8 +31,8 @@
     return mID;
 }
 
-void * BaseObj::getObjID(const BaseObj *o) {
-    return o == NULL ? NULL : o->getID();
+void * BaseObj::getObjID(sp<const BaseObj> o) {
+    return o.get() == NULL ? NULL : o->getID();
 }
 
 
diff --git a/cpp/BaseObj.h b/cpp/BaseObj.h
index 79761b1..23ae85c 100644
--- a/cpp/BaseObj.h
+++ b/cpp/BaseObj.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008-2012 The Android Open Source Project
+ * Copyright (C) 2012 The Android Open Source Project
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -18,12 +18,17 @@
 #define __ANDROID_BASE_OBJ_H__
 
 
+#include "utils/RefBase.h"
 #include <pthread.h>
 #include <rs.h>
 
 #include "RenderScript.h"
 
-class BaseObj {
+namespace android {
+namespace renderscriptCpp {
+
+
+class BaseObj : public android::LightRefBase<BaseObj> {
 protected:
     friend class Element;
     friend class Type;
@@ -40,7 +45,7 @@
     BaseObj(void *id, RenderScript *rs);
     void checkValid();
 
-    static void * getObjID(const BaseObj *o);
+    static void * getObjID(sp<const BaseObj> o);
 
 public:
 
@@ -49,4 +54,6 @@
     virtual bool equals(const BaseObj *obj);
 };
 
+}
+}
 #endif
diff --git a/cpp/Element.cpp b/cpp/Element.cpp
index f318d40..2be4166 100644
--- a/cpp/Element.cpp
+++ b/cpp/Element.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008-2012 The Android Open Source Project
+ * Copyright (C) 2012 The Android Open Source Project
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -23,8 +23,10 @@
 #include "RenderScript.h"
 #include "Element.h"
 
+using namespace android;
+using namespace renderscriptCpp;
 
-const Element * Element::getSubElement(uint32_t index) {
+sp<const Element> Element::getSubElement(uint32_t index) {
     if (!mVisibleElementMap.size()) {
         mRS->throwError("Element contains no sub-elements");
     }
@@ -65,7 +67,7 @@
 }
 
 
-#define CREATE_USER(N, T) const Element * Element::N(RenderScript *rs) { \
+#define CREATE_USER(N, T) sp<const Element> Element::N(RenderScript *rs) { \
     return createUser(rs, RS_TYPE_##T); \
 }
 CREATE_USER(BOOLEAN, BOOLEAN);
@@ -93,7 +95,7 @@
 CREATE_USER(MATRIX_3X3, MATRIX_3X3);
 CREATE_USER(MATRIX_2X2, MATRIX_2X2);
 
-#define CREATE_PIXEL(N, T, K) const Element * Element::N(RenderScript *rs) { \
+#define CREATE_PIXEL(N, T, K) sp<const Element> Element::N(RenderScript *rs) { \
     return createPixel(rs, RS_TYPE_##T, RS_KIND_##K); \
 }
 CREATE_PIXEL(A_8, UNSIGNED_8, PIXEL_A);
@@ -102,13 +104,13 @@
 CREATE_PIXEL(RGBA_4444, UNSIGNED_4_4_4_4, PIXEL_RGBA);
 CREATE_PIXEL(RGBA_8888, UNSIGNED_8, PIXEL_RGBA);
 
-#define CREATE_VECTOR(N, T) const Element * Element::N##_2(RenderScript *rs) { \
+#define CREATE_VECTOR(N, T) sp<const Element> Element::N##_2(RenderScript *rs) { \
     return createVector(rs, RS_TYPE_##T, 2); \
 } \
-const Element * Element::N##_3(RenderScript *rs) { \
+sp<const Element> Element::N##_3(RenderScript *rs) { \
     return createVector(rs, RS_TYPE_##T, 3); \
 } \
-const Element * Element::N##_4(RenderScript *rs) { \
+sp<const Element> Element::N##_4(RenderScript *rs) { \
     return createVector(rs, RS_TYPE_##T, 4); \
 }
 CREATE_VECTOR(U8, UNSIGNED_8);
@@ -147,7 +149,7 @@
 }
 
 Element::Element(void *id, RenderScript *rs,
-                 android::Vector<const Element *> &elements,
+                 android::Vector<sp</*const*/ Element> > &elements,
                  android::Vector<android::String8> &elementNames,
                  android::Vector<uint32_t> &arraySizes) : BaseObj(id, rs) {
     mSizeBytes = 0;
@@ -293,12 +295,12 @@
     updateVisibleSubElements();
 }
 
-const Element * Element::createUser(RenderScript *rs, RsDataType dt) {
+sp<const Element> Element::createUser(RenderScript *rs, RsDataType dt) {
     void * id = rsElementCreate(rs->mContext, dt, RS_KIND_USER, false, 1);
     return new Element(id, rs, dt, RS_KIND_USER, false, 1);
 }
 
-const Element * Element::createVector(RenderScript *rs, RsDataType dt, uint32_t size) {
+sp<const Element> Element::createVector(RenderScript *rs, RsDataType dt, uint32_t size) {
     if (size < 2 || size > 4) {
         rs->throwError("Vector size out of range 2-4.");
     }
@@ -306,7 +308,7 @@
     return new Element(id, rs, dt, RS_KIND_USER, false, size);
 }
 
-const Element * Element::createPixel(RenderScript *rs, RsDataType dt, RsDataKind dk) {
+sp<const Element> Element::createPixel(RenderScript *rs, RsDataType dt, RsDataKind dk) {
     if (!(dk == RS_KIND_PIXEL_L ||
           dk == RS_KIND_PIXEL_A ||
           dk == RS_KIND_PIXEL_LA ||
@@ -357,9 +359,9 @@
     return new Element(id, rs, dt, dk, true, size);
 }
 
-bool Element::isCompatible(const Element *e) {
+bool Element::isCompatible(sp<const Element>e) {
     // Try strict BaseObj equality to start with.
-    if (this == e) {
+    if (this == e.get()) {
         return true;
     }
 
@@ -368,7 +370,7 @@
     // field must be non-null since we require name equivalence for
     // user-created Elements.
     return ((mSizeBytes == e->mSizeBytes) &&
-            (mType != NULL) &&
+            (mType != RS_TYPE_NONE) &&
             (mType == e->mType) &&
             (mVectorSize == e->mVectorSize));
 }
@@ -378,7 +380,7 @@
     mSkipPadding = false;
 }
 
-void Element::Builder::add(const Element *e, android::String8 &name, uint32_t arraySize) {
+void Element::Builder::add(sp</*const*/ Element>e, android::String8 &name, uint32_t arraySize) {
     // Skip padding fields after a vector 3 type.
     if (mSkipPadding) {
         const char *s1 = "#padding_";
@@ -403,26 +405,27 @@
     mArraySizes.add(arraySize);
 }
 
-const Element * Element::Builder::create() {
+sp<const Element> Element::Builder::create() {
     size_t fieldCount = mElements.size();
     const char ** nameArray = (const char **)calloc(fieldCount, sizeof(char *));
+    const Element ** elementArray = (const Element **)calloc(fieldCount, sizeof(Element *));
     size_t* sizeArray = (size_t*)calloc(fieldCount, sizeof(size_t));
 
     for (size_t ct = 0; ct < fieldCount; ct++) {
         nameArray[ct] = mElementNames[ct].string();
+        elementArray[ct] = mElements[ct].get();
         sizeArray[ct] = mElementNames[ct].length();
     }
 
     void *id = rsElementCreate2(mRS->mContext,
-                                (RsElement *)mElements.array(), fieldCount,
+                                (RsElement *)elementArray, fieldCount,
                                 nameArray, fieldCount * sizeof(size_t),  sizeArray,
                                 (const uint32_t *)mArraySizes.array(), fieldCount);
 
 
     free(nameArray);
     free(sizeArray);
-
-    Element *e = new Element(id, mRS, mElements, mElementNames, mArraySizes);
-    return e;
+    free(elementArray);
+    return new Element(id, mRS, mElements, mElementNames, mArraySizes);
 }
 
diff --git a/cpp/Element.h b/cpp/Element.h
index a579dc3..dea3570 100644
--- a/cpp/Element.h
+++ b/cpp/Element.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008-2012 The Android Open Source Project
+ * Copyright (C) 2012 The Android Open Source Project
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -21,6 +21,9 @@
 #include "RenderScript.h"
 #include "BaseObj.h"
 
+namespace android {
+namespace renderscriptCpp {
+
 class Element : public BaseObj {
 public:
     /**
@@ -44,7 +47,7 @@
     * @param index index of the sub-element to return
     * @return sub-element in this element at given index
     */
-    const Element * getSubElement(uint32_t index);
+    sp<const Element> getSubElement(uint32_t index);
 
     /**
     * @hide
@@ -88,71 +91,71 @@
     }
 
 
-    static const Element * BOOLEAN(RenderScript *rs);
-    static const Element * U8(RenderScript *rs);
-    static const Element * I8(RenderScript *rs);
-    static const Element * U16(RenderScript *rs);
-    static const Element * I16(RenderScript *rs);
-    static const Element * U32(RenderScript *rs);
-    static const Element * I32(RenderScript *rs);
-    static const Element * U64(RenderScript *rs);
-    static const Element * I64(RenderScript *rs);
-    static const Element * F32(RenderScript *rs);
-    static const Element * F64(RenderScript *rs);
-    static const Element * ELEMENT(RenderScript *rs);
-    static const Element * TYPE(RenderScript *rs);
-    static const Element * ALLOCATION(RenderScript *rs);
-    static const Element * SAMPLER(RenderScript *rs);
-    static const Element * SCRIPT(RenderScript *rs);
-    static const Element * MESH(RenderScript *rs);
-    static const Element * PROGRAM_FRAGMENT(RenderScript *rs);
-    static const Element * PROGRAM_VERTEX(RenderScript *rs);
-    static const Element * PROGRAM_RASTER(RenderScript *rs);
-    static const Element * PROGRAM_STORE(RenderScript *rs);
+    static sp<const Element> BOOLEAN(RenderScript *rs);
+    static sp<const Element> U8(RenderScript *rs);
+    static sp<const Element> I8(RenderScript *rs);
+    static sp<const Element> U16(RenderScript *rs);
+    static sp<const Element> I16(RenderScript *rs);
+    static sp<const Element> U32(RenderScript *rs);
+    static sp<const Element> I32(RenderScript *rs);
+    static sp<const Element> U64(RenderScript *rs);
+    static sp<const Element> I64(RenderScript *rs);
+    static sp<const Element> F32(RenderScript *rs);
+    static sp<const Element> F64(RenderScript *rs);
+    static sp<const Element> ELEMENT(RenderScript *rs);
+    static sp<const Element> TYPE(RenderScript *rs);
+    static sp<const Element> ALLOCATION(RenderScript *rs);
+    static sp<const Element> SAMPLER(RenderScript *rs);
+    static sp<const Element> SCRIPT(RenderScript *rs);
+    static sp<const Element> MESH(RenderScript *rs);
+    static sp<const Element> PROGRAM_FRAGMENT(RenderScript *rs);
+    static sp<const Element> PROGRAM_VERTEX(RenderScript *rs);
+    static sp<const Element> PROGRAM_RASTER(RenderScript *rs);
+    static sp<const Element> PROGRAM_STORE(RenderScript *rs);
 
-    static const Element * A_8(RenderScript *rs);
-    static const Element * RGB_565(RenderScript *rs);
-    static const Element * RGB_888(RenderScript *rs);
-    static const Element * RGBA_5551(RenderScript *rs);
-    static const Element * RGBA_4444(RenderScript *rs);
-    static const Element * RGBA_8888(RenderScript *rs);
+    static sp<const Element> A_8(RenderScript *rs);
+    static sp<const Element> RGB_565(RenderScript *rs);
+    static sp<const Element> RGB_888(RenderScript *rs);
+    static sp<const Element> RGBA_5551(RenderScript *rs);
+    static sp<const Element> RGBA_4444(RenderScript *rs);
+    static sp<const Element> RGBA_8888(RenderScript *rs);
 
-    static const Element * F32_2(RenderScript *rs);
-    static const Element * F32_3(RenderScript *rs);
-    static const Element * F32_4(RenderScript *rs);
-    static const Element * F64_2(RenderScript *rs);
-    static const Element * F64_3(RenderScript *rs);
-    static const Element * F64_4(RenderScript *rs);
-    static const Element * U8_2(RenderScript *rs);
-    static const Element * U8_3(RenderScript *rs);
-    static const Element * U8_4(RenderScript *rs);
-    static const Element * I8_2(RenderScript *rs);
-    static const Element * I8_3(RenderScript *rs);
-    static const Element * I8_4(RenderScript *rs);
-    static const Element * U16_2(RenderScript *rs);
-    static const Element * U16_3(RenderScript *rs);
-    static const Element * U16_4(RenderScript *rs);
-    static const Element * I16_2(RenderScript *rs);
-    static const Element * I16_3(RenderScript *rs);
-    static const Element * I16_4(RenderScript *rs);
-    static const Element * U32_2(RenderScript *rs);
-    static const Element * U32_3(RenderScript *rs);
-    static const Element * U32_4(RenderScript *rs);
-    static const Element * I32_2(RenderScript *rs);
-    static const Element * I32_3(RenderScript *rs);
-    static const Element * I32_4(RenderScript *rs);
-    static const Element * U64_2(RenderScript *rs);
-    static const Element * U64_3(RenderScript *rs);
-    static const Element * U64_4(RenderScript *rs);
-    static const Element * I64_2(RenderScript *rs);
-    static const Element * I64_3(RenderScript *rs);
-    static const Element * I64_4(RenderScript *rs);
-    static const Element * MATRIX_4X4(RenderScript *rs);
-    static const Element * MATRIX_3X3(RenderScript *rs);
-    static const Element * MATRIX_2X2(RenderScript *rs);
+    static sp<const Element> F32_2(RenderScript *rs);
+    static sp<const Element> F32_3(RenderScript *rs);
+    static sp<const Element> F32_4(RenderScript *rs);
+    static sp<const Element> F64_2(RenderScript *rs);
+    static sp<const Element> F64_3(RenderScript *rs);
+    static sp<const Element> F64_4(RenderScript *rs);
+    static sp<const Element> U8_2(RenderScript *rs);
+    static sp<const Element> U8_3(RenderScript *rs);
+    static sp<const Element> U8_4(RenderScript *rs);
+    static sp<const Element> I8_2(RenderScript *rs);
+    static sp<const Element> I8_3(RenderScript *rs);
+    static sp<const Element> I8_4(RenderScript *rs);
+    static sp<const Element> U16_2(RenderScript *rs);
+    static sp<const Element> U16_3(RenderScript *rs);
+    static sp<const Element> U16_4(RenderScript *rs);
+    static sp<const Element> I16_2(RenderScript *rs);
+    static sp<const Element> I16_3(RenderScript *rs);
+    static sp<const Element> I16_4(RenderScript *rs);
+    static sp<const Element> U32_2(RenderScript *rs);
+    static sp<const Element> U32_3(RenderScript *rs);
+    static sp<const Element> U32_4(RenderScript *rs);
+    static sp<const Element> I32_2(RenderScript *rs);
+    static sp<const Element> I32_3(RenderScript *rs);
+    static sp<const Element> I32_4(RenderScript *rs);
+    static sp<const Element> U64_2(RenderScript *rs);
+    static sp<const Element> U64_3(RenderScript *rs);
+    static sp<const Element> U64_4(RenderScript *rs);
+    static sp<const Element> I64_2(RenderScript *rs);
+    static sp<const Element> I64_3(RenderScript *rs);
+    static sp<const Element> I64_4(RenderScript *rs);
+    static sp<const Element> MATRIX_4X4(RenderScript *rs);
+    static sp<const Element> MATRIX_3X3(RenderScript *rs);
+    static sp<const Element> MATRIX_2X2(RenderScript *rs);
 
     Element(void *id, RenderScript *rs,
-            android::Vector<const Element *> &elements,
+            android::Vector<sp</*const*/ Element> > &elements,
             android::Vector<android::String8> &elementNames,
             android::Vector<uint32_t> &arraySizes);
     Element(void *id, RenderScript *rs, RsDataType dt, RsDataKind dk, bool norm, uint32_t size);
@@ -160,15 +163,15 @@
     virtual ~Element();
 
     void updateFromNative();
-    static const Element * createUser(RenderScript *rs, RsDataType dt);
-    static const Element * createVector(RenderScript *rs, RsDataType dt, uint32_t size);
-    static const Element * createPixel(RenderScript *rs, RsDataType dt, RsDataKind dk);
-    bool isCompatible(const Element *e);
+    static sp<const Element> createUser(RenderScript *rs, RsDataType dt);
+    static sp<const Element> createVector(RenderScript *rs, RsDataType dt, uint32_t size);
+    static sp<const Element> createPixel(RenderScript *rs, RsDataType dt, RsDataKind dk);
+    bool isCompatible(sp<const Element>e);
 
     class Builder {
     private:
         RenderScript *mRS;
-        android::Vector<const Element *> mElements;
+        android::Vector<sp</*const*/ Element> > mElements;
         android::Vector<android::String8> mElementNames;
         android::Vector<uint32_t> mArraySizes;
         bool mSkipPadding;
@@ -176,14 +179,14 @@
     public:
         Builder(RenderScript *rs);
         ~Builder();
-        void add(const Element *, android::String8 &name, uint32_t arraySize = 1);
-        const Element * create();
+        void add(sp</*const*/ Element>, android::String8 &name, uint32_t arraySize = 1);
+        sp<const Element> create();
     };
 
 private:
     void updateVisibleSubElements();
 
-    android::Vector<const Element *> mElements;
+    android::Vector<sp</*const*/ Element> > mElements;
     android::Vector<android::String8> mElementNames;
     android::Vector<uint32_t> mArraySizes;
     android::Vector<uint32_t> mVisibleElementMap;
@@ -196,4 +199,6 @@
     size_t mVectorSize;
 };
 
+}
+}
 #endif
diff --git a/cpp/RenderScript.cpp b/cpp/RenderScript.cpp
index 217b921..889f27b 100644
--- a/cpp/RenderScript.cpp
+++ b/cpp/RenderScript.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008-2012 The Android Open Source Project
+ * Copyright (C) 2012 The Android Open Source Project
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -23,6 +23,9 @@
 #include "RenderScript.h"
 #include "rs.h"
 
+using namespace android;
+using namespace renderscriptCpp;
+
 bool RenderScript::gInitialized = false;
 pthread_mutex_t RenderScript::gInitMutex = PTHREAD_MUTEX_INITIALIZER;
 
diff --git a/cpp/RenderScript.h b/cpp/RenderScript.h
index 5ad76e2..e6e7279 100644
--- a/cpp/RenderScript.h
+++ b/cpp/RenderScript.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008-2012 The Android Open Source Project
+ * Copyright (C) 2012 The Android Open Source Project
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -24,6 +24,9 @@
 
 #include "rsDefines.h"
 
+namespace android {
+namespace renderscriptCpp {
+
 class Element;
 class Type;
 class Allocation;
@@ -152,5 +155,7 @@
 
 };
 
+}
+}
 #endif
 
diff --git a/cpp/Script.cpp b/cpp/Script.cpp
index c87d460..63d5fab 100644
--- a/cpp/Script.cpp
+++ b/cpp/Script.cpp
@@ -25,11 +25,14 @@
 #include "Allocation.h"
 #include "Script.h"
 
+using namespace android;
+using namespace renderscriptCpp;
+
 void Script::invoke(uint32_t slot, const void *v, size_t len) const {
     rsScriptInvokeV(mRS->mContext, getID(), slot, v, len);
 }
 
-void Script::forEach(uint32_t slot, const Allocation *ain, const Allocation *aout,
+void Script::forEach(uint32_t slot, sp<const Allocation> ain, sp<const Allocation> aout,
                        const void *usr, size_t usrLen) const {
     if ((ain == NULL) && (aout == NULL)) {
         mRS->throwError("At least one of ain or aout is required to be non-null.");
@@ -44,12 +47,12 @@
 }
 
 
-void Script::bindAllocation(const Allocation *va, uint32_t slot) const {
+void Script::bindAllocation(sp<Allocation> va, uint32_t slot) const {
     rsScriptBindAllocation(mRS->mContext, getID(), BaseObj::getObjID(va), slot);
 }
 
 
-void Script::setVar(uint32_t index, const BaseObj *o) const {
+void Script::setVar(uint32_t index, sp<const BaseObj> o) const {
     rsScriptSetVarObj(mRS->mContext, getID(), index, (o == NULL) ? 0 : o->getID());
 }
 
diff --git a/cpp/Script.h b/cpp/Script.h
index 0700898..235560a 100644
--- a/cpp/Script.h
+++ b/cpp/Script.h
@@ -23,17 +23,24 @@
 #include "RenderScript.h"
 #include "Allocation.h"
 
+namespace android {
+namespace renderscriptCpp {
+
+
 class Type;
 class Element;
 class Allocation;
 
 class Script : public BaseObj {
+private:
+
 protected:
     Script(void *id, RenderScript *rs);
-    void forEach(uint32_t slot, const Allocation *in, const Allocation *out, const void *v, size_t) const;
-    void bindAllocation(const Allocation *va, uint32_t slot) const;
+    void forEach(uint32_t slot, sp<const Allocation> in, sp<const Allocation> out,
+            const void *v, size_t) const;
+    void bindAllocation(sp<Allocation> va, uint32_t slot) const;
     void setVar(uint32_t index, const void *, size_t len) const;
-    void setVar(uint32_t index, const BaseObj *o) const;
+    void setVar(uint32_t index, sp<const BaseObj> o) const;
     void invoke(uint32_t slot, const void *v, size_t len) const;
 
 
@@ -59,21 +66,21 @@
 public:
     class FieldBase {
     protected:
-        const Element *mElement;
-        Allocation *mAllocation;
+        sp<const Element> mElement;
+        sp<Allocation> mAllocation;
 
         void init(RenderScript *rs, uint32_t dimx, uint32_t usages = 0);
 
     public:
-        const Element *getElement() {
+        sp<const Element> getElement() {
             return mElement;
         }
 
-        const Type *getType() {
+        sp<const Type> getType() {
             return mAllocation->getType();
         }
 
-        const Allocation *getAllocation() {
+        sp<const Allocation> getAllocation() {
             return mAllocation;
         }
 
@@ -81,4 +88,6 @@
     };
 };
 
+}
+}
 #endif
diff --git a/cpp/ScriptC.cpp b/cpp/ScriptC.cpp
index 80e8efc..39b17d4 100644
--- a/cpp/ScriptC.cpp
+++ b/cpp/ScriptC.cpp
@@ -21,6 +21,9 @@
 
 #include "ScriptC.h"
 
+using namespace android;
+using namespace renderscriptCpp;
+
 ScriptC::ScriptC(RenderScript *rs,
                  const void *codeTxt, size_t codeLength,
                  const char *cachedName, size_t cachedNameLength,
diff --git a/cpp/ScriptC.h b/cpp/ScriptC.h
index b68f61c..8711b8d 100644
--- a/cpp/ScriptC.h
+++ b/cpp/ScriptC.h
@@ -22,6 +22,9 @@
 
 #include "Script.h"
 
+namespace android {
+namespace renderscriptCpp {
+
 class ScriptC : public Script {
 protected:
     ScriptC(RenderScript *rs,
@@ -31,4 +34,7 @@
 
 };
 
+
+}
+}
 #endif
diff --git a/cpp/Type.cpp b/cpp/Type.cpp
index 1352bd7..23327b0 100644
--- a/cpp/Type.cpp
+++ b/cpp/Type.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008 The Android Open Source Project
+ * Copyright (C) 2012 The Android Open Source Project
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -24,6 +24,9 @@
 #include "Element.h"
 #include "Type.h"
 
+using namespace android;
+using namespace renderscriptCpp;
+
 void Type::calcElementCount() {
     bool hasLod = hasMipmaps();
     uint32_t x = getX();
@@ -93,7 +96,7 @@
     */
 }
 
-Type::Builder::Builder(RenderScript *rs, const Element *e) {
+Type::Builder::Builder(RenderScript *rs, sp<const Element> e) {
     mRS = rs;
     mElement = e;
     mDimX = 0;
@@ -125,7 +128,7 @@
     mDimFaces = value;
 }
 
-const Type * Type::Builder::create() {
+sp<const Type> Type::Builder::create() {
     if (mDimZ > 0) {
         if ((mDimX < 1) || (mDimY < 1)) {
             ALOGE("Both X and Y dimension required when Z is present.");
@@ -145,7 +148,8 @@
         }
     }
 
-    void * id = rsTypeCreate(mRS->mContext, mElement->getID(), mDimX, mDimY, mDimZ, mDimMipmaps, mDimFaces);
+    void * id = rsTypeCreate(mRS->mContext, mElement->getID(), mDimX, mDimY, mDimZ,
+            mDimMipmaps, mDimFaces);
     Type *t = new Type(id, mRS);
     t->mElement = mElement;
     t->mDimX = mDimX;
diff --git a/cpp/Type.h b/cpp/Type.h
index 53481c3..0890c04 100644
--- a/cpp/Type.h
+++ b/cpp/Type.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008 The Android Open Source Project
+ * Copyright (C) 2012 The Android Open Source Project
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -21,6 +21,9 @@
 #include "RenderScript.h"
 #include "Element.h"
 
+namespace android {
+namespace renderscriptCpp {
+
 class Type : public BaseObj {
 protected:
     friend class Allocation;
@@ -31,14 +34,14 @@
     bool mDimMipmaps;
     bool mDimFaces;
     size_t mElementCount;
-    const Element *mElement;
+    sp<const Element> mElement;
 
     void calcElementCount();
     virtual void updateFromNative();
 
 public:
 
-    const Element* getElement() const {
+    sp<const Element> getElement() const {
         return mElement;
     }
 
@@ -82,18 +85,20 @@
         uint32_t mDimZ;
         bool mDimMipmaps;
         bool mDimFaces;
-        const Element *mElement;
+        sp<const Element> mElement;
 
     public:
-        Builder(RenderScript *rs, const Element *e);
+        Builder(RenderScript *rs, sp<const Element> e);
 
         void setX(uint32_t value);
         void setY(int value);
         void setMipmaps(bool value);
         void setFaces(bool value);
-        const Type * create();
+        sp<const Type> create();
     };
 
 };
 
+}
+}
 #endif
diff --git a/tests/ScriptC_mono.cpp b/tests/ScriptC_mono.cpp
index a6c63a8..88d041c 100644
--- a/tests/ScriptC_mono.cpp
+++ b/tests/ScriptC_mono.cpp
@@ -23,6 +23,10 @@
 
 #include "ScriptC_mono.h"
 
+using namespace android;
+using namespace renderscriptCpp;
+
+
 static const unsigned char __txt[] = {
     0xde,0xc0,0x17,0x0b,0x00,0x00,0x00,0x00,0x18,0x00,0x00,0x00,0xd0,0x04,0x00,0x00,
     0x00,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x42,0x43,0xc0,0xde,0x21,0x0c,0x00,0x00,
@@ -112,7 +116,7 @@
 ScriptC_mono::~ScriptC_mono() {
 }
 
-void ScriptC_mono::forEach_root(const Allocation *ain, const Allocation *aout) const {
+void ScriptC_mono::forEach_root(sp<const Allocation> ain, sp<const Allocation> aout) const {
     forEach(0, ain, aout, NULL, 0);
 }
 
diff --git a/tests/ScriptC_mono.h b/tests/ScriptC_mono.h
index 69c41ac..ebe6fb8 100644
--- a/tests/ScriptC_mono.h
+++ b/tests/ScriptC_mono.h
@@ -23,14 +23,15 @@
 
 #include "ScriptC.h"
 
-class ScriptC_mono : protected ScriptC {
+class ScriptC_mono : public android::renderscriptCpp::ScriptC {
 private:
     int32_t __gInt;
     bool __gBool;
 public:
-    ScriptC_mono(RenderScript *rs, const char *cacheDir, size_t cacheDirLength);
+    ScriptC_mono(android::renderscriptCpp::RenderScript *rs,
+            const char *cacheDir, size_t cacheDirLength);
     virtual ~ScriptC_mono();
-    
+
     void set_gInt(int32_t v) {
         setVar(0, v);
         __gInt = v;
@@ -38,11 +39,11 @@
     int32_t get_gInt() const {
         return __gInt;
     }
-    
+
     float get_cFloat() const {
         return 1.2f;
     }
-    
+
     void set_gBool(bool v) {
         setVar(2, v);
         __gBool = v;
@@ -50,6 +51,7 @@
     bool get_gBool() const {
         return __gBool;
     }
-    
-    void forEach_root(const Allocation *ain, const Allocation *aout) const;
+
+    void forEach_root(android::sp<const android::renderscriptCpp::Allocation> ain,
+            android::sp<const android::renderscriptCpp::Allocation> aout) const;
 };
diff --git a/tests/compute.cpp b/tests/compute.cpp
index 42eaa52..351627b 100644
--- a/tests/compute.cpp
+++ b/tests/compute.cpp
@@ -6,6 +6,9 @@
 
 #include "ScriptC_mono.h"
 
+using namespace android;
+using namespace renderscriptCpp;
+
 int main(int argc, char** argv)
 {
 
@@ -15,24 +18,24 @@
     bool r = rs->init(16);
     printf("Init returned %i\n", r);
 
-    const Element *e = Element::RGBA_8888(rs);
-    printf("Element %p\n", e);
+    sp<const Element> e = Element::RGBA_8888(rs);
+    printf("Element %p\n", e.get());
 
     Type::Builder tb(rs, e);
     tb.setX(128);
     tb.setY(128);
-    const Type *t = tb.create();
-    printf("Type %p\n", t);
+    sp<const Type> t = tb.create();
+    printf("Type %p\n", t.get());
 
 
-    Allocation *a1 = Allocation::createSized(rs, e, 1000);
-    printf("Allocation %p\n", a1);
+    sp<Allocation> a1 = Allocation::createSized(rs, e, 1000);
+    printf("Allocation %p\n", a1.get());
 
-    Allocation *ain = Allocation::createTyped(rs, t);
-    Allocation *aout = Allocation::createTyped(rs, t);
-    printf("Allocation %p %p\n", ain, aout);
+    sp<Allocation> ain = Allocation::createTyped(rs, t);
+    sp<Allocation> aout = Allocation::createTyped(rs, t);
+    printf("Allocation %p %p\n", ain.get(), aout.get());
 
-    ScriptC_mono * sc = new ScriptC_mono(rs, NULL, 0);
+    sp<ScriptC_mono> sc = new ScriptC_mono(rs, NULL, 0);
     printf("new script\n");
 
     uint32_t *buf = new uint32_t[t->getCount()];
@@ -49,10 +52,10 @@
 
 
     printf("Deleting stuff\n");
-    delete sc;
-    delete t;
-    delete a1;
-    delete e;
+    sc.clear();
+    t.clear();
+    a1.clear();
+    e.clear();
     delete rs;
     printf("Delete OK\n");
 }