查看: 1187|回复: 0
打印 上一主题 下一主题

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

[复制链接]
跳转到指定楼层
沙发
发表于 2015-10-7 13:01:45 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

在使用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 里面实现了:

/*----------------------------------------------------------------------------
*      Standard Library multithreading interface
*---------------------------------------------------------------------------*/

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

/*--------------------------- __user_perthread_libspace ---------------------*/

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

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

/*--------------------------- _mutex_initialize -----------------------------*/

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

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


/*--------------------------- _mutex_acquire --------------------------------*/

__attribute__((used)) void _mutex_acquire (OS_ID *mutex) {
  /* Acquire a system mutex, lock stdlib resources. */
  if (os_running) {
    /* RTX running, acquire a mutex. */
    mutex_wait (*mutex);
  }
}


/*--------------------------- _mutex_release --------------------------------*/

__attribute__((used)) void _mutex_release (OS_ID *mutex) {
  /* Release a system mutex, unlock stdlib resources. */
  if (os_running) {
    /* RTX running, release a mutex. */
    mutex_rel (*mutex);
  }
}

#endif
复制代码
转载

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 加入中科因仑

本版积分规则

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