Skip to content

Commit c351620

Browse files
author
马犇
committed
fixed bug
1 parent a61cf6c commit c351620

File tree

2 files changed

+62
-78
lines changed

2 files changed

+62
-78
lines changed

cfc.c

+61-77
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
#include <fcntl.h>
3434

3535
#define HASH_TABLE_NAME "cfc_hash"
36-
#define BUFFER_SIZE 4096
36+
#define BUFFER_SIZE 1024
3737

3838
/* If you declare any globals in php_cfc.h uncomment this:
3939
ZEND_DECLARE_MODULE_GLOBALS(cfc)
@@ -211,7 +211,7 @@ int redis_incr(char *func)
211211
return redis_incr(func);
212212
}
213213
}
214-
freeReplyObject(reply);
214+
freeReplyObject(reply);
215215
return r;
216216
}
217217

@@ -225,11 +225,11 @@ int set_nonblocking(int fd)
225225
return 0;
226226
}
227227

228-
static char *get_function_name(zend_execute_data * execute_data)
228+
static char *get_function_name(zend_execute_data * execute_data, size_t *output_len)
229229
{
230230
zend_execute_data *data;
231231
char *ret = NULL;
232-
int len;
232+
size_t len;
233233
const char * cls;
234234
const char * func;
235235
zend_function *curr_func;
@@ -252,14 +252,18 @@ static char *get_function_name(zend_execute_data * execute_data)
252252
data->called_scope->name->val : NULL);
253253
if (cls)
254254
{
255-
len = strlen(cls) + strlen(func) + 10;
256-
ret = (char*) emalloc(len);
257-
snprintf(ret, len, "%s::%s", cls, func);
255+
len = strlen(cls) + strlen(func) + strlen("::") + 1;
256+
ret = (char*) emalloc(len + sizeof(size_t));
257+
memcpy(ret, &len, sizeof(size_t));
258+
sprintf(ret + sizeof(size_t), "%s::%s", cls, func);
259+
*output_len = len + sizeof(size_t);
258260
}
259261
else
260262
{
261-
ret = (char*) emalloc(len);
262-
snprintf(ret, len, "%s", func);
263+
ret = (char*) emalloc(len + sizeof(size_t));
264+
memcpy(ret, &len, sizeof(size_t));
265+
sprintf(ret + sizeof(size_t), "%s", func);
266+
*output_len = len + sizeof(size_t);
263267
}
264268
}
265269
else
@@ -291,12 +295,12 @@ static char *get_function_name(zend_execute_data * execute_data)
291295
return ret;
292296
}
293297

294-
static void push_func_to_queue(char *func)
298+
static void push_func_to_queue(char *func, size_t len)
295299
{
296300
if (NULL == func) {
297301
return;
298302
}
299-
write(manager_ptr->queues[1], func, strlen(func) + 1);
303+
write(manager_ptr->queues[1], func, len);
300304
}
301305

302306
static void my_zend_execute_ex(zend_execute_data *execute_data)
@@ -305,20 +309,21 @@ static void my_zend_execute_ex(zend_execute_data *execute_data)
305309
goto end;
306310
}
307311
char *func = NULL;
308-
func = get_function_name(execute_data TSRMLS_CC);
312+
size_t len;
313+
func = get_function_name(execute_data, &len);
309314
if (!func) {
310315
goto end;
311316
}
312317
if (cfc_prefixs.count) {
313318
for (int i = 0; i < cfc_prefixs.count; i++) {
314-
if (strncmp(cfc_prefixs.val[i], func, strlen(cfc_prefixs.val[i])) == 0) {
315-
push_func_to_queue(func);
319+
if (strncmp(cfc_prefixs.val[i], func + sizeof(size_t), strlen(cfc_prefixs.val[i])) == 0) {
320+
push_func_to_queue(func, len);
316321
}
317322
}
318323
} else {
319-
push_func_to_queue(func);
324+
push_func_to_queue(func, len);
320325
}
321-
efree(func);
326+
efree(func);
322327
end:
323328
old_zend_execute_ex(execute_data TSRMLS_CC);
324329
}
@@ -353,7 +358,6 @@ void *cfc_thread_worker(void *arg)
353358
break;
354359
}
355360

356-
/* Get item from worker queue */
357361
spin_lock(&manager_ptr->qlock);
358362

359363
item = manager_ptr->head;
@@ -389,8 +393,7 @@ void *cfc_thread_queue(void *arg)
389393
CFC_LOG_DEBUG("Queue thread started");
390394
fd_set read_set;
391395
int queue = manager_ptr->queues[0];
392-
char read_buf[BUFFER_SIZE], *read_buf_ptr = read_buf;;
393-
char unfinished[BUFFER_SIZE];
396+
char read_buf[BUFFER_SIZE];
394397
for (;;) {
395398

396399
FD_ZERO(&read_set);
@@ -404,73 +407,54 @@ void *cfc_thread_queue(void *arg)
404407
continue;
405408
}
406409

410+
#define check_read_result(r) \
411+
if (r == -1) { \
412+
break; \
413+
} \
414+
if (r == 0) { \
415+
pthread_exit(0); \
416+
}
417+
407418
if (FD_ISSET(queue, &read_set)) {
408-
int len;
409419
char *offset;
410-
memset(unfinished, 0, BUFFER_SIZE);
420+
size_t len;
421+
int r;
411422
for (;;) {
412423
memset(read_buf, 0, BUFFER_SIZE);
413-
if (strlen(unfinished)) {
414-
strcpy(read_buf_ptr, unfinished);
415-
len = read(queue, read_buf_ptr + strlen(unfinished), BUFFER_SIZE - strlen(unfinished));
416-
memset(unfinished, 0, BUFFER_SIZE);
417-
} else {
418-
len = read(queue, read_buf_ptr, BUFFER_SIZE);
419-
}
420-
if (len == -1) {
424+
r = read(queue, &len, sizeof(size_t));
425+
check_read_result(r);
426+
r = read(queue, read_buf, len);
427+
check_read_result(r);
428+
if (r != len) {
429+
CFC_LOG_WARN("read failure");
421430
break;
422431
}
423-
424-
if (len == 0) {
425-
pthread_exit(0);
432+
cfc_item_t *item;
433+
item = (cfc_item_t *)pemalloc(sizeof(*item) + len, 1);
434+
if (!item) {
435+
CFC_LOG_WARN("Memory malloc failure");
436+
continue;
426437
}
427-
offset = read_buf;
428-
if (offset[len - 1] != '\0') { /* 有未读完的数据 */
429-
int i = 1;
430-
while (1) {
431-
if (i >= len) {
432-
memcpy(unfinished, offset, len);
433-
break;
434-
}
435-
if (*(offset + len - 1 - i) == '\0') {
436-
memcpy(unfinished, offset + len - i, i - 1);
437-
memset(offset + len - i, 0, 1);
438-
break;
439-
}
440-
i++;
441-
}
438+
439+
item->size = len;
440+
item->next = NULL;
441+
memcpy(item->buffer, read_buf, len);
442+
443+
spin_lock(&manager_ptr->qlock);
444+
445+
if (!manager_ptr->head) {
446+
manager_ptr->head = item;
442447
}
443-
while (strlen(offset)) {
444-
int offset_len = strlen(offset);
445-
cfc_item_t *item;
446-
int length = 0;
447-
length = sizeof(*item) + offset_len + 1;
448-
item = (cfc_item_t *)pemalloc(length, 1);
449-
if (!item) {
450-
CFC_LOG_WARN("Memory malloc failure");
451-
continue;
452-
}
453-
item->size = offset_len + 1;
454-
item->next = NULL;
455-
memcpy(item->buffer, offset, offset_len + 1);
456-
457-
spin_lock(&manager_ptr->qlock);
458-
459-
if (!manager_ptr->head) {
460-
manager_ptr->head = item;
461-
}
462-
463-
if (manager_ptr->tail) {
464-
manager_ptr->tail->next = item;
465-
}
466-
467-
manager_ptr->tail = item;
468-
469-
spin_unlock(&manager_ptr->qlock);
470-
/* notify worker thread */
471-
write(manager_ptr->notifiers[1], "\0", 1);
472-
offset = offset + offset_len + 1;
448+
449+
if (manager_ptr->tail) {
450+
manager_ptr->tail->next = item;
473451
}
452+
453+
manager_ptr->tail = item;
454+
455+
spin_unlock(&manager_ptr->qlock);
456+
/* notify worker thread */
457+
write(manager_ptr->notifiers[1], "\0", 1);
474458
}
475459
}
476460
}

php_cfc.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ ZEND_END_MODULE_GLOBALS(cfc)
5151
typedef struct _cfc_item_s {
5252
struct _cfc_item_s *next;
5353
int size;
54-
char buffer[1];
54+
char buffer[];
5555
} cfc_item_t;
5656

5757
typedef struct {

0 commit comments

Comments
 (0)