From e36f6a7e52737aedafed6ba55b0cc224eca7ac52 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 8 Nov 2016 17:39:26 +0100 Subject: libnm: add nm_utils_uuid_generate_buf() util --- libnm-core/nm-core-internal.h | 8 ++++++++ libnm-core/nm-utils.c | 24 +++++++++++++++++------- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/libnm-core/nm-core-internal.h b/libnm-core/nm-core-internal.h index 8fba357167..1b4c469420 100644 --- a/libnm-core/nm-core-internal.h +++ b/libnm-core/nm-core-internal.h @@ -194,6 +194,14 @@ char *nm_utils_uuid_generate_from_string (const char *s, gssize slen, int uuid_t char *_nm_utils_uuid_generate_from_strings (const char *string1, ...) G_GNUC_NULL_TERMINATED; +char *nm_utils_uuid_generate_buf_ (char *buf); +#define nm_utils_uuid_generate_buf(buf) \ + ({ \ + G_STATIC_ASSERT (sizeof (buf) == G_N_ELEMENTS (buf) && sizeof (buf) >= 37); \ + nm_utils_uuid_generate_buf_ (buf); \ + }) +#define nm_utils_uuid_generate_a() (nm_utils_uuid_generate_buf_ (g_alloca (37))) + void _nm_dbus_errors_init (void); extern gboolean _nm_utils_is_manager_process; diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c index cfd97149ca..41b6304c82 100644 --- a/libnm-core/nm-utils.c +++ b/libnm-core/nm-utils.c @@ -2147,23 +2147,33 @@ next: /*****************************************************************************/ /** - * nm_utils_uuid_generate: + * nm_utils_uuid_generate_buf_: + * @buf: input buffer, must contain at least 37 bytes * - * Returns: a newly allocated UUID suitable for use as the #NMSettingConnection - * object's #NMSettingConnection:id: property. Should be freed with g_free() + * Returns: generates a new random UUID, writes it to @buf and returns @buf. **/ char * -nm_utils_uuid_generate (void) +nm_utils_uuid_generate_buf_ (char *buf) { uuid_t uuid; - char *buf; - buf = g_malloc0 (37); uuid_generate_random (uuid); - uuid_unparse_lower (uuid, &buf[0]); + uuid_unparse_lower (uuid, buf); return buf; } +/** + * nm_utils_uuid_generate: + * + * Returns: a newly allocated UUID suitable for use as the #NMSettingConnection + * object's #NMSettingConnection:id: property. Should be freed with g_free() + **/ +char * +nm_utils_uuid_generate (void) +{ + return nm_utils_uuid_generate_buf_ (g_malloc (37)); +} + /** * nm_utils_uuid_generate_from_string: * @s: a string to use as the seed for the UUID -- cgit v1.2.1