shared: make NM_STR_BUF_INIT() an inline function

In the previous form, NM_STR_BUF_INIT() was a macro. That makes sense,
however it's not really possible to make that a macro without evaluating
the reservation length multiple times. That means,

    NMStrBuf strbuf = NM_STR_BUF_INIT (nmtst_get_rand_uint32 () % 100, FALSE);

leads to a crash. That is unfortunate, so instead make it an inline
function that returns a NMStrBut struct. Usually, we avoid functions
that returns structs, but here we do it.
This commit is contained in:
Thomas Haller 2020-06-22 21:19:42 +02:00
parent 8a13b02d96
commit c6809df4cd
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728

View file

@ -39,13 +39,18 @@ _nm_str_buf_assert (NMStrBuf *strbuf)
nm_assert (strbuf->_priv_len <= strbuf->_priv_allocated);
}
#define NM_STR_BUF_INIT(len, do_bzero_mem) \
((NMStrBuf) { \
._priv_str = (len) ? g_malloc (len) : NULL, \
._priv_allocated = (len), \
._priv_len = 0, \
._priv_do_bzero_mem = (do_bzero_mem), \
})
static inline NMStrBuf
NM_STR_BUF_INIT (gsize allocated, gboolean do_bzero_mem)
{
NMStrBuf strbuf = {
._priv_str = allocated ? g_malloc (allocated) : NULL,
._priv_allocated = allocated,
._priv_len = 0,
._priv_do_bzero_mem = do_bzero_mem,
};
return strbuf;
}
static inline void
nm_str_buf_init (NMStrBuf *strbuf,