A co-worker asked me to explain a bit of C code in memcached. I am at the point where I admit I do not understand it either.
It has to do with C function-like macro definitions with parameters that do not also have a replacement list. For example, starting at line 2751 in memcached.c:
if (return_cas)
{
MEMCACHED_COMMAND_GET(c->sfd, ITEM_key(it), it->nkey,
it->nbytes, ITEM_get_cas(it));
/* Goofy mid-flight realloc. */
if (i >= c->suffixsize) {
char **new_suffix_list = realloc(c->suffixlist,
sizeof(char *) * c->suffixsize * 2);
if (new_suffix_list) {
c->suffixsize *= 2;
c->suffixlist = new_suffix_list;
MEMCACHED_COMMAND_GET() is defined on line 23 in trace.h:
#define MEMCACHED_COMMAND_GET(arg0, arg1, arg2, arg3, arg4)
C macro function-like definition with arguments, ok. No replacement list.
The output of cpp on memcached.c shows the function-like macro turns into ‘;’:
# 2751 "memcached.c"
if (return_cas)
{
;
if (i >= c->suffixsize) {
char **new_suffix_list = realloc(c->suffixlist,
sizeof(char *) * c->suffixsize * 2);
if (new_suffix_list) {
c->suffixsize *= 2;
c->suffixlist = new_suffix_list;
Does anyone have any insight into why a programmer would include function-like macros without a replacement list like MEMCACHED_COMMAND_GET() that have no effect on the code produced? What purpose is being served by doing doing this? Thank you kindly in advance.
On Solaris and Mac OSX it’s replaced to triggering a DTrace probe so it expands to something else.