MCPcopy Create free account
hub / github.com/RT-Thread/rt-thread / rt_smem_free

Function rt_smem_free

src/mem.c:497–539  ·  view source on GitHub ↗

* @brief This function will release the previously allocated memory block by * rt_mem_alloc. The released memory block is taken back to system heap. * * @param rmem the address of memory which will be released. */

Source from the content-addressed store, hash-verified

495 * @param rmem the address of memory which will be released.
496 */
497void rt_smem_free(void *rmem)
498{
499 struct rt_small_mem_item *mem;
500 struct rt_small_mem *small_mem;
501
502 if (rmem == RT_NULL)
503 return;
504
505 RT_ASSERT((((rt_uintptr_t)rmem) & (RT_ALIGN_SIZE - 1)) == 0);
506
507 /* Get the corresponding struct rt_small_mem_item ... */
508 mem = (struct rt_small_mem_item *)((rt_uint8_t *)rmem - SIZEOF_STRUCT_MEM);
509 /* ... which has to be in a used state ... */
510 small_mem = MEM_POOL(mem);
511 RT_ASSERT(small_mem != RT_NULL);
512 RT_ASSERT(MEM_ISUSED(mem));
513 RT_ASSERT(rt_object_get_type(&small_mem->parent.parent) == RT_Object_Class_Memory);
514 RT_ASSERT(rt_object_is_systemobject(&small_mem->parent.parent));
515 RT_ASSERT((rt_uint8_t *)rmem >= (rt_uint8_t *)small_mem->heap_ptr &&
516 (rt_uint8_t *)rmem < (rt_uint8_t *)small_mem->heap_end);
517 RT_ASSERT(MEM_POOL(&small_mem->heap_ptr[mem->next]) == small_mem);
518
519 LOG_D("release memory 0x%x, size: %d",
520 (rt_uintptr_t)rmem,
521 (rt_uintptr_t)(mem->next - ((rt_uint8_t *)mem - small_mem->heap_ptr)));
522
523 /* ... and is now unused. */
524 mem->pool_ptr = MEM_FREED(small_mem);
525#ifdef RT_USING_MEMTRACE
526 rt_smem_setname(mem, " ");
527#endif /* RT_USING_MEMTRACE */
528
529 if (mem < small_mem->lfree)
530 {
531 /* the newly freed struct is now the lowest */
532 small_mem->lfree = mem;
533 }
534
535 small_mem->parent.used -= (mem->next - ((rt_uint8_t *)mem - small_mem->heap_ptr));
536
537 /* finally, see if prev or next are free also */
538 plug_holes(small_mem, mem);
539}
540RTM_EXPORT(rt_smem_free);
541
542#ifdef RT_USING_FINSH

Callers 3

rt_smem_reallocFunction · 0.85
mem_functional_testFunction · 0.85
mem_alloc_testFunction · 0.85

Calls 4

rt_object_get_typeFunction · 0.85
rt_smem_setnameFunction · 0.85
plug_holesFunction · 0.70

Tested by

no test coverage detected