Skip to content

Commit 7e438db

Browse files
committed
Move JNIHelp to a C implementation
Completes the move to C for libnativehelper. Reduces the size of libnativehelper_compat_c++.so as it no longer statically links against lbc++: arm 125kB -> 14kB arm8 181kB -> 20kB x86 172kB -> 16kB x86_64 180kB -> 18kB Marginal reduction in the size of libnativehelper.so. Bug: 151443957 Test: m && boot Test: ExpandableString_test Change-Id: I562897337901131d3f855b66307bb2a12c04d477
1 parent 962ee00 commit 7e438db

12 files changed

+629
-381
lines changed

ALog-priv.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
*/
3636

3737
#ifndef ALOG
38-
#define ALOG(priority, tag, fmt...) \
39-
__android_log_print(ANDROID_##priority, tag, fmt)
38+
#define ALOG(priority, tag, fmt, ...) \
39+
__android_log_print(ANDROID_##priority, tag, fmt, __VA_ARGS__)
4040
#endif
4141

4242
#ifndef ALOGV

Android.bp

+16-7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
cc_defaults {
16+
name: "libnativehelper_defaults",
17+
cflags: [
18+
"-fvisibility=protected",
19+
"-std=c11",
20+
],
21+
}
22+
1523
cc_library_headers {
1624
name: "jni_headers",
1725
host_supported: true,
@@ -69,25 +77,24 @@ cc_library_headers {
6977

7078
cc_library {
7179
name: "libnativehelper",
80+
defaults: ["libnativehelper_defaults"],
7281
host_supported: true,
7382
srcs: [
7483
"DlHelp.c",
75-
"JNIHelp.cpp",
84+
"ExpandableString.c",
85+
"JNIHelp.c",
7686
"JniConstants.c",
7787
"JniInvocation.c",
7888
],
7989
shared_libs: ["liblog"],
80-
cflags: [
81-
"-Werror",
82-
"-fvisibility=protected",
83-
],
8490
export_include_dirs: [
8591
"header_only_include",
8692
"include",
8793
"platform_include",
8894
],
8995
header_libs: ["jni_headers"],
9096
export_header_lib_headers: ["jni_headers"],
97+
stl: "none",
9198
stubs: {
9299
symbol_file: "libnativehelper.map.txt",
93100
versions: ["1"],
@@ -116,6 +123,7 @@ cc_library {
116123

117124
cc_library_shared {
118125
name: "libnativehelper_compat_libc++",
126+
defaults: ["libnativehelper_defaults"],
119127
header_libs: ["jni_headers"],
120128
cflags: ["-Werror"],
121129
export_header_lib_headers: ["jni_headers"],
@@ -129,14 +137,15 @@ cc_library_shared {
129137
],
130138
srcs: [
131139
"DlHelp.c",
132-
"JNIHelp.cpp",
140+
"ExpandableString.c",
141+
"JNIHelp.c",
133142
"JniConstants.c",
134143
],
135144
shared_libs: [
136145
"liblog",
137146
],
138147
sdk_version: "19",
139-
stl: "c++_static",
148+
stl: "none",
140149
}
141150

142151
ndk_headers {

ExpandableString.c

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (C) 2020 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 "ExpandableString.h"
18+
19+
#include <stdio.h>
20+
#include <stdlib.h>
21+
#include <string.h>
22+
23+
void ExpandableStringInitialize(struct ExpandableString *s) {
24+
memset(s, 0, sizeof(*s));
25+
}
26+
27+
void ExpandableStringRelease(struct ExpandableString* s) {
28+
free(s->data);
29+
memset(s, 0, sizeof(*s));
30+
}
31+
32+
bool ExpandableStringAppend(struct ExpandableString* s, const char* text) {
33+
size_t textSize = strlen(text);
34+
size_t requiredSize = s->dataSize + textSize + 1;
35+
char* data = (char*) realloc(s->data, requiredSize);
36+
if (data == NULL) {
37+
return false;
38+
}
39+
s->data = data;
40+
memcpy(s->data + s->dataSize, text, textSize + 1);
41+
s->dataSize += textSize;
42+
return true;
43+
}
44+
45+
bool ExpandableStringAssign(struct ExpandableString* s, const char* text) {
46+
ExpandableStringRelease(s);
47+
return ExpandableStringAppend(s, text);
48+
}

ExpandableString.h

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (C) 2020 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+
#pragma once
18+
19+
#include <sys/cdefs.h>
20+
21+
#include <stdbool.h>
22+
#include <stddef.h>
23+
24+
__BEGIN_DECLS
25+
26+
struct ExpandableString {
27+
size_t dataSize; // The length of the C string data (not including the null-terminator).
28+
char* data; // The C string data.
29+
};
30+
31+
// Initialize ExpandableString.
32+
void ExpandableStringInitialize(struct ExpandableString* s);
33+
34+
// Release memory associated with ExpandableString.
35+
void ExpandableStringRelease(struct ExpandableString* s);
36+
37+
// Append null-terminated string |text| to ExpandableString, expanding the storage if required.
38+
// Returns true on success, false othewise.
39+
bool ExpandableStringAppend(struct ExpandableString* s, const char* text);
40+
41+
// Assign null-terminate string |text| to ExpandableString, expanding the storage if required.
42+
// Returns true on success, false othewise.
43+
bool ExpandableStringAssign(struct ExpandableString*s, const char* text);
44+
45+
__END_DECLS

0 commit comments

Comments
 (0)