硬汉嵌入式论坛

 找回密码
 立即注册
查看: 3155|回复: 8
收起左侧

[Embedded Studio] Embedded Studio带的malloc,使用的buddy算法

[复制链接]

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
107101
QQ
发表于 2020-8-18 14:00:36 | 显示全部楼层 |阅读模式

伙伴算法(buddy算法),计算机算法的一种,是为了核心内存管理能够快速响应请求,尽可能地在提高内存利用率的同时减少内存碎片的一种算法。

  1. // **********************************************************************
  2. // *                    SEGGER Microcontroller GmbH                     *
  3. // *                        The Embedded Experts                        *
  4. // **********************************************************************
  5. // *                                                                    *
  6. // *            (c) 2014 - 2020 SEGGER Microcontroller GmbH             *
  7. // *            (c) 2001 - 2020 Rowley Associates Limited               *
  8. // *                                                                    *
  9. // *           www.segger.com     Support: support@segger.com           *
  10. // *                                                                    *
  11. // **********************************************************************
  12. // *                                                                    *
  13. // * All rights reserved.                                               *
  14. // *                                                                    *
  15. // * Redistribution and use in source and binary forms, with or         *
  16. // * without modification, are permitted provided that the following    *
  17. // * condition is met:                                                  *
  18. // *                                                                    *
  19. // * - Redistributions of source code must retain the above copyright   *
  20. // *   notice, this condition and the following disclaimer.             *
  21. // *                                                                    *
  22. // * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND             *
  23. // * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,        *
  24. // * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF           *
  25. // * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE           *
  26. // * DISCLAIMED. IN NO EVENT SHALL SEGGER Microcontroller BE LIABLE FOR *
  27. // * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR           *
  28. // * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT  *
  29. // * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;    *
  30. // * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF      *
  31. // * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT          *
  32. // * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE  *
  33. // * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH   *
  34. // * DAMAGE.                                                            *
  35. // *                                                                    *
  36. // **********************************************************************

  37. #ifndef BUDDY_HEAP_H
  38. #define BUDDY_HEAP_H

  39. #include <stdlib.h>

  40. typedef struct buddy_link_tag
  41. {
  42.   struct buddy_link_tag *next;
  43.   struct buddy_link_tag *prev;
  44. } __BUDDY_LINKS_t;

  45. typedef struct {
  46.   long total_capacity;     // total size of the heap
  47.   long alloc_requests;     // number of times alloc called
  48.   long alloc_failures;     // how many times alloc has failed
  49.   long free_requests;      // number of times free called with non-zero argument
  50.   long free_failures;      // how many times free has been called with a bad argument or corrupt heap
  51.   long used_bytes;         // how many bytes are used in the heap (a multiple of the allocation unit)
  52.   long free_bytes;         // how many bytes are free in the heap
  53.   long min_free_bytes;     // lowest number of free bytes bytes in the heap
  54.   long blocks_split;       // how many times blocks have needed to be split.
  55.   long blocks_coalesed;    // how many times blocks have been coalesced.
  56. } BUDDY_HEAP_STATS_t;

  57. typedef struct
  58. {
  59.   char *mem;
  60.   unsigned max_k;     // 'm' in Fundamental Algorithms
  61.   unsigned min_k;     // minimum block size returned
  62.   unsigned min_size;  // 1 << min_k
  63.   unsigned size;
  64.   BUDDY_HEAP_STATS_t stats;
  65.   __BUDDY_LINKS_t free_head[sizeof(void *)*8];  // 4 entries unused, could do something here...
  66. } BUDDY_HEAP_t;


  67. void *buddy_alloc(BUDDY_HEAP_t *h, size_t size);
  68. void buddy_free(BUDDY_HEAP_t *h, void *data);
  69. int buddy_heap_init(BUDDY_HEAP_t *h, void *mem, size_t size);

  70. // For the future...
  71. //
  72. //typedef struct
  73. //{
  74. //  BUDDY_HEAP_t heap;
  75. //  CTL_MUTEX_t mutex;
  76. //  CTL_EVENT_SET_t events;
  77. //} CTL_BUDDY_HEAP_t;
  78. //
  79. //int ctl_buddy_heap_init(CTL_BUDDY_HEAP_t *h, void *mem, size_t size);
  80. //void *ctl_buddy_alloc(CTL_BUDDY_HEAP_t *h, size_t size, CTL_TIMEOUT_t timeout, CTL_);
  81. //void *ctl_buddy_alloc_with_timeout(CTL_BUDDY_HEAP_t *h, size_t size, CTL_TIMEOUT_t timeout, CTL_TIME_t t);
  82. //void ctl_buddy_free(CTL_BUDDY_HEAP_t *h, void *data);

  83. #endif
复制代码
回复

使用道具 举报

78

主题

693

回帖

927

积分

金牌会员

积分
927
发表于 2020-8-18 17:51:04 | 显示全部楼层
在实时操作系统中,移植tlsf我个人觉得应该会更好一点。
回复

使用道具 举报

610

主题

3063

回帖

4913

积分

至尊会员

积分
4913
发表于 2020-9-3 08:57:09 | 显示全部楼层
这个有没有完整的程序啊 ?
回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
107101
QQ
 楼主| 发表于 2020-9-3 13:44:29 | 显示全部楼层
hpdell 发表于 2020-9-3 08:57
这个有没有完整的程序啊 ?

这个没有完整程序,我就是简单浏览了下他们的底层。
回复

使用道具 举报

2

主题

28

回帖

34

积分

新手上路

积分
34
发表于 2021-10-22 16:30:18 | 显示全部楼层
eric2013 发表于 2020-9-3 13:44
这个没有完整程序,我就是简单浏览了下他们的底层。

我这边有一份参考程序,不过我在项目使用时有些问题(ARM7芯片上链表申请内存,会出现指针飞了情况),硬汉哥有空一起看看,我怀疑是有使用平台要求
回复

使用道具 举报

2

主题

28

回帖

34

积分

新手上路

积分
34
发表于 2021-10-22 16:32:23 | 显示全部楼层
这个别人写的,仅供参考

BuddyMemoryMallocFree-master.zip

5.79 KB, 下载次数: 26

回复

使用道具 举报

41

主题

215

回帖

338

积分

高级会员

积分
338
发表于 2021-12-2 20:06:19 | 显示全部楼层
庄永 发表于 2020-8-18 17:51
在实时操作系统中,移植tlsf我个人觉得应该会更好一点。

请问是不是liteos里面带的?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-5-18 09:55 , Processed in 0.188792 second(s), 28 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表