搜索
bottom↓
回复: 4

MDK 关于heap,calloc(), free(), malloc(), realloc()

[复制链接]

出0入0汤圆

发表于 2015-10-6 18:50:49 | 显示全部楼层 |阅读模式
在使用calloc(), free(), malloc(), realloc(),道听途说可能会有种内存泄漏问题,一直不敢尝试:
最近在使用MDK使用到了 free(), malloc(),刚看了下帮助文档,发现关于heap一些很有用的几个东西,决定进一步测试下:

__heapstats()
Defined in stdlib.h, the __heapstats() function displays statistics on the state of the storage allocation heap.

__heapvalid()


calloc(), free(), malloc(), realloc()
The heap functions are thread-safe if the _mutex_* functions are implemented.
All threads share a single heap and use mutexes to avoid data corruption when there is concurrent access.
Each heap implementation is responsible for doing its own locking. If you supply your own allocator, it must
also do its own locking. This enables it to do fine-grained locking if required, rather than protecting the entire
heap with a single mutex (coarse-grained locking).


How to ensure re-implemented mutex functions are called:
If your re-implemented _mutex_*() functions are within an object that is contained within a library file, the linker does not automatically include the object.

This can result in the _mutex_*() functions being excluded from the image you have built.
To avoid this problem, that is, to ensure that your _mutex_*() functions are called, you can either:
Place your mutex functions in a non-library object file. This helps to ensure that they are resolved at link time.
Place your mutex functions in a library object file, and arrange a non-weak reference to something in the object.
Place your mutex functions in a library object file, and have the linker explicitly extract the specific object from the library on the command line by writing libraryname.a(objectfilename.o) when you invoke the linker.


在 RTX_CM_LIB.H 里面实现了:

  1. /*----------------------------------------------------------------------------
  2. *      Standard Library multithreading interface
  3. *---------------------------------------------------------------------------*/

  4. #if defined (__CC_ARM) && !defined (__MICROLIB)

  5. /*--------------------------- __user_perthread_libspace ---------------------*/

  6. void *__user_perthread_libspace (void) {
  7.   /* Provide a separate libspace for each task. */
  8.   uint32_t idx;

  9.   idx = (os_running != 0U) ? runtask_id () : 0U;
  10.   if (idx == 0U) {
  11.     /* RTX not running yet. */
  12.     return (&__libspace_start);
  13.   }
  14.   return ((void *)&std_libspace[idx-1]);
  15. }

  16. /*--------------------------- _mutex_initialize -----------------------------*/

  17. int _mutex_initialize (OS_ID *mutex) {
  18.   /* Allocate and initialize a system mutex. */

  19.   if (nr_mutex >= OS_MUTEXCNT) {
  20.     /* If you are here, you need to increase the number OS_MUTEXCNT. */
  21.     for (;;);
  22.   }
  23.   *mutex = &std_libmutex[nr_mutex++];
  24.   mutex_init (*mutex);
  25.   return (1);
  26. }


  27. /*--------------------------- _mutex_acquire --------------------------------*/

  28. __attribute__((used)) void _mutex_acquire (OS_ID *mutex) {
  29.   /* Acquire a system mutex, lock stdlib resources. */
  30.   if (os_running) {
  31.     /* RTX running, acquire a mutex. */
  32.     mutex_wait (*mutex);
  33.   }
  34. }


  35. /*--------------------------- _mutex_release --------------------------------*/

  36. __attribute__((used)) void _mutex_release (OS_ID *mutex) {
  37.   /* Release a system mutex, unlock stdlib resources. */
  38.   if (os_running) {
  39.     /* RTX running, release a mutex. */
  40.     mutex_rel (*mutex);
  41.   }
  42. }

  43. #endif
复制代码



出0入8汤圆

发表于 2015-10-7 11:13:18 | 显示全部楼层
所以呢? ????

出0入0汤圆

 楼主| 发表于 2015-10-7 12:10:11 | 显示全部楼层
lw2012 发表于 2015-10-7 11:13
所以呢? ????

所以呢,可以试着在RTOS环境下放心使用动态内存哎,在程序里面实现链表等高级数据结构在...
如果有内存泄漏,可以试着执行
__heapstats()
__heapvalid()
查看和检查堆状态,了解程序内存状态吧...

比如在嵌入式环境下使用JSON需要使用malloc free...

  1. {
  2.     "jsonrpc": "2.0",
  3.     "id": 1,
  4.     "method": "heapstats"
  5. }
  6. {
  7.     "id": 1,
  8.     "jsonrpc": "2.0",
  9.     "result": {
  10.         "heapstats": [
  11.             "7692 bytes in 3 free blocks (avge size 2564)\n",
  12.             "2 blocks 2^3+1 to 2^4\n",
  13.             "1 blocks 2^11+1 to 2^12\n"
  14.         ]
  15.     }
  16. }
复制代码

出0入0汤圆

发表于 2021-9-17 10:08:07 | 显示全部楼层
MicroLib里没有__heapstats()
肿么办?

出0入0汤圆

发表于 2021-9-18 11:34:00 来自手机 | 显示全部楼层
用十六进制表达数据他不香吗
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-4-19 19:27

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

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