zoukankan      html  css  js  c++  java
  • rt-thread中动态内存分配之小内存管理模块方法的一点理解

    @2019-01-18

    【小记】

    rt-thread中动态内存分配之小内存管理模块方法的一点理解

    > 内存初始化后的布局示意

    lfree指向内存空闲区首地址

     1 /**
     2  * @ingroup SystemInit
     3  *
     4  * This function will initialize system heap memory.
     5  *
     6  * @param begin_addr the beginning address of system heap memory.
     7  * @param end_addr the end address of system heap memory.
     8  */
     9 void rt_system_heap_init(void *begin_addr, void *end_addr)
    10 {
    11     struct heap_mem *mem;
    12     rt_ubase_t begin_align = RT_ALIGN((rt_ubase_t)begin_addr, RT_ALIGN_SIZE);
    13     rt_ubase_t end_align   = RT_ALIGN_DOWN((rt_ubase_t)end_addr, RT_ALIGN_SIZE);
    14 
    15     RT_DEBUG_NOT_IN_INTERRUPT;
    16 
    17     /* alignment addr */
    18     if ((end_align > (2 * SIZEOF_STRUCT_MEM)) &&
    19         ((end_align - 2 * SIZEOF_STRUCT_MEM) >= begin_align))
    20     {
    21         /* calculate the aligned memory size */
    22         mem_size_aligned = end_align - begin_align - 2 * SIZEOF_STRUCT_MEM;        
    23     }                                                                            
    24     else
    25     {
    26         rt_kprintf("mem init, error begin address 0x%x, and end address 0x%x
    ",
    27                    (rt_ubase_t)begin_addr, (rt_ubase_t)end_addr);
    28 
    29         return;
    30     }
    31 
    32     /* point to begin address of heap */
    33     heap_ptr = (rt_uint8_t *)begin_align;
    34 
    35     RT_DEBUG_LOG(RT_DEBUG_MEM, ("mem init, heap begin address 0x%x, size %d
    ",
    36                                 (rt_ubase_t)heap_ptr, mem_size_aligned));
    37 
    38     /* initialize the start of the heap */
    39     mem        = (struct heap_mem *)heap_ptr;
    40     mem->magic = HEAP_MAGIC;
    41     mem->next  = mem_size_aligned + SIZEOF_STRUCT_MEM;
    42     mem->prev  = 0;
    43     mem->used  = 0;
    44 #ifdef RT_USING_MEMTRACE
    45     rt_mem_setname(mem, "INIT");
    46 #endif
    47 
    48     /* initialize the end of the heap */
    49     heap_end        = (struct heap_mem *)&heap_ptr[mem->next];
    50     heap_end->magic = HEAP_MAGIC;
    51     heap_end->used  = 1;
    52     heap_end->next  = mem_size_aligned + SIZEOF_STRUCT_MEM;
    53     heap_end->prev  = mem_size_aligned + SIZEOF_STRUCT_MEM;
    54 #ifdef RT_USING_MEMTRACE
    55     rt_mem_setname(heap_end, "INIT");
    56 #endif
    57 
    58     rt_sem_init(&heap_sem, "heap", 1, RT_IPC_FLAG_FIFO);
    59 
    60     /* initialize the lowest-free pointer to the start of the heap */
    61     lfree = (struct heap_mem *)heap_ptr;
    62 }
  • 相关阅读:
    C++窗体应用程序
    C++继承方式简介及公有继承
    C++(继承的基本概念和语法)
    python(12)---科赫特雪花
    python(11)---pyinstaller
    20200914 day9 数据结构复习(一)
    20200914 day9 刷题记录
    20200913 day8模拟(二)
    20200912 day7 刷题记录
    20200912 day7 图论复习(一)
  • 原文地址:https://www.cnblogs.com/skullboyer/p/10287177.html
Copyright © 2011-2022 走看看