summaryrefslogtreecommitdiff
path: root/src/basic
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2018-03-22 16:53:26 +0100
committerLennart Poettering <lennart@poettering.net>2018-03-22 20:21:42 +0100
commitae2a15bc14bc448e625ad93fd044bc077ede4b3f (patch)
treee503e6cf3b571d0a150dc2cea7d1838f55aaa6ab /src/basic
parent1147eef0b6a5937526247ff81ca1e5e45205ed16 (diff)
downloadsystemd-ae2a15bc14bc448e625ad93fd044bc077ede4b3f.tar.gz
macro: introduce TAKE_PTR() macro
This macro will read a pointer of any type, return it, and set the pointer to NULL. This is useful as an explicit concept of passing ownership of a memory area between pointers. This takes inspiration from Rust: https://doc.rust-lang.org/std/option/enum.Option.html#method.take and was suggested by Alan Jenkins (@sourcejedi). It drops ~160 lines of code from our codebase, which makes me like it. Also, I think it clarifies passing of ownership, and thus helps readability a bit (at least for the initiated who know the new macro)
Diffstat (limited to 'src/basic')
-rw-r--r--src/basic/alloc-util.h9
-rw-r--r--src/basic/cap-list.c3
-rw-r--r--src/basic/cgroup-util.c13
-rw-r--r--src/basic/extract-word.c3
-rw-r--r--src/basic/fileio.c9
-rw-r--r--src/basic/fs-util.c19
-rw-r--r--src/basic/locale-util.c3
-rw-r--r--src/basic/mount-util.c9
-rw-r--r--src/basic/parse-util.c3
-rw-r--r--src/basic/path-util.c3
-rw-r--r--src/basic/proc-cmdline.c6
-rw-r--r--src/basic/process-util.c3
-rw-r--r--src/basic/securebits-util.c3
-rw-r--r--src/basic/socket-util.c3
-rw-r--r--src/basic/strv.c6
-rw-r--r--src/basic/terminal-util.c13
-rw-r--r--src/basic/unit-name.c3
17 files changed, 45 insertions, 66 deletions
diff --git a/src/basic/alloc-util.h b/src/basic/alloc-util.h
index ec7808c1f7..b1e0edbb7f 100644
--- a/src/basic/alloc-util.h
+++ b/src/basic/alloc-util.h
@@ -130,3 +130,12 @@ void* greedy_realloc0(void **p, size_t *allocated, size_t need, size_t size);
_new_ = alloca_align(_size_, (align)); \
(void*)memset(_new_, 0, _size_); \
})
+
+/* Takes inspiration from Rusts's Option::take() method: reads and returns a pointer, but at the same time resets it to
+ * NULL. See: https://doc.rust-lang.org/std/option/enum.Option.html#method.take */
+#define TAKE_PTR(ptr) \
+ ({ \
+ typeof(ptr) _ptr_ = (ptr); \
+ (ptr) = NULL; \
+ _ptr_; \
+ })
diff --git a/src/basic/cap-list.c b/src/basic/cap-list.c
index c4557666ef..9416391a53 100644
--- a/src/basic/cap-list.c
+++ b/src/basic/cap-list.c
@@ -103,8 +103,7 @@ int capability_set_to_string_alloc(uint64_t set, char **s) {
str[n > 0 ? n - 1 : 0] = '\0'; /* truncate the last space, if it's there */
- *s = str;
- str = NULL;
+ *s = TAKE_PTR(str);
return 0;
}
diff --git a/src/basic/cgroup-util.c b/src/basic/cgroup-util.c
index 1a2d7fc087..5934ee6e8a 100644
--- a/src/basic/cgroup-util.c
+++ b/src/basic/cgroup-util.c
@@ -1424,10 +1424,9 @@ int cg_pid_get_path_shifted(pid_t pid, const char *root, char **cgroup) {
if (r < 0)
return r;
- if (c == raw) {
- *cgroup = raw;
- raw = NULL;
- } else {
+ if (c == raw)
+ *cgroup = TAKE_PTR(raw);
+ else {
char *n;
n = strdup(c);
@@ -2010,8 +2009,7 @@ int cg_slice_to_path(const char *unit, char **ret) {
if (!strextend(&s, e, NULL))
return -ENOMEM;
- *ret = s;
- s = NULL;
+ *ret = TAKE_PTR(s);
return 0;
}
@@ -2301,8 +2299,7 @@ int cg_mask_to_string(CGroupMask mask, char **ret) {
assert(s);
s[n] = 0;
- *ret = s;
- s = NULL;
+ *ret = TAKE_PTR(s);
return 0;
}
diff --git a/src/basic/extract-word.c b/src/basic/extract-word.c
index 5e42560487..7a6b56f071 100644
--- a/src/basic/extract-word.c
+++ b/src/basic/extract-word.c
@@ -194,8 +194,7 @@ finish:
finish_force_next:
s[sz] = 0;
- *ret = s;
- s = NULL;
+ *ret = TAKE_PTR(s);
return 1;
}
diff --git a/src/basic/fileio.c b/src/basic/fileio.c
index fb26274afa..f807842c31 100644
--- a/src/basic/fileio.c
+++ b/src/basic/fileio.c
@@ -330,8 +330,7 @@ int read_full_stream(FILE *f, char **contents, size_t *size) {
}
buf[l] = 0;
- *contents = buf;
- buf = NULL; /* do not free */
+ *contents = TAKE_PTR(buf);
if (size)
*size = l;
@@ -1432,8 +1431,7 @@ int open_tmpfile_linkable(const char *target, int flags, char **ret_path) {
if (fd < 0)
return -errno;
- *ret_path = tmp;
- tmp = NULL;
+ *ret_path = TAKE_PTR(tmp);
return fd;
}
@@ -1519,8 +1517,7 @@ int read_nul_string(FILE *f, char **ret) {
return -ENOMEM;
}
- *ret = x;
- x = NULL;
+ *ret = TAKE_PTR(x);
return 0;
}
diff --git a/src/basic/fs-util.c b/src/basic/fs-util.c
index c65ba4bfe5..aec8b00744 100644
--- a/src/basic/fs-util.c
+++ b/src/basic/fs-util.c
@@ -458,10 +458,8 @@ int get_files_in_directory(const char *path, char ***list) {
n++;
}
- if (list) {
- *list = l;
- l = NULL; /* avoid freeing */
- }
+ if (list)
+ *list = TAKE_PTR(l);
return n;
}
@@ -838,10 +836,9 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags,
}
/* If this is not a symlink, then let's just add the name we read to what we already verified. */
- if (!done) {
- done = first;
- first = NULL;
- } else {
+ if (!done)
+ done = TAKE_PTR(first);
+ else {
/* If done is "/", as first also contains slash at the head, then remove this redundant slash. */
if (streq(done, "/"))
*done = '\0';
@@ -863,10 +860,8 @@ int chase_symlinks(const char *path, const char *original_root, unsigned flags,
return -ENOMEM;
}
- if (ret) {
- *ret = done;
- done = NULL;
- }
+ if (ret)
+ *ret = TAKE_PTR(done);
if (flags & CHASE_OPEN) {
int q;
diff --git a/src/basic/locale-util.c b/src/basic/locale-util.c
index 266cb29936..de3d7c8c89 100644
--- a/src/basic/locale-util.c
+++ b/src/basic/locale-util.c
@@ -341,8 +341,7 @@ int get_keymaps(char ***ret) {
strv_sort(l);
- *ret = l;
- l = NULL;
+ *ret = TAKE_PTR(l);
return 0;
}
diff --git a/src/basic/mount-util.c b/src/basic/mount-util.c
index 8151b3a4e4..b6e7c94017 100644
--- a/src/basic/mount-util.c
+++ b/src/basic/mount-util.c
@@ -81,10 +81,8 @@ int name_to_handle_at_loop(
if (name_to_handle_at(fd, path, h, &mnt_id, flags) >= 0) {
- if (ret_handle) {
- *ret_handle = h;
- h = NULL;
- }
+ if (ret_handle)
+ *ret_handle = TAKE_PTR(h);
if (ret_mnt_id)
*ret_mnt_id = mnt_id;
@@ -951,8 +949,7 @@ int mount_option_mangle(
}
*ret_mount_flags = mount_flags;
- *ret_remaining_options = ret;
- ret = NULL;
+ *ret_remaining_options = TAKE_PTR(ret);
return 0;
}
diff --git a/src/basic/parse-util.c b/src/basic/parse-util.c
index fa5b4a353a..8d4912fdbf 100644
--- a/src/basic/parse-util.c
+++ b/src/basic/parse-util.c
@@ -324,8 +324,7 @@ int parse_syscall_and_errno(const char *in, char **name, int *error) {
return -EINVAL;
*error = e;
- *name = n;
- n = NULL;
+ *name = TAKE_PTR(n);
return 0;
}
diff --git a/src/basic/path-util.c b/src/basic/path-util.c
index df94629385..d4c4a02cb3 100644
--- a/src/basic/path-util.c
+++ b/src/basic/path-util.c
@@ -290,8 +290,7 @@ char **path_strv_resolve(char **l, const char *root) {
r = chase_symlinks(t, root, 0, &u);
if (r == -ENOENT) {
if (root) {
- u = orig;
- orig = NULL;
+ u = TAKE_PTR(orig);
free(t);
} else
u = t;
diff --git a/src/basic/proc-cmdline.c b/src/basic/proc-cmdline.c
index c5d1fb1d41..c51e3c0a3b 100644
--- a/src/basic/proc-cmdline.c
+++ b/src/basic/proc-cmdline.c
@@ -204,10 +204,8 @@ int proc_cmdline_get_key(const char *key, unsigned flags, char **value) {
}
}
- if (value) {
- *value = ret;
- ret = NULL;
- }
+ if (value)
+ *value = TAKE_PTR(ret);
return found;
}
diff --git a/src/basic/process-util.c b/src/basic/process-util.c
index 853e0e3449..2583b310a4 100644
--- a/src/basic/process-util.c
+++ b/src/basic/process-util.c
@@ -623,8 +623,7 @@ int get_process_environ(pid_t pid, char **env) {
} else
outcome[sz] = '\0';
- *env = outcome;
- outcome = NULL;
+ *env = TAKE_PTR(outcome);
return 0;
}
diff --git a/src/basic/securebits-util.c b/src/basic/securebits-util.c
index 441d386f9e..75675bf4c0 100644
--- a/src/basic/securebits-util.c
+++ b/src/basic/securebits-util.c
@@ -48,8 +48,7 @@ int secure_bits_to_string_alloc(int i, char **s) {
if (len != 0)
str[len - 1] = '\0';
- *s = str;
- str = NULL;
+ *s = TAKE_PTR(str);
return 0;
}
diff --git a/src/basic/socket-util.c b/src/basic/socket-util.c
index b91b093132..fd26ae7137 100644
--- a/src/basic/socket-util.c
+++ b/src/basic/socket-util.c
@@ -985,8 +985,7 @@ int getpeersec(int fd, char **ret) {
if (isempty(s))
return -EOPNOTSUPP;
- *ret = s;
- s = NULL;
+ *ret = TAKE_PTR(s);
return 0;
}
diff --git a/src/basic/strv.c b/src/basic/strv.c
index e80ff4a62c..8010016451 100644
--- a/src/basic/strv.c
+++ b/src/basic/strv.c
@@ -341,8 +341,7 @@ int strv_split_extract(char ***t, const char *s, const char *separators, Extract
if (!GREEDY_REALLOC(l, allocated, n + 2))
return -ENOMEM;
- l[n++] = word;
- word = NULL;
+ l[n++] = TAKE_PTR(word);
l[n] = NULL;
}
@@ -353,8 +352,7 @@ int strv_split_extract(char ***t, const char *s, const char *separators, Extract
return -ENOMEM;
}
- *t = l;
- l = NULL;
+ *t = TAKE_PTR(l);
return (int) n;
}
diff --git a/src/basic/terminal-util.c b/src/basic/terminal-util.c
index eacfd14677..87691d1d38 100644
--- a/src/basic/terminal-util.c
+++ b/src/basic/terminal-util.c
@@ -707,10 +707,9 @@ int vtnr_from_tty(const char *tty) {
tty = active;
}
- if (tty == active) {
- *ret = active;
- active = NULL;
- } else {
+ if (tty == active)
+ *ret = TAKE_PTR(active);
+ else {
char *tmp;
tmp = strdup(tty);
@@ -778,8 +777,7 @@ int get_kernel_consoles(char ***ret) {
goto fallback;
}
- *ret = l;
- l = NULL;
+ *ret = TAKE_PTR(l);
return 0;
@@ -788,8 +786,7 @@ fallback:
if (r < 0)
return r;
- *ret = l;
- l = NULL;
+ *ret = TAKE_PTR(l);
return 0;
}
diff --git a/src/basic/unit-name.c b/src/basic/unit-name.c
index 3937dfc5ee..7d9367334b 100644
--- a/src/basic/unit-name.c
+++ b/src/basic/unit-name.c
@@ -363,8 +363,7 @@ int unit_name_unescape(const char *f, char **ret) {
*t = 0;
- *ret = r;
- r = NULL;
+ *ret = TAKE_PTR(r);
return 0;
}