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