blob: 6b273885fcf5230a352419c5c8b2a8e0526a0dec [file] [log] [blame]
Loganf7f0ac52011-01-07 03:53:43 +08001/*
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
Logan856ceb22011-01-07 05:21:26 +080027#include <stdlib.h>
28
Loganf7f0ac52011-01-07 03:53:43 +080029namespace bcc {
30
31ScriptCached::~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
43void 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
61void 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
79void 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
99void *ScriptCached::lookup(const char *name) {
Logan216ec712011-01-07 12:12:31 +0800100 FuncTable::const_iterator I = mFunctions.find(name);
101 return (I == mFunctions.end()) ? NULL : I->second.first;
Loganf7f0ac52011-01-07 03:53:43 +0800102}
103
104
105void ScriptCached::getFunctions(BCCsizei *actualFunctionCount,
106 BCCsizei maxFunctionCount,
107 BCCchar **functions) {
Logan216ec712011-01-07 12:12:31 +0800108 int functionCount = mFunctions.size();
Loganf7f0ac52011-01-07 03:53:43 +0800109
Logan216ec712011-01-07 12:12:31 +0800110 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 }
Loganf7f0ac52011-01-07 03:53:43 +0800120 }
121}
122
123
124void ScriptCached::getFunctionBinary(BCCchar *funcname,
125 BCCvoid **base,
126 BCCsizei *length) {
Logan216ec712011-01-07 12:12:31 +0800127 FuncTable::const_iterator I = mFunctions.find(funcname);
128 if (I == mFunctions.end()) {
Loganf7f0ac52011-01-07 03:53:43 +0800129 *base = NULL;
Loganf7f0ac52011-01-07 03:53:43 +0800130 *length = 0;
Logan216ec712011-01-07 12:12:31 +0800131 } else {
132 *base = I->second.first;
133 *length = I->second.second;
Loganf7f0ac52011-01-07 03:53:43 +0800134 }
135}
136
137
138} // namespace bcc