-
Notifications
You must be signed in to change notification settings - Fork 8k
Add str_extend() #20527
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Add str_extend() #20527
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1671,7 +1671,10 @@ static zend_never_inline void *zend_mm_realloc_huge(zend_mm_heap *heap, void *pt | |
| return zend_mm_realloc_slow(heap, ptr, size, MIN(old_size, copy_size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); | ||
| } | ||
|
|
||
| static zend_always_inline void *zend_mm_realloc_heap(zend_mm_heap *heap, void *ptr, size_t size, bool use_copy_size, size_t copy_size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) | ||
| #define EREALLOC_DEFAULT 0 | ||
| #define EREALLOC_COPY 1 | ||
| #define EREALLOC_NOSHRINK 2 | ||
| static zend_always_inline void *zend_mm_realloc_heap(zend_mm_heap *heap, void *ptr, size_t size, int mode, size_t copy_size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) | ||
| { | ||
| size_t page_offset; | ||
| size_t old_size; | ||
|
|
@@ -1686,6 +1689,12 @@ static zend_always_inline void *zend_mm_realloc_heap(zend_mm_heap *heap, void *p | |
| if (EXPECTED(ptr == NULL)) { | ||
| return _zend_mm_alloc(heap, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); | ||
| } else { | ||
| if (mode == EREALLOC_NOSHRINK) { | ||
| old_size = zend_mm_get_huge_block_size(heap, ptr ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you use huge_block_size? |
||
| if (EXPECTED(size <= old_size)) { | ||
| return ptr; | ||
| } | ||
| } | ||
| return zend_mm_realloc_huge(heap, ptr, size, copy_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); | ||
| } | ||
| } else { | ||
|
|
@@ -1713,10 +1722,10 @@ static zend_always_inline void *zend_mm_realloc_heap(zend_mm_heap *heap, void *p | |
| /* Check if requested size fits into current bin */ | ||
| if (size <= old_size) { | ||
| /* Check if truncation is necessary */ | ||
| if (old_bin_num > 0 && size < bin_data_size[old_bin_num - 1]) { | ||
| if (mode != EREALLOC_NOSHRINK && old_bin_num > 0 && size < bin_data_size[old_bin_num - 1]) { | ||
| /* truncation */ | ||
| ret = zend_mm_alloc_small(heap, ZEND_MM_SMALL_SIZE_TO_BIN(size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); | ||
| copy_size = use_copy_size ? MIN(size, copy_size) : size; | ||
| copy_size = mode != EREALLOC_DEFAULT ? MIN(size, copy_size) : size; | ||
| memcpy(ret, ptr, copy_size); | ||
| zend_mm_free_small(heap, ptr, old_bin_num); | ||
| } else { | ||
|
|
@@ -1731,7 +1740,7 @@ static zend_always_inline void *zend_mm_realloc_heap(zend_mm_heap *heap, void *p | |
| size_t orig_peak = heap->peak; | ||
| #endif | ||
| ret = zend_mm_alloc_small(heap, ZEND_MM_SMALL_SIZE_TO_BIN(size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); | ||
| copy_size = use_copy_size ? MIN(old_size, copy_size) : old_size; | ||
| copy_size = mode != EREALLOC_DEFAULT ? MIN(old_size, copy_size) : old_size; | ||
| memcpy(ret, ptr, copy_size); | ||
| zend_mm_free_small(heap, ptr, old_bin_num); | ||
| #if ZEND_MM_STAT | ||
|
|
@@ -1759,7 +1768,7 @@ static zend_always_inline void *zend_mm_realloc_heap(zend_mm_heap *heap, void *p | |
| old_size = ZEND_MM_LRUN_PAGES(info) * ZEND_MM_PAGE_SIZE; | ||
| if (size > ZEND_MM_MAX_SMALL_SIZE && size <= ZEND_MM_MAX_LARGE_SIZE) { | ||
| new_size = ZEND_MM_ALIGNED_SIZE_EX(size, ZEND_MM_PAGE_SIZE); | ||
| if (new_size == old_size) { | ||
| if (mode == EREALLOC_NOSHRINK ? new_size <= old_size : (new_size == old_size)) { | ||
| #if ZEND_DEBUG | ||
| dbg = zend_mm_get_debug_info(heap, ptr); | ||
| dbg->size = real_size; | ||
|
|
@@ -2579,12 +2588,17 @@ ZEND_API void ZEND_FASTCALL _zend_mm_free(zend_mm_heap *heap, void *ptr ZEND_FIL | |
|
|
||
| void* ZEND_FASTCALL _zend_mm_realloc(zend_mm_heap *heap, void *ptr, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) | ||
| { | ||
| return zend_mm_realloc_heap(heap, ptr, size, 0, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); | ||
| return zend_mm_realloc_heap(heap, ptr, size, EREALLOC_DEFAULT, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); | ||
| } | ||
|
|
||
| void* ZEND_FASTCALL _zend_mm_realloc2(zend_mm_heap *heap, void *ptr, size_t size, size_t copy_size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) | ||
| { | ||
| return zend_mm_realloc_heap(heap, ptr, size, 1, copy_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); | ||
| return zend_mm_realloc_heap(heap, ptr, size, EREALLOC_COPY, copy_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); | ||
| } | ||
|
|
||
| void* ZEND_FASTCALL _zend_mm_realloc3(zend_mm_heap *heap, void *ptr, size_t size, size_t copy_size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) | ||
| { | ||
| return zend_mm_realloc_heap(heap, ptr, size, EREALLOC_NOSHRINK, copy_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); | ||
| } | ||
|
|
||
| ZEND_API size_t ZEND_FASTCALL _zend_mm_block_size(zend_mm_heap *heap, void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) | ||
|
|
@@ -2801,7 +2815,7 @@ ZEND_API void* ZEND_FASTCALL _erealloc(void *ptr, size_t size ZEND_FILE_LINE_DC | |
| return AG(mm_heap)->custom_heap._realloc(ptr, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); | ||
| } | ||
| #endif | ||
| return zend_mm_realloc_heap(AG(mm_heap), ptr, size, 0, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); | ||
| return zend_mm_realloc_heap(AG(mm_heap), ptr, size, EREALLOC_DEFAULT, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); | ||
| } | ||
|
|
||
| ZEND_API void* ZEND_FASTCALL _erealloc2(void *ptr, size_t size, size_t copy_size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) | ||
|
|
@@ -2811,7 +2825,17 @@ ZEND_API void* ZEND_FASTCALL _erealloc2(void *ptr, size_t size, size_t copy_size | |
| return AG(mm_heap)->custom_heap._realloc(ptr, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); | ||
| } | ||
| #endif | ||
| return zend_mm_realloc_heap(AG(mm_heap), ptr, size, 1, copy_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); | ||
| return zend_mm_realloc_heap(AG(mm_heap), ptr, size, EREALLOC_COPY, copy_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); | ||
| } | ||
|
|
||
| ZEND_API void* ZEND_FASTCALL _erealloc3(void *ptr, size_t size, size_t copy_size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) | ||
| { | ||
| #if ZEND_MM_CUSTOM | ||
| if (UNEXPECTED(AG(mm_heap)->use_custom_heap)) { | ||
| return AG(mm_heap)->custom_heap._realloc(ptr, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); | ||
| } | ||
| #endif | ||
| return zend_mm_realloc_heap(AG(mm_heap), ptr, size, EREALLOC_NOSHRINK, copy_size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); | ||
| } | ||
|
|
||
| ZEND_API size_t ZEND_FASTCALL _zend_mem_block_size(void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2448,6 +2448,11 @@ function substr_replace(array|string $string, array|string $replace, array|int $ | |
| */ | ||
| function quotemeta(string $string): string {} | ||
|
|
||
| /** | ||
| * @compile-time-eval | ||
| */ | ||
| function str_extend(string $string, int $size): string {} | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if this is the best self-explainable name
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree. The string does not become longer, so "extend" is the incorrect word. "reserve" may be better. |
||
|
|
||
| /** @compile-time-eval */ | ||
| function ord(string $character): int {} | ||
|
|
||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| --TEST-- | ||
| Append to string allocated with str_extend() | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| $str = str_extend("a", 1 << 22); | ||
| for ($i = 0; $i < 1 << 21; ++$i) { | ||
| $str .= "a"; | ||
| } | ||
|
|
||
| var_dump(array_filter(count_chars($str))); | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| array(1) { | ||
| [97]=> | ||
| int(2097153) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EREALLOC_COPYdoesn't keep the explanation of semantic ofuse_copy_size.In case
use_copy_sizeis set, erealloc() copies only the size specified incopy_sizeargument.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EREALLOC_NOSHRINKalso embracesuse_copy_sizemode. Probably mode should be represented as a bitset.