MCPcopy Create free account
hub / github.com/PaddlePaddle/Paddle / PD_PString_Reserve

Function PD_PString_Reserve

paddle/phi/common/cpstring_impl.h:398–441  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

396}
397
398HOSTDEVICE static inline void PD_PString_Reserve(PD_PString *str,
399 size_t new_cap) {
400 PD_PString_Type curr_type = PD_PString_GetType(str);
401
402 if (new_cap <= PD_PString_SmallCapacity) {
403 // We do nothing, we let Resize/GetMutableDataPointer handle the
404 // conversion to SMALL from VIEW/OFFSET when the need arises.
405 // In the degenerate case, where new_cap <= PD_PString_SmallCapacity,
406 // curr_size > PD_PString_SmallCapacity, and the type is VIEW/OFFSET, we
407 // defer the malloc to Resize/GetMutableDataPointer.
408 return;
409 }
410
411 if (curr_type == PD_PSTR_LARGE && new_cap <= str->u.large.cap) {
412 // We handle reduced cap in resize.
413 return;
414 }
415
416 // Case: VIEW/OFFSET -> LARGE or grow an existing LARGE type
417 size_t curr_size = PD_PString_GetSize(str);
418 const char *curr_ptr = PD_PString_GetDataPointer(str);
419
420 // Since VIEW and OFFSET types are read-only, their capacity is effectively 0.
421 // So we make sure we have enough room in the VIEW and OFFSET cases.
422 new_cap = PD_align16(PD_max(new_cap, curr_size) + 1) - 1;
423 size_t curr_cap = PD_PString_GetCapacity(str);
424
425 if (curr_type == PD_PSTR_LARGE) {
426 str->u.large.ptr = (char *)PD_Realloc( // NOLINT
427 str->u.large.ptr,
428 curr_cap + 1,
429 new_cap + 1);
430 } else {
431 // Convert to Large
432 char *new_ptr = (char *)PD_Malloc(new_cap + 1); // NOLINT
433 PD_Memcpy(new_ptr, curr_ptr, curr_size);
434
435 str->u.large.size = PD_PString_ToInternalSizeT(curr_size, PD_PSTR_LARGE);
436 str->u.large.ptr = new_ptr;
437 str->u.large.ptr[curr_size] = '\0';
438 }
439
440 str->u.large.cap = new_cap;
441}
442
443HOSTDEVICE static inline void PD_PString_ReserveAmortized(PD_PString *str,
444 size_t new_cap) {

Callers 2

reserveMethod · 0.85

Calls 9

PD_PString_GetSizeFunction · 0.85
PD_align16Function · 0.85
PD_maxFunction · 0.85
PD_PString_GetCapacityFunction · 0.85
PD_ReallocFunction · 0.85
PD_MallocFunction · 0.85
PD_MemcpyFunction · 0.85

Tested by

no test coverage detected