Dan*_*iel 4 c macros c-preprocessor
我正在阅读囤积内存分配器的源代码,并在gnuwrapper.cpp的文件中,有以下代码
#define CUSTOM_MALLOC(x) CUSTOM_PREFIX(malloc)(x)
Run Code Online (Sandbox Code Playgroud)
这是什么意思CUSTOM_PREFIX(malloc)(x)?是 CUSTOM_PREFIX一个功能?但作为一个功能,它没有在任何地方定义.如果它是可变的,那么我们如何才能使用变量var(malloc)(x)?
更多代码:
#ifndef __GNUC__
#error "This file requires the GNU compiler."
#endif
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#ifndef CUSTOM_PREFIX ==> here looks like it's a variable, so if it doesn't define, then define here.
#define CUSTOM_PREFIX
#endif
#define CUSTOM_MALLOC(x) CUSTOM_PREFIX(malloc)(x) ===> what's the meaning of this?
#define CUSTOM_FREE(x) CUSTOM_PREFIX(free)(x)
#define CUSTOM_REALLOC(x,y) CUSTOM_PREFIX(realloc)(x,y)
#define CUSTOM_MEMALIGN(x,y) CUSTOM_PREFIX(memalign)(x,y)
Run Code Online (Sandbox Code Playgroud)
在您的代码中,由于CUSTOM_PREFIX被定义为空,字符串CUSTOM_PREFIX(malloc)(x)将扩展为
(malloc)(x)
Run Code Online (Sandbox Code Playgroud)
这相当于平常
malloc(x)
Run Code Online (Sandbox Code Playgroud)
但是,CUSTOM_PREFIX允许开发人员选择不同的内存管理功能.例如,如果我们定义
#define CUSTOM_PREFIX(f) my_##f
Run Code Online (Sandbox Code Playgroud)
然后CUSTOM_PREFIX(malloc)(x)将扩展到
my_malloc(x)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
890 次 |
| 最近记录: |