1 /* 2 * Copyright (C) Texas Instruments - http://www.ti.com/ 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 "DebugUtils.h" 18 19 #include "utils/Debug.h" 20 21 22 23 24 namespace Ti { 25 26 27 28 29 // shared const buffer with spaces for indentation string 30 extern const char sIndentStringBuffer[] = 31 " " 32 " "; 33 template class android::CompileTimeAssert<sizeof(sIndentStringBuffer) - 1 == kIndentStringMaxLength>; 34 35 36 37 38 static const int kDebugThreadInfoGrowSize = 16; 39 40 41 42 43 Debug Debug::sInstance; 44 45 46 47 48 Debug::Debug() 49 { 50 grow(); 51 } 52 53 54 void Debug::grow() 55 { 56 android::AutoMutex locker(mMutex); 57 (void)locker; 58 59 const int size = kDebugThreadInfoGrowSize; 60 61 const int newSize = (mData.get() ? mData->threads.size() : 0) + size; 62 63 Data * const newData = new Data; 64 newData->threads.setCapacity(newSize); 65 66 // insert previous thread info pointers 67 if ( mData.get() ) 68 newData->threads.insertVectorAt(mData->threads, 0); 69 70 // populate data with new thread infos 71 for ( int i = 0; i < size; ++i ) 72 newData->threads.add(new ThreadInfo); 73 74 // replace old data with new one 75 mData = newData; 76 } 77 78 79 Debug::ThreadInfo * Debug::registerThread(Data * const data, const int32_t threadId) 80 { 81 const int size = data->threads.size(); 82 for ( int i = 0; i < size; ++i ) 83 { 84 ThreadInfo * const threadInfo = data->threads.itemAt(i); 85 if ( android_atomic_acquire_cas(0, threadId, &threadInfo->threadId) == 0 ) 86 return threadInfo; 87 } 88 89 // failed to find empty slot for thread 90 return 0; 91 } 92 93 94 95 96 } // namespace Ti 97