heap_malloc.c 1.2 KB
Newer Older
B
Bernard Xiong 已提交
1 2 3 4 5 6 7 8 9 10 11
#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)
	{
G
Grissiom 已提交
12 13
		if (*ptr != value)
            return RT_FALSE;
B
Bernard Xiong 已提交
14 15 16 17 18 19 20 21 22
		ptr ++;
		len --;
	}

	return RT_TRUE;
}

static void heap_malloc_init()
{
G
Grissiom 已提交
23
	rt_uint8_t res = TC_STAT_PASSED;
B
Bernard Xiong 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36
	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);

G
Grissiom 已提交
37 38 39 40 41 42 43 44
	if (mem_check(ptr1, 1, 1)   == RT_FALSE)
        res = TC_STAT_FAILED;
	if (mem_check(ptr2, 2, 13)  == RT_FALSE)
        res = TC_STAT_FAILED;
	if (mem_check(ptr3, 3, 31)  == RT_FALSE)
        res = TC_STAT_FAILED;
	if (mem_check(ptr4, 4, 127) == RT_FALSE)
        res = TC_STAT_FAILED;
B
Bernard Xiong 已提交
45 46 47

	rt_free(ptr4);
	rt_free(ptr3);
G
Grissiom 已提交
48
	rt_free(ptr2);
B
Bernard Xiong 已提交
49 50 51 52 53 54 55
	rt_free(ptr1);

	if (ptr5 != RT_NULL)
	{
		rt_free(ptr5);
	}

G
Grissiom 已提交
56
	tc_done(res);
B
Bernard Xiong 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
}

#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