MCPcopy Create free account
hub / github.com/ashvardanian/StringZilla / sz_string_reserve

Function sz_string_reserve

include/stringzilla/small_string.h:341–367  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

339}
340
341SZ_PUBLIC sz_ptr_t sz_string_reserve(sz_string_t *string, sz_size_t new_capacity, sz_memory_allocator_t *allocator) {
342
343 sz_assert_(string && allocator && "Strings and allocators can't be SZ_NULL.");
344
345 sz_size_t new_space = new_capacity + 1;
346 if (new_space <= SZ_STRING_INTERNAL_SPACE) return string->external.start;
347
348 sz_ptr_t string_start;
349 sz_size_t string_length;
350 sz_size_t string_space;
351 sz_bool_t string_is_external;
352 sz_string_unpack(string, &string_start, &string_length, &string_space, &string_is_external);
353 sz_assert_(new_space > string_space && "New space must be larger than current.");
354
355 sz_ptr_t new_start = (sz_ptr_t)allocator->allocate(new_space, allocator->handle);
356 if (!new_start) return SZ_NULL_CHAR;
357
358 sz_copy(new_start, string_start, string_length);
359 string->external.start = new_start;
360 string->external.space = new_space;
361 string->external.padding = 0;
362 string->external.length = string_length;
363
364 // Deallocate the old string.
365 if (string_is_external) allocator->free(string_start, string_space, allocator->handle);
366 return string->external.start;
367}
368
369SZ_PUBLIC sz_ptr_t sz_string_shrink_to_fit(sz_string_t *string, sz_memory_allocator_t *allocator) {
370

Callers 2

try_reserveMethod · 0.85
sz_string_expandFunction · 0.85

Calls 3

sz_string_unpackFunction · 0.85
sz_copyFunction · 0.70
allocateMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…