[systemd-devel] [PATCH] libsystemd-bus: trivial macro KDBUS_PART_HEADER_SIZE replace

Lennart Poettering lennart at poettering.net
Tue Dec 10 11:56:20 PST 2013


On Wed, 04.12.13 15:28, Yin Kangkai (kangkai.yin at intel.com) wrote:

> It's a little bit cleaner(?) to replace offsetof(struct kdbus_item, data) with
> KDBUS_PART_HEADER_SIZE.

Hmm, is this really cleaner? I kinda like using offsetof() for this,
since it explains less opaquely what we are putting together here I
think...

> ---
>  src/libsystemd-bus/bus-control.c | 16 ++++++++--------
>  src/libsystemd-bus/bus-kernel.c  | 41 ++++++++++++++++++++--------------------
>  src/libsystemd-bus/bus-kernel.h  |  2 +-
>  3 files changed, 29 insertions(+), 30 deletions(-)
> 
> diff --git a/src/libsystemd-bus/bus-control.c b/src/libsystemd-bus/bus-control.c
> index 7a772ff..073e7aa 100644
> --- a/src/libsystemd-bus/bus-control.c
> +++ b/src/libsystemd-bus/bus-control.c
> @@ -765,7 +765,7 @@ static int add_name_change_match(sd_bus *bus,
>                  l = name ? strlen(name) : 0;
>  
>                  sz = ALIGN8(offsetof(struct kdbus_cmd_match, items) +
> -                            offsetof(struct kdbus_item, name_change) +
> +                            KDBUS_PART_HEADER_SIZE +
>                              offsetof(struct kdbus_notify_name_change, name) +
>                              l+1);
>  
> @@ -776,7 +776,7 @@ static int add_name_change_match(sd_bus *bus,
>  
>                  item = m->items;
>                  item->size =
> -                        offsetof(struct kdbus_item, name_change) +
> +                        KDBUS_PART_HEADER_SIZE +
>                          offsetof(struct kdbus_notify_name_change, name) +
>                          l+1;
>  
> @@ -828,7 +828,7 @@ static int add_name_change_match(sd_bus *bus,
>                   * for it */
>  
>                  sz = ALIGN8(offsetof(struct kdbus_cmd_match, items) +
> -                            offsetof(struct kdbus_item, id_change) +
> +                            KDBUS_PART_HEADER_SIZE +
>                              sizeof(struct kdbus_notify_id_change));
>  
>                  m = alloca0(sz);
> @@ -837,7 +837,7 @@ static int add_name_change_match(sd_bus *bus,
>                  m->src_id = KDBUS_SRC_ID_KERNEL;
>  
>                  item = m->items;
> -                item->size = offsetof(struct kdbus_item, id_change) + sizeof(struct kdbus_notify_id_change);
> +                item->size = KDBUS_PART_HEADER_SIZE + sizeof(struct kdbus_notify_id_change);
>                  item->id_change.id = name_id;
>  
>                  /* If the old name is unset or empty, then this can
> @@ -907,7 +907,7 @@ static int bus_add_match_internal_kernel(
>                          if (r > 0) {
>                                  sender = c->value_str;
>                                  sender_length = strlen(sender);
> -                                sz += ALIGN8(offsetof(struct kdbus_item, str) + sender_length + 1);
> +                                sz += KDBUS_PART_SIZE(sender_length + 1);
>                          }
>  
>                          break;
> @@ -999,7 +999,7 @@ static int bus_add_match_internal_kernel(
>          }
>  
>          if (using_bloom)
> -                sz += ALIGN8(offsetof(struct kdbus_item, data64) + BLOOM_SIZE);
> +                sz += KDBUS_PART_SIZE(BLOOM_SIZE);
>  
>          m = alloca0(sz);
>          m->size = sz;
> @@ -1009,7 +1009,7 @@ static int bus_add_match_internal_kernel(
>          item = m->items;
>  
>          if (using_bloom) {
> -                item->size = offsetof(struct kdbus_item, data64) + BLOOM_SIZE;
> +                item->size = KDBUS_PART_HEADER_SIZE + BLOOM_SIZE;
>                  item->type = KDBUS_MATCH_BLOOM;
>                  memcpy(item->data64, bloom, BLOOM_SIZE);
>  
> @@ -1017,7 +1017,7 @@ static int bus_add_match_internal_kernel(
>          }
>  
>          if (sender) {
> -                item->size = offsetof(struct kdbus_item, str) + sender_length + 1;
> +                item->size = KDBUS_PART_HEADER_SIZE + sender_length + 1;
>                  item->type = KDBUS_MATCH_SRC_NAME;
>                  memcpy(item->str, sender, sender_length + 1);
>          }
> diff --git a/src/libsystemd-bus/bus-kernel.c b/src/libsystemd-bus/bus-kernel.c
> index b85a10d..56259f5 100644
> --- a/src/libsystemd-bus/bus-kernel.c
> +++ b/src/libsystemd-bus/bus-kernel.c
> @@ -63,7 +63,7 @@ static void append_payload_vec(struct kdbus_item **d, const void *p, size_t sz)
>           * zeroes, which is useful to optimize certain padding
>           * conditions */
>  
> -        (*d)->size = offsetof(struct kdbus_item, vec) + sizeof(struct kdbus_vec);
> +        (*d)->size = KDBUS_PART_HEADER_SIZE + sizeof(struct kdbus_vec);
>          (*d)->type = KDBUS_ITEM_PAYLOAD_VEC;
>          (*d)->vec.address = PTR_TO_UINT64(p);
>          (*d)->vec.size = sz;
> @@ -77,7 +77,7 @@ static void append_payload_memfd(struct kdbus_item **d, int memfd, size_t sz) {
>          assert(sz > 0);
>  
>          *d = ALIGN8_PTR(*d);
> -        (*d)->size = offsetof(struct kdbus_item, memfd) + sizeof(struct kdbus_memfd);
> +        (*d)->size = KDBUS_PART_HEADER_SIZE + sizeof(struct kdbus_memfd);
>          (*d)->type = KDBUS_ITEM_PAYLOAD_MEMFD;
>          (*d)->memfd.fd = memfd;
>          (*d)->memfd.size = sz;
> @@ -91,7 +91,7 @@ static void append_destination(struct kdbus_item **d, const char *s, size_t leng
>  
>          *d = ALIGN8_PTR(*d);
>  
> -        (*d)->size = offsetof(struct kdbus_item, str) + length + 1;
> +        (*d)->size = KDBUS_PART_HEADER_SIZE + length + 1;
>          (*d)->type = KDBUS_ITEM_DST_NAME;
>          memcpy((*d)->str, s, length + 1);
>  
> @@ -105,7 +105,7 @@ static void* append_bloom(struct kdbus_item **d, size_t length) {
>  
>          *d = ALIGN8_PTR(*d);
>  
> -        (*d)->size = offsetof(struct kdbus_item, data) + length;
> +        (*d)->size = KDBUS_PART_HEADER_SIZE + length;
>          (*d)->type = KDBUS_ITEM_BLOOM;
>          r = (*d)->data;
>  
> @@ -120,7 +120,7 @@ static void append_fds(struct kdbus_item **d, const int fds[], unsigned n_fds) {
>          assert(n_fds > 0);
>  
>          *d = ALIGN8_PTR(*d);
> -        (*d)->size = offsetof(struct kdbus_item, fds) + sizeof(int) * n_fds;
> +        (*d)->size = KDBUS_PART_HEADER_SIZE + sizeof(int) * n_fds;
>          (*d)->type = KDBUS_ITEM_FDS;
>          memcpy((*d)->fds, fds, sizeof(int) * n_fds);
>  
> @@ -218,25 +218,24 @@ static int bus_message_setup_kmsg(sd_bus *b, sd_bus_message *m) {
>  
>          sz = offsetof(struct kdbus_msg, items);
>  
> -        assert_cc(ALIGN8(offsetof(struct kdbus_item, vec) + sizeof(struct kdbus_vec)) ==
> -                  ALIGN8(offsetof(struct kdbus_item, memfd) + sizeof(struct kdbus_memfd)));
> +        assert_cc(KDBUS_PART_SIZE(sizeof(struct kdbus_vec)) ==
> +                  KDBUS_PART_SIZE(sizeof(struct kdbus_memfd)));
>  
>          /* Add in fixed header, fields header and payload */
> -        sz += (1 + m->n_body_parts) *
> -                ALIGN8(offsetof(struct kdbus_item, vec) + sizeof(struct kdbus_vec));
> +        sz += (1 + m->n_body_parts) * KDBUS_PART_SIZE(sizeof(struct kdbus_vec));
>  
>          /* Add space for bloom filter */
> -        sz += ALIGN8(offsetof(struct kdbus_item, data) + BLOOM_SIZE);
> +        sz += KDBUS_PART_SIZE(BLOOM_SIZE);
>  
>          /* Add in well-known destination header */
>          if (well_known) {
>                  dl = strlen(m->destination);
> -                sz += ALIGN8(offsetof(struct kdbus_item, str) + dl + 1);
> +                sz += KDBUS_PART_SIZE(dl + 1);
>          }
>  
>          /* Add space for unix fds */
>          if (m->n_fds > 0)
> -                sz += ALIGN8(offsetof(struct kdbus_item, fds) + sizeof(int)*m->n_fds);
> +                sz += KDBUS_PART_SIZE(sizeof(int) * m->n_fds);
>  
>          m->kdbus = memalign(8, sz);
>          if (!m->kdbus) {
> @@ -464,7 +463,7 @@ static void close_kdbus_msg(sd_bus *bus, struct kdbus_msg *k) {
>          KDBUS_PART_FOREACH(d, k, items) {
>  
>                  if (d->type == KDBUS_ITEM_FDS)
> -                        close_many(d->fds, (d->size - offsetof(struct kdbus_item, fds)) / sizeof(int));
> +                        close_many(d->fds, (d->size - KDBUS_PART_HEADER_SIZE) / sizeof(int));
>                  else if (d->type == KDBUS_ITEM_PAYLOAD_MEMFD)
>                          close_nointr_nofail(d->memfd.fd);
>          }
> @@ -622,7 +621,7 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k) {
>          KDBUS_PART_FOREACH(d, k, items) {
>                  size_t l;
>  
> -                l = d->size - offsetof(struct kdbus_item, data);
> +                l = d->size - KDBUS_PART_HEADER_SIZE;
>  
>                  switch (d->type) {
>  
> @@ -682,7 +681,7 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k) {
>          KDBUS_PART_FOREACH(d, k, items) {
>                  size_t l;
>  
> -                l = d->size - offsetof(struct kdbus_item, data);
> +                l = d->size - KDBUS_PART_HEADER_SIZE;
>  
>                  switch (d->type) {
>  
> @@ -1055,12 +1054,12 @@ int bus_kernel_create_bus(const char *name, char **s) {
>                  return -errno;
>  
>          make = alloca0(ALIGN8(offsetof(struct kdbus_cmd_bus_make, items) +
> -                              offsetof(struct kdbus_item, str) +
> +                              KDBUS_PART_HEADER_SIZE +
>                                DECIMAL_STR_MAX(uid_t) + 1 + strlen(name) + 1));
>  
>          n = make->items;
>          sprintf(n->str, "%lu-%s", (unsigned long) getuid(), name);
> -        n->size = offsetof(struct kdbus_item, str) + strlen(n->str) + 1;
> +        n->size = KDBUS_PART_HEADER_SIZE + strlen(n->str) + 1;
>          n->type = KDBUS_MAKE_NAME;
>  
>          make->size = ALIGN8(offsetof(struct kdbus_cmd_bus_make, items) + n->size);
> @@ -1113,12 +1112,12 @@ int bus_kernel_create_starter(const char *bus, const char *name) {
>                  return -errno;
>  
>          hello = alloca0(ALIGN8(offsetof(struct kdbus_cmd_hello, items) +
> -                               offsetof(struct kdbus_item, str) +
> +                               KDBUS_PART_HEADER_SIZE +
>                                 strlen(name) + 1));
>  
>          n = hello->items;
>          strcpy(n->str, name);
> -        n->size = offsetof(struct kdbus_item, str) + strlen(n->str) + 1;
> +        n->size = KDBUS_PART_HEADER_SIZE + strlen(n->str) + 1;
>          n->type = KDBUS_ITEM_STARTER_NAME;
>  
>          hello->size = ALIGN8(offsetof(struct kdbus_cmd_hello, items) + n->size);
> @@ -1159,12 +1158,12 @@ int bus_kernel_create_namespace(const char *name, char **s) {
>                  return -errno;
>  
>          make = alloca0(ALIGN8(offsetof(struct kdbus_cmd_ns_make, items) +
> -                              offsetof(struct kdbus_item, str) +
> +                              KDBUS_PART_HEADER_SIZE +
>                                strlen(name) + 1));
>  
>          n = make->items;
>          strcpy(n->str, name);
> -        n->size = offsetof(struct kdbus_item, str) + strlen(n->str) + 1;
> +        n->size = KDBUS_PART_HEADER_SIZE + strlen(n->str) + 1;
>          n->type = KDBUS_MAKE_NAME;
>  
>          make->size = ALIGN8(offsetof(struct kdbus_cmd_ns_make, items) + n->size);
> diff --git a/src/libsystemd-bus/bus-kernel.h b/src/libsystemd-bus/bus-kernel.h
> index a9dddc2..ff323c3 100644
> --- a/src/libsystemd-bus/bus-kernel.h
> +++ b/src/libsystemd-bus/bus-kernel.h
> @@ -32,7 +32,7 @@
>               part = KDBUS_PART_NEXT(part))
>  
>  #define KDBUS_PART_HEADER_SIZE offsetof(struct kdbus_item, data)
> -#define KDBUS_ITEM_SIZE(s) ALIGN8((s) + KDBUS_PART_HEADER_SIZE)
> +#define KDBUS_PART_SIZE(s) ALIGN8((s) + KDBUS_PART_HEADER_SIZE)
>  
>  #define MEMFD_CACHE_MAX 32
>  


Lennart

-- 
Lennart Poettering, Red Hat


More information about the systemd-devel mailing list