diff --git a/include/rtdef.h b/include/rtdef.h index 01667001b..f2f1266de 100644 --- a/include/rtdef.h +++ b/include/rtdef.h @@ -137,9 +137,14 @@ typedef rt_uint32_t rt_off_t; /* Type for offset. */ /** * @def RT_ALIGN(size, align) * Return the most contiguous size aligned at specified width. RT_ALIGN(13, 4) - * would equal to 16. It is needed in some critical contexts. + * would return 16. + * + * @def RT_ALIGN_DOWN(size, align) + * Return the down number of aligned at specified width. RT_ALIGN_DOWN(13, 4) + * would return 12. */ #define RT_ALIGN(size, align) (((size) + (align) - 1) & ~((align)-1)) +#define RT_ALIGN_DOWN(size, align) ((size) & ~((align) -1)) /** * @def RT_NULL diff --git a/src/clock.c b/src/clock.c index 6ca918d1f..f34d09e32 100644 --- a/src/clock.c +++ b/src/clock.c @@ -14,6 +14,7 @@ * 2006-08-10 Bernard remove the last rt_schedule in rt_tick_increase * 2010-03-08 Bernard remove rt_passed_second * 2010-05-20 Bernard fix the tick exceeds the maximum limits + * 2010-07-13 Bernard fix rt_tick_from_millisecond issue found by kuronca */ #include @@ -95,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); + return (RT_TICK_PER_SECOND * ms) / 1000 + (RT_TICK_PER_SECOND * ms) % 1000 ? 1:0; } /*@}*/ diff --git a/src/mem.c b/src/mem.c index cd09bc199..ac091c6f7 100644 --- a/src/mem.c +++ b/src/mem.c @@ -12,6 +12,7 @@ * 2008-7-12 Bernard the first version * 2010-06-09 Bernard fix the end stub of heap * fix memory check in rt_realloc function + * 2010-07-13 Bernard fix RT_ALIGN issue found by kuronca */ /* @@ -170,7 +171,7 @@ void rt_system_heap_init(void* begin_addr, void* end_addr) begin_addr = (void*)RT_ALIGN((rt_uint32_t)begin_addr, RT_ALIGN_SIZE); /* calculate the aligned memory size */ - mem_size_aligned = RT_ALIGN((rt_uint32_t)end_addr - (rt_uint32_t)begin_addr, RT_ALIGN_SIZE) - 2 * SIZEOF_STRUCT_MEM; + mem_size_aligned = RT_ALIGN_DOWN((rt_uint32_t)end_addr - (rt_uint32_t)begin_addr, RT_ALIGN_SIZE) - 2 * SIZEOF_STRUCT_MEM; /* point to begin address of heap */ heap_ptr = begin_addr; @@ -220,7 +221,10 @@ void *rt_malloc(rt_size_t size) if (size == 0) return RT_NULL; #ifdef RT_MEM_DEBUG - rt_kprintf("malloc size %d, but align to %d\n", size, RT_ALIGN(size, RT_ALIGN_SIZE)); + if (size != RT_ALIGN(size, RT_ALIGN_SIZE) + rt_kprintf("malloc size %d, but align to %d\n", size, RT_ALIGN(size, RT_ALIGN_SIZE)); + else + rt_kprintf("malloc size %d\n", size); #endif /* alignment size */ @@ -351,7 +355,7 @@ void *rt_realloc(void *rmem, rt_size_t newsize) if (newsize > mem_size_aligned) { #ifdef RT_MEM_DEBUG - rt_kprintf("no memory\n"); + rt_kprintf("realloc: out of memory\n"); #endif return RT_NULL; } @@ -498,15 +502,15 @@ void rt_free(void *rmem) rt_sem_release(&heap_sem); } -#ifdef RT_MEM_STATS -void rt_memory_info(rt_uint32_t *total, - rt_uint32_t *used, - rt_uint32_t *max_used) -{ - if (total != RT_NULL) *total = mem_size_aligned; - if (used != RT_NULL) *used = used_mem; - if (max_used != RT_NULL) *max_used = max_mem; -} +#ifdef RT_MEM_STATS +void rt_memory_info(rt_uint32_t *total, + rt_uint32_t *used, + rt_uint32_t *max_used) +{ + if (total != RT_NULL) *total = mem_size_aligned; + if (used != RT_NULL) *used = used_mem; + if (max_used != RT_NULL) *max_used = max_mem; +} #ifdef RT_USING_FINSH #include diff --git a/src/mempool.c b/src/mempool.c index 34b4d4c51..53f116280 100644 --- a/src/mempool.c +++ b/src/mempool.c @@ -14,6 +14,7 @@ * 2006-06-30 Bernard fix the allocate/free block bug * 2006-08-04 Bernard add hook support * 2006-08-10 Bernard fix interrupt bug in rt_mp_alloc + * 2010-07-13 Bernard fix RT_ALIGN issue found by kuronca */ #include @@ -88,7 +89,7 @@ rt_err_t rt_mp_init(struct rt_mempool* mp, const char* name, void *start, rt_siz /* init memory pool */ mp->start_address = start; - mp->size = RT_ALIGN(size, RT_ALIGN_SIZE); + mp->size = RT_ALIGN_DOWN(size, RT_ALIGN_SIZE); mp->block_size = block_size; @@ -294,11 +295,11 @@ void *rt_mp_alloc (rt_mp_t mp, rt_int32_t time) else { /* memory block is unavailable. */ - if (time == 0) - { + if (time == 0) + { /* enable interrupt */ rt_hw_interrupt_enable(level); - return RT_NULL; + return RT_NULL; } else { diff --git a/src/scheduler.c b/src/scheduler.c index e5c82279c..be7123437 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -19,7 +19,9 @@ * 2006-09-05 Bernard add 32 priority level support * 2006-09-24 Bernard add rt_system_scheduler_start function * 2009-09-16 Bernard fix _rt_scheduler_stack_check - * 2010-04-11 yi.qiu add module feature + * 2010-04-11 yi.qiu add module feature + * 2010-07-13 Bernard fix the maximal number of rt_scheduler_lock_nest + * issue found by kuronca */ #include @@ -396,8 +398,9 @@ void rt_enter_critical() /* disable interrupt */ level = rt_hw_interrupt_disable(); - if (rt_scheduler_lock_nest < 255u) - rt_scheduler_lock_nest++; + /* the maximal number of nest is RT_UINT16_MAX, which is big + * enough and does not check here */ + rt_scheduler_lock_nest++; /* enable interrupt */ rt_hw_interrupt_enable(level); diff --git a/src/slab.c b/src/slab.c index dff6dd471..216ee1950 100644 --- a/src/slab.c +++ b/src/slab.c @@ -10,6 +10,7 @@ * Change Logs: * Date Author Notes * 2008-07-12 Bernard the first version + * 2010-07-13 Bernard fix RT_ALIGN issue found by kuronca */ /* @@ -326,7 +327,7 @@ void rt_system_heap_init(void *begin_addr, void* end_addr) /* align begin and end addr to page */ heap_start = RT_ALIGN((rt_uint32_t)begin_addr, RT_MM_PAGE_SIZE); - heap_end = RT_ALIGN((rt_uint32_t)end_addr, RT_MM_PAGE_SIZE); + heap_end = RT_ALIGN_DOWN((rt_uint32_t)end_addr, RT_MM_PAGE_SIZE); limsize = heap_end - heap_start; npages = limsize / RT_MM_PAGE_SIZE;