mirror of
https://github.com/RT-Thread/rt-thread.git
synced 2026-10-25 18:53:08 +08:00
Merge pull request #4756 from Guozhanxin/ac6
[src][memheap]Fix the crash problem after opening Oz optimization on ac6.
This commit is contained in:
8 files changed
+465
-35
No files matched your search
@@ -11,7 +11,7 @@ jobs:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
legs:
|
||||
- {UTEST: "kernel/ipc", RTT_BSP: "bsp/qemu-vexpress-a9", QEMU_ARCH: "vexpress-a9", CONFIG_FILE: "examples/utest/configs/utest_self/config.h"}
|
||||
- {UTEST: "kernel/mem", RTT_BSP: "bsp/qemu-vexpress-a9", QEMU_ARCH: "vexpress-a9", CONFIG_FILE: "examples/utest/configs/kernel/config.h"}
|
||||
- {UTEST: "components/utest", RTT_BSP: "bsp/qemu-vexpress-a9", QEMU_ARCH: "vexpress-a9", CONFIG_FILE: "examples/utest/configs/utest_self/config.h"}
|
||||
|
||||
env:
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -8,6 +8,7 @@ config RT_USING_UTESTCASES
|
||||
if RT_USING_UTESTCASES
|
||||
|
||||
source "$RTT_DIR/examples/utest/testcases/utest/Kconfig"
|
||||
source "$RTT_DIR/examples/utest/testcases/kernel/Kconfig"
|
||||
|
||||
endif
|
||||
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
menu "Kernel Testcase"
|
||||
|
||||
config UTEST_MEMHEAP_TC
|
||||
bool "memheap stability test"
|
||||
default y
|
||||
depends on RT_USING_MEMHEAP
|
||||
|
||||
endmenu
|
||||
@@ -0,0 +1,15 @@
|
||||
Import('rtconfig')
|
||||
from building import *
|
||||
|
||||
cwd = GetCurrentDir()
|
||||
src = Split('''
|
||||
''')
|
||||
|
||||
if GetDepend(['UTEST_MEMHEAP_TC']):
|
||||
src += ['memheap_tc.c']
|
||||
|
||||
CPPPATH = [cwd]
|
||||
|
||||
group = DefineGroup('utestcases', src, depend = [], CPPPATH = CPPPATH)
|
||||
|
||||
Return('group')
|
||||
@@ -0,0 +1,100 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2019, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2019-01-16 flybreak the first version
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <stdlib.h>
|
||||
#include "utest.h"
|
||||
|
||||
#define HEAP_SIZE (64 * 1024)
|
||||
#define HEAP_ALIGN (4)
|
||||
#define SLICE_NUM (40)
|
||||
#define TEST_TIMES (100000)
|
||||
#define HEAP_NAME "heap1"
|
||||
#define SLICE_SIZE_MAX (HEAP_SIZE/SLICE_NUM)
|
||||
|
||||
static void memheap_test(void)
|
||||
{
|
||||
struct rt_memheap heap1;
|
||||
rt_uint32_t ptr_start;
|
||||
void *ptr[SLICE_NUM];
|
||||
int i, cnt = 0;
|
||||
|
||||
/* init heap */
|
||||
ptr_start = (rt_uint32_t)rt_malloc_align(HEAP_SIZE, HEAP_ALIGN);
|
||||
if (ptr_start == RT_NULL)
|
||||
{
|
||||
rt_kprintf("totle size too big,can not malloc memory!");
|
||||
return;
|
||||
}
|
||||
|
||||
rt_memheap_init(&heap1, HEAP_NAME, (void *)ptr_start, HEAP_SIZE);
|
||||
|
||||
/* test start */
|
||||
for (i = 0; i < SLICE_NUM; i++)
|
||||
{
|
||||
ptr[i] = 0;
|
||||
}
|
||||
/* test alloc */
|
||||
for (i = 0; i < SLICE_NUM; i++)
|
||||
{
|
||||
rt_uint32_t slice_size = rand() % SLICE_SIZE_MAX;
|
||||
ptr[i] = rt_memheap_alloc(&heap1, slice_size);
|
||||
}
|
||||
/* test realloc */
|
||||
while (cnt < TEST_TIMES)
|
||||
{
|
||||
rt_uint32_t slice_size = rand() % SLICE_SIZE_MAX;
|
||||
rt_uint32_t ptr_index = rand() % SLICE_NUM;
|
||||
rt_uint32_t operation = rand() % 2;
|
||||
|
||||
if (ptr[ptr_index])
|
||||
{
|
||||
if (operation == 0) /* free and malloc */
|
||||
{
|
||||
if (ptr[ptr_index])
|
||||
{
|
||||
rt_memheap_free(ptr[ptr_index]);
|
||||
}
|
||||
ptr[ptr_index] = rt_memheap_alloc(&heap1, slice_size);
|
||||
}
|
||||
else /* realloc */
|
||||
{
|
||||
ptr[ptr_index] = rt_memheap_realloc(&heap1, ptr[ptr_index], slice_size);
|
||||
}
|
||||
}
|
||||
cnt ++;
|
||||
if (cnt % (TEST_TIMES / 10) == 0)
|
||||
{
|
||||
rt_kprintf(">");
|
||||
}
|
||||
}
|
||||
|
||||
rt_kprintf("test OK!\n");
|
||||
|
||||
/* test end */
|
||||
rt_memheap_detach(&heap1);
|
||||
rt_free_align((void *)ptr_start);
|
||||
}
|
||||
|
||||
static rt_err_t utest_tc_init(void)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static rt_err_t utest_tc_cleanup(void)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static void testcase(void)
|
||||
{
|
||||
UTEST_UNIT_RUN(memheap_test);
|
||||
}
|
||||
UTEST_TC_EXPORT(testcase, "testcases.kernel.memheap_tc", utest_tc_init, utest_tc_cleanup, 10);
|
||||
@@ -43,4 +43,4 @@ static void testcase(void)
|
||||
{
|
||||
UTEST_UNIT_RUN(test_assert_pass);
|
||||
}
|
||||
UTEST_TC_EXPORT(testcase, "testcases.utest.pass_tc", utest_tc_init, utest_tc_cleanup, 10);
|
||||
UTEST_TC_EXPORT(testcase, "testcases.utest.pass_tc", utest_tc_init, utest_tc_cleanup, 10);
|
||||
+35
-33
@@ -17,6 +17,7 @@
|
||||
* 2013-05-24 Bernard fix the rt_memheap_realloc issue.
|
||||
* 2013-07-11 Grissiom fix the memory block splitting issue.
|
||||
* 2013-07-15 Grissiom optimize rt_memheap_realloc
|
||||
* 2021-06-03 Flybreak Fix the crash problem after opening Oz optimization on ac6.
|
||||
*/
|
||||
|
||||
#include <rthw.h>
|
||||
@@ -41,31 +42,31 @@
|
||||
rt_inline void rt_memheap_setname(struct rt_memheap_item *item, const char *name)
|
||||
{
|
||||
int index;
|
||||
rt_uint8_t* ptr;
|
||||
rt_uint8_t *ptr;
|
||||
|
||||
ptr = (rt_uint8_t*)&(item->next_free);
|
||||
for (index = 0; index < sizeof(void*); index ++)
|
||||
ptr = (rt_uint8_t *) & (item->next_free);
|
||||
for (index = 0; index < sizeof(void *); index ++)
|
||||
{
|
||||
if (name[index] == '\0') break;
|
||||
ptr[index] = name[index];
|
||||
}
|
||||
if (name[index] == '\0') ptr[index] = '\0';
|
||||
else
|
||||
else
|
||||
{
|
||||
ptr = (rt_uint8_t*)&(item->prev_free);
|
||||
for (index = 0; index < sizeof(void*) && (index + sizeof(void*))< RT_NAME_MAX; index ++)
|
||||
ptr = (rt_uint8_t *) & (item->prev_free);
|
||||
for (index = 0; index < sizeof(void *) && (index + sizeof(void *)) < RT_NAME_MAX; index ++)
|
||||
{
|
||||
if (name[sizeof(void*) + index] == '\0') break;
|
||||
ptr[index] = name[sizeof(void*) + index];
|
||||
if (name[sizeof(void *) + index] == '\0') break;
|
||||
ptr[index] = name[sizeof(void *) + index];
|
||||
}
|
||||
|
||||
if (name[sizeof(void*) + index] == '\0') ptr[index] = '\0';
|
||||
if (name[sizeof(void *) + index] == '\0') ptr[index] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
void rt_mem_set_tag(void* ptr, const char* name)
|
||||
void rt_mem_set_tag(void *ptr, const char *name)
|
||||
{
|
||||
struct rt_memheap_item* item;
|
||||
struct rt_memheap_item *item;
|
||||
|
||||
if (ptr && name)
|
||||
{
|
||||
@@ -369,7 +370,8 @@ void *rt_memheap_realloc(struct rt_memheap *heap, void *ptr, rt_size_t newsize)
|
||||
if (newsize > oldsize)
|
||||
{
|
||||
void *new_ptr;
|
||||
struct rt_memheap_item *next_ptr;
|
||||
/* Fix the crash problem after opening Oz optimization on ac6 */
|
||||
volatile struct rt_memheap_item *next_ptr;
|
||||
|
||||
/* lock memheap */
|
||||
result = rt_sem_take(&(heap->lock), RT_WAITING_FOREVER);
|
||||
@@ -441,14 +443,14 @@ void *rt_memheap_realloc(struct rt_memheap *heap, void *ptr, rt_size_t newsize)
|
||||
|
||||
next_ptr->prev = header_ptr;
|
||||
next_ptr->next = header_ptr->next;
|
||||
header_ptr->next->prev = next_ptr;
|
||||
header_ptr->next = next_ptr;
|
||||
header_ptr->next->prev = (struct rt_memheap_item *)next_ptr;
|
||||
header_ptr->next = (struct rt_memheap_item *)next_ptr;
|
||||
|
||||
/* insert next_ptr to free list */
|
||||
next_ptr->next_free = heap->free_list->next_free;
|
||||
next_ptr->prev_free = heap->free_list;
|
||||
heap->free_list->next_free->prev_free = next_ptr;
|
||||
heap->free_list->next_free = next_ptr;
|
||||
heap->free_list->next_free->prev_free = (struct rt_memheap_item *)next_ptr;
|
||||
heap->free_list->next_free = (struct rt_memheap_item *)next_ptr;
|
||||
RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("new ptr: next_free 0x%08x, prev_free 0x%08x",
|
||||
next_ptr->next_free,
|
||||
next_ptr->prev_free));
|
||||
@@ -666,17 +668,17 @@ void rt_memheap_free(void *ptr)
|
||||
RTM_EXPORT(rt_memheap_free);
|
||||
|
||||
#ifdef RT_USING_FINSH
|
||||
static void _memheap_dump_tag(struct rt_memheap_item* item)
|
||||
static void _memheap_dump_tag(struct rt_memheap_item *item)
|
||||
{
|
||||
rt_uint8_t name[2 * sizeof(void*)];
|
||||
rt_uint8_t* ptr;
|
||||
rt_uint8_t name[2 * sizeof(void *)];
|
||||
rt_uint8_t *ptr;
|
||||
|
||||
ptr = (rt_uint8_t*)&(item->next_free);
|
||||
rt_memcpy(name, ptr, sizeof(void*));
|
||||
ptr = (rt_uint8_t*)&(item->prev_free);
|
||||
rt_memcpy(&name[sizeof(void*)], ptr, sizeof(void*));
|
||||
ptr = (rt_uint8_t *) & (item->next_free);
|
||||
rt_memcpy(name, ptr, sizeof(void *));
|
||||
ptr = (rt_uint8_t *) & (item->prev_free);
|
||||
rt_memcpy(&name[sizeof(void *)], ptr, sizeof(void *));
|
||||
|
||||
rt_kprintf("%.*s", 2 * sizeof(void*), name);
|
||||
rt_kprintf("%.*s", 2 * sizeof(void *), name);
|
||||
}
|
||||
|
||||
int rt_memheap_dump(struct rt_memheap *heap)
|
||||
@@ -686,15 +688,15 @@ int rt_memheap_dump(struct rt_memheap *heap)
|
||||
if (heap == RT_NULL) return 0;
|
||||
RT_ASSERT(rt_object_get_type(&heap->parent) == RT_Object_Class_MemHeap);
|
||||
|
||||
rt_kprintf("\n[%.*s] [0x%08x - 0x%08x]->\n", RT_NAME_MAX, heap->parent.name,
|
||||
(rt_ubase_t)heap->start_addr, (rt_ubase_t)heap->start_addr + heap->pool_size);
|
||||
rt_kprintf("\n[%.*s] [0x%08x - 0x%08x]->\n", RT_NAME_MAX, heap->parent.name,
|
||||
(rt_ubase_t)heap->start_addr, (rt_ubase_t)heap->start_addr + heap->pool_size);
|
||||
rt_kprintf("------------------------------\n");
|
||||
|
||||
/* lock memheap */
|
||||
rt_sem_take(&(heap->lock), RT_WAITING_FOREVER);
|
||||
item = heap->block_list;
|
||||
|
||||
end = (struct rt_memheap_item *) ((rt_uint8_t *)heap->start_addr + heap->pool_size - RT_MEMHEAP_SIZE);
|
||||
end = (struct rt_memheap_item *)((rt_uint8_t *)heap->start_addr + heap->pool_size - RT_MEMHEAP_SIZE);
|
||||
|
||||
/* for each memory block */
|
||||
while ((rt_ubase_t)item < ((rt_ubase_t)end))
|
||||
@@ -730,13 +732,13 @@ int memheaptrace(void)
|
||||
int index;
|
||||
extern int list_memheap(void);
|
||||
|
||||
heaps = (struct rt_memheap**)rt_malloc(sizeof(struct rt_memheap*) * count);
|
||||
heaps = (struct rt_memheap **)rt_malloc(sizeof(struct rt_memheap *) * count);
|
||||
if (heaps == RT_NULL) return 0;
|
||||
|
||||
list_memheap();
|
||||
|
||||
rt_kprintf("memheap header size: %d\n", RT_MEMHEAP_SIZE);
|
||||
count = rt_object_get_pointers(RT_Object_Class_MemHeap, (rt_object_t*)heaps, count);
|
||||
count = rt_object_get_pointers(RT_Object_Class_MemHeap, (rt_object_t *)heaps, count);
|
||||
for (index = 0; index < count; index++)
|
||||
{
|
||||
rt_memheap_dump(heaps[index]);
|
||||
@@ -966,15 +968,15 @@ void dump_used_memheap(struct rt_memheap *mh)
|
||||
{
|
||||
/* dump information */
|
||||
rt_kprintf("[0x%08x - %d - %c%c%c%c] used\n", header_ptr, block_size,
|
||||
header_ptr->owner_thread_name[0], header_ptr->owner_thread_name[1],
|
||||
header_ptr->owner_thread_name[2], header_ptr->owner_thread_name[3]);
|
||||
header_ptr->owner_thread_name[0], header_ptr->owner_thread_name[1],
|
||||
header_ptr->owner_thread_name[2], header_ptr->owner_thread_name[3]);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* dump information */
|
||||
rt_kprintf("[0x%08x - %d - %c%c%c%c] free\n", header_ptr, block_size,
|
||||
header_ptr->owner_thread_name[0], header_ptr->owner_thread_name[1],
|
||||
header_ptr->owner_thread_name[2], header_ptr->owner_thread_name[3]);
|
||||
header_ptr->owner_thread_name[0], header_ptr->owner_thread_name[1],
|
||||
header_ptr->owner_thread_name[2], header_ptr->owner_thread_name[3]);
|
||||
}
|
||||
|
||||
/* move to next used memory block */
|
||||
|
||||
Reference in new issue
Block a user