diff options
| author | bernard.xiong <[email protected]> | 2010-03-17 09:57:07 +0000 |
|---|---|---|
| committer | bernard.xiong <[email protected]> | 2010-03-17 09:57:07 +0000 |
| commit | 2d54fcc40581c348959e50a853ef7d4c0478974f (patch) | |
| tree | 34028e9e3a19c6f3f1755b9ffdf17724f0f5f0a6 /examples/kernel/heap_malloc.c | |
| parent | 824dc0b00e6d390f78de43d806bebb170635cda5 (diff) | |
add heap and semaphore examples.
git-svn-id: https://rt-thread.googlecode.com/svn/trunk@501 bbd45198-f89e-11dd-88c7-29a3b14d5316
Diffstat (limited to 'examples/kernel/heap_malloc.c')
| -rw-r--r-- | examples/kernel/heap_malloc.c | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/examples/kernel/heap_malloc.c b/examples/kernel/heap_malloc.c new file mode 100644 index 0000000000..236a982fdf --- /dev/null +++ b/examples/kernel/heap_malloc.c @@ -0,0 +1,72 @@ +#include <rtthread.h>
+#include "tc_comm.h"
+
+/*
+ * This is an example for heap malloc
+ */
+
+static rt_bool_t mem_check(rt_uint8_t *ptr, rt_uint8_t value, rt_uint32_t len)
+{
+ while (len)
+ {
+ if (*ptr != value) return RT_FALSE;
+
+ ptr ++;
+ len --;
+ }
+
+ return RT_TRUE;
+}
+
+static void heap_malloc_init()
+{
+ rt_uint8_t *ptr1, *ptr2, *ptr3, *ptr4, *ptr5;
+
+ ptr1 = rt_malloc(1);
+ ptr2 = rt_malloc(13);
+ ptr3 = rt_malloc(31);
+ ptr4 = rt_malloc(127);
+ ptr5 = rt_malloc(0);
+
+ memset(ptr1, 1, 1);
+ memset(ptr2, 2, 13);
+ memset(ptr3, 3, 31);
+ memset(ptr4, 4, 127);
+
+ if (mem_check(ptr1, 1, 1) != RT_FALSE) goto _failed;
+ if (mem_check(ptr2, 2, 13) != RT_FALSE) goto _failed;
+ if (mem_check(ptr3, 3, 31) != RT_FALSE) goto _failed;
+ if (mem_check(ptr4, 4, 127) != RT_FALSE) goto _failed;
+
+ rt_free(ptr4);
+ rt_free(ptr3);
+ rt_free(ptr3);
+ rt_free(ptr1);
+
+ if (ptr5 != RT_NULL)
+ {
+ rt_free(ptr5);
+ }
+
+ tc_done(TC_STAT_PASSED);
+
+_failed:
+ tc_done(TC_STAT_FAILED);
+}
+
+#ifdef RT_USING_TC
+int _tc_heap_malloc()
+{
+ heap_malloc_init();
+
+ return 0;
+}
+FINSH_FUNCTION_EXPORT(_tc_heap_malloc, a heap malloc test);
+#else
+int rt_application_init()
+{
+ heap_malloc_init();
+
+ return 0;
+}
+#endif
|
