summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
Diffstat (limited to 'common')
-rw-r--r--common/array.c21
-rw-r--r--common/array.h2
-rw-r--r--common/tests/test-array.c27
3 files changed, 43 insertions, 7 deletions
diff --git a/common/array.c b/common/array.c
index f2930a9..9802100 100644
--- a/common/array.c
+++ b/common/array.c
@@ -81,16 +81,10 @@ p11_array_new (p11_destroyer destroyer)
void
p11_array_free (p11_array *array)
{
- unsigned int i;
-
if (array == NULL)
return;
- if (array->destroyer) {
- for (i = 0; i < array->num; i++)
- (array->destroyer) (array->elem[i]);
- }
-
+ p11_array_clear (array);
free (array->elem);
free (array);
}
@@ -117,3 +111,16 @@ p11_array_remove (p11_array *array,
(array->num - (index + 1)) * sizeof (void*));
array->num--;
}
+
+void
+p11_array_clear (p11_array *array)
+{
+ int i;
+
+ if (array->destroyer) {
+ for (i = 0; i < array->num; i++)
+ (array->destroyer) (array->elem[i]);
+ }
+
+ array->num = 0;
+}
diff --git a/common/array.h b/common/array.h
index 8964b83..94be29c 100644
--- a/common/array.h
+++ b/common/array.h
@@ -63,4 +63,6 @@ bool p11_array_push (p11_array *array,
void p11_array_remove (p11_array *array,
unsigned int index);
+void p11_array_clear (p11_array *array);
+
#endif /* __P11_ARRAY_H__ */
diff --git a/common/tests/test-array.c b/common/tests/test-array.c
index a52f3b5..a796365 100644
--- a/common/tests/test-array.c
+++ b/common/tests/test-array.c
@@ -167,6 +167,32 @@ test_p11_array_remove_and_count (CuTest *tc)
p11_array_free (array);
}
+static void
+test_p11_array_clear_destroys (CuTest *tc)
+{
+ p11_array *array;
+ int value = 0;
+
+ array = p11_array_new (destroy_value);
+ if (!p11_array_push (array, &value))
+ CuFail (tc, "should not be reached");
+
+ CuAssertIntEquals (tc, 1, array->num);
+
+ p11_array_clear (array);
+
+ CuAssertIntEquals (tc, 2, value);
+ CuAssertIntEquals (tc, 0, array->num);
+
+ /* should not be destroyed again */
+ value = 0;
+
+ p11_array_free (array);
+
+ CuAssertIntEquals (tc, 0, value);
+}
+
+
int
main (void)
{
@@ -181,6 +207,7 @@ main (void)
SUITE_ADD_TEST (suite, test_p11_array_remove_and_count);
SUITE_ADD_TEST (suite, test_p11_array_free_null);
SUITE_ADD_TEST (suite, test_p11_array_free_destroys);
+ SUITE_ADD_TEST (suite, test_p11_array_clear_destroys);
CuSuiteRun (suite);
CuSuiteSummary (suite, output);