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)
This commit is contained in:
@@ -730,8 +730,7 @@ int ask_password_agent(
|
||||
if (keyname)
|
||||
(void) add_to_keyring_and_log(keyname, flags, l);
|
||||
|
||||
*ret = l;
|
||||
l = NULL;
|
||||
*ret = TAKE_PTR(l);
|
||||
r = 0;
|
||||
|
||||
finish:
|
||||
|
||||
@@ -1094,8 +1094,7 @@ static int map_basic(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_
|
||||
return r;
|
||||
|
||||
strv_free(*p);
|
||||
*p = l;
|
||||
l = NULL;
|
||||
*p = TAKE_PTR(l);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -189,8 +189,7 @@ int show_cgroup_by_path(
|
||||
free(last);
|
||||
}
|
||||
|
||||
last = k;
|
||||
k = NULL;
|
||||
last = TAKE_PTR(k);
|
||||
}
|
||||
|
||||
if (r < 0)
|
||||
|
||||
@@ -295,8 +295,7 @@ int dns_label_escape_new(const char *p, size_t l, char **ret) {
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
*ret = s;
|
||||
s = NULL;
|
||||
*ret = TAKE_PTR(s);
|
||||
|
||||
return r;
|
||||
}
|
||||
@@ -601,8 +600,7 @@ int dns_name_endswith(const char *name, const char *suffix) {
|
||||
|
||||
/* Not the same, let's jump back, and try with the next label again */
|
||||
s = suffix;
|
||||
n = saved_n;
|
||||
saved_n = NULL;
|
||||
n = TAKE_PTR(saved_n);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -249,8 +249,7 @@ int efi_get_variable(
|
||||
((char*) buf)[st.st_size - 4] = 0;
|
||||
((char*) buf)[st.st_size - 4 + 1] = 0;
|
||||
|
||||
*value = buf;
|
||||
buf = NULL;
|
||||
*value = TAKE_PTR(buf);
|
||||
*size = (size_t) st.st_size - 4;
|
||||
|
||||
if (attribute)
|
||||
@@ -563,8 +562,7 @@ int efi_get_boot_order(uint16_t **order) {
|
||||
l / sizeof(uint16_t) > INT_MAX)
|
||||
return -EINVAL;
|
||||
|
||||
*order = buf;
|
||||
buf = NULL;
|
||||
*order = TAKE_PTR(buf);
|
||||
return (int) (l / sizeof(uint16_t));
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -25,6 +25,7 @@
|
||||
|
||||
#include "sd-daemon.h"
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "dirent-util.h"
|
||||
#include "fd-util.h"
|
||||
#include "fdset.h"
|
||||
@@ -168,8 +169,7 @@ int fdset_new_fill(FDSet **_s) {
|
||||
}
|
||||
|
||||
r = 0;
|
||||
*_s = s;
|
||||
s = NULL;
|
||||
*_s = TAKE_PTR(s);
|
||||
|
||||
finish:
|
||||
/* We won't close the fds here! */
|
||||
|
||||
@@ -169,10 +169,8 @@ answer:
|
||||
|
||||
*filtered = f;
|
||||
}
|
||||
if (value) {
|
||||
*value = v;
|
||||
v = NULL;
|
||||
}
|
||||
if (value)
|
||||
*value = TAKE_PTR(v);
|
||||
|
||||
return !!n;
|
||||
}
|
||||
@@ -201,8 +199,7 @@ int fstab_extract_values(const char *opts, const char *name, char ***values) {
|
||||
return r;
|
||||
}
|
||||
|
||||
*values = res;
|
||||
res = NULL;
|
||||
*values = TAKE_PTR(res);
|
||||
|
||||
return !!*values;
|
||||
}
|
||||
|
||||
@@ -153,8 +153,7 @@ int raw_strip_suffixes(const char *p, char **ret) {
|
||||
break;
|
||||
}
|
||||
|
||||
*ret = q;
|
||||
q = NULL;
|
||||
*ret = TAKE_PTR(q);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -50,10 +50,8 @@ static int specifier_prefix_and_instance(char specifier, void *data, void *userd
|
||||
if (!ans)
|
||||
return -ENOMEM;
|
||||
*ret = ans;
|
||||
} else {
|
||||
*ret = prefix;
|
||||
prefix = NULL;
|
||||
}
|
||||
} else
|
||||
*ret = TAKE_PTR(prefix);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1422,8 +1422,7 @@ static int unit_file_search(
|
||||
r = unit_file_load_or_readlink(c, info, path, paths->root_dir, flags);
|
||||
|
||||
if (r >= 0) {
|
||||
info->path = path;
|
||||
path = NULL;
|
||||
info->path = TAKE_PTR(path);
|
||||
result = r;
|
||||
found_unit = true;
|
||||
break;
|
||||
@@ -1446,8 +1445,7 @@ static int unit_file_search(
|
||||
|
||||
r = unit_file_load_or_readlink(c, info, path, paths->root_dir, flags);
|
||||
if (r >= 0) {
|
||||
info->path = path;
|
||||
path = NULL;
|
||||
info->path = TAKE_PTR(path);
|
||||
result = r;
|
||||
found_unit = true;
|
||||
break;
|
||||
@@ -1754,8 +1752,7 @@ static int install_info_symlink_wants(
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
path = instance.path;
|
||||
instance.path = NULL;
|
||||
path = TAKE_PTR(instance.path);
|
||||
|
||||
if (instance.type == UNIT_FILE_TYPE_MASKED) {
|
||||
unit_file_changes_add(changes, n_changes, -ERFKILL, path, NULL);
|
||||
|
||||
@@ -80,7 +80,6 @@ Image *image_unref(Image *i) {
|
||||
|
||||
static char **image_settings_path(Image *image) {
|
||||
_cleanup_strv_free_ char **l = NULL;
|
||||
char **ret;
|
||||
const char *fn, *s;
|
||||
unsigned i = 0;
|
||||
|
||||
@@ -104,10 +103,7 @@ static char **image_settings_path(Image *image) {
|
||||
if (!l[i])
|
||||
return NULL;
|
||||
|
||||
ret = l;
|
||||
l = NULL;
|
||||
|
||||
return ret;
|
||||
return TAKE_PTR(l);
|
||||
}
|
||||
|
||||
static char *image_roothash_path(Image *image) {
|
||||
|
||||
@@ -114,8 +114,7 @@ int namespace_flag_to_string_many(unsigned long flags, char **ret) {
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
*ret = s;
|
||||
s = NULL;
|
||||
*ret = TAKE_PTR(s);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -181,7 +181,6 @@ static char** user_dirs(
|
||||
_cleanup_strv_free_ char **config_dirs = NULL, **data_dirs = NULL;
|
||||
_cleanup_free_ char *data_home = NULL;
|
||||
_cleanup_strv_free_ char **res = NULL;
|
||||
char **tmp;
|
||||
int r;
|
||||
|
||||
r = xdg_user_dirs(&config_dirs, &data_dirs);
|
||||
@@ -242,10 +241,7 @@ static char** user_dirs(
|
||||
if (path_strv_make_absolute_cwd(res) < 0)
|
||||
return NULL;
|
||||
|
||||
tmp = res;
|
||||
res = NULL;
|
||||
|
||||
return tmp;
|
||||
return TAKE_PTR(res);
|
||||
}
|
||||
|
||||
bool path_is_user_data_dir(const char *path) {
|
||||
@@ -374,8 +370,7 @@ static int acquire_config_dirs(UnitFileScope scope, char **persistent, char **ru
|
||||
*runtime = NULL;
|
||||
}
|
||||
|
||||
*persistent = a;
|
||||
a = NULL;
|
||||
*persistent = TAKE_PTR(a);
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -413,8 +408,7 @@ static int acquire_control_dirs(UnitFileScope scope, char **persistent, char **r
|
||||
if (!b)
|
||||
return -ENOMEM;
|
||||
|
||||
*runtime = b;
|
||||
b = NULL;
|
||||
*runtime = TAKE_PTR(b);
|
||||
|
||||
break;
|
||||
}
|
||||
@@ -443,8 +437,7 @@ static int acquire_control_dirs(UnitFileScope scope, char **persistent, char **r
|
||||
assert_not_reached("Hmm, unexpected scope value.");
|
||||
}
|
||||
|
||||
*persistent = a;
|
||||
a = NULL;
|
||||
*persistent = TAKE_PTR(a);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -117,8 +117,7 @@ int specifier_printf(const char *text, const Specifier table[], void *userdata,
|
||||
*(t++) = '%';
|
||||
|
||||
*t = 0;
|
||||
*_ret = ret;
|
||||
ret = NULL;
|
||||
*_ret = TAKE_PTR(ret);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user