Showing the changes to src/vm/string.cpp (1 of 6 files). Show all
Unified · Split
| @@ -301,23 +301,17 @@ vm_string_t::vm_string_t(const vm_string_t &other, word_t begin_index, word_t en · 300 unmodified lines | ||
| 301 | 301 | |
| 302 | 302 | vm_string_t::~vm_string_t() |
| 303 | 303 | { |
| 304 | − // Free memory and string buffer | |
| 305 | − if (_is_alloc) | |
| 306 | − { | |
| 307 | − if (static_cast<void *>(_mem.alloc) != static_cast<void *>(_encoded_str)) | |
| 308 | − free_memory(_encoded_str); | |
| 304 | + // Determine if local or allocated memory | |
| 305 | + auto source = (_is_alloc) ? _mem.alloc : _mem.local; | |
| 306 | + if (static_cast<void *>(source) != static_cast<void *>(_encoded_str)) | |
| 307 | + free_memory(_encoded_str); | |
| 309 | 308 | |
| 310 | − free_memory(_mem.alloc); | |
| 311 | − } | |
| 312 | − else | |
| 313 | − { | |
| 314 | − if (static_cast<void *>(_mem.local) != static_cast<void *>(_encoded_str)) | |
| 315 | − free_memory(_encoded_str); | |
| 316 | − } | |
| 309 | + if (_is_alloc) | |
| 310 | + free_memory(source); | |
| 317 | 311 | |
| 318 | 312 | debug::log_msg(debug::component_trace_t::memory, debug::log_level_t::debug, "destroy: vm string"); |
| 319 | 313 | |
| 320 | − debug::assign_debug_pointer(&_mem.alloc); | |
| 314 | + debug::assign_debug_pointer(&source); | |
| 321 | 315 | debug::assign_debug_pointer(&_encoded_str); |
| 322 | 316 | } |
| 323 | 317 | |
| @@ -341,14 +335,10 @@ vm_string_t &vm_string_t::append(const vm_string_t &other) · 17 unmodified lines | ||
| 341 | 335 | const auto local_source = (_is_alloc) ? _mem.alloc : _mem.local; |
| 342 | 336 | |
| 343 | 337 | // Release the encoded string for this instance. |
| 344 | − { | |
| 345 | − std::lock_guard<std::mutex> lock{ _encoded_str_lock }; | |
| 346 | − if (_encoded_str != reinterpret_cast<char*>(local_source)) | |
| 347 | − { | |
| 348 | − free_memory(_encoded_str); | |
| 349 | − _encoded_str = nullptr; | |
| 350 | − } | |
| 351 | − } | |
| 338 | + if (_encoded_str != reinterpret_cast<char*>(local_source)) | |
| 339 | + free_memory(_encoded_str); | |
| 340 | + | |
| 341 | + _encoded_str = nullptr; | |
| 352 | 342 | |
| 353 | 343 | // Define the values for the appended string |
| 354 | 344 | const auto new_length = local_length + other_length; |
| @@ -427,17 +417,12 @@ void vm_string_t::set_rune(word_t index, rune_t value) · 72 unmodified lines | ||
| 427 | 417 | throw index_out_of_range_memory{ 0, _length, index }; |
| 428 | 418 | |
| 429 | 419 | // Determine if local or allocated memory |
| 430 | − auto source = static_cast<uint8_t *>(_mem.local); | |
| 431 | − if (_is_alloc) | |
| 432 | − { | |
| 433 | − source = static_cast<uint8_t *>(_mem.alloc); | |
| 420 | + auto source = (_is_alloc) ? _mem.alloc : _mem.local; | |
| 434 | 421 | |
| 435 | − // Since we are setting a rune, the string value should be freed. | |
| 436 | − if (_encoded_str != nullptr) | |
| 437 | − free_memory(_encoded_str); | |
| 438 | − } | |
| 422 | + // Release the encoded string for this instance. | |
| 423 | + if (_encoded_str != reinterpret_cast<char*>(source)) | |
| 424 | + free_memory(_encoded_str); | |
| 439 | 425 | |
| 440 | − // Reset the string so it can be regenerated. | |
| 441 | 426 | _encoded_str = nullptr; |
| 442 | 427 | |
| 443 | 428 | const auto new_character_size = (value > utf8::constants::max_codepoint_ascii) ? sizeof(rune_t) : sizeof(char); |
| @@ -513,6 +498,7 @@ const char *vm_string_t::str() const · 69 unmodified lines | ||
| 513 | 498 | if (_character_size == sizeof(char)) |
| 514 | 499 | { |
| 515 | 500 | _encoded_str = (_is_alloc) ? reinterpret_cast<char *>(_mem.alloc) : reinterpret_cast<char *>(_mem.local); |
| 501 | + _encoded_str[_length] = '\0'; | |
| 516 | 502 | return _encoded_str; |
| 517 | 503 | } |
| 518 | 504 | |