Logan | f7f0ac5 | 2011-01-07 03:53:43 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2010, 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 | #define LOG_TAG "bcc" |
| 18 | #include <cutils/log.h> |
| 19 | |
| 20 | #include "ScriptCached.h" |
| 21 | |
| 22 | #include <bcc/bcc_cache.h> |
| 23 | |
| 24 | #include "ContextManager.h" |
| 25 | #include "EmittedFuncInfo.h" |
| 26 | |
Logan | 856ceb2 | 2011-01-07 05:21:26 +0800 | [diff] [blame] | 27 | #include <stdlib.h> |
| 28 | |
Logan | f7f0ac5 | 2011-01-07 03:53:43 +0800 | [diff] [blame] | 29 | namespace bcc { |
| 30 | |
| 31 | ScriptCached::~ScriptCached() { |
| 32 | // Deallocate the bcc script context |
| 33 | if (mContext) { |
| 34 | deallocateContext(mContext); |
| 35 | } |
| 36 | |
| 37 | // Deallocate string pool, exported var list, exported func list |
| 38 | if (mpStringPoolRaw) { free(mpStringPoolRaw); } |
| 39 | if (mpExportVars) { free(mpExportVars); } |
| 40 | if (mpExportFuncs) { free(mpExportFuncs); } |
| 41 | } |
| 42 | |
| 43 | void ScriptCached::getExportVars(BCCsizei *actualVarCount, |
| 44 | BCCsizei maxVarCount, |
| 45 | BCCvoid **vars) { |
| 46 | int varCount = static_cast<int>(mpExportVars->count); |
| 47 | |
| 48 | if (actualVarCount) |
| 49 | *actualVarCount = varCount; |
| 50 | if (varCount > maxVarCount) |
| 51 | varCount = maxVarCount; |
| 52 | if (vars) { |
| 53 | void **ptr = mpExportVars->cached_addr_list; |
| 54 | for (int i = 0; i < varCount; i++) { |
| 55 | *vars++ = *ptr++; |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | |
| 61 | void ScriptCached::getExportFuncs(BCCsizei *actualFuncCount, |
| 62 | BCCsizei maxFuncCount, |
| 63 | BCCvoid **funcs) { |
| 64 | int funcCount = static_cast<int>(mpExportFuncs->count); |
| 65 | |
| 66 | if (actualFuncCount) |
| 67 | *actualFuncCount = funcCount; |
| 68 | if (funcCount > maxFuncCount) |
| 69 | funcCount = maxFuncCount; |
| 70 | if (funcs) { |
| 71 | void **ptr = mpExportFuncs->cached_addr_list; |
| 72 | for (int i = 0; i < funcCount; i++) { |
| 73 | *funcs++ = *ptr++; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | |
| 79 | void ScriptCached::getPragmas(BCCsizei *actualStringCount, |
| 80 | BCCsizei maxStringCount, |
| 81 | BCCchar **strings) { |
| 82 | int stringCount = static_cast<int>(mPragmas.size()) * 2; |
| 83 | |
| 84 | if (actualStringCount) |
| 85 | *actualStringCount = stringCount; |
| 86 | |
| 87 | if (stringCount > maxStringCount) |
| 88 | stringCount = maxStringCount; |
| 89 | |
| 90 | if (strings) { |
| 91 | for (int i = 0; stringCount >= 2; stringCount -= 2, ++i) { |
| 92 | *strings++ = const_cast<BCCchar *>(mPragmas[i].first); |
| 93 | *strings++ = const_cast<BCCchar *>(mPragmas[i].second); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | |
| 99 | void *ScriptCached::lookup(const char *name) { |
Logan | 216ec71 | 2011-01-07 12:12:31 +0800 | [diff] [blame] | 100 | FuncTable::const_iterator I = mFunctions.find(name); |
| 101 | return (I == mFunctions.end()) ? NULL : I->second.first; |
Logan | f7f0ac5 | 2011-01-07 03:53:43 +0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | |
| 105 | void ScriptCached::getFunctions(BCCsizei *actualFunctionCount, |
| 106 | BCCsizei maxFunctionCount, |
| 107 | BCCchar **functions) { |
Logan | 216ec71 | 2011-01-07 12:12:31 +0800 | [diff] [blame] | 108 | int functionCount = mFunctions.size(); |
Logan | f7f0ac5 | 2011-01-07 03:53:43 +0800 | [diff] [blame] | 109 | |
Logan | 216ec71 | 2011-01-07 12:12:31 +0800 | [diff] [blame] | 110 | if (actualFunctionCount) |
| 111 | *actualFunctionCount = functionCount; |
| 112 | if (functionCount > maxFunctionCount) |
| 113 | functionCount = maxFunctionCount; |
| 114 | if (functions) { |
| 115 | for (FuncTable::const_iterator |
| 116 | I = mFunctions.begin(), E = mFunctions.end(); |
| 117 | I != E && (functionCount > 0); I++, functionCount--) { |
| 118 | *functions++ = const_cast<BCCchar*>(I->first.c_str()); |
| 119 | } |
Logan | f7f0ac5 | 2011-01-07 03:53:43 +0800 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
| 123 | |
| 124 | void ScriptCached::getFunctionBinary(BCCchar *funcname, |
| 125 | BCCvoid **base, |
| 126 | BCCsizei *length) { |
Logan | 216ec71 | 2011-01-07 12:12:31 +0800 | [diff] [blame] | 127 | FuncTable::const_iterator I = mFunctions.find(funcname); |
| 128 | if (I == mFunctions.end()) { |
Logan | f7f0ac5 | 2011-01-07 03:53:43 +0800 | [diff] [blame] | 129 | *base = NULL; |
Logan | f7f0ac5 | 2011-01-07 03:53:43 +0800 | [diff] [blame] | 130 | *length = 0; |
Logan | 216ec71 | 2011-01-07 12:12:31 +0800 | [diff] [blame] | 131 | } else { |
| 132 | *base = I->second.first; |
| 133 | *length = I->second.second; |
Logan | f7f0ac5 | 2011-01-07 03:53:43 +0800 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
| 137 | |
| 138 | } // namespace bcc |