[kernel][mem.c] tighten size before check with mem_size_aligned

This commit is contained in:
rewine 2022-06-07 23:57:18 +08:00 committed by guo
parent 2c10d5ad01
commit c13b03604f
1 changed files with 4 additions and 4 deletions

View File

@ -299,6 +299,10 @@ void *rt_smem_alloc(rt_smem_t m, rt_size_t size)
/* alignment size */ /* alignment size */
size = RT_ALIGN(size, RT_ALIGN_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) if (size > small_mem->mem_size_aligned)
{ {
RT_DEBUG_LOG(RT_DEBUG_MEM, ("no memory\n")); 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; 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; for (ptr = (rt_uint8_t *)small_mem->lfree - small_mem->heap_ptr;
ptr <= small_mem->mem_size_aligned - size; ptr <= small_mem->mem_size_aligned - size;
ptr = ((struct rt_small_mem_item *)&small_mem->heap_ptr[ptr])->next) ptr = ((struct rt_small_mem_item *)&small_mem->heap_ptr[ptr])->next)