Don't copy strerror(3) or strsignal(3) strings if we can share.

Change-Id: Ic405269f63b945c6fb347e7c4346cd6f104aff35
This commit is contained in:
Elliott Hughes 2012-09-17 15:23:35 -07:00
parent 4e9d953a0e
commit 4198fa4c23
3 changed files with 31 additions and 8 deletions

View file

@ -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;

View file

@ -27,11 +27,25 @@ static const Pair _sys_error_strings[] = {
{ 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 @@ int strerror_r(int error_number, char* buf, size_t buf_len) {
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;
}

View file

@ -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));
}