From 0d5e72e76a5254dde1cad8edf58c0d311503ef53 Mon Sep 17 00:00:00 2001 From: matyhtf Date: Thu, 17 Dec 2020 11:51:35 +0800 Subject: [PATCH] fix table bugs --- include/table.h | 32 +++------------------------- src/memory/table.c | 35 +++++++------------------------ swoole_table.cc | 52 +++++++++------------------------------------- 3 files changed, 21 insertions(+), 98 deletions(-) diff --git a/include/table.h b/include/table.h index 9113a214af6..fe8f84a72db 100644 --- a/include/table.h +++ b/include/table.h @@ -87,12 +87,6 @@ typedef struct enum swoole_table_type { SW_TABLE_INT = 1, - SW_TABLE_INT8, - SW_TABLE_INT16, - SW_TABLE_INT32, -#ifdef __x86_64__ - SW_TABLE_INT64, -#endif SW_TABLE_FLOAT, SW_TABLE_STRING, }; @@ -169,32 +163,12 @@ typedef uint32_t swTable_string_length_t; static sw_inline void swTableRow_set_value(swTableRow *row, swTableColumn * col, void *value, size_t vlen) { - int8_t _i8; - int16_t _i16; - int32_t _i32; -#ifdef __x86_64__ - int64_t _i64; -#endif + switch(col->type) { - case SW_TABLE_INT8: - _i8 = *(int8_t *) value; - memcpy(row->data + col->index, &_i8, 1); - break; - case SW_TABLE_INT16: - _i16 = *(int16_t *) value; - memcpy(row->data + col->index, &_i16, 2); - break; - case SW_TABLE_INT32: - _i32 = *(int32_t *) value; - memcpy(row->data + col->index, &_i32, 4); - break; -#ifdef __x86_64__ - case SW_TABLE_INT64: - _i64 = *(int64_t *) value; - memcpy(row->data + col->index, &_i64, 8); + case SW_TABLE_INT: + memcpy(row->data + col->index, value, sizeof(long)); break; -#endif case SW_TABLE_FLOAT: memcpy(row->data + col->index, value, sizeof(double)); break; diff --git a/src/memory/table.c b/src/memory/table.c index f4fd7b6d5fa..ef67c005e0e 100644 --- a/src/memory/table.c +++ b/src/memory/table.c @@ -106,41 +106,20 @@ int swTableColumn_add(swTable *table, const char *name, int len, int type, int s switch(type) { case SW_TABLE_INT: - switch(size) - { - case 1: - col->size = 1; - col->type = SW_TABLE_INT8; - break; - case 2: - col->size = 2; - col->type = SW_TABLE_INT16; - break; -#ifdef __x86_64__ - case 8: - col->size = 8; - col->type = SW_TABLE_INT64; - break; -#endif - default: - col->size = 4; - col->type = SW_TABLE_INT32; - break; - } + col->size = sizeof(long); break; case SW_TABLE_FLOAT: col->size = sizeof(double); - col->type = SW_TABLE_FLOAT; break; case SW_TABLE_STRING: col->size = size + sizeof(swTable_string_length_t); - col->type = SW_TABLE_STRING; break; default: swWarn("unkown column type"); swTableColumn_free(col); return SW_ERR; } + col->type = type; col->index = table->item_size; table->item_size += col->size; ++table->column_num; @@ -369,6 +348,8 @@ swTableRow* swTableRow_set(swTable *table, const char *key, int keylen, swTableR } //add row_num bzero(new_row, sizeof(swTableRow)); + bzero((char *)new_row + sizeof(swTableRow), table->item_size); + sw_atomic_fetch_add(&(table->row_num), 1); row->next = new_row; row = new_row; @@ -389,6 +370,7 @@ swTableRow* swTableRow_set(swTable *table, const char *key, int keylen, swTableR insert_count ++; #endif sw_atomic_fetch_add(&(table->row_num), 1); + bzero((char *)row + sizeof(swTableRow), table->item_size); } memcpy(row->key, key, keylen); @@ -400,8 +382,7 @@ swTableRow* swTableRow_set(swTable *table, const char *key, int keylen, swTableR static inline void swTable_clear_row(swTable *table, swTableRow *row) { - sw_memset_zero((char*) &row->lock_pid, sizeof(*row) - offsetof(swTableRow, lock_pid)); - bzero(row + sizeof(swTableRow), table->item_size); + bzero((char*) &row->lock_pid, sizeof(*row) - offsetof(swTableRow, lock_pid)); } int swTableRow_del(swTable *table, char *key, int keylen) @@ -423,7 +404,7 @@ int swTableRow_del(swTable *table, char *key, int keylen) { if (sw_mem_equal(row->key, row->key_len, key, keylen)) { - swTable_clear_row(row); + swTable_clear_row(table, row); goto _delete_element; } else @@ -468,7 +449,7 @@ int swTableRow_del(swTable *table, char *key, int keylen) prev->next = tmp->next; } table->lock.lock(&table->lock); - swTable_clear_row(tmp); + swTable_clear_row(table, tmp); table->pool->free(table->pool, tmp); table->lock.unlock(&table->lock); } diff --git a/swoole_table.cc b/swoole_table.cc index 7ba1430b7ee..3afdfe0dc93 100644 --- a/swoole_table.cc +++ b/swoole_table.cc @@ -24,8 +24,6 @@ static inline void php_swoole_table_row2array(swTable *table, swTableRow *row, z swTableColumn *col = NULL; swTable_string_length_t vlen = 0; - double dval = 0; - int64_t lval = 0; char *k; swHashMap_rewind(table->columns); @@ -43,30 +41,15 @@ static inline void php_swoole_table_row2array(swTable *table, swTableRow *row, z } else if (col->type == SW_TABLE_FLOAT) { + double dval = 0; memcpy(&dval, row->data + col->index, sizeof(dval)); add_assoc_double_ex(return_value, col->name->str, col->name->length, dval); } else { - switch (col->type) - { - case SW_TABLE_INT8: - memcpy(&lval, row->data + col->index, 1); - add_assoc_long_ex(return_value, col->name->str, col->name->length, (int8_t) lval); - break; - case SW_TABLE_INT16: - memcpy(&lval, row->data + col->index, 2); - add_assoc_long_ex(return_value, col->name->str, col->name->length, (int16_t) lval); - break; - case SW_TABLE_INT32: - memcpy(&lval, row->data + col->index, 4); - add_assoc_long_ex(return_value, col->name->str, col->name->length, (int32_t) lval); - break; - default: - memcpy(&lval, row->data + col->index, 8); - add_assoc_long_ex(return_value, col->name->str, col->name->length, lval); - break; - } + long lval = 0; + memcpy(&lval, row->data + col->index, sizeof(lval)); + add_assoc_long_ex(return_value, col->name->str, col->name->length, lval); } } } @@ -74,8 +57,8 @@ static inline void php_swoole_table_row2array(swTable *table, swTableRow *row, z static inline void php_swoole_table_get_field_value(swTable *table, swTableRow *row, zval *return_value, char *field, uint16_t field_len) { swTable_string_length_t vlen = 0; - double dval = 0; - int64_t lval = 0; + + swTableColumn *col = (swTableColumn *) swHashMap_find(table->columns, field, field_len); if (!col) @@ -90,30 +73,15 @@ static inline void php_swoole_table_get_field_value(swTable *table, swTableRow * } else if (col->type == SW_TABLE_FLOAT) { + double dval = 0; memcpy(&dval, row->data + col->index, sizeof(dval)); ZVAL_DOUBLE(return_value, dval); } else { - switch (col->type) - { - case SW_TABLE_INT8: - memcpy(&lval, row->data + col->index, 1); - ZVAL_LONG(return_value, (int8_t) lval); - break; - case SW_TABLE_INT16: - memcpy(&lval, row->data + col->index, 2); - ZVAL_LONG(return_value, (int16_t) lval); - break; - case SW_TABLE_INT32: - memcpy(&lval, row->data + col->index, 4); - ZVAL_LONG(return_value, (int32_t) lval); - break; - default: - memcpy(&lval, row->data + col->index, 8); - ZVAL_LONG(return_value, lval); - break; - } + int64_t lval = 0; + memcpy(&lval, row->data + col->index, sizeof(lval)); + ZVAL_LONG(return_value, lval); } }