liblog: remove endianness functions

Android is little endian.

Test: liblog unit tests
Change-Id: Ieea7af39013a97f9f0d138a6d1ada3524d94e710
This commit is contained in:
Tom Cherry 2019-09-30 14:33:46 -07:00
parent ebb7cdd0ac
commit 4c63e04f2a
7 changed files with 32 additions and 86 deletions

View File

@ -399,22 +399,6 @@ int android_log_write_list_buffer(android_log_context ctx, const char** buffer)
return len;
}
/*
* Extract a 4-byte value from a byte stream.
*/
static inline uint32_t get4LE(const uint8_t* src) {
return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
}
/*
* Extract an 8-byte value from a byte stream.
*/
static inline uint64_t get8LE(const uint8_t* src) {
uint32_t low = src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
uint32_t high = src[4] | (src[5] << 8) | (src[6] << 16) | (src[7] << 24);
return ((uint64_t)high << 32) | (uint64_t)low;
}
/*
* Gets the next element. Parsing errors result in an EVENT_TYPE_UNKNOWN type.
* If there is nothing to process, the complete field is set to non-zero. If
@ -488,7 +472,7 @@ static android_log_list_element android_log_read_next_internal(android_log_conte
elem.type = EVENT_TYPE_UNKNOWN;
return elem;
}
elem.data.int32 = get4LE(&context->storage[pos]);
elem.data.int32 = *reinterpret_cast<int32_t*>(&context->storage[pos]);
/* common tangeable object suffix */
pos += elem.len;
elem.complete = !context->list_nest_depth && !context->count[0];
@ -507,7 +491,7 @@ static android_log_list_element android_log_read_next_internal(android_log_conte
elem.type = EVENT_TYPE_UNKNOWN;
return elem;
}
elem.data.int64 = get8LE(&context->storage[pos]);
elem.data.int64 = *reinterpret_cast<int64_t*>(&context->storage[pos]);
/* common tangeable object suffix */
pos += elem.len;
elem.complete = !context->list_nest_depth && !context->count[0];
@ -526,7 +510,7 @@ static android_log_list_element android_log_read_next_internal(android_log_conte
elem.complete = true;
return elem;
}
elem.len = get4LE(&context->storage[pos]);
elem.len = *reinterpret_cast<int32_t*>(&context->storage[pos]);
pos += sizeof(int32_t);
if ((pos + elem.len) > context->len) {
elem.len = context->len - pos; /* truncate string */

View File

@ -14,7 +14,6 @@
* limitations under the License.
*/
#include <endian.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>

View File

@ -14,7 +14,6 @@
* limitations under the License.
*/
#include <endian.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
@ -184,9 +183,9 @@ static int logdWrite(log_id_t logId, struct timespec* ts, struct iovec* vec, siz
android_log_event_int_t buffer;
header.id = LOG_ID_SECURITY;
buffer.header.tag = htole32(LIBLOG_LOG_TAG);
buffer.header.tag = LIBLOG_LOG_TAG;
buffer.payload.type = EVENT_TYPE_INT;
buffer.payload.data = htole32(snapshot);
buffer.payload.data = snapshot;
newVec[headerLength].iov_base = &buffer;
newVec[headerLength].iov_len = sizeof(buffer);
@ -202,9 +201,9 @@ static int logdWrite(log_id_t logId, struct timespec* ts, struct iovec* vec, siz
android_log_event_int_t buffer;
header.id = LOG_ID_EVENTS;
buffer.header.tag = htole32(LIBLOG_LOG_TAG);
buffer.header.tag = LIBLOG_LOG_TAG;
buffer.payload.type = EVENT_TYPE_INT;
buffer.payload.data = htole32(snapshot);
buffer.payload.data = snapshot;
newVec[headerLength].iov_base = &buffer;
newVec[headerLength].iov_len = sizeof(buffer);

View File

@ -582,24 +582,6 @@ int android_log_processLogBuffer(struct logger_entry* buf, AndroidLogEntry* entr
return 0;
}
/*
* Extract a 4-byte value from a byte stream.
*/
static inline uint32_t get4LE(const uint8_t* src) {
return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
}
/*
* Extract an 8-byte value from a byte stream.
*/
static inline uint64_t get8LE(const uint8_t* src) {
uint32_t low, high;
low = src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
high = src[4] | (src[5] << 8) | (src[6] << 16) | (src[7] << 24);
return ((uint64_t)high << 32) | (uint64_t)low;
}
static bool findChar(const char** cp, size_t* len, int c) {
while ((*len) && isspace(*(*cp))) {
++(*cp);
@ -746,7 +728,7 @@ static int android_log_printBinaryEvent(const unsigned char** pEventData, size_t
int32_t ival;
if (eventDataLen < 4) return -1;
ival = get4LE(eventData);
ival = *reinterpret_cast<const int32_t*>(eventData);
eventData += 4;
eventDataLen -= 4;
@ -756,7 +738,7 @@ static int android_log_printBinaryEvent(const unsigned char** pEventData, size_t
case EVENT_TYPE_LONG:
/* 64-bit signed long */
if (eventDataLen < 8) return -1;
lval = get8LE(eventData);
lval = *reinterpret_cast<const int64_t*>(eventData);
eventData += 8;
eventDataLen -= 8;
pr_lval:
@ -776,7 +758,7 @@ static int android_log_printBinaryEvent(const unsigned char** pEventData, size_t
float fval;
if (eventDataLen < 4) return -1;
ival = get4LE(eventData);
ival = *reinterpret_cast<const uint32_t*>(eventData);
fval = *(float*)&ival;
eventData += 4;
eventDataLen -= 4;
@ -797,7 +779,7 @@ static int android_log_printBinaryEvent(const unsigned char** pEventData, size_t
unsigned int strLen;
if (eventDataLen < 4) return -1;
strLen = get4LE(eventData);
strLen = *reinterpret_cast<const uint32_t*>(eventData);
eventData += 4;
eventDataLen -= 4;
@ -1036,7 +1018,7 @@ int android_log_processBinaryLogBuffer(
}
inCount = buf->len;
if (inCount < 4) return -1;
tagIndex = get4LE(eventData);
tagIndex = *reinterpret_cast<const uint32_t*>(eventData);
eventData += 4;
inCount -= 4;

View File

@ -87,13 +87,6 @@ static int pmsgAvailable(log_id_t logId) {
return 1;
}
/*
* Extract a 4-byte value from a byte stream.
*/
static inline uint32_t get4LE(const uint8_t* src) {
return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
}
static int pmsgWrite(log_id_t logId, struct timespec* ts, struct iovec* vec, size_t nr) {
static const unsigned headerLength = 2;
struct iovec newVec[nr + headerLength];
@ -107,7 +100,7 @@ static int pmsgWrite(log_id_t logId, struct timespec* ts, struct iovec* vec, siz
return -EINVAL;
}
if (SNET_EVENT_LOG_TAG != get4LE(static_cast<uint8_t*>(vec[0].iov_base))) {
if (SNET_EVENT_LOG_TAG != *static_cast<uint8_t*>(vec[0].iov_base)) {
return -EPERM;
}
}

View File

@ -17,7 +17,6 @@
#include <fcntl.h>
#include <inttypes.h>
#include <poll.h>
#include <sys/endian.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#include <sys/types.h>
@ -227,14 +226,14 @@ static void BM_pmsg_short(benchmark::State& state) {
buffer.header.tag = 0;
buffer.payload.type = EVENT_TYPE_INT;
uint32_t snapshot = 0;
buffer.payload.data = htole32(snapshot);
buffer.payload.data = snapshot;
newVec[2].iov_base = &buffer;
newVec[2].iov_len = sizeof(buffer);
while (state.KeepRunning()) {
++snapshot;
buffer.payload.data = htole32(snapshot);
buffer.payload.data = snapshot;
writev(pstore_fd, newVec, nr);
}
state.PauseTiming();
@ -303,11 +302,11 @@ static void BM_pmsg_short_aligned(benchmark::State& state) {
buffer->payload.header.tag = 0;
buffer->payload.payload.type = EVENT_TYPE_INT;
uint32_t snapshot = 0;
buffer->payload.payload.data = htole32(snapshot);
buffer->payload.payload.data = snapshot;
while (state.KeepRunning()) {
++snapshot;
buffer->payload.payload.data = htole32(snapshot);
buffer->payload.payload.data = snapshot;
write(pstore_fd, &buffer->pmsg_header,
sizeof(android_pmsg_log_header_t) + sizeof(android_log_header_t) +
sizeof(android_log_event_int_t));
@ -378,11 +377,11 @@ static void BM_pmsg_short_unaligned1(benchmark::State& state) {
buffer->payload.header.tag = 0;
buffer->payload.payload.type = EVENT_TYPE_INT;
uint32_t snapshot = 0;
buffer->payload.payload.data = htole32(snapshot);
buffer->payload.payload.data = snapshot;
while (state.KeepRunning()) {
++snapshot;
buffer->payload.payload.data = htole32(snapshot);
buffer->payload.payload.data = snapshot;
write(pstore_fd, &buffer->pmsg_header,
sizeof(android_pmsg_log_header_t) + sizeof(android_log_header_t) +
sizeof(android_log_event_int_t));
@ -453,11 +452,11 @@ static void BM_pmsg_long_aligned(benchmark::State& state) {
buffer->payload.header.tag = 0;
buffer->payload.payload.type = EVENT_TYPE_INT;
uint32_t snapshot = 0;
buffer->payload.payload.data = htole32(snapshot);
buffer->payload.payload.data = snapshot;
while (state.KeepRunning()) {
++snapshot;
buffer->payload.payload.data = htole32(snapshot);
buffer->payload.payload.data = snapshot;
write(pstore_fd, &buffer->pmsg_header, LOGGER_ENTRY_MAX_PAYLOAD);
}
state.PauseTiming();
@ -526,11 +525,11 @@ static void BM_pmsg_long_unaligned1(benchmark::State& state) {
buffer->payload.header.tag = 0;
buffer->payload.payload.type = EVENT_TYPE_INT;
uint32_t snapshot = 0;
buffer->payload.payload.data = htole32(snapshot);
buffer->payload.payload.data = snapshot;
while (state.KeepRunning()) {
++snapshot;
buffer->payload.payload.data = htole32(snapshot);
buffer->payload.payload.data = snapshot;
write(pstore_fd, &buffer->pmsg_header, LOGGER_ENTRY_MAX_PAYLOAD);
}
state.PauseTiming();

View File

@ -227,16 +227,6 @@ TEST(liblog, __android_log_btwrite__android_logger_list_read) {
#endif
}
#ifdef __ANDROID__
static inline uint32_t get4LE(const uint8_t* src) {
return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
}
static inline uint32_t get4LE(const char* src) {
return get4LE(reinterpret_cast<const uint8_t*>(src));
}
#endif
static void bswrite_test(const char* message) {
#ifdef __ANDROID__
struct logger_list* logger_list;
@ -299,7 +289,7 @@ static void bswrite_test(const char* message) {
continue;
}
size_t len = get4LE(reinterpret_cast<char*>(&eventData->length));
size_t len = eventData->length;
if (len == total) {
++count;
@ -1914,7 +1904,7 @@ static void android_errorWriteWithInfoLog_helper(int TAG, const char* SUBTAG,
char* original = eventData;
// Tag
int tag = get4LE(eventData);
int tag = *reinterpret_cast<int32_t*>(eventData);
eventData += 4;
if (tag != TAG) {
@ -1941,7 +1931,7 @@ static void android_errorWriteWithInfoLog_helper(int TAG, const char* SUBTAG,
unsigned subtag_len = strlen(SUBTAG);
if (subtag_len > 32) subtag_len = 32;
ASSERT_EQ(subtag_len, get4LE(eventData));
ASSERT_EQ(subtag_len, *reinterpret_cast<uint32_t*>(eventData));
eventData += 4;
if (memcmp(SUBTAG, eventData, subtag_len)) {
@ -1953,14 +1943,14 @@ static void android_errorWriteWithInfoLog_helper(int TAG, const char* SUBTAG,
ASSERT_EQ(EVENT_TYPE_INT, eventData[0]);
eventData++;
ASSERT_EQ(UID, (int)get4LE(eventData));
ASSERT_EQ(UID, *reinterpret_cast<int32_t*>(eventData));
eventData += 4;
// Element #3: string type for data
ASSERT_EQ(EVENT_TYPE_STRING, eventData[0]);
eventData++;
size_t dataLen = get4LE(eventData);
size_t dataLen = *reinterpret_cast<int32_t*>(eventData);
eventData += 4;
if (DATA_LEN < 512) ASSERT_EQ(DATA_LEN, (int)dataLen);
@ -2072,7 +2062,7 @@ static void android_errorWriteLog_helper(int TAG, const char* SUBTAG,
if (!eventData) continue;
// Tag
int tag = get4LE(eventData);
int tag = *reinterpret_cast<int32_t*>(eventData);
eventData += 4;
if (tag != TAG) continue;
@ -2125,7 +2115,7 @@ static void android_errorWriteLog_helper(int TAG, const char* SUBTAG,
}
// Tag
int tag = get4LE(eventData);
int tag = *reinterpret_cast<int32_t*>(eventData);
eventData += 4;
if (tag != TAG) {
@ -2150,7 +2140,7 @@ static void android_errorWriteLog_helper(int TAG, const char* SUBTAG,
ASSERT_EQ(EVENT_TYPE_STRING, eventData[0]);
eventData++;
ASSERT_EQ(strlen(SUBTAG), get4LE(eventData));
ASSERT_EQ(strlen(SUBTAG), *reinterpret_cast<uint32_t*>(eventData));
eventData += 4;
if (memcmp(SUBTAG, eventData, strlen(SUBTAG))) {
@ -2663,7 +2653,7 @@ static void create_android_logger(const char* (*fn)(uint32_t tag,
// test buffer reading API
int buffer_to_string = -1;
if (eventData) {
snprintf(msgBuf, sizeof(msgBuf), "I/[%" PRIu32 "]", get4LE(eventData));
snprintf(msgBuf, sizeof(msgBuf), "I/[%" PRIu32 "]", *reinterpret_cast<uint32_t*>(eventData));
print_barrier();
fprintf(stderr, "%-10s(%5u): ", msgBuf, pid);
memset(msgBuf, 0, sizeof(msgBuf));