硬汉嵌入式论坛

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

keil里面malloc可以重定义吗?

[复制链接]

22

主题

47

回帖

113

积分

初级会员

积分
113
发表于 2022-6-28 16:42:37 | 显示全部楼层 |阅读模式
很多情况,都跑了实时操作系统,附带了操作系统的内存管理,但是像一些系统函数,比如va_start、va_end等一类的,不还是用的系统的malloc函数吗?该如何避免呢,请大佬指点
回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106721
QQ
发表于 2022-6-28 17:33:08 | 显示全部楼层
这个要专门做互斥保护,以MDK原装的RTX5为例,他是里面已经实现了,有对应的代码,你也可以参考。

image.png
回复

使用道具 举报

22

主题

47

回帖

113

积分

初级会员

积分
113
 楼主| 发表于 2022-6-29 10:29:25 | 显示全部楼层
eric2013 发表于 2022-6-28 17:33
这个要专门做互斥保护,以MDK原装的RTX5为例,他是里面已经实现了,有对应的代码,你也可以参考。

硬汉哥,那他这个是怎么规避掉 调用系统函数的时候,避免系统函数内部调用的是系统的malloc的呢?
回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106721
QQ
发表于 2022-6-30 01:34:29 | 显示全部楼层
熊大 发表于 2022-6-29 10:29
硬汉哥,那他这个是怎么规避掉 调用系统函数的时候,避免系统函数内部调用的是系统的malloc的呢?

RTX5就是对C库的api做了互斥,包括malloc
回复

使用道具 举报

22

主题

47

回帖

113

积分

初级会员

积分
113
 楼主| 发表于 2022-6-30 10:11:18 | 显示全部楼层
eric2013 发表于 2022-6-30 01:34
RTX5就是对C库的api做了互斥,包括malloc

硬汉哥有样例吗?一时不知道从哪里开始下手
回复

使用道具 举报

12

主题

153

回帖

204

积分

高级会员

积分
204
发表于 2022-6-30 17:25:24 | 显示全部楼层
分享一个重定向C库部分函数的方法,包含malloc、free、fopen等函数
https://www.armbbs.cn/forum.php? ... 5&fromuid=24016
(出处: 硬汉嵌入式论坛)
这篇帖子讲了重定向malloc的方法
回复

使用道具 举报

22

主题

47

回帖

113

积分

初级会员

积分
113
 楼主| 发表于 2022-7-1 11:01:53 | 显示全部楼层
WZH 发表于 2022-6-30 17:25
分享一个重定向C库部分函数的方法,包含malloc、free、fopen等函数
https://www.armbbs.cn/forum.php?mod= ...

感谢
回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106721
QQ
发表于 2022-7-3 00:27:59 | 显示全部楼层
image.png
[C] 纯文本查看 复制代码
// C/C++ Standard Library Multithreading Interface
// ===============================================

#if ( !defined(RTX_NO_MULTITHREAD_CLIB) && \
     ( defined(__CC_ARM) || \
      (defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))) && \
      !defined(__MICROLIB))

#define LIBSPACE_SIZE 96

//lint -esym(714,__user_perthread_libspace,_mutex_*) "Referenced by C library"
//lint -esym(765,__user_perthread_libspace,_mutex_*) "Global scope"
//lint -esym(9003, os_libspace*) "variables 'os_libspace*' defined at module scope"

// Memory for libspace
static uint32_t os_libspace[OS_THREAD_LIBSPACE_NUM+1][LIBSPACE_SIZE/4] \
__attribute__((section(".bss.os.libspace")));

// Thread IDs for libspace
static osThreadId_t os_libspace_id[OS_THREAD_LIBSPACE_NUM] \
__attribute__((section(".bss.os.libspace")));

// Check if Kernel has been started
static uint32_t os_kernel_is_active (void) {
  static uint8_t os_kernel_active = 0U;

  if (os_kernel_active == 0U) {
    if (osKernelGetState() > osKernelReady) {
      os_kernel_active = 1U;
    }
  }
  return (uint32_t)os_kernel_active;
}

// Provide libspace for current thread
void *__user_perthread_libspace (void);
void *__user_perthread_libspace (void) {
  osThreadId_t id;
  uint32_t     n;

  if (os_kernel_is_active() != 0U) {
    id = osThreadGetId();
    for (n = 0U; n < (uint32_t)OS_THREAD_LIBSPACE_NUM; n++) {
      if (os_libspace_id[n] == NULL) {
        os_libspace_id[n] = id;
      }
      if (os_libspace_id[n] == id) {
        break;
      }
    }
    if (n == (uint32_t)OS_THREAD_LIBSPACE_NUM) {
      (void)osRtxKernelErrorNotify(osRtxErrorClibSpace, id);
    }
  } else {
    n = OS_THREAD_LIBSPACE_NUM;
  }

  //lint -e{9087} "cast between pointers to different object types"
  return (void *)&os_libspace[n][0];
}

// Mutex identifier
typedef void *mutex;

//lint -save "Function prototypes defined in C library"
//lint -e970 "Use of 'int' outside of a typedef"
//lint -e818 "Pointer 'm' could be declared as pointing to const"

// Initialize mutex
__USED
int _mutex_initialize(mutex *m);
int _mutex_initialize(mutex *m) {
  int result;

  *m = osMutexNew(NULL);
  if (*m != NULL) {
    result = 1;
  } else {
    result = 0;
    (void)osRtxKernelErrorNotify(osRtxErrorClibMutex, m);
  }
  return result;
}

// Acquire mutex
__USED
void _mutex_acquire(mutex *m);
void _mutex_acquire(mutex *m) {
  if (os_kernel_is_active() != 0U) {
    (void)osMutexAcquire(*m, osWaitForever);
  }
}

// Release mutex
__USED
void _mutex_release(mutex *m);
void _mutex_release(mutex *m) {
  if (os_kernel_is_active() != 0U) {
    (void)osMutexRelease(*m);
  }
}

// Free mutex
__USED
void _mutex_free(mutex *m);
void _mutex_free(mutex *m) {
  (void)osMutexDelete(*m);
}

//lint -restore

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-1 03:04 , Processed in 0.183516 second(s), 28 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

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