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

Function PD_PString_ResizeUninitialized

paddle/phi/common/cpstring_impl.h:320–377  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

318}
319
320HOSTDEVICE static inline char *PD_PString_ResizeUninitialized(PD_PString *str,
321 size_t new_size) {
322 size_t curr_size = PD_PString_GetSize(str);
323 size_t copy_size = PD_min(new_size, curr_size);
324
325 PD_PString_Type curr_type = PD_PString_GetType(str);
326 const char *curr_ptr = PD_PString_GetDataPointer(str);
327
328 // Case: SMALL/LARGE/VIEW/OFFSET -> SMALL
329 if (new_size <= PD_PString_SmallCapacity) {
330 str->u.small.size = (uint8_t)((new_size << 2) | PD_PSTR_SMALL); // NOLINT
331 str->u.small.str[new_size] = '\0';
332
333 if (curr_type != PD_PSTR_SMALL && copy_size) {
334 PD_Memcpy(str->u.small.str, curr_ptr, copy_size);
335 }
336
337 if (curr_type == PD_PSTR_LARGE) {
338 PD_Free((void *)curr_ptr, str->u.large.cap + 1); // NOLINT
339 }
340
341 return str->u.small.str;
342 }
343
344 // Case: SMALL/LARGE/VIEW/OFFSET -> LARGE
345 size_t new_cap;
346 size_t curr_cap = PD_PString_GetCapacity(str);
347
348 if (new_size < curr_size && new_size < curr_cap / 2) {
349 new_cap = PD_align16(curr_cap / 2 + 1) - 1;
350 } else if (new_size > curr_cap) {
351 new_cap = PD_align16(new_size + 1) - 1;
352 } else {
353 new_cap = curr_cap;
354 }
355
356 char *new_ptr;
357 if (new_cap == curr_cap) {
358 new_ptr = str->u.large.ptr;
359 } else if (curr_type == PD_PSTR_LARGE) {
360 new_ptr = (char *)PD_Realloc( // NOLINT
361 str->u.large.ptr,
362 curr_cap + 1,
363 new_cap + 1);
364 } else {
365 new_ptr = (char *)PD_Malloc(new_cap + 1); // NOLINT
366 if (copy_size) {
367 PD_Memcpy(new_ptr, curr_ptr, copy_size);
368 }
369 }
370
371 str->u.large.size = PD_PString_ToInternalSizeT(new_size, PD_PSTR_LARGE);
372 str->u.large.ptr = new_ptr;
373 str->u.large.ptr[new_size] = '\0';
374 str->u.large.cap = new_cap;
375
376 return str->u.large.ptr;
377}

Callers 7

PD_PString_ResizeFunction · 0.85
PD_PString_AppendNFunction · 0.85
PD_PString_CopyFunction · 0.85
resize_uninitializedMethod · 0.85
clearMethod · 0.85
pstring.hFile · 0.85

Calls 10

PD_PString_GetSizeFunction · 0.85
PD_minFunction · 0.85
PD_MemcpyFunction · 0.85
PD_FreeFunction · 0.85
PD_PString_GetCapacityFunction · 0.85
PD_align16Function · 0.85
PD_ReallocFunction · 0.85
PD_MallocFunction · 0.85

Tested by

no test coverage detected