Patrick Georgi submitted this change.

View Change

Approvals: build bot (Jenkins): Verified Angel Pons: Looks good to me, approved
lib: Add obvious definition for `calloc`

The calloc() function is useful in addition to malloc and friends, so
add the obvious definition.

Change-Id: I57a568e323344a97b35014b7b8bec16adc2fd720
Signed-off-by: Tim Wawrzynczak <twawrzynczak@chromium.org>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/51949
Reviewed-by: Angel Pons <th3fanbus@gmail.com>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
---
M src/include/stdlib.h
M src/lib/malloc.c
M tests/lib/malloc-test.c
3 files changed, 34 insertions(+), 1 deletion(-)

diff --git a/src/include/stdlib.h b/src/include/stdlib.h
index f6369bf..8636ea4 100644
--- a/src/include/stdlib.h
+++ b/src/include/stdlib.h
@@ -5,6 +5,7 @@

void *memalign(size_t boundary, size_t size);
void *malloc(size_t size);
+void *calloc(size_t nitems, size_t size);
void free(void *ptr);

#endif /* STDLIB_H */
diff --git a/src/lib/malloc.c b/src/lib/malloc.c
index cd86f99..7d787d6 100644
--- a/src/lib/malloc.c
+++ b/src/lib/malloc.c
@@ -1,5 +1,6 @@
-#include <stdlib.h>
#include <console/console.h>
+#include <stdlib.h>
+#include <string.h>

#if CONFIG(DEBUG_MALLOC)
#define MALLOCDBG(x...) printk(BIOS_SPEW, x)
@@ -54,6 +55,15 @@
return memalign(sizeof(u64), size);
}

+void *calloc(size_t nitems, size_t size)
+{
+ void *p = malloc(nitems * size);
+ if (p)
+ memset(p, 0, nitems * size);
+
+ return p;
+}
+
void free(void *ptr)
{
if (ptr == NULL)
diff --git a/tests/lib/malloc-test.c b/tests/lib/malloc-test.c
index e358fff..fefacbb 100644
--- a/tests/lib/malloc-test.c
+++ b/tests/lib/malloc-test.c
@@ -2,6 +2,7 @@

/* Include malloc() and memalign() source code and alter its name to indicate the functions
source origin. */
+#define calloc cb_calloc
#define malloc cb_malloc
#define free cb_free
#define memalign cb_memalign
@@ -10,6 +11,7 @@

#include "../lib/malloc.c"

+#undef calloc
#undef malloc
#undef free
#undef memalign
@@ -46,6 +48,12 @@
return 0;
}

+static int setup_calloc_test(void **state)
+{
+ memset(&_heap, 0xFF, TEST_HEAP_SZ);
+ return setup_test(state);
+}
+
static void test_malloc_out_of_memory(void **state)
{
/* Expect die() call if out of memory */
@@ -130,6 +138,19 @@
}
}

+static void test_calloc_memory_is_zeroed(void **state)
+{
+ const size_t nitems = 42;
+ const size_t size = sizeof(uint32_t);
+ void *ptr = cb_calloc(nitems, size);
+ assert_non_null(ptr);
+
+ for (size_t i = 0; i < nitems; i++) {
+ const uint32_t *p = (const uint32_t *)ptr + i;
+ assert_int_equal(*p, 0);
+ }
+}
+
int main(void)
{
const struct CMUnitTest tests[] = {
@@ -140,6 +161,7 @@
cmocka_unit_test_setup(test_memalign_out_of_memory, setup_test),
cmocka_unit_test_setup(test_memalign_zero, setup_test),
cmocka_unit_test_setup(test_memalign_multiple_small_allocations, setup_test),
+ cmocka_unit_test_setup(test_calloc_memory_is_zeroed, setup_calloc_test),
};

return cmocka_run_group_tests(tests, NULL, NULL);

To view, visit change 51949. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: coreboot
Gerrit-Branch: master
Gerrit-Change-Id: I57a568e323344a97b35014b7b8bec16adc2fd720
Gerrit-Change-Number: 51949
Gerrit-PatchSet: 11
Gerrit-Owner: Tim Wawrzynczak <twawrzynczak@chromium.org>
Gerrit-Reviewer: Angel Pons <th3fanbus@gmail.com>
Gerrit-Reviewer: Furquan Shaikh <furquan@google.com>
Gerrit-Reviewer: Jakub Czapiga <jacz@semihalf.com>
Gerrit-Reviewer: Nico Huber <nico.h@gmx.de>
Gerrit-Reviewer: Patrick Georgi <pgeorgi@google.com>
Gerrit-Reviewer: build bot (Jenkins) <no-reply@coreboot.org>
Gerrit-CC: Paul Menzel <paulepanter@users.sourceforge.net>
Gerrit-MessageType: merged