diff --git a/src/clock.c b/src/clock.c index f34d09e32f..ac45d4fad7 100644 --- a/src/clock.c +++ b/src/clock.c @@ -96,7 +96,7 @@ void rt_tick_increase() rt_tick_t rt_tick_from_millisecond(rt_uint32_t ms) { /* return the calculated tick */ - return (RT_TICK_PER_SECOND * ms) / 1000 + (RT_TICK_PER_SECOND * ms) % 1000 ? 1:0; + return (RT_TICK_PER_SECOND * ms+999) / 1000; } /*@}*/ diff --git a/src/mem.c b/src/mem.c index ac091c6f76..22921253ea 100644 --- a/src/mem.c +++ b/src/mem.c @@ -166,15 +166,22 @@ static void plug_holes(struct heap_mem *mem) void rt_system_heap_init(void* begin_addr, void* end_addr) { struct heap_mem *mem; + rt_uint32_t begin_align = RT_ALIGN((rt_uint32_t)begin_addr, RT_ALIGN_SIZE); + rt_uint32_t end_align = RT_ALIGN_DOWN((rt_uint32_t)end_addr, RT_ALIGN_SIZE); /* alignment addr */ - begin_addr = (void*)RT_ALIGN((rt_uint32_t)begin_addr, RT_ALIGN_SIZE); - + if((end_align > (2 * SIZEOF_STRUCT_MEM) ) && + ((end_align - 2 * SIZEOF_STRUCT_MEM) >= begin_align )) { /* calculate the aligned memory size */ - mem_size_aligned = RT_ALIGN_DOWN((rt_uint32_t)end_addr - (rt_uint32_t)begin_addr, RT_ALIGN_SIZE) - 2 * SIZEOF_STRUCT_MEM; + mem_size_aligned = end_align - begin_align - 2 * SIZEOF_STRUCT_MEM; + } + else { + rt_kprintf("mem init, error begin address 0x%x, and end address 0x%x\n", (rt_uint32_t)begin_addr, (rt_uint32_t)end_addr); + return; + } /* point to begin address of heap */ - heap_ptr = begin_addr; + heap_ptr = (rt_uint8_t *)begin_align; #ifdef RT_MEM_DEBUG rt_kprintf("mem init, heap begin address 0x%x, size %d\n", (rt_uint32_t)heap_ptr, mem_size_aligned); diff --git a/src/slab.c b/src/slab.c index 216ee19500..dc904230f2 100644 --- a/src/slab.c +++ b/src/slab.c @@ -329,6 +329,11 @@ void rt_system_heap_init(void *begin_addr, void* end_addr) heap_start = RT_ALIGN((rt_uint32_t)begin_addr, RT_MM_PAGE_SIZE); heap_end = RT_ALIGN_DOWN((rt_uint32_t)end_addr, RT_MM_PAGE_SIZE); + if(heap_start >= heap_end) { + rt_kprintf("rt_system_heap_init, error begin address 0x%x, and end address 0x%x\n", (rt_uint32_t)begin_addr, (rt_uint32_t)end_addr); + return; + } + limsize = heap_end - heap_start; npages = limsize / RT_MM_PAGE_SIZE;