Don't copy strerror(3) or strsignal(3) strings if we can share.
Change-Id: Ic405269f63b945c6fb347e7c4346cd6f104aff35
diff --git a/libc/bionic/strerror.cpp b/libc/bionic/strerror.cpp
index 036ec8d..455dc52 100644
--- a/libc/bionic/strerror.cpp
+++ b/libc/bionic/strerror.cpp
@@ -29,9 +29,17 @@
#include <string.h>
#include "ThreadLocalBuffer.h"
+extern "C" const char* __strerror_lookup(int);
+
GLOBAL_INIT_THREAD_LOCAL_BUFFER(strerror);
char* strerror(int error_number) {
+ // Just return the original constant in the easy cases.
+ char* result = const_cast<char*>(__strerror_lookup(error_number));
+ if (result != NULL) {
+ return result;
+ }
+
LOCAL_INIT_THREAD_LOCAL_BUFFER(char*, strerror, NL_TEXTMAX);
strerror_r(error_number, strerror_buffer, strerror_buffer_size);
return strerror_buffer;
diff --git a/libc/bionic/strerror_r.cpp b/libc/bionic/strerror_r.cpp
index 92235a5..a00ebe5 100644
--- a/libc/bionic/strerror_r.cpp
+++ b/libc/bionic/strerror_r.cpp
@@ -27,11 +27,25 @@
{ 0, NULL }
};
+extern "C" const char* __strerror_lookup(int error_number) {
+ return __code_string_lookup(_sys_error_strings, error_number);
+}
+
+static const Pair _sys_signal_strings[] = {
+#define __BIONIC_SIGDEF(x,y,z) { y, z },
+#include <sys/_sigdefs.h>
+ { 0, NULL }
+};
+
+extern "C" const char* __strsignal_lookup(int signal_number) {
+ return __code_string_lookup(_sys_signal_strings, signal_number);
+}
+
int strerror_r(int error_number, char* buf, size_t buf_len) {
int saved_errno = errno;
size_t length;
- const char* error_name = __code_string_lookup(_sys_error_strings, error_number);
+ const char* error_name = __strerror_lookup(error_number);
if (error_name != NULL) {
length = snprintf(buf, buf_len, "%s", error_name);
} else {
@@ -46,14 +60,8 @@
return 0;
}
-static const Pair _sys_signal_strings[] = {
-#define __BIONIC_SIGDEF(x,y,z) { y, z },
-#include <sys/_sigdefs.h>
- { 0, NULL }
-};
-
extern "C" const char* __strsignal(int signal_number, char* buf, size_t buf_len) {
- const char* signal_name = __code_string_lookup(_sys_signal_strings, signal_number);
+ const char* signal_name = __strsignal_lookup(signal_number);
if (signal_name != NULL) {
return signal_name;
}
diff --git a/libc/bionic/strsignal.cpp b/libc/bionic/strsignal.cpp
index 1cbec9b..9b046d4 100644
--- a/libc/bionic/strsignal.cpp
+++ b/libc/bionic/strsignal.cpp
@@ -29,11 +29,18 @@
#include <string.h>
#include "ThreadLocalBuffer.h"
+extern "C" const char* __strsignal_lookup(int);
extern "C" const char* __strsignal(int, char*, size_t);
GLOBAL_INIT_THREAD_LOCAL_BUFFER(strsignal);
char* strsignal(int signal_number) {
+ // Just return the original constant in the easy cases.
+ char* result = const_cast<char*>(__strsignal_lookup(signal_number));
+ if (result != NULL) {
+ return result;
+ }
+
LOCAL_INIT_THREAD_LOCAL_BUFFER(char*, strsignal, NL_TEXTMAX);
return const_cast<char*>(__strsignal(signal_number, strsignal_buffer, strsignal_buffer_size));
}