问题描述
我正在研究ESP8266串行wifi芯片的编程。在其SDK示例中,它广泛使用了名为 os_zalloc
的函数,我希望在其中使用 malloc
。
I am looking into programming the ESP8266 serial-wifi chip. In its SDK examples it makes extensive use of a function called os_zalloc
where I would expect malloc
.
虽然有时也使用 os_malloc
。因此,它们在功能上似乎并不相同。
Occasionally though, os_malloc
is used as well. So they do not appear to be identical in function.
不幸的是,没有文档。有人可以从以下头文件中做出有根据的猜测吗?
Unfortunately there is no documentation. Can anybody make an educated guess from the following header file?
#ifndef __MEM_H__
#define __MEM_H__
//void *pvPortMalloc( size_t xWantedSize );
//void vPortFree( void *pv );
//void *pvPortZalloc(size_t size);
#define os_malloc pvPortMalloc
#define os_free vPortFree
#define os_zalloc pvPortZalloc
#endif
推荐答案
由于 os_zalloc
是宏,其定义为在mem.h中给出的一个更好的问题是关于 pvPortZalloc
的作用。
Since os_zalloc
is a macro, and the definition is given in mem.h, a better question to ask would be about what pvPortZalloc
does.
给出函数名 pvPortMalloc
, vPortFree
和 pvPortZalloc
使用的是FreeRTOS(或它的商业许可等效的OpenRTOS),尽管没有特别说明 pvPortZalloc
,但已记录在案,但如果不是简单地 allocate和零初始化-例如。这些功能是FreeRTOS的目标移植层的一部分,通常不被应用程序级别调用,但我想这里宏包装器用于访问应用程序用户的移植层代码,而不是编写两次。
Given the function names pvPortMalloc
, vPortFree
and pvPortZalloc
it would appear that the OS in use is FreeRTOS (or it's commercially licensed equivalent OpenRTOS), which is documented - although not specifically pvPortZalloc
, but it would be strange if it was not simply allocate and zero initialise - that is for example what it means here. The functions are part of the target porting layer for FreeRTOS, and are not normally called by the application level, but I imagine here the macro wrapper is used to access the porting layer code for application user rather than write it twice.
在RTOS内核中,需要RTOS感知的动态内存分配功能来确保线程安全,尽管某些标准库实现包括使用RTOS互斥调用实现的线程安全存根,这是一个更好的方法,因为可以更容易地使用现有的库和C ++ new
/ 删除
。
In an RTOS kernel RTOS aware dynamic memory allocation functions are required to ensure thread safety, although some standard library implementations include thread safety stubs that you implement using the RTOS mutex calls, which is a better method since existing libaries and C++ new
/delete
can be more easily used.
这篇关于嵌入式编程中的zalloc是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!