Skip to content

Commit

Permalink
[glibutil] Added gutil_ptrv_free()
Browse files Browse the repository at this point in the history
  • Loading branch information
monich committed Feb 25, 2021
1 parent 2664835 commit 3a5616b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
10 changes: 7 additions & 3 deletions include/gutil_misc.h
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2016-2020 Jolla Ltd.
* Copyright (C) 2016-2020 Slava Monich <slava.monich@jolla.com>
* Copyright (C) 2016-2021 Jolla Ltd.
* Copyright (C) 2016-2021 Slava Monich <slava.monich@jolla.com>
*
* You may use this file under the terms of BSD license as follows:
*
Expand Down Expand Up @@ -119,7 +119,11 @@ gutil_bytes_equal_data(

gsize
gutil_ptrv_length(
gconstpointer ptrv); /* Since 1.0.50 */
const void* ptrv); /* Since 1.0.50 */

void
gutil_ptrv_free(
void** ptrv); /* Since 1.0.51 */

G_END_DECLS

Expand Down
19 changes: 16 additions & 3 deletions src/gutil_misc.c
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2016-2020 Jolla Ltd.
* Copyright (C) 2016-2020 Slava Monich <slava.monich@jolla.com>
* Copyright (C) 2016-2021 Jolla Ltd.
* Copyright (C) 2016-2021 Slava Monich <slava.monich@jolla.com>
*
* You may use this file under the terms of BSD license as follows:
*
Expand Down Expand Up @@ -418,7 +418,7 @@ gutil_bytes_equal_data(
/* Calculates the length of NULL-terminated array of pointers */
gsize
gutil_ptrv_length(
gconstpointer ptrv) /* Since 1.0.50 */
const void* ptrv) /* Since 1.0.50 */
{
if (G_LIKELY(ptrv)) {
gsize len = 0;
Expand All @@ -431,6 +431,19 @@ gutil_ptrv_length(
}
}

/* Frees NULL-terminated array of pointers and whatever they're pointing to. */
void
gutil_ptrv_free(
void** ptrv) /* Since 1.0.51 */
{
if (G_LIKELY(ptrv)) {
void** ptr = ptrv;

while (*ptr) g_free(*ptr++);
g_free(ptrv);
}
}

/*
* Local Variables:
* mode: C
Expand Down
29 changes: 24 additions & 5 deletions test/test_misc/test_misc.c
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2016-2020 Jolla Ltd.
* Copyright (C) 2016-2020 Slava Monich <slava.monich@jolla.com>
* Copyright (C) 2016-2021 Jolla Ltd.
* Copyright (C) 2016-2021 Slava Monich <slava.monich@jolla.com>
*
* You may use this file under the terms of BSD license as follows:
*
Expand Down Expand Up @@ -485,12 +485,12 @@ test_bytes_equal(
}

/*==========================================================================*
* ptrv
* ptrv_length
*==========================================================================*/

static
void
test_ptrv(
test_ptrv_length(
void)
{
static const gconstpointer ptrv0[] = { NULL };
Expand All @@ -501,6 +501,24 @@ test_ptrv(
g_assert_cmpuint(gutil_ptrv_length(ptrv1), == ,1);
}

/*==========================================================================*
* ptrv_free
*==========================================================================*/

static
void
test_ptrv_free(
void)
{
void** ptrv0 = g_new0(void*, 1);
void** ptrv1 = g_new0(void*, 2);

ptrv1[0] = g_new0(int, 1);
gutil_ptrv_free(NULL);
gutil_ptrv_free(ptrv0);
gutil_ptrv_free(ptrv1);
}

/*==========================================================================*
* Common
*==========================================================================*/
Expand Down Expand Up @@ -530,7 +548,8 @@ int main(int argc, char* argv[])
g_test_add_func(TEST_("bytes_concat"), test_bytes_concat);
g_test_add_func(TEST_("bytes_xor"), test_bytes_xor);
g_test_add_func(TEST_("bytes_equal"), test_bytes_equal);
g_test_add_func(TEST_("ptrv"), test_ptrv);
g_test_add_func(TEST_("ptrv_lenght"), test_ptrv_length);
g_test_add_func(TEST_("ptrv_free"), test_ptrv_free);
test_init(&test_opt, argc, argv);
return g_test_run();
}
Expand Down

0 comments on commit 3a5616b

Please sign in to comment.