chiark / gitweb /
sd-bus: optimize how we generate the well-known-names lists in messages from kdbus
authorLennart Poettering <lennart@poettering.net>
Thu, 27 Nov 2014 01:28:23 +0000 (02:28 +0100)
committerLennart Poettering <lennart@poettering.net>
Thu, 27 Nov 2014 21:02:12 +0000 (22:02 +0100)
src/libsystemd/sd-bus/bus-creds.c
src/libsystemd/sd-bus/bus-kernel.c

index 8aa5336..ad0e2e1 100644 (file)
@@ -51,8 +51,14 @@ void bus_creds_done(sd_bus_creds *c) {
         free(c->slice);
         free(c->unescaped_description);
 
         free(c->slice);
         free(c->unescaped_description);
 
+        free(c->well_known_names); /* note that this is an strv, but
+                                    * we only free the array, not the
+                                    * strings the array points to. The
+                                    * full strv we only free if
+                                    * c->allocated is set, see
+                                    * below. */
+
         strv_free(c->cmdline_array);
         strv_free(c->cmdline_array);
-        strv_free(c->well_known_names);
 }
 
 _public_ sd_bus_creds *sd_bus_creds_ref(sd_bus_creds *c) {
 }
 
 _public_ sd_bus_creds *sd_bus_creds_ref(sd_bus_creds *c) {
@@ -83,8 +89,6 @@ _public_ sd_bus_creds *sd_bus_creds_unref(sd_bus_creds *c) {
                 c->n_ref--;
 
                 if (c->n_ref == 0) {
                 c->n_ref--;
 
                 if (c->n_ref == 0) {
-                        bus_creds_done(c);
-
                         free(c->comm);
                         free(c->tid_comm);
                         free(c->exe);
                         free(c->comm);
                         free(c->tid_comm);
                         free(c->exe);
@@ -96,6 +100,12 @@ _public_ sd_bus_creds *sd_bus_creds_unref(sd_bus_creds *c) {
                         free(c->cgroup_root);
                         free(c->description);
                         free(c->supplementary_gids);
                         free(c->cgroup_root);
                         free(c->description);
                         free(c->supplementary_gids);
+
+                        strv_free(c->well_known_names);
+                        c->well_known_names = NULL;
+
+                        bus_creds_done(c);
+
                         free(c);
                 }
         } else {
                         free(c);
                 }
         } else {
index ef157d6..1276cd8 100644 (file)
@@ -660,9 +660,23 @@ static int bus_kernel_make_message(sd_bus *bus, struct kdbus_msg *k) {
                         }
 
                         if (bus->creds_mask & SD_BUS_CREDS_WELL_KNOWN_NAMES) {
                         }
 
                         if (bus->creds_mask & SD_BUS_CREDS_WELL_KNOWN_NAMES) {
-                                r = strv_extend(&m->creds.well_known_names, d->name.name);
-                                if (r < 0)
+                                char **wkn;
+                                size_t n;
+
+                                /* We just extend the array here, but
+                                 * do not allocate the strings inside
+                                 * of it, instead we just point to our
+                                 * buffer directly. */
+                                n = strv_length(m->creds.well_known_names);
+                                wkn = realloc(m->creds.well_known_names, (n + 2) * sizeof(char*));
+                                if (!wkn) {
+                                        r = -ENOMEM;
                                         goto fail;
                                         goto fail;
+                                }
+
+                                wkn[n] = d->name.name;
+                                wkn[n+1] = NULL;
+                                m->creds.well_known_names = wkn;
 
                                 m->creds.mask |= SD_BUS_CREDS_WELL_KNOWN_NAMES;
                         }
 
                                 m->creds.mask |= SD_BUS_CREDS_WELL_KNOWN_NAMES;
                         }