diff options
Diffstat (limited to 'php/ext/google/protobuf/map.c')
-rw-r--r-- | php/ext/google/protobuf/map.c | 44 |
1 files changed, 36 insertions, 8 deletions
diff --git a/php/ext/google/protobuf/map.c b/php/ext/google/protobuf/map.c index 2680b547..ab8a518a 100644 --- a/php/ext/google/protobuf/map.c +++ b/php/ext/google/protobuf/map.c @@ -293,13 +293,46 @@ static bool map_field_read_dimension(zval *object, zval *key, int type, } } +static bool map_index_unset(Map *intern, const char* keyval, int length) { + upb_value old_value; + if (upb_strtable_remove2(&intern->table, keyval, length, &old_value)) { + switch (intern->value_type) { + case UPB_TYPE_MESSAGE: { +#if PHP_MAJOR_VERSION < 7 + zval_ptr_dtor(upb_value_memory(&old_value)); +#else + zend_object* object = *(zend_object**)upb_value_memory(&old_value); + if(--GC_REFCOUNT(object) == 0) { + zend_objects_store_del(object); + } +#endif + break; + } + case UPB_TYPE_STRING: + case UPB_TYPE_BYTES: { +#if PHP_MAJOR_VERSION < 7 + zval_ptr_dtor(upb_value_memory(&old_value)); +#else + zend_string* object = *(zend_string**)upb_value_memory(&old_value); + zend_string_release(object); +#endif + break; + } + default: + break; + } + } +} + bool map_index_set(Map *intern, const char* keyval, int length, upb_value v) { // Replace any existing value by issuing a 'remove' operation first. - upb_strtable_remove2(&intern->table, keyval, length, NULL); + map_index_unset(intern, keyval, length); + if (!upb_strtable_insert2(&intern->table, keyval, length, v)) { zend_error(E_USER_ERROR, "Could not insert into table"); return false; } + return true; } @@ -326,12 +359,7 @@ static void map_field_write_dimension(zval *object, zval *key, v.ctype = UPB_CTYPE_UINT64; #endif - // Replace any existing value by issuing a 'remove' operation first. - upb_strtable_remove2(&intern->table, keyval, length, NULL); - if (!upb_strtable_insert2(&intern->table, keyval, length, v)) { - zend_error(E_USER_ERROR, "Could not insert into table"); - return; - } + map_index_set(intern, keyval, length, v); } static bool map_field_unset_dimension(zval *object, zval *key TSRMLS_DC) { @@ -348,7 +376,7 @@ static bool map_field_unset_dimension(zval *object, zval *key TSRMLS_DC) { v.ctype = UPB_CTYPE_UINT64; #endif - upb_strtable_remove2(&intern->table, keyval, length, &v); + map_index_unset(intern, keyval, length); return true; } |