rsObjectBase.cpp revision bf3c14ebf456c745c084605dddeda08afdfc7987
1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "rsObjectBase.h"
18#include "rsContext.h"
19
20using namespace android;
21using namespace android::renderscript;
22
23ObjectBase::ObjectBase(Context *rsc)
24{
25    mUserRefCount = 0;
26    mSysRefCount = 0;
27    mName = NULL;
28    mRSC = NULL;
29    mNext = NULL;
30    mPrev = NULL;
31    mAllocFile = __FILE__;
32    mAllocLine = __LINE__;
33    setContext(rsc);
34}
35
36ObjectBase::~ObjectBase()
37{
38    //LOGV("~ObjectBase %p  ref %i,%i", this, mUserRefCount, mSysRefCount);
39    rsAssert(!mUserRefCount);
40    rsAssert(!mSysRefCount);
41    remove();
42    delete[] mName;
43}
44
45void ObjectBase::dumpLOGV(const char *op) const
46{
47    if (mName) {
48        LOGV("%s RSobj %p, name %s, refs %i,%i  from %s,%i links %p,%p,%p",
49             op, this, mName, mUserRefCount, mSysRefCount, mAllocFile, mAllocLine, mNext, mPrev, mRSC);
50    } else {
51        LOGV("%s RSobj %p, no-name, refs %i,%i  from %s,%i links %p,%p,%p",
52             op, this, mUserRefCount, mSysRefCount, mAllocFile, mAllocLine, mNext, mPrev, mRSC);
53    }
54}
55
56void ObjectBase::setContext(Context *rsc)
57{
58    if (mRSC) {
59        remove();
60    }
61    mRSC = rsc;
62    if (rsc) {
63        add();
64    }
65}
66
67void ObjectBase::incUserRef() const
68{
69    mUserRefCount ++;
70    //LOGV("ObjectBase %p inc ref %i", this, mRefCount);
71}
72
73void ObjectBase::incSysRef() const
74{
75    mSysRefCount ++;
76    //LOGV("ObjectBase %p inc ref %i", this, mRefCount);
77}
78
79bool ObjectBase::checkDelete() const
80{
81    if (!(mSysRefCount | mUserRefCount)) {
82        if (mRSC && mRSC->props.mLogObjects) {
83            dumpLOGV("checkDelete");
84        }
85        delete this;
86        return true;
87    }
88    return false;
89}
90
91bool ObjectBase::decUserRef() const
92{
93    rsAssert(mUserRefCount > 0);
94    mUserRefCount --;
95    //dumpObj("decUserRef");
96    return checkDelete();
97}
98
99bool ObjectBase::zeroUserRef() const
100{
101    mUserRefCount = 0;
102    //dumpObj("zeroUserRef");
103    return checkDelete();
104}
105
106bool ObjectBase::decSysRef() const
107{
108    rsAssert(mSysRefCount > 0);
109    mSysRefCount --;
110    //dumpObj("decSysRef");
111    return checkDelete();
112}
113
114void ObjectBase::setName(const char *name)
115{
116    delete mName;
117    mName = NULL;
118    if (name) {
119        mName = new char[strlen(name) +1];
120        strcpy(mName, name);
121    }
122}
123
124void ObjectBase::setName(const char *name, uint32_t len)
125{
126    delete mName;
127    mName = NULL;
128    if (name) {
129        mName = new char[len + 1];
130        memcpy(mName, name, len);
131        mName[len] = 0;
132    }
133}
134
135void ObjectBase::add() const
136{
137    rsAssert(!mNext);
138    rsAssert(!mPrev);
139    //LOGV("calling add  rsc %p", mRSC);
140    mNext = mRSC->mObjHead;
141    if (mRSC->mObjHead) {
142        mRSC->mObjHead->mPrev = this;
143    }
144    mRSC->mObjHead = this;
145}
146
147void ObjectBase::remove() const
148{
149    //LOGV("calling remove  rsc %p", mRSC);
150    if (!mRSC) {
151        rsAssert(!mPrev);
152        rsAssert(!mNext);
153        return;
154    }
155    if (mRSC->mObjHead == this) {
156        mRSC->mObjHead = mNext;
157    }
158    if (mPrev) {
159        mPrev->mNext = mNext;
160    }
161    if (mNext) {
162        mNext->mPrev = mPrev;
163    }
164    mPrev = NULL;
165    mNext = NULL;
166}
167
168void ObjectBase::zeroAllUserRef(Context *rsc)
169{
170    if (rsc->props.mLogObjects) {
171        LOGV("Forcing release of all outstanding user refs.");
172    }
173
174    // This operation can be slow, only to be called during context cleanup.
175    const ObjectBase * o = rsc->mObjHead;
176    while (o) {
177        //LOGE("o %p", o);
178        if (o->zeroUserRef()) {
179            // deleted the object and possibly others, restart from head.
180            o = rsc->mObjHead;
181            //LOGE("o head %p", o);
182        } else {
183            o = o->mNext;
184            //LOGE("o next %p", o);
185        }
186    }
187
188    if (rsc->props.mLogObjects) {
189        LOGV("Objects remaining.");
190        o = rsc->mObjHead;
191        while (o) {
192            o->dumpLOGV("  ");
193            o = o->mNext;
194        }
195    }
196}
197
198