[PATCH 1/2] libtest: Make test case allocator configurable
Sebastian Huber
sebastian.huber at embedded-brains.de
Tue Nov 10 09:55:20 UTC 2020
---
cpukit/include/rtems/test.h | 7 ++
cpukit/libtest/t-test-malloc.c | 80 ++-------------------
cpukit/libtest/t-test.c | 88 +++++++++++++++++++++++
cpukit/libtest/testrun.c | 2 +
testsuites/libtests/ttest01/init.c | 2 +
testsuites/smptests/smpmulticast01/init.c | 4 +-
testsuites/validation/ts-validation-0.c | 2 +
7 files changed, 110 insertions(+), 75 deletions(-)
diff --git a/cpukit/include/rtems/test.h b/cpukit/include/rtems/test.h
index 935e796bf0..e24d9d29ac 100644
--- a/cpukit/include/rtems/test.h
+++ b/cpukit/include/rtems/test.h
@@ -2253,6 +2253,10 @@ T_time T_case_begin_time(void);
/** @} */
+void *T_heap_allocate(size_t, void *);
+
+void T_heap_deallocate(void *, void *);
+
void *T_malloc(size_t);
void *T_calloc(size_t, size_t);
@@ -2286,6 +2290,9 @@ typedef struct {
void *putchar_arg;
T_verbosity verbosity;
T_time (*now)(void);
+ void *(*allocate)(size_t, void *);
+ void (*deallocate)(void *, void *);
+ void *allocator_arg;
size_t action_count;
const T_action *actions;
} T_config;
diff --git a/cpukit/libtest/t-test-malloc.c b/cpukit/libtest/t-test-malloc.c
index 369bff9a57..c54b5f6672 100644
--- a/cpukit/libtest/t-test-malloc.c
+++ b/cpukit/libtest/t-test-malloc.c
@@ -29,84 +29,16 @@
#include <stdlib.h>
-#ifdef __BIGGEST_ALIGNMENT__
-#define T_BIGGEST_ALIGNMENT __BIGGEST_ALIGNMENT__
-#else
-#define T_BIGGEST_ALIGNMENT sizeof(long long)
-#endif
-
-typedef struct __attribute__((__aligned__(T_BIGGEST_ALIGNMENT))) {
- T_destructor base;
- void (*destroy)(void *);
-} T_malloc_destructor;
-
-static void
-T_malloc_destroy(T_destructor *base)
-{
- T_malloc_destructor *dtor;
-
- dtor = (T_malloc_destructor *)(uintptr_t)base;
-
- if (dtor->destroy != NULL) {
- (*dtor->destroy)(dtor + 1);
- }
-
- free(dtor);
-}
-
-static void *
-T_do_malloc(size_t size, void (*destroy)(void *))
-{
- T_malloc_destructor *dtor;
- size_t new_size;
-
- new_size = sizeof(*dtor) + size;
- if (new_size <= size) {
- return NULL;
- }
-
- dtor = malloc(new_size);
- if (dtor != NULL) {
- dtor->destroy = destroy;
- T_add_destructor(&dtor->base, T_malloc_destroy);
- ++dtor;
- }
-
- return dtor;
-}
-
-void *
-T_malloc(size_t size)
-{
- return T_do_malloc(size, NULL);
-}
-
-void *
-T_calloc(size_t nelem, size_t elsize)
-{
- return T_zalloc(nelem * elsize, NULL);
-}
-
void *
-T_zalloc(size_t size, void (*destroy)(void *))
+T_heap_allocate(size_t size, void *arg)
{
- void *p;
-
- p = T_do_malloc(size, destroy);
- if (p != NULL) {
- p = memset(p, 0, size);
- }
-
- return p;
+ (void)arg;
+ return malloc(size);
}
void
-T_free(void *ptr)
+T_heap_deallocate(void *ptr, void *arg)
{
- T_malloc_destructor *dtor;
-
- dtor = ptr;
- --dtor;
- T_remove_destructor(&dtor->base);
- free(dtor);
+ (void)arg;
+ free(ptr);
}
diff --git a/cpukit/libtest/t-test.c b/cpukit/libtest/t-test.c
index 11f1cf70e9..64c3d33703 100644
--- a/cpukit/libtest/t-test.c
+++ b/cpukit/libtest/t-test.c
@@ -1299,3 +1299,91 @@ T_get_scope(const char * const * const *desc, char *buf, size_t n,
return n - c;
}
+
+#ifdef __BIGGEST_ALIGNMENT__
+#define T_BIGGEST_ALIGNMENT __BIGGEST_ALIGNMENT__
+#else
+#define T_BIGGEST_ALIGNMENT sizeof(long long)
+#endif
+
+typedef struct __attribute__((__aligned__(T_BIGGEST_ALIGNMENT))) {
+ T_destructor base;
+ void (*destroy)(void *);
+} T_malloc_destructor;
+
+static void
+T_malloc_destroy(T_destructor *base)
+{
+ const T_config *config;
+ T_malloc_destructor *dtor;
+
+ dtor = (T_malloc_destructor *)(uintptr_t)base;
+
+ if (dtor->destroy != NULL) {
+ (*dtor->destroy)(dtor + 1);
+ }
+
+ config = T_instance.config;
+ (*config->deallocate)(dtor, config->allocator_arg);
+}
+
+static void *
+T_do_malloc(size_t size, void (*destroy)(void *))
+{
+ const T_config *config;
+ T_malloc_destructor *dtor;
+ size_t new_size;
+
+ new_size = sizeof(*dtor) + size;
+ if (new_size <= size) {
+ return NULL;
+ }
+
+ config = T_instance.config;
+ dtor = (*config->allocate)(new_size, config->allocator_arg);
+ if (dtor != NULL) {
+ dtor->destroy = destroy;
+ T_add_destructor(&dtor->base, T_malloc_destroy);
+ ++dtor;
+ }
+
+ return dtor;
+}
+
+void *
+T_malloc(size_t size)
+{
+ return T_do_malloc(size, NULL);
+}
+
+void *
+T_calloc(size_t nelem, size_t elsize)
+{
+ return T_zalloc(nelem * elsize, NULL);
+}
+
+void *
+T_zalloc(size_t size, void (*destroy)(void *))
+{
+ void *p;
+
+ p = T_do_malloc(size, destroy);
+ if (p != NULL) {
+ p = memset(p, 0, size);
+ }
+
+ return p;
+}
+
+void
+T_free(void *ptr)
+{
+ const T_config *config;
+ T_malloc_destructor *dtor;
+
+ dtor = ptr;
+ --dtor;
+ T_remove_destructor(&dtor->base);
+ config = T_instance.config;
+ (*config->deallocate)(dtor, config->allocator_arg);
+}
diff --git a/cpukit/libtest/testrun.c b/cpukit/libtest/testrun.c
index d11bf63c17..9ea648f2eb 100644
--- a/cpukit/libtest/testrun.c
+++ b/cpukit/libtest/testrun.c
@@ -65,6 +65,8 @@ static const T_config config = {
.putchar = T_putchar_default,
.verbosity = T_VERBOSE,
.now = T_now_clock,
+ .allocate = T_heap_allocate,
+ .deallocate = T_heap_deallocate,
.action_count = T_ARRAY_SIZE( actions ),
.actions = actions
};
diff --git a/testsuites/libtests/ttest01/init.c b/testsuites/libtests/ttest01/init.c
index c0ccee9aa4..e97e204e3e 100644
--- a/testsuites/libtests/ttest01/init.c
+++ b/testsuites/libtests/ttest01/init.c
@@ -255,6 +255,8 @@ static const T_config config = {
.putchar_arg = &test_instance,
.verbosity = T_VERBOSE,
.now = now,
+ .allocate = T_heap_allocate,
+ .deallocate = T_heap_deallocate,
.action_count = T_ARRAY_SIZE(actions),
.actions = actions
};
diff --git a/testsuites/smptests/smpmulticast01/init.c b/testsuites/smptests/smpmulticast01/init.c
index 3d2412427c..ebc8897ae0 100644
--- a/testsuites/smptests/smpmulticast01/init.c
+++ b/testsuites/smptests/smpmulticast01/init.c
@@ -45,7 +45,9 @@ static const T_config config = {
.name = "SMPMultiCast",
.putchar = T_putchar_default,
.verbosity = RTEMS_TEST_VERBOSITY,
- .now = T_now_clock
+ .now = T_now_clock,
+ .allocate = T_heap_allocate,
+ .deallocate = T_heap_deallocate
};
typedef struct {
diff --git a/testsuites/validation/ts-validation-0.c b/testsuites/validation/ts-validation-0.c
index baaa679c05..a2e10bc8af 100644
--- a/testsuites/validation/ts-validation-0.c
+++ b/testsuites/validation/ts-validation-0.c
@@ -95,6 +95,8 @@ static const T_config test_config = {
.putchar = rtems_put_char,
.verbosity = RTEMS_TEST_VERBOSITY,
.now = T_now_clock,
+ .allocate = T_heap_allocate,
+ .deallocate = T_heap_deallocate,
.action_count = T_ARRAY_SIZE( actions ),
.actions = actions
};
--
2.26.2
More information about the devel
mailing list