RT-Thread系列--内存堆MEMHEAP源码分析

news2025/1/20 7:09:06

一、目的

在上一篇《RT-Thread系列--内存池MEMPOOL源码分析》我们介绍了RT-Thread的内存池MEMPOOL实现细节,包括内存池结构和内存块管理。

本篇我们来分析一下RT-Thread的内存堆MEMHEAP的实现原理。

内存堆MEMHEAP的特点:

  • 按需分配,需要多少内存就分配多少内存(存在最小内存块限制);

  • 内存回收时一般会进行头部和尾部拼接,尽量减少内存碎片

  • 内存容易碎片化

  • 实现较为复杂

  • 支持多内存堆

在实际应用中我们可以创建多个MEMHEAP,例如memheap A的内存是noncached的,memheap B的内存是DMA属性类型的。

二、介绍

在分析实现原理之前,我们首先分析一下MEMHEAP两个重要的数据结构,分别为:

  • struct rt_memheap

  • struct rt_memheap_item

struct rt_memheap
{
    struct rt_object        parent;                     /**< inherit from rt_object */

    void                   *start_addr;                 /**< pool start address and size */

    rt_size_t               pool_size;                  /**< pool size */
    rt_size_t               available_size;             /**< available size */
    rt_size_t               max_used_size;              /**< maximum allocated size */

    struct rt_memheap_item *block_list;                 /**< used block list */

    struct rt_memheap_item *free_list;                  /**< free block list */
    struct rt_memheap_item  free_header;                /**< free block list header */

    struct rt_semaphore     lock;                       /**< semaphore lock */
    rt_bool_t               locked;                     /**< External lock mark */
};

各个字段的含义:

parent:每个memheap对象都是一个struct rt_object类型的对象(基类),通过parent字段可以将memheap统一管理;

start_addr:当前内存堆所管理的整个内存的首地址

pool_size:当前内存堆所管理的整个内存的大小

available_size:可分配的内存大小

max_used_size:已经使用的内存大小

block_list:指向当前已使用内存块

free_list:指向当前未使用内存块

free_header:空闲内存块的链表头

lock:互斥锁

locked:是否已经上锁


内存堆虽然是按需分配,但是每块分配出来的内存都需要一个数据结构用于管理此内存块;实际每次分配占用的内存大小为:所需内存大小(对齐)+内存块头部结构(对齐)两者之和。

struct rt_memheap_item
{
    rt_uint32_t             magic;                      /**< magic number for memheap */
    struct rt_memheap      *pool_ptr;                   /**< point of pool */

    struct rt_memheap_item *next;                       /**< next memheap item */
    struct rt_memheap_item *prev;                       /**< prev memheap item */

    struct rt_memheap_item *next_free;                  /**< next free memheap item */
    struct rt_memheap_item *prev_free;                  /**< prev free memheap item */
#ifdef RT_USING_MEMTRACE
    rt_uint8_t              owner_thread_name[4];       /**< owner thread name */
#endif
};

各字段含义:

magic:用于标记此块内存是内存块首部

pool_ptr:指向此内存块从属的堆对象

next:指向下一个memheap item

prev:指向上一个memheap item

next_free:指向下一个空闲内存块

prev_free:指向上一次空闲内存块

通过next/prev指针将已经分配的内存块连接成链表;通过next_free/prev_free指针将未分配的内存连接成空闲内存链表


宏定义说明

#define RT_MEMHEAP_MINIALLOC    12

#define RT_MEMHEAP_SIZE         RT_ALIGN(sizeof(struct rt_memheap_item), RT_ALIGN_SIZE)
#define MEMITEM_SIZE(item)      ((rt_ubase_t)item->next - (rt_ubase_t)item - RT_MEMHEAP_SIZE)
#define MEMITEM(ptr)            (struct rt_memheap_item*)((rt_uint8_t*)ptr - RT_MEMHEAP_SIZE)

RT_MEMHEAP_MINIALLOC定义最小的内存分配大小

RT_MEMHEAP_SIZE定义每个内存块管理结构的大小(对齐)

MEMITEM_SIZE(item)获取当前内存块管理的内存大小

MEMITEM(ptr) 通过ptr获取当前内存块头的地址


内存堆的初始化,内存堆在分配之前必须对此内存堆管理的内存进行初始化,主要包括空闲链表的初始化、各个指针的初始化等

rt_err_t rt_memheap_init(struct rt_memheap *memheap,
                         const char        *name,
                         void              *start_addr,
                         rt_size_t         size)
{
    struct rt_memheap_item *item;

    RT_ASSERT(memheap != RT_NULL);

    /* initialize pool object */
    rt_object_init(&(memheap->parent), RT_Object_Class_MemHeap, name);

    memheap->start_addr     = start_addr;                                //内存首地址
    memheap->pool_size      = RT_ALIGN_DOWN(size, RT_ALIGN_SIZE);        //对齐
    // 此处减去(2 * RT_MEMHEAP_SIZE)的目的是第一个空闲块需要一个头部,最后一个内存块(实际内存长度为0)也需要一个管理头部
    memheap->available_size = memheap->pool_size - (2 * RT_MEMHEAP_SIZE); 
    memheap->max_used_size  = memheap->pool_size - memheap->available_size;

    // 对free_header进行初始化
    // next_free/prev_free指向自身
    // prev/next指向NULL
    /* initialize the free list header */
    item            = &(memheap->free_header);
    item->magic     = (RT_MEMHEAP_MAGIC | RT_MEMHEAP_FREED);
    item->pool_ptr  = memheap;
    item->next      = RT_NULL;
    item->prev      = RT_NULL;
    item->next_free = item;
    item->prev_free = item;

    /* set the free list to free list header */
    memheap->free_list = item;

    /* initialize the first big memory block */
    item            = (struct rt_memheap_item *)start_addr;  //指向内存首地址
    item->magic     = (RT_MEMHEAP_MAGIC | RT_MEMHEAP_FREED);
    item->pool_ptr  = memheap;
    item->next      = RT_NULL;
    item->prev      = RT_NULL;
    item->next_free = item;
    item->prev_free = item;

#ifdef RT_USING_MEMTRACE
    rt_memset(item->owner_thread_name, ' ', sizeof(item->owner_thread_name));
#endif /* RT_USING_MEMTRACE */

    item->next = (struct rt_memheap_item *)
                 ((rt_uint8_t *)item + memheap->available_size + RT_MEMHEAP_SIZE); //指向最后一个内存块
    item->prev = item->next; //指向最后一个内存块

    /* block list header */
    memheap->block_list = item;

    /* place the big memory block to free list */
    item->next_free = memheap->free_list->next_free;
    item->prev_free = memheap->free_list;
    memheap->free_list->next_free->prev_free = item;
    memheap->free_list->next_free            = item;

    /* move to the end of memory pool to build a small tailer block,
     * which prevents block merging
     */
    item = item->next;
    /* it's a used memory block */
    item->magic     = (RT_MEMHEAP_MAGIC | RT_MEMHEAP_USED);
    item->pool_ptr  = memheap;
    item->next      = (struct rt_memheap_item *)start_addr;
    item->prev      = (struct rt_memheap_item *)start_addr;
    /* not in free list */
    item->next_free = item->prev_free = RT_NULL;

    /* initialize semaphore lock */
    rt_sem_init(&(memheap->lock), name, 1, RT_IPC_FLAG_PRIO);
    memheap->locked = RT_FALSE;

    RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
                 ("memory heap: start addr 0x%08x, size %d, free list header 0x%08x\n",
                  start_addr, size, &(memheap->free_header)));

    return RT_EOK;
}

初始化后内存堆内部结构如下图


从上图我们可以清晰看到,内存堆中最大的可分配的内存块为蓝色的块结构A管理的内存(内存状态为RT_MEMHEAP_FREED),大小为available_size;

其next/prev指向黄色内存块B(内存状态为RT_MEMHEAP_USED);并且rt_memheap_item B的next/prev也指向内存块A;

其next_free/prev_free指向rt_memheap_item C块结构,也就是free_header;并且free_header的next_free/prev_free也指向内存块A


内存的分配

void *rt_memheap_alloc(struct rt_memheap *heap, rt_size_t size)
{
    rt_err_t result;
    rt_size_t free_size;
    struct rt_memheap_item *header_ptr;

    RT_ASSERT(heap != RT_NULL);
    RT_ASSERT(rt_object_get_type(&heap->parent) == RT_Object_Class_MemHeap);

    // 最小内存块大小判断
    /* align allocated size */
    size = RT_ALIGN(size, RT_ALIGN_SIZE);
    if (size < RT_MEMHEAP_MINIALLOC)
        size = RT_MEMHEAP_MINIALLOC;

    RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("allocate %d on heap:%8.*s",
                                    size, RT_NAME_MAX, heap->parent.name));

    if (size < heap->available_size)
    {
        /* search on free list */
        free_size = 0;

        /* lock memheap */
        if (heap->locked == RT_FALSE)
        {
            result = rt_sem_take(&(heap->lock), RT_WAITING_FOREVER);
            if (result != RT_EOK)
            {
                rt_set_errno(result);

                return RT_NULL;
            }
        }

        /* get the first free memory block */
        header_ptr = heap->free_list->next_free;
        while (header_ptr != heap->free_list && free_size < size)
        {
            /* get current freed memory block size */
            free_size = MEMITEM_SIZE(header_ptr);
            if (free_size < size)
            {
                /* move to next free memory block */
                header_ptr = header_ptr->next_free;
            }
        }

        /* determine if the memory is available. */
        if (free_size >= size)
        {
            /* a block that satisfies the request has been found. */

            /* determine if the block needs to be split. */
            if (free_size >= (size + RT_MEMHEAP_SIZE + RT_MEMHEAP_MINIALLOC))
            {
                struct rt_memheap_item *new_ptr;

                /* split the block. */
                new_ptr = (struct rt_memheap_item *)
                          (((rt_uint8_t *)header_ptr) + size + RT_MEMHEAP_SIZE);

                RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
                             ("split: block[0x%08x] nextm[0x%08x] prevm[0x%08x] to new[0x%08x]\n",
                              header_ptr,
                              header_ptr->next,
                              header_ptr->prev,
                              new_ptr));

                /* mark the new block as a memory block and freed. */
                new_ptr->magic = (RT_MEMHEAP_MAGIC | RT_MEMHEAP_FREED);

                /* put the pool pointer into the new block. */
                new_ptr->pool_ptr = heap;

#ifdef RT_USING_MEMTRACE
                rt_memset(new_ptr->owner_thread_name, ' ', sizeof(new_ptr->owner_thread_name));
#endif /* RT_USING_MEMTRACE */

                /* break down the block list */
                new_ptr->prev          = header_ptr;
                new_ptr->next          = header_ptr->next;
                header_ptr->next->prev = new_ptr;
                header_ptr->next       = new_ptr;

                /* remove header ptr from free list */
                header_ptr->next_free->prev_free = header_ptr->prev_free;
                header_ptr->prev_free->next_free = header_ptr->next_free;
                header_ptr->next_free = RT_NULL;
                header_ptr->prev_free = RT_NULL;

                /* insert new_ptr to free list */
                new_ptr->next_free = heap->free_list->next_free;
                new_ptr->prev_free = heap->free_list;
                heap->free_list->next_free->prev_free = new_ptr;
                heap->free_list->next_free            = new_ptr;
                RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("new ptr: next_free 0x%08x, prev_free 0x%08x\n",
                                                new_ptr->next_free,
                                                new_ptr->prev_free));

                /* decrement the available byte count.  */
                heap->available_size = heap->available_size -
                                       size -
                                       RT_MEMHEAP_SIZE;
                if (heap->pool_size - heap->available_size > heap->max_used_size)
                    heap->max_used_size = heap->pool_size - heap->available_size;
            }
            else
            {
                /* decrement the entire free size from the available bytes count. */
                heap->available_size = heap->available_size - free_size;
                if (heap->pool_size - heap->available_size > heap->max_used_size)
                    heap->max_used_size = heap->pool_size - heap->available_size;

                /* remove header_ptr from free list */
                RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
                             ("one block: block[0x%08x], next_free 0x%08x, prev_free 0x%08x\n",
                              header_ptr,
                              header_ptr->next_free,
                              header_ptr->prev_free));

                header_ptr->next_free->prev_free = header_ptr->prev_free;
                header_ptr->prev_free->next_free = header_ptr->next_free;
                header_ptr->next_free = RT_NULL;
                header_ptr->prev_free = RT_NULL;
            }

            /* Mark the allocated block as not available. */
            header_ptr->magic = (RT_MEMHEAP_MAGIC | RT_MEMHEAP_USED);

#ifdef RT_USING_MEMTRACE
            if (rt_thread_self())
                rt_memcpy(header_ptr->owner_thread_name, rt_thread_self()->name, sizeof(header_ptr->owner_thread_name));
            else
                rt_memcpy(header_ptr->owner_thread_name, "NONE", sizeof(header_ptr->owner_thread_name));
#endif /* RT_USING_MEMTRACE */

            if (heap->locked == RT_FALSE)
            {
                /* release lock */
                rt_sem_release(&(heap->lock));
            }

            /* Return a memory address to the caller.  */
            RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
                         ("alloc mem: memory[0x%08x], heap[0x%08x], size: %d\n",
                          (void *)((rt_uint8_t *)header_ptr + RT_MEMHEAP_SIZE),
                          header_ptr,
                          size));

            return (void *)((rt_uint8_t *)header_ptr + RT_MEMHEAP_SIZE);
        }

        if (heap->locked == RT_FALSE)
        {
            /* release lock */
            rt_sem_release(&(heap->lock));
        }
    }

    RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("allocate memory: failed\n"));

    /* Return the completion status.  */
    return RT_NULL;
}

首先对需要的内存的size进行大小限定,然后判断是否小于heap->available_size;否则直接返回NULL

    /* get the first free memory block */
        header_ptr = heap->free_list->next_free;
        while (header_ptr != heap->free_list && free_size < size)
        {
            /* get current freed memory block size */
            free_size = MEMITEM_SIZE(header_ptr);
            if (free_size < size)
            {
                /* move to next free memory block */
                header_ptr = header_ptr->next_free;
            }
        }

获取空闲内存块的首地址,然后遍历空闲内存块,找到空闲内存块大小不小于size的内存块的地址(注意此处从头往后找,只要找到大小足够大的内存块就认为找到,但未必是大小相匹配的,所以存在内存碎片的问题)。

if (free_size >= (size + RT_MEMHEAP_SIZE + RT_MEMHEAP_MINIALLOC)) {
} else {
}

判断当前空闲块的内存减去size后是否仍然足够放下最小内存块(RT_MEMHEAP_SIZE + RT_MEMHEAP_MINIALLOC),如果足够则需要对此块内存进行切割;否则直接将这块内存分配出来。

下面我们分析一下切割操作:

                struct rt_memheap_item *new_ptr;

                /* split the block. */
                new_ptr = (struct rt_memheap_item *)
                          (((rt_uint8_t *)header_ptr) + size + RT_MEMHEAP_SIZE);

                RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
                             ("split: block[0x%08x] nextm[0x%08x] prevm[0x%08x] to new[0x%08x]\n",
                              header_ptr,
                              header_ptr->next,
                              header_ptr->prev,
                              new_ptr));

                /* mark the new block as a memory block and freed. */
                new_ptr->magic = (RT_MEMHEAP_MAGIC | RT_MEMHEAP_FREED);

                /* put the pool pointer into the new block. */
                new_ptr->pool_ptr = heap;

#ifdef RT_USING_MEMTRACE
                rt_memset(new_ptr->owner_thread_name, ' ', sizeof(new_ptr->owner_thread_name));
#endif /* RT_USING_MEMTRACE */

                /* break down the block list */
                new_ptr->prev          = header_ptr;
                new_ptr->next          = header_ptr->next;
                header_ptr->next->prev = new_ptr;
                header_ptr->next       = new_ptr;

                /* remove header ptr from free list */
                header_ptr->next_free->prev_free = header_ptr->prev_free;
                header_ptr->prev_free->next_free = header_ptr->next_free;
                header_ptr->next_free = RT_NULL;
                header_ptr->prev_free = RT_NULL;

                /* insert new_ptr to free list */
                new_ptr->next_free = heap->free_list->next_free;
                new_ptr->prev_free = heap->free_list;
                heap->free_list->next_free->prev_free = new_ptr;
                heap->free_list->next_free            = new_ptr;
                RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("new ptr: next_free 0x%08x, prev_free 0x%08x\n",
                                                new_ptr->next_free,
                                                new_ptr->prev_free));

                /* decrement the available byte count.  */
                heap->available_size = heap->available_size -
                                       size -
                                       RT_MEMHEAP_SIZE;
                if (heap->pool_size - heap->available_size > heap->max_used_size)
                    heap->max_used_size = heap->pool_size - heap->available_size;

获取切割后剩余块的首地址并设置内存状态为RT_MEMHEAP_FREED

new_ptr = (struct rt_memheap_item *)
                          (((rt_uint8_t *)header_ptr) + size + RT_MEMHEAP_SIZE);
                /* mark the new block as a memory block and freed. */
                new_ptr->magic = (RT_MEMHEAP_MAGIC | RT_MEMHEAP_FREED);

                /* put the pool pointer into the new block. */
                new_ptr->pool_ptr = heap;

切割后new_ptr是新的空闲内存块,需要挂载到空闲链表中

 /* break down the block list */
                new_ptr->prev          = header_ptr;
                new_ptr->next          = header_ptr->next;
                header_ptr->next->prev = new_ptr;
                header_ptr->next       = new_ptr;

                /* remove header ptr from free list */
                header_ptr->next_free->prev_free = header_ptr->prev_free;
                header_ptr->prev_free->next_free = header_ptr->next_free;
                header_ptr->next_free = RT_NULL;
                header_ptr->prev_free = RT_NULL;

                /* insert new_ptr to free list */
                new_ptr->next_free = heap->free_list->next_free;
                new_ptr->prev_free = heap->free_list;
                heap->free_list->next_free->prev_free = new_ptr;
                heap->free_list->next_free            = new_ptr;

内存分配后的数据结构如下


最后我们再来分析一下rt_memheap_free的实现

void rt_memheap_free(void *ptr)
{
    rt_err_t result;
    struct rt_memheap *heap;
    struct rt_memheap_item *header_ptr, *new_ptr;
    rt_bool_t insert_header;

    /* NULL check */
    if (ptr == RT_NULL) return;

    /* set initial status as OK */
    insert_header = RT_TRUE;
    new_ptr       = RT_NULL;
    header_ptr    = (struct rt_memheap_item *)
                    ((rt_uint8_t *)ptr - RT_MEMHEAP_SIZE);

    RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("free memory: memory[0x%08x], block[0x%08x]\n",
                                    ptr, header_ptr));

    /* check magic */
    if (header_ptr->magic != (RT_MEMHEAP_MAGIC | RT_MEMHEAP_USED) ||
       (header_ptr->next->magic & RT_MEMHEAP_MASK) != RT_MEMHEAP_MAGIC)
    {
        RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("bad magic:0x%08x @ memheap\n",
                                        header_ptr->magic));
        RT_ASSERT(header_ptr->magic == (RT_MEMHEAP_MAGIC | RT_MEMHEAP_USED));
        /* check whether this block of memory has been over-written. */
        RT_ASSERT((header_ptr->next->magic & RT_MEMHEAP_MASK) == RT_MEMHEAP_MAGIC);
    }

    /* get pool ptr */
    heap = header_ptr->pool_ptr;

    RT_ASSERT(heap);
    RT_ASSERT(rt_object_get_type(&heap->parent) == RT_Object_Class_MemHeap);

    if (heap->locked == RT_FALSE)
    {
        /* lock memheap */
        result = rt_sem_take(&(heap->lock), RT_WAITING_FOREVER);
        if (result != RT_EOK)
        {
            rt_set_errno(result);

            return ;
        }
    }

    /* Mark the memory as available. */
    header_ptr->magic = (RT_MEMHEAP_MAGIC | RT_MEMHEAP_FREED);
    /* Adjust the available number of bytes. */
    heap->available_size += MEMITEM_SIZE(header_ptr);

    /* Determine if the block can be merged with the previous neighbor. */
    if (!RT_MEMHEAP_IS_USED(header_ptr->prev))
    {
        RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("merge: left node 0x%08x\n",
                                        header_ptr->prev));

        /* adjust the available number of bytes. */
        heap->available_size += RT_MEMHEAP_SIZE;

        /* yes, merge block with previous neighbor. */
        (header_ptr->prev)->next = header_ptr->next;
        (header_ptr->next)->prev = header_ptr->prev;

        /* move header pointer to previous. */
        header_ptr = header_ptr->prev;
        /* don't insert header to free list */
        insert_header = RT_FALSE;
    }

    /* determine if the block can be merged with the next neighbor. */
    if (!RT_MEMHEAP_IS_USED(header_ptr->next))
    {
        /* adjust the available number of bytes. */
        heap->available_size += RT_MEMHEAP_SIZE;

        /* merge block with next neighbor. */
        new_ptr = header_ptr->next;

        RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
                     ("merge: right node 0x%08x, next_free 0x%08x, prev_free 0x%08x\n",
                      new_ptr, new_ptr->next_free, new_ptr->prev_free));

        new_ptr->next->prev = header_ptr;
        header_ptr->next    = new_ptr->next;

        /* remove new ptr from free list */
        new_ptr->next_free->prev_free = new_ptr->prev_free;
        new_ptr->prev_free->next_free = new_ptr->next_free;
    }

    if (insert_header)
    {
        struct rt_memheap_item *n = heap->free_list->next_free;;
#if defined(RT_MEMHEAP_BSET_MODE)
        rt_size_t blk_size = MEMITEM_SIZE(header_ptr);
        for (;n != heap->free_list; n = n->next_free)
        {
            rt_size_t m = MEMITEM_SIZE(n);
            if (blk_size <= m)
            {
                break;
            }
        }
#endif
        /* no left merge, insert to free list */
        header_ptr->next_free = n;
        header_ptr->prev_free = n->prev_free;
        n->prev_free->next_free = header_ptr;
        n->prev_free = header_ptr;

        RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
                     ("insert to free list: next_free 0x%08x, prev_free 0x%08x\n",
                      header_ptr->next_free, header_ptr->prev_free));
    }

#ifdef RT_USING_MEMTRACE
    rt_memset(header_ptr->owner_thread_name, ' ', sizeof(header_ptr->owner_thread_name));
#endif /* RT_USING_MEMTRACE */

    if (heap->locked == RT_FALSE)
    {
        /* release lock */
        rt_sem_release(&(heap->lock));
    }
}

其中代码片段用于判断需要释放的内存是否是合法内存

header_ptr    = (struct rt_memheap_item *)
                    ((rt_uint8_t *)ptr - RT_MEMHEAP_SIZE);

    RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("free memory: memory[0x%08x], block[0x%08x]\n",
                                    ptr, header_ptr));

    /* check magic */
    if (header_ptr->magic != (RT_MEMHEAP_MAGIC | RT_MEMHEAP_USED) ||
       (header_ptr->next->magic & RT_MEMHEAP_MASK) != RT_MEMHEAP_MAGIC)
    {
        RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("bad magic:0x%08x @ memheap\n",
                                        header_ptr->magic));
        RT_ASSERT(header_ptr->magic == (RT_MEMHEAP_MAGIC | RT_MEMHEAP_USED));
        /* check whether this block of memory has been over-written. */
        RT_ASSERT((header_ptr->next->magic & RT_MEMHEAP_MASK) == RT_MEMHEAP_MAGIC);
    }

判断当前释放的内存块前一个内存块是否是空闲内存块,如果是则拼接在一起


    /* Determine if the block can be merged with the previous neighbor. */
    if (!RT_MEMHEAP_IS_USED(header_ptr->prev))
    {
        RT_DEBUG_LOG(RT_DEBUG_MEMHEAP, ("merge: left node 0x%08x\n",
                                        header_ptr->prev));

        /* adjust the available number of bytes. */
        heap->available_size += RT_MEMHEAP_SIZE;

        /* yes, merge block with previous neighbor. */
        (header_ptr->prev)->next = header_ptr->next;
        (header_ptr->next)->prev = header_ptr->prev;

        /* move header pointer to previous. */
        header_ptr = header_ptr->prev;
        /* don't insert header to free list */
        insert_header = RT_FALSE;
    }

判断当前释放的内存块下一个内存块是否是空闲内存块,如果是则拼接在一起

 /* determine if the block can be merged with the next neighbor. */
    if (!RT_MEMHEAP_IS_USED(header_ptr->next))
    {
        /* adjust the available number of bytes. */
        heap->available_size += RT_MEMHEAP_SIZE;

        /* merge block with next neighbor. */
        new_ptr = header_ptr->next;

        RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
                     ("merge: right node 0x%08x, next_free 0x%08x, prev_free 0x%08x\n",
                      new_ptr, new_ptr->next_free, new_ptr->prev_free));

        new_ptr->next->prev = header_ptr;
        header_ptr->next    = new_ptr->next;

        /* remove new ptr from free list */
        new_ptr->next_free->prev_free = new_ptr->prev_free;
        new_ptr->prev_free->next_free = new_ptr->next_free;
    }

如果当前的内存块前一个内存块非空闲,那么当前释放的内存块是需要放到空闲链表中

if (insert_header)
    {
        struct rt_memheap_item *n = heap->free_list->next_free;;
#if defined(RT_MEMHEAP_BSET_MODE)
        rt_size_t blk_size = MEMITEM_SIZE(header_ptr);
        for (;n != heap->free_list; n = n->next_free)
        {
            rt_size_t m = MEMITEM_SIZE(n);
            if (blk_size <= m)
            {
                break;
            }
        }
#endif
        /* no left merge, insert to free list */
        header_ptr->next_free = n;
        header_ptr->prev_free = n->prev_free;
        n->prev_free->next_free = header_ptr;
        n->prev_free = header_ptr;

        RT_DEBUG_LOG(RT_DEBUG_MEMHEAP,
                     ("insert to free list: next_free 0x%08x, prev_free 0x%08x\n",
                      header_ptr->next_free, header_ptr->prev_free));
    }

至此,我们基本就分析完MEMHEAP的实现。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/161406.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

小伟婚后首次看望丈母娘,大衣哥准备了一大三轮车礼品,太豪横了

伴随着春节的临近&#xff0c;大衣哥的儿子小伟&#xff0c; 又一次迎来自己的高光时刻&#xff0c;他和未婚妻一起携手&#xff0c;步入了神圣的婚姻殿堂&#xff0c;大衣哥也长出一口气。儿子小伟再婚&#xff0c;大衣哥完成了一小半任务&#xff0c;接下来还要生孩子&#x…

【Python基础篇】Python中的七种数据类型

文章目录前导一、数字类型&#xff08;Number&#xff09;二、布尔类型&#xff08;Boolean&#xff09;三、字符串类型&#xff08;String&#xff09;3.1 字符串索引3.2 字符串截取、拼接和复用四、列表类型&#xff08;List&#xff09;4.1 in和not in4.2 len()4.3 添加/删除…

Three.js 打造酷炫的三角形

在前一章中&#xff0c;我们通过 BufferGeometry 创建了一个正方形&#xff0c;本节我们利用 BufferGeometry 打造一簇酷炫的三角形&#xff0c;最终的效果如下图所示&#xff1a; 从效果图中可以发现&#xff0c;每一个三角形的位置、大小、颜色都是随机的。 // 共有50个三角…

医疗影像工具LEADTOOLS 入门教程: 检测和提取机读旅行证件 - 控制台 C#

LEADTOOLS 是一个综合工具包的集合&#xff0c;用于将识别、文档、医疗、成像和多媒体技术整合到桌面、服务器、平板电脑、网络和移动解决方案中&#xff0c;是一项企业级文档自动化解决方案&#xff0c;有捕捉&#xff0c;OCR&#xff0c;OMR&#xff0c;表单识别和处理&#…

专注分布式架构 莱商银行的数字化转型实践

本文刊登于《金融电子化》杂志 2022 年 12 月下&#xff0c;作者魏汝浩为莱商银行首席信息官。2021 年 12 月 29 日&#xff0c;中国人民银行印发《金融科技发展规划&#xff08;2022—2025 年&#xff09;》&#xff08;银发〔2021〕335 号&#xff09;&#xff0c;提出新时期…

分享166个ASP源码,总有一款适合您

166个ASP源码下载链接&#xff1a;https://pan.baidu.com/s/19W7FmY9nADL1Rge8e5uqiQ?pwdskhh 提取码&#xff1a;skhh import os import shutil import time from time import sleepimport requests from bs4 import BeautifulSoup from docx import Document from docx.sh…

Shader从入门到放弃(四) —— 绘制闪耀星际

前言 经过3个章节的学习&#xff0c;相信大家对shader编程也逐渐的有了一些感觉&#xff0c;所以这次我们玩个“大”的&#xff01;。 今天的学习内容是绘制“闪耀星际”&#xff0c;正如歌中唱的那样&#xff1a; 星际闪耀光影&#xff0c;落入你的眼睛&#xff0c;如迷人的…

Dubbo泛化调用

Dubbo泛化调用 1. 场景 场景一&#xff1a;我们要搭建一个统一的测试平台&#xff0c;可以让各个业务方在测试平台中通过输入接口、分组名、方法名以及参数值&#xff0c;在线测试自己发布的 RPC 服务。这时我们就有一个问题要解决&#xff0c;我们搭建统一的测试平台实际上是…

【论文速递】CVPR2022 - 泛化的小样本语义分割

【论文速递】CVPR2022 - 泛化的小样本语义分割 【论文原文】&#xff1a;Generalized Few-shot Semantic Segmentation 获取地址&#xff1a;https://openaccess.thecvf.com/content/CVPR2022/papers/Tian_Generalized_Few-Shot_Semantic_Segmentation_CVPR_2022_paper.pdf博…

【ROS2 入门】Jeston TX1 JetPack_4.6.3环境 ubuntu 18.04 ROS2 安装

大家好&#xff0c;我是虎哥&#xff0c;从今天开始&#xff0c;我将花一段时间&#xff0c;开始将自己从ROS1切换到ROS2&#xff0c;在上一篇中&#xff0c;我们再虚拟机环境中安装了 ROS2 eloquent版本&#xff0c;并完成了初步的验证&#xff0c;但是做为一个偏硬件的博主&a…

nginx禁止外网访问

1、安装 libmaxminddb 库 apt updateapt install libmaxminddb0 libmaxminddb-dev mmdb-bin上面安装的软件包是&#xff1a; libmaxminddb0 libmaxminddb-dev 是MaxMind地理定位数据库mmdb-bin – 二进制。 从命令行调用的程序。 使用此命令手动定位 IP 安装参考 2、下载geoi…

node学习笔记

阶段一 1 初始Node.js javascript 运行环境 1.2 Node.js中的javacript 运行环境 1.3 Node.js环境安装 百度 1.4 node.js 执行javaScript 代码 2 fs文件系统模块 2.1 fs文件系统模块概念 导入文件系统模块&#xff1a; const fs require(fs)fs.readFile() // 1 导入fs文件…

【进阶C语言】通讯录(后期会升级)

文章目录一.基本框架与功能二.头文件的详细内容三.函数的实现1.打印菜单2.初始化通讯录3.添加联系人信息4.打印联系人信息5.查找名字6.删除联系人信息7.查找联系人8.修改联系人信息9.排序联系人&#xff08;按照名字&#xff09;四.总结1.test.c2.contact.c3.contact.h一.基本框…

智能电风扇(stm32f103c8t6)(直流电机,热敏传感器)(TIM,ADC)

前言 我的毕业论文的课题 提示&#xff1a;以下是本篇文章正文内容&#xff0c;下面案例可供参考 一、热敏传感器计算温度&#xff08;ADC采样单通道&#xff09; #include "stm32f10x.h" // Device header#define T25 298.15 #define B 3…

看完这篇 教你玩转渗透测试靶机vulnhub——MONEYBOX: 1

Vulnhub靶机MONEYBOX: 1渗透测试详解Vulnhub靶机介绍&#xff1a;Vulnhub靶机下载&#xff1a;Vulnhub靶机安装&#xff1a;Vulnhub靶机漏洞详解&#xff1a;①&#xff1a;信息收集&#xff1a;②&#xff1a;FTP匿名登入&#xff1a;③&#xff1a;SSH暴力破解④&#xff1a;…

Java操作Word模板产生全新内容Word

1. spire.doc的jar引用 首先我们需要用到国产word处理工具jar包spire.doc&#xff0c;可以通过maven仓库寻找&#xff0c;然后在pom文件中直接引用。 此处需要注意&#xff0c;我们需要使用的是spire.doc.free&#xff08;免费版的&#xff09;&#xff0c;切勿使用spire.doc&a…

c++ - 第23节 - C++的类型转换

1.C语言中的类型转换 在C语言中&#xff0c;如果赋值运算符左右两侧类型不同&#xff0c;或者形参与实参类型不匹配&#xff0c;或者返回值类型与接收返回值类型不一致时&#xff0c;就需要发生类型转化&#xff0c;C语言中总共有两种形式的类型转换&#xff1a;隐式类型转换和…

业务安全情报 | 数十万元的数据报告,竟被50元批量转售

近期监测到某咨询公司针数据报告大量泄漏事件&#xff0c;该机构历年的数据报告以及近期更新的针对VIP会员的付费报告均在电商等渠道可以低价获取。 BSL-2022-a3c28号情报文件显示黑灰产通过作弊方式获取查看、下载权限&#xff0c;绕过限制将报告数据大量下载&#xff0c;并通…

javaEE初阶 — 认识文件

文章目录认识文件1. 树型结构组织和目录2. 文件路径&#xff08;Path&#xff09;2.1 绝对路径2.2 相对路径3. 文件的类型认识文件 文件分为 狭义 和 广义 两种 狭义的文件&#xff1a;指的是硬盘上的 文件 和 目录 广义的文件&#xff1a; 泛指计算机中很多的软硬件资源。操…

2022(一等奖)D926刘家峡库区潜在滑坡InSAR识别与分析

作品介绍 1 应用背景 滑坡是普遍存在于世界各地山区的主要灾害之一&#xff0c;严重威胁着人类的生命财产安全和自然环境。滑坡不但会直接破坏人类生命财产安全和建筑物&#xff0c;而且还会造成堰塞湖等次生灾害&#xff0c;进而对人类的生命财产安全和基础设施等造成二次破坏…