blob: 8011458d7c190d483410b1982e6001f06912b4ec [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
Loganbe79ada2011-01-13 01:33:45 +080043void ScriptCached::getExportVarList(size_t varListSize, void **varList) {
44 if (varList) {
45 size_t varCount = getExportVarCount();
Loganf7f0ac52011-01-07 03:53:43 +080046
Loganbe79ada2011-01-13 01:33:45 +080047 if (varCount > varListSize) {
48 varCount = varListSize;
Loganf7f0ac52011-01-07 03:53:43 +080049 }
Loganbe79ada2011-01-13 01:33:45 +080050
51 memcpy(varList, mpExportVars->cached_addr_list, sizeof(void *) * varCount);
Loganf7f0ac52011-01-07 03:53:43 +080052 }
53}
54
55
Loganbe79ada2011-01-13 01:33:45 +080056void ScriptCached::getExportFuncList(size_t funcListSize, void **funcList) {
57 if (funcList) {
58 size_t funcCount = getExportFuncCount();
Loganf7f0ac52011-01-07 03:53:43 +080059
Loganbe79ada2011-01-13 01:33:45 +080060 if (funcCount > funcListSize) {
61 funcCount = funcListSize;
Loganf7f0ac52011-01-07 03:53:43 +080062 }
Loganbe79ada2011-01-13 01:33:45 +080063
64 memcpy(funcList, mpExportFuncs->cached_addr_list,
65 sizeof(void *) * funcCount);
Loganf7f0ac52011-01-07 03:53:43 +080066 }
67}
68
69
Loganbe79ada2011-01-13 01:33:45 +080070void ScriptCached::getPragmaList(size_t pragmaListSize,
71 char const **keyList,
72 char const **valueList) {
73 size_t pragmaCount = getPragmaCount();
Loganf7f0ac52011-01-07 03:53:43 +080074
Loganbe79ada2011-01-13 01:33:45 +080075 if (pragmaCount > pragmaListSize) {
76 pragmaCount = pragmaListSize;
77 }
Loganf7f0ac52011-01-07 03:53:43 +080078
Loganbe79ada2011-01-13 01:33:45 +080079 if (keyList) {
80 for (size_t i = 0; i < pragmaCount; ++i) {
81 *keyList++ = mPragmas[i].first;
82 }
83 }
Loganf7f0ac52011-01-07 03:53:43 +080084
Loganbe79ada2011-01-13 01:33:45 +080085 if (valueList) {
86 for (size_t i = 0; i < pragmaCount; ++i) {
87 *valueList++ = mPragmas[i].second;
Loganf7f0ac52011-01-07 03:53:43 +080088 }
89 }
90}
91
92
93void *ScriptCached::lookup(const char *name) {
Logan216ec712011-01-07 12:12:31 +080094 FuncTable::const_iterator I = mFunctions.find(name);
95 return (I == mFunctions.end()) ? NULL : I->second.first;
Loganf7f0ac52011-01-07 03:53:43 +080096}
97
98
Loganbe79ada2011-01-13 01:33:45 +080099void ScriptCached::getFuncNameList(size_t funcNameListSize,
100 char const **funcNameList) {
101 if (funcNameList) {
102 size_t funcCount = getFuncCount();
Loganf7f0ac52011-01-07 03:53:43 +0800103
Loganbe79ada2011-01-13 01:33:45 +0800104 if (funcCount > funcNameListSize) {
105 funcCount = funcNameListSize;
106 }
107
Logan216ec712011-01-07 12:12:31 +0800108 for (FuncTable::const_iterator
109 I = mFunctions.begin(), E = mFunctions.end();
Loganbe79ada2011-01-13 01:33:45 +0800110 I != E && funcCount > 0; I++, funcCount--) {
111 *funcNameList++ = I->first.c_str();
Logan216ec712011-01-07 12:12:31 +0800112 }
Loganf7f0ac52011-01-07 03:53:43 +0800113 }
114}
115
116
Loganbe79ada2011-01-13 01:33:45 +0800117void ScriptCached::getFuncBinary(char const *funcname,
118 void **base,
119 size_t *length) {
Logan216ec712011-01-07 12:12:31 +0800120 FuncTable::const_iterator I = mFunctions.find(funcname);
Loganbe79ada2011-01-13 01:33:45 +0800121
122#define DEREF_ASSIGN(VAR, VALUE) if (VAR) { *(VAR) = (VALUE); }
123
Logan216ec712011-01-07 12:12:31 +0800124 if (I == mFunctions.end()) {
Loganbe79ada2011-01-13 01:33:45 +0800125 DEREF_ASSIGN(base, NULL);
126 DEREF_ASSIGN(length, 0);
Logan216ec712011-01-07 12:12:31 +0800127 } else {
Loganbe79ada2011-01-13 01:33:45 +0800128 DEREF_ASSIGN(base, I->second.first);
129 DEREF_ASSIGN(length, I->second.second);
Loganf7f0ac52011-01-07 03:53:43 +0800130 }
Loganbe79ada2011-01-13 01:33:45 +0800131
132#undef DEREF_ASSIGN
Loganf7f0ac52011-01-07 03:53:43 +0800133}
134
135
136} // namespace bcc