Skip to content

Commit 097274b

Browse files
committed
Add more ZEND_API string functions
1 parent 323eb91 commit 097274b

File tree

2 files changed

+24
-21
lines changed

2 files changed

+24
-21
lines changed

Zend/zend_string.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,9 @@ ZEND_API extern inline bool zend_string_equals_cstr(const zend_string *s1, const
562562
ZEND_API extern inline void zend_string_forget_hash_val(zend_string *s);
563563
ZEND_API extern inline zend_ulong zend_string_hash_val(zend_string *s);
564564
ZEND_API extern inline zend_string *zend_string_init(const char *str, size_t len, bool persistent);
565+
ZEND_API extern inline zend_string *zend_string_init_fast(const char *str, size_t len);
565566
ZEND_API extern inline zend_string *zend_string_realloc(zend_string *s, size_t len, bool persistent);
566567
ZEND_API extern inline uint32_t zend_string_refcount(const zend_string *s);
568+
ZEND_API extern inline zend_string *zend_string_safe_alloc(size_t n, size_t m, size_t l, bool persistent);
569+
ZEND_API extern inline zend_string *zend_string_safe_realloc(zend_string *s, size_t n, size_t m, size_t l, bool persistent);
567570
ZEND_API extern inline zend_string *zend_string_separate(zend_string *s, bool persistent);

Zend/zend_string.h

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,8 @@ ZEND_API zend_always_inline zend_string *zend_string_init(const char *str, size_
196196
ZSTR_VAL(ret)[len] = '\0';
197197
return ret;
198198
}
199-
END_EXTERN_C()
200199

201-
static zend_always_inline zend_string *zend_string_safe_alloc(size_t n, size_t m, size_t l, bool persistent)
200+
ZEND_API zend_always_inline zend_string *zend_string_safe_alloc(size_t n, size_t m, size_t l, bool persistent)
202201
{
203202
zend_string *ret = (zend_string *)safe_pemalloc(n, m, ZEND_MM_ALIGNED_SIZE(_ZSTR_STRUCT_SIZE(l)), persistent);
204203

@@ -209,7 +208,7 @@ static zend_always_inline zend_string *zend_string_safe_alloc(size_t n, size_t m
209208
return ret;
210209
}
211210

212-
static zend_always_inline zend_string *zend_string_init_fast(const char *str, size_t len)
211+
ZEND_API zend_always_inline zend_string *zend_string_init_fast(const char *str, size_t len)
213212
{
214213
if (len > 1) {
215214
return zend_string_init(str, len, 0);
@@ -220,7 +219,6 @@ static zend_always_inline zend_string *zend_string_init_fast(const char *str, si
220219
}
221220
}
222221

223-
BEGIN_EXTERN_C()
224222
ZEND_API inline zend_string *zend_string_copy(zend_string *s)
225223
{
226224
if (!ZSTR_IS_INTERNED(s)) {
@@ -270,34 +268,33 @@ ZEND_API zend_always_inline zend_string *zend_string_realloc(zend_string *s, siz
270268
}
271269
return ret;
272270
}
273-
END_EXTERN_C()
274271

275-
static zend_always_inline zend_string *zend_string_extend(zend_string *s, size_t len, bool persistent)
272+
ZEND_API zend_always_inline zend_string *zend_string_safe_realloc(zend_string *s, size_t n, size_t m, size_t l, bool persistent)
276273
{
277274
zend_string *ret;
278275

279-
ZEND_ASSERT(len >= ZSTR_LEN(s));
280276
if (!ZSTR_IS_INTERNED(s)) {
281-
if (EXPECTED(GC_REFCOUNT(s) == 1)) {
282-
ret = (zend_string *)perealloc(s, ZEND_MM_ALIGNED_SIZE(_ZSTR_STRUCT_SIZE(len)), persistent);
283-
ZSTR_LEN(ret) = len;
277+
if (GC_REFCOUNT(s) == 1) {
278+
ret = (zend_string *)safe_perealloc(s, n, m, ZEND_MM_ALIGNED_SIZE(_ZSTR_STRUCT_SIZE(l)), persistent);
279+
ZSTR_LEN(ret) = (n * m) + l;
284280
zend_string_forget_hash_val(ret);
285281
return ret;
286282
}
287283
}
288-
ret = zend_string_alloc(len, persistent);
289-
memcpy(ZSTR_VAL(ret), ZSTR_VAL(s), ZSTR_LEN(s) + 1);
284+
ret = zend_string_safe_alloc(n, m, l, persistent);
285+
memcpy(ZSTR_VAL(ret), ZSTR_VAL(s), MIN((n * m) + l, ZSTR_LEN(s)) + 1);
290286
if (!ZSTR_IS_INTERNED(s)) {
291287
GC_DELREF(s);
292288
}
293289
return ret;
294290
}
291+
END_EXTERN_C()
295292

296-
static zend_always_inline zend_string *zend_string_truncate(zend_string *s, size_t len, bool persistent)
293+
static zend_always_inline zend_string *zend_string_extend(zend_string *s, size_t len, bool persistent)
297294
{
298295
zend_string *ret;
299296

300-
ZEND_ASSERT(len <= ZSTR_LEN(s));
297+
ZEND_ASSERT(len >= ZSTR_LEN(s));
301298
if (!ZSTR_IS_INTERNED(s)) {
302299
if (EXPECTED(GC_REFCOUNT(s) == 1)) {
303300
ret = (zend_string *)perealloc(s, ZEND_MM_ALIGNED_SIZE(_ZSTR_STRUCT_SIZE(len)), persistent);
@@ -307,33 +304,35 @@ static zend_always_inline zend_string *zend_string_truncate(zend_string *s, size
307304
}
308305
}
309306
ret = zend_string_alloc(len, persistent);
310-
memcpy(ZSTR_VAL(ret), ZSTR_VAL(s), len + 1);
307+
memcpy(ZSTR_VAL(ret), ZSTR_VAL(s), ZSTR_LEN(s) + 1);
311308
if (!ZSTR_IS_INTERNED(s)) {
312309
GC_DELREF(s);
313310
}
314311
return ret;
315312
}
316313

317-
static zend_always_inline zend_string *zend_string_safe_realloc(zend_string *s, size_t n, size_t m, size_t l, bool persistent)
314+
static zend_always_inline zend_string *zend_string_truncate(zend_string *s, size_t len, bool persistent)
318315
{
319316
zend_string *ret;
320317

318+
ZEND_ASSERT(len <= ZSTR_LEN(s));
321319
if (!ZSTR_IS_INTERNED(s)) {
322-
if (GC_REFCOUNT(s) == 1) {
323-
ret = (zend_string *)safe_perealloc(s, n, m, ZEND_MM_ALIGNED_SIZE(_ZSTR_STRUCT_SIZE(l)), persistent);
324-
ZSTR_LEN(ret) = (n * m) + l;
320+
if (EXPECTED(GC_REFCOUNT(s) == 1)) {
321+
ret = (zend_string *)perealloc(s, ZEND_MM_ALIGNED_SIZE(_ZSTR_STRUCT_SIZE(len)), persistent);
322+
ZSTR_LEN(ret) = len;
325323
zend_string_forget_hash_val(ret);
326324
return ret;
327325
}
328326
}
329-
ret = zend_string_safe_alloc(n, m, l, persistent);
330-
memcpy(ZSTR_VAL(ret), ZSTR_VAL(s), MIN((n * m) + l, ZSTR_LEN(s)) + 1);
327+
ret = zend_string_alloc(len, persistent);
328+
memcpy(ZSTR_VAL(ret), ZSTR_VAL(s), len + 1);
331329
if (!ZSTR_IS_INTERNED(s)) {
332330
GC_DELREF(s);
333331
}
334332
return ret;
335333
}
336334

335+
/* These functions have noinline variants */
337336
static zend_always_inline void zend_string_free(zend_string *s)
338337
{
339338
if (!ZSTR_IS_INTERNED(s)) {
@@ -373,6 +372,7 @@ static zend_always_inline void zend_string_release_ex(zend_string *s, bool persi
373372
}
374373
}
375374
}
375+
/* }}} */
376376

377377
BEGIN_EXTERN_C()
378378
ZEND_API zend_always_inline bool zend_string_equals_cstr(const zend_string *s1, const char *s2, size_t s2_length)

0 commit comments

Comments
 (0)