Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-os by
Diff: TESTS/mbedmicro-rtos-mbed/malloc/main.cpp
- Revision:
- 0:f269e3021894
diff -r 000000000000 -r f269e3021894 TESTS/mbedmicro-rtos-mbed/malloc/main.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/TESTS/mbedmicro-rtos-mbed/malloc/main.cpp Sun Oct 23 15:10:02 2016 +0000
@@ -0,0 +1,68 @@
+#include "mbed.h"
+#include "test_env.h"
+#include "rtos.h"
+
+#if defined(MBED_RTOS_SINGLE_THREAD)
+ #error [NOT_SUPPORTED] test not supported
+#endif
+
+#define NUM_THREADS 5
+#define THREAD_STACK_SIZE 256
+
+DigitalOut led1(LED1);
+volatile bool should_exit = false;
+volatile bool allocation_failure = false;
+
+void task_using_malloc(void)
+{
+ void* data;
+ while (1) {
+ // Repeatedly allocate and free memory
+ data = malloc(100);
+ if (data != NULL) {
+ memset(data, 0, 100);
+ } else {
+ allocation_failure = true;
+ }
+ free(data);
+
+ if (should_exit) {
+ return;
+ }
+ }
+}
+
+int main()
+{
+ Thread *thread_list[NUM_THREADS];
+ int test_time = 15;
+ GREENTEA_SETUP(20, "default_auto");
+
+ // Allocate threads for the test
+ for (int i = 0; i < NUM_THREADS; i++) {
+ thread_list[i] = new Thread(osPriorityNormal, THREAD_STACK_SIZE);
+ if (NULL == thread_list[i]) {
+ allocation_failure = true;
+ }
+ thread_list[i]->start(task_using_malloc);
+ }
+
+ // Give the test time to run
+ while (test_time) {
+ led1 = !led1;
+ Thread::wait(1000);
+ test_time--;
+ }
+
+ // Join and delete all threads
+ should_exit = 1;
+ for (int i = 0; i < NUM_THREADS; i++) {
+ if (NULL == thread_list[i]) {
+ continue;
+ }
+ thread_list[i]->join();
+ delete thread_list[i];
+ }
+
+ GREENTEA_TESTSUITE_RESULT(!allocation_failure);
+}
