我正在阅读hoard内存分配器的源代码,在gnuwrapper.cpp文件中,有以下代码
#define CUSTOM_MALLOC(x) CUSTOM_PREFIX(malloc)(x)
是什么意思CUSTOM_PREFIX(malloc)(x)
? is 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)
在您的代码中,由于 CUSTOM_PREFIX 被定义为空,因此字符串CUSTOM_PREFIX(malloc)(x)
将扩展到
(malloc)(x)
这相当于通常的
malloc(x)
然而,CUSTOM_PREFIX允许开发人员选择不同的内存管理功能。例如,如果我们定义
#define CUSTOM_PREFIX(f) my_##f
then CUSTOM_PREFIX(malloc)(x)
将扩展到
my_malloc(x)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)