微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > MCU和单片机设计讨论 > rt-memory动态内存分配改进

rt-memory动态内存分配改进

时间:10-02 整理:3721RD 点击:
keil带的rt-memory动态内存分配源代码挺简练的,注释也很详尽。
但是每个内存块的控制部分需要8个字节,不大适合stm32/stm8这种小内存的片子,纯属浪费资源,现改成4字节。
请自行添加版权信息,测试。
修改前的代码:

  1. /*----------------------------------------------------------------------------
  2. *      RL-ARM - RTX
  3. *----------------------------------------------------------------------------
  4. *      Name:    RT_MEMORY.H
  5. *      Purpose: Interface functions for Dynamic Memory Management System
  6. *      Rev.:    V4.70
  7. *----------------------------------------------------------------------------
  8. *
  9. * Copyright (c) 1999-2009 KEIL, 2009-2013 ARM Germany GmbH
  10. * All rights reserved.
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *  - Redistributions of source code must retain the above copyright
  14. *    notice, this list of conditions and the following disclaimer.
  15. *  - Redistributions in binary form must reproduce the above copyright
  16. *    notice, this list of conditions and the following disclaimer in the
  17. *    documentation and/or other materials provided with the distribution.
  18. *  - Neither the name of ARM  nor the names of its contributors may be used
  19. *    to endorse or promote products derived from this software without
  20. *    specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *---------------------------------------------------------------------------*/

  34. #define U32 long
  35. #define NULL 0

  36. /* Types */
  37. typedef struct mem {              /* << Memory Pool management struct >>     */
  38.   struct mem *next;               /* Next Memory Block in the list           */
  39.   U32         len;                /* Length of data block                    */
  40. } MEMP,*PMEMP;

  41. /* Functions */
  42. extern int   rt_init_mem  (void *pool, U32  size);
  43. extern void *rt_alloc_mem (void *pool, U32  size);
  44. extern int   rt_free_mem  (void *pool, void *mem);

复制代码


  1. /*----------------------------------------------------------------------------
  2. *      RL-ARM - RTX
  3. *----------------------------------------------------------------------------
  4. *      Name:    RT_MEMORY.C
  5. *      Purpose: Interface functions for Dynamic Memory Management System
  6. *      Rev.:    V4.70
  7. *----------------------------------------------------------------------------
  8. *
  9. * Copyright (c) 1999-2009 KEIL, 2009-2013 ARM Germany GmbH
  10. * All rights reserved.
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *  - Redistributions of source code must retain the above copyright
  14. *    notice, this list of conditions and the following disclaimer.
  15. *  - Redistributions in binary form must reproduce the above copyright
  16. *    notice, this list of conditions and the following disclaimer in the
  17. *    documentation and/or other materials provided with the distribution.
  18. *  - Neither the name of ARM  nor the names of its contributors may be used
  19. *    to endorse or promote products derived from this software without
  20. *    specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *---------------------------------------------------------------------------*/

  34. //#include "rt_TypeDef.h"
  35. #include "rt_Memory.h"


  36. /* Functions */

  37. // Initialize Dynamic Memory pool
  38. //   Parameters:
  39. //     pool:    Pointer to memory pool
  40. //     size:    Size of memory pool in bytes
  41. //   Return:    0 - OK, 1 - Error

  42. int rt_init_mem (void *pool, U32 size) {
  43.   MEMP *ptr;

  44.   if ((pool == NULL) || (size < sizeof(MEMP))) return (1);

  45.   ptr = (MEMP *)pool;
  46.   ptr->next = (MEMP *)((U32)pool + size - sizeof(MEMP *));
  47.   ptr->next->next = NULL;
  48.   ptr->len = 0;

  49.   return (0);
  50. }

  51. // Allocate Memory from Memory pool
  52. //   Parameters:
  53. //     pool:    Pointer to memory pool
  54. //     size:    Size of memory in bytes to allocate
  55. //   Return:    Pointer to allocated memory

  56. void *rt_alloc_mem (void *pool, U32 size) {
  57.   MEMP *p, *p_search, *p_new;
  58.   U32   hole_size;

  59.   if ((pool == NULL) || (size == 0)) return NULL;

  60.   /* Add header offset to 'size' */
  61.   size += sizeof(MEMP);
  62.   /* Make sure that block is 4-byte aligned  */
  63.   size = (size + 3) & ~3;

  64.   p_search = (MEMP *)pool;
  65.   while (1) {
  66.     hole_size  = (U32)p_search->next - (U32)p_search;
  67.     hole_size -= p_search->len;
  68.     /* Check if hole size is big enough */
  69.     if (hole_size >= size) break;
  70.     p_search = p_search->next;
  71.     if (p_search->next == NULL) {
  72.       /* Failed, we are at the end of the list */
  73.       return NULL;
  74.     }
  75.   }

  76.   if (p_search->len == 0) {
  77.     /* No block is allocated, set the Length of the first element */
  78.     p_search->len = size;
  79.     p = (MEMP *)(((U32)p_search) + sizeof(MEMP));
  80.   } else {
  81.     /* Insert new list element into the memory list */
  82.     p_new       = (MEMP *)((U32)p_search + p_search->len);
  83.     p_new->next = p_search->next;
  84.     p_new->len  = size;
  85.     p_search->next = p_new;
  86.     p = (MEMP *)(((U32)p_new) + sizeof(MEMP));
  87.   }

  88.   return (p);
  89. }

  90. // Free Memory and return it to Memory pool
  91. //   Parameters:
  92. //     pool:    Pointer to memory pool
  93. //     mem:     Pointer to memory to free
  94. //   Return:    0 - OK, 1 - Error

  95. int rt_free_mem (void *pool, void *mem) {
  96.   MEMP *p_search, *p_prev, *p_return;

  97.   if ((pool == NULL) || (mem == NULL)) return (1);

  98.   p_return = (MEMP *)((U32)mem - sizeof(MEMP));
  99.   
  100.   /* Set list header */
  101.   p_prev = NULL;
  102.   p_search = (MEMP *)pool;
  103.   while (p_search != p_return) {
  104.     p_prev   = p_search;
  105.     p_search = p_search->next;
  106.     if (p_search == NULL) {
  107.       /* Valid Memory block not found */
  108.       return (1);
  109.     }
  110.   }

  111.   if (p_prev == NULL) {
  112.     /* First block to be released, only set length to 0 */
  113.     p_search->len = 0;
  114.   } else {
  115.     /* Discard block from chain list */
  116.     p_prev->next = p_search->next;
  117.   }

  118.   return (0);
  119. }

复制代码

修改后的代码:

  1. #define U32 long
  2. #define U16 short
  3. #define NULL 0

  4. /* Types */
  5. #pragma pack(1)
  6. typedef struct mem {              /* << Memory Pool management struct >>     */
  7.   U16 next;               /* Next Memory Block in the list           */
  8.   U16 len;                /* Length of data block                    */
  9. } MEMP,*PMEMP;
  10. #pragma pack()

  11. /* Functions */
  12. extern int   rt_init_mem  (void *pool, U16  size);
  13. extern void *rt_alloc_mem (void *pool, U16  size);
  14. extern int   rt_free_mem  (void *pool, void *mem);

复制代码

  1. /* Functions */
  2. // Initialize Dynamic Memory pool
  3. //   Parameters:
  4. //     pool:    Pointer to memory pool
  5. //     size:    Size of memory pool in bytes
  6. //   Return:    0 - OK, 1 - Error
  7. int rt_init_mem (void *pool, U16 size) {
  8.   MEMP *ptr;

  9.   if ((pool == NULL) || (size < sizeof(MEMP))) return (1);

  10.   ptr = (MEMP *)pool;
  11.   ptr->next = size - 4;
  12.   ((PMEMP)((U32)pool + (ptr->next)))->next = NULL;
  13.   ptr->len = 0;

  14.   return (0);
  15. }
  16. // Allocate Memory from Memory pool
  17. //   Parameters:
  18. //     pool:    Pointer to memory pool
  19. //     size:    Size of memory in bytes to allocate
  20. //   Return:    Pointer to allocated memory
  21. void *rt_alloc_mem (void *pool, U16 size) {
  22.   MEMP *p, *p_search, *p_new;
  23.   U32   hole_size;

  24.   if ((pool == NULL) || (size == 0)) return NULL;

  25.   /* Add header offset to 'size' */
  26.   size += sizeof(MEMP);
  27.   /* Make sure that block is 4-byte aligned  */
  28.   size = (size + 3) & ~3;

  29.   p_search = (MEMP *)pool;
  30.   while (1) {
  31.         hole_size  = (U32)p_search->next - ((U32)p_search - (U32)pool);
  32.         hole_size -= p_search->len;
  33.         /* Check if hole size is big enough */
  34.         if (hole_size >= size) break;
  35.         p_search = (MEMP *)((U32)pool + (p_search->next));
  36.         if (p_search->next == NULL) {
  37.           /* Failed, we are at the end of the list */
  38.           return NULL;
  39.         }
  40.   }

  41.   if (p_search->len == 0) {
  42.         /* No block is allocated, set the Length of the first element */
  43.         p_search->len = size;
  44.         p = (MEMP *)(((U32)p_search) + sizeof(MEMP));
  45.   } else {
  46.         /* Insert new list element into the memory list */
  47.         p_new       = (MEMP *)((U32)p_search + p_search->len);
  48.         p_new->next = p_search->next;
  49.         p_new->len  = size;
  50.         p_search->next = (U32)p_new - (U32)pool;
  51.         p = (MEMP *)(((U32)p_new) + sizeof(MEMP));
  52.   }

  53.   return (p);
  54. }

  55. // Free Memory and return it to Memory pool
  56. //   Parameters:
  57. //     pool:    Pointer to memory pool
  58. //     mem:     Pointer to memory to free
  59. //   Return:    0 - OK, 1 - Error

  60. int rt_free_mem (void *pool, void *mem) {
  61.   MEMP *p_search, *p_prev, *p_return;

  62.   if ((pool == NULL) || (mem == NULL)) return (1);

  63.   p_return = (MEMP *)((U32)mem - sizeof(MEMP));

  64.   /* Set list header */
  65.   p_prev = NULL;
  66.   p_search = (MEMP *)pool;
  67.   while (p_search != p_return) {
  68.         p_prev   = p_search;
  69.         if (p_search->next == NULL) {
  70.           /* Valid Memory block not found */
  71.           return (1);
  72.         }
  73.         p_search = (MEMP *)((U32)pool + (p_search->next));
  74.   }

  75.   if (p_prev == NULL) {
  76.         /* First block to be released, only set length to 0 */
  77.         p_search->len = 0;
  78.   } else {
  79.         /* Discard block from chain list */
  80.         p_prev->next = p_search->next;
  81.   }
  82.   return (0);
  83. }

复制代码


Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top