diff --git a/src/mem.c b/src/mem.c index 4261229256d8e6b5d5553f4aef9e382d80693589..307391cac1849fd1c1cec3ece5ab3be9c670bf0f 100644 --- a/src/mem.c +++ b/src/mem.c @@ -299,6 +299,10 @@ void *rt_smem_alloc(rt_smem_t m, rt_size_t size) /* alignment size */ size = RT_ALIGN(size, RT_ALIGN_SIZE); + /* every data block must be at least MIN_SIZE_ALIGNED long */ + if (size < MIN_SIZE_ALIGNED) + size = MIN_SIZE_ALIGNED; + if (size > small_mem->mem_size_aligned) { RT_DEBUG_LOG(RT_DEBUG_MEM, ("no memory\n")); @@ -306,10 +310,6 @@ void *rt_smem_alloc(rt_smem_t m, rt_size_t size) return RT_NULL; } - /* every data block must be at least MIN_SIZE_ALIGNED long */ - if (size < MIN_SIZE_ALIGNED) - size = MIN_SIZE_ALIGNED; - for (ptr = (rt_uint8_t *)small_mem->lfree - small_mem->heap_ptr; ptr <= small_mem->mem_size_aligned - size; ptr = ((struct rt_small_mem_item *)&small_mem->heap_ptr[ptr])->next)