improve alignment algorithm

This commit is contained in:
hippoz 2023-01-19 05:16:45 +02:00
parent 98c7d8b648
commit d2ee4cb71d
Signed by: hippoz
GPG key ID: 56C4E02A85F2FBED
3 changed files with 29 additions and 30 deletions

View file

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

28
wire.c
View file

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

27
wire.h
View file

@ -4,6 +4,7 @@
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#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); \