glib-aux/tests: embed testpath in NmtstTestData struct

Only allocate one chunk of memory to contain all data of
NmtstTestData.

This isn't about performance (which doesn't matter for test code).
It's about packing all in one struct and being able to free all at
once with a simple g_free(). We no longer need _nmtst_test_data_free()
with this.

Note that NmtstTestData is never mutated, it just holds some data.
As such, the single place where such a structure gets initialized,
can become a bit more complicated, in exchange for having a trivial
free operation (and anyway there no functions that modify the data
or that would care about the data layout).
This commit is contained in:
Thomas Haller 2023-01-04 08:27:49 +01:00
parent e4104a9f12
commit d0dff07687
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728

View file

@ -778,10 +778,7 @@ typedef struct _NmtstTestData NmtstTestData;
typedef void (*NmtstTestHandler)(const NmtstTestData *test_data);
struct _NmtstTestData {
union {
const char *testpath;
char *_testpath;
};
const char *testpath;
gsize n_args;
NmtstTestHandler _func_setup;
GTestDataFunc _func_test;
@ -827,17 +824,6 @@ nmtst_test_get_path(void)
return __nmtst_internal.testpath;
}
static inline void
_nmtst_test_data_free(gpointer data)
{
NmtstTestData *test_data = data;
g_assert(test_data);
g_free(test_data->_testpath);
g_free(test_data);
}
static inline void
_nmtst_test_run(gconstpointer data)
{
@ -874,14 +860,18 @@ _nmtst_add_test_func_full(const char *testpath,
gsize i;
NmtstTestData *data;
va_list ap;
gsize testpath_len;
g_assert(testpath && testpath[0]);
g_assert(func_test);
data = g_malloc(G_STRUCT_OFFSET(NmtstTestData, args) + (sizeof(gpointer) * (n_args + 1u)));
testpath_len = strlen(testpath) + 1u;
data = g_malloc(G_STRUCT_OFFSET(NmtstTestData, args)
+ (sizeof(gpointer) * (n_args + 1u) + testpath_len));
*data = (NmtstTestData){
._testpath = g_strdup(testpath),
.testpath = (gpointer) &data->args[n_args + 1u],
._func_test = func_test,
._func_setup = func_setup,
._func_teardown = func_teardown,
@ -894,8 +884,13 @@ _nmtst_add_test_func_full(const char *testpath,
data->args[i] = NULL;
va_end(ap);
g_test_add_data_func_full(testpath, data, _nmtst_test_run, _nmtst_test_data_free);
g_assert(data->testpath == (gpointer) &data->args[i + 1]);
memcpy((char *) data->testpath, testpath, testpath_len);
g_test_add_data_func_full(testpath, data, _nmtst_test_run, g_free);
}
#define nmtst_add_test_func_full(testpath, func_test, func_setup, func_teardown, ...) \
_nmtst_add_test_func_full(testpath, \
func_test, \
@ -903,6 +898,7 @@ _nmtst_add_test_func_full(const char *testpath,
func_teardown, \
NM_NARG(__VA_ARGS__), \
##__VA_ARGS__)
#define nmtst_add_test_func(testpath, func_test, ...) \
nmtst_add_test_func_full(testpath, func_test, NULL, NULL, ##__VA_ARGS__)