Skip to content

Commit fc2453c

Browse files
committed
refactoring
Modify redis_response_enqueued function. Wrap macros into do/while blocks.
1 parent d4c2663 commit fc2453c

File tree

2 files changed

+33
-36
lines changed

2 files changed

+33
-36
lines changed

common.h

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -474,51 +474,48 @@ typedef enum _PUBSUB_TYPE {
474474
#define IF_PIPELINE() if (redis_sock->mode == PIPELINE)
475475
#define IF_NOT_PIPELINE() if (redis_sock->mode != PIPELINE)
476476

477-
#define PIPELINE_ENQUEUE_COMMAND(cmd, cmd_len) request_item *tmp; \
478-
struct request_item *current_request;\
479-
tmp = malloc(sizeof(request_item));\
477+
#define PIPELINE_ENQUEUE_COMMAND(cmd, cmd_len) do { \
478+
request_item *tmp = malloc(sizeof(request_item)); \
480479
tmp->request_str = calloc(cmd_len, 1);\
481480
memcpy(tmp->request_str, cmd, cmd_len);\
482481
tmp->request_size = cmd_len;\
483482
tmp->next = NULL;\
484-
current_request = redis_sock->pipeline_current; \
485-
if(current_request) {\
486-
current_request->next = tmp;\
483+
if (redis_sock->pipeline_current) { \
484+
redis_sock->pipeline_current->next = tmp; \
487485
} \
488486
redis_sock->pipeline_current = tmp; \
489487
if(NULL == redis_sock->pipeline_head) { \
490488
redis_sock->pipeline_head = redis_sock->pipeline_current;\
491-
}
489+
} \
490+
} while (0)
492491

493492
#define SOCKET_WRITE_COMMAND(redis_sock, cmd, cmd_len) \
494493
if(redis_sock_write(redis_sock, cmd, cmd_len TSRMLS_CC) < 0) { \
495494
efree(cmd); \
496495
RETURN_FALSE; \
497496
}
498497

499-
#define REDIS_SAVE_CALLBACK(callback, closure_context) \
500-
IF_NOT_ATOMIC() { \
501-
fold_item *f1, *current; \
502-
f1 = malloc(sizeof(fold_item)); \
503-
f1->fun = (void *)callback; \
504-
f1->ctx = closure_context; \
505-
f1->next = NULL; \
506-
current = redis_sock->current;\
507-
if(current) current->next = f1; \
508-
redis_sock->current = f1; \
509-
if(NULL == redis_sock->head) { \
510-
redis_sock->head = redis_sock->current;\
511-
}\
512-
}
498+
#define REDIS_SAVE_CALLBACK(callback, closure_context) do { \
499+
fold_item *f1 = malloc(sizeof(fold_item)); \
500+
f1->fun = (void *)callback; \
501+
f1->ctx = closure_context; \
502+
f1->next = NULL; \
503+
if (redis_sock->current) { \
504+
redis_sock->current->next = f1; \
505+
} \
506+
redis_sock->current = f1; \
507+
if (NULL == redis_sock->head) { \
508+
redis_sock->head = redis_sock->current; \
509+
} \
510+
} while (0)
513511

514512
#define REDIS_ELSE_IF_MULTI(function, closure_context) \
515513
else IF_MULTI() { \
516-
if(redis_response_enqueued(redis_sock TSRMLS_CC) == 1) {\
517-
REDIS_SAVE_CALLBACK(function, closure_context); \
518-
RETURN_ZVAL(getThis(), 1, 0);\
519-
} else {\
520-
RETURN_FALSE;\
521-
}\
514+
if (redis_response_enqueued(redis_sock TSRMLS_CC) != SUCCESS) { \
515+
RETURN_FALSE; \
516+
} \
517+
REDIS_SAVE_CALLBACK(function, closure_context); \
518+
RETURN_ZVAL(getThis(), 1, 0);\
522519
}
523520

524521
#define REDIS_ELSE_IF_PIPELINE(function, closure_context) \

redis.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2430,18 +2430,18 @@ PHP_METHOD(Redis, exec)
24302430
}
24312431
}
24322432

2433-
PHP_REDIS_API int redis_response_enqueued(RedisSock *redis_sock TSRMLS_DC) {
2433+
PHP_REDIS_API int
2434+
redis_response_enqueued(RedisSock *redis_sock TSRMLS_DC)
2435+
{
24342436
char *resp;
2435-
int resp_len, ret = 0;
2436-
2437-
if ((resp = redis_sock_read(redis_sock, &resp_len TSRMLS_CC)) == NULL) {
2438-
return 0;
2439-
}
2437+
int resp_len, ret = FAILURE;
24402438

2441-
if(strncmp(resp, "+QUEUED", 7) == 0) {
2442-
ret = 1;
2439+
if ((resp = redis_sock_read(redis_sock, &resp_len TSRMLS_CC)) != NULL) {
2440+
if (strncmp(resp, "+QUEUED", 7) == 0) {
2441+
ret = SUCCESS;
2442+
}
2443+
efree(resp);
24432444
}
2444-
efree(resp);
24452445
return ret;
24462446
}
24472447

0 commit comments

Comments
 (0)