diff --git a/server.c b/server.c index 5bbfc75..b5df0ee 100644 --- a/server.c +++ b/server.c @@ -743,7 +743,7 @@ int bus_client_process_message(Bus *s, BusClient *client, WireCtx *ctx) // message needs to be routed // TODO: perform checks here like making sure all method calls have the 'member' field, etc. - if (!client->unique_name) { + if (unlikely(!client->unique_name)) { return -1; } @@ -760,7 +760,7 @@ int bus_client_process_message(Bus *s, BusClient *client, WireCtx *ctx) } end_nonfatal: - if (ctx->byte_cursor != body_end) { + if (unlikely(ctx->byte_cursor != body_end)) { // The code above did not properly consume the entire message while parsing. // This is bad, because we expect the byte_cursor after this function to be // exactly at the end of a message. We rely on this invariant for message diff --git a/wire.c b/wire.c index 4b7ffee..4023d7e 100644 --- a/wire.c +++ b/wire.c @@ -4,34 +4,6 @@ #include "wire.h" #include "log.h" -bool wire_align(WireCtx *c, uint32_t alignment) -{ - if ((c->byte_cursor % alignment) == 0) { - return true; - } - uint32_t new_cursor = c->byte_cursor + alignment - (c->byte_cursor % alignment); - if (unlikely(new_cursor >= c->data_len || new_cursor < c->byte_cursor)) { - return false; - } - c->byte_cursor = new_cursor; - return true; -} - -bool wire_write_align(WireCtx *c, uint32_t alignment) -{ - if ((c->byte_cursor % alignment) == 0) { - return true; - } - uint32_t new_cursor = c->byte_cursor + alignment - (c->byte_cursor % alignment); - if (unlikely(new_cursor >= c->data_len || new_cursor < c->byte_cursor)) { - return false; - } - while (c->byte_cursor < new_cursor) { - c->data[c->byte_cursor++] = '\0'; - } - return true; -} - // SPEC: https://dbus.freedesktop.org/doc/dbus-specification.html#id-1.4.6 _WIRE_DEF_BYTE_TYPE(i8, int8_t) _WIRE_DEF_BYTE_TYPE(u8, uint8_t) diff --git a/wire.h b/wire.h index 696e192..6dc0e83 100644 --- a/wire.h +++ b/wire.h @@ -4,6 +4,7 @@ #include #include #include +#include "util.h" #define DBUS_PROTOCOL_VERSION 1 @@ -64,6 +65,32 @@ typedef struct { char *str; } WireMsgBodyString; +static inline bool wire_write_align(WireCtx *c, uint32_t alignment) +{ + // Thanks: https://github.com/c-util/c-stdaux/blob/ed5fee49a3ec13fe79f30324b23c73cd41805514/src/c-stdaux-gnuc.h#L431 + uint32_t new_cursor = ((c->byte_cursor) + (alignment) - 1) & ~((alignment) - 1); + + if (unlikely(new_cursor >= c->data_len || new_cursor < c->byte_cursor)) { + return false; + } + while (c->byte_cursor < new_cursor) { + c->data[c->byte_cursor++] = '\0'; + } + return true; +} + +static inline bool wire_align(WireCtx *c, uint32_t alignment) +{ + // Thanks: https://github.com/c-util/c-stdaux/blob/ed5fee49a3ec13fe79f30324b23c73cd41805514/src/c-stdaux-gnuc.h#L431 + uint32_t new_cursor = ((c->byte_cursor) + (alignment) - 1) & ~((alignment) - 1); + + if (unlikely(new_cursor >= c->data_len || new_cursor < c->byte_cursor)) { + return false; + } + c->byte_cursor = new_cursor; + return true; +} + #define _WIRE_DECL_BYTE_TYPE(M_mnemonic, M_type) \ M_type *wire_get_##M_mnemonic(WireCtx *c); \ M_type *wire_set_##M_mnemonic(WireCtx *c, M_type val); \