Skip to content

Commit d4ef7ef

Browse files
committed
Inline unneeded indirection for mbstring memory management
All memory allocation and deallocation for mbstring bounces through a table of function pointers before going to emalloc/efree/etc. But this is unnecessary. The allocators are never swapped out. Better to just call them directly.
1 parent dc98c13 commit d4ef7ef

14 files changed

+51
-213
lines changed

ext/mbstring/config.m4

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,6 @@ AC_DEFUN([PHP_MBSTRING_SETUP_LIBMBFL], [
167167
libmbfl/mbfl/mbfl_language.c
168168
libmbfl/mbfl/mbfl_memory_device.c
169169
libmbfl/mbfl/mbfl_string.c
170-
libmbfl/mbfl/mbfl_allocators.c
171170
libmbfl/nls/nls_de.c
172171
libmbfl/nls/nls_en.c
173172
libmbfl/nls/nls_ja.c
@@ -181,7 +180,7 @@ AC_DEFUN([PHP_MBSTRING_SETUP_LIBMBFL], [
181180
libmbfl/nls/nls_ua.c
182181
])
183182
PHP_MBSTRING_ADD_CFLAG([-DHAVE_CONFIG_H])
184-
PHP_MBSTRING_ADD_INSTALL_HEADERS([libmbfl/config.h libmbfl/mbfl/eaw_table.h libmbfl/mbfl/mbfilter.h libmbfl/mbfl/mbfilter_8bit.h libmbfl/mbfl/mbfilter_pass.h libmbfl/mbfl/mbfilter_wchar.h libmbfl/mbfl/mbfl_allocators.h libmbfl/mbfl/mbfl_consts.h libmbfl/mbfl/mbfl_convert.h libmbfl/mbfl/mbfl_defs.h libmbfl/mbfl/mbfl_encoding.h libmbfl/mbfl/mbfl_filter_output.h libmbfl/mbfl/mbfl_ident.h libmbfl/mbfl/mbfl_language.h libmbfl/mbfl/mbfl_memory_device.h libmbfl/mbfl/mbfl_string.h])
183+
PHP_MBSTRING_ADD_INSTALL_HEADERS([libmbfl/config.h libmbfl/mbfl/eaw_table.h libmbfl/mbfl/mbfilter.h libmbfl/mbfl/mbfilter_8bit.h libmbfl/mbfl/mbfilter_pass.h libmbfl/mbfl/mbfilter_wchar.h libmbfl/mbfl/mbfl_consts.h libmbfl/mbfl/mbfl_convert.h libmbfl/mbfl/mbfl_defs.h libmbfl/mbfl/mbfl_encoding.h libmbfl/mbfl/mbfl_filter_output.h libmbfl/mbfl/mbfl_ident.h libmbfl/mbfl/mbfl_language.h libmbfl/mbfl/mbfl_memory_device.h libmbfl/mbfl/mbfl_string.h])
185184
])
186185

187186
dnl

ext/mbstring/config.w32

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ if (PHP_MBSTRING != "no") {
4242
ADD_SOURCES("ext/mbstring/libmbfl/mbfl", "mbfilter.c mbfilter_8bit.c \
4343
mbfilter_pass.c mbfilter_wchar.c mbfl_convert.c mbfl_encoding.c \
4444
mbfl_filter_output.c mbfl_ident.c mbfl_language.c mbfl_memory_device.c \
45-
mbfl_string.c mbfl_allocators.c", "mbstring");
45+
mbfl_string.c", "mbstring");
4646

4747
ADD_SOURCES("ext/mbstring/libmbfl/nls", "nls_de.c nls_en.c nls_ja.c \
4848
nls_kr.c nls_neutral.c nls_ru.c nls_uni.c nls_zh.c nls_hy.c \
4949
nls_ua.c nls_tr.c", "mbstring");
5050

51-
PHP_INSTALL_HEADERS("ext/mbstring", "mbstring.h libmbfl/config.h libmbfl/mbfl/eaw_table.h libmbfl/mbfl/mbfilter.h libmbfl/mbfl/mbfilter_8bit.h libmbfl/mbfl/mbfilter_pass.h libmbfl/mbfl/mbfilter_wchar.h libmbfl/mbfl/mbfl_allocators.h libmbfl/mbfl/mbfl_consts.h libmbfl/mbfl/mbfl_convert.h libmbfl/mbfl/mbfl_defs.h libmbfl/mbfl/mbfl_encoding.h libmbfl/mbfl/mbfl_filter_output.h libmbfl/mbfl/mbfl_ident.h libmbfl/mbfl/mbfl_language.h libmbfl/mbfl/mbfl_memory_device.h libmbfl/mbfl/mbfl_string.h");
51+
PHP_INSTALL_HEADERS("ext/mbstring", "mbstring.h libmbfl/config.h libmbfl/mbfl/eaw_table.h libmbfl/mbfl/mbfilter.h libmbfl/mbfl/mbfilter_8bit.h libmbfl/mbfl/mbfilter_pass.h libmbfl/mbfl/mbfilter_wchar.h libmbfl/mbfl/mbfl_consts.h libmbfl/mbfl/mbfl_convert.h libmbfl/mbfl/mbfl_defs.h libmbfl/mbfl/mbfl_encoding.h libmbfl/mbfl/mbfl_filter_output.h libmbfl/mbfl/mbfl_ident.h libmbfl/mbfl/mbfl_language.h libmbfl/mbfl/mbfl_memory_device.h libmbfl/mbfl/mbfl_string.h");
5252

5353
AC_DEFINE('HAVE_MBSTRING', 1, 'Have mbstring support');
5454

ext/mbstring/libmbfl/filters/mbfilter_cp5022x.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ mbfl_filt_conv_wchar_cp50220_ctor(mbfl_convert_filter *filt)
587587

588588
mbfl_filt_conv_common_ctor(filt);
589589

590-
ctx = mbfl_malloc(sizeof(mbfl_filt_conv_wchar_cp50220_ctx));
590+
ctx = emalloc(sizeof(mbfl_filt_conv_wchar_cp50220_ctx));
591591
ctx->tl_param.mode = MBFL_FILT_TL_HAN2ZEN_KATAKANA | MBFL_FILT_TL_HAN2ZEN_GLUE;
592592

593593
ctx->last = *filt;
@@ -608,7 +608,7 @@ mbfl_filt_conv_wchar_cp50220_copy(mbfl_convert_filter *src, mbfl_convert_filter
608608
mbfl_filt_conv_wchar_cp50220_ctx *ctx;
609609

610610
*dest = *src;
611-
ctx = mbfl_malloc(sizeof(mbfl_filt_conv_wchar_cp50220_ctx));
611+
ctx = emalloc(sizeof(mbfl_filt_conv_wchar_cp50220_ctx));
612612
dest->opaque = ctx;
613613
dest->data = &ctx->last;
614614
}
@@ -619,7 +619,7 @@ mbfl_filt_conv_wchar_cp50220_dtor(mbfl_convert_filter *filt)
619619
vtbl_tl_jisx0201_jisx0208.filter_dtor(filt);
620620

621621
if (filt->opaque != NULL) {
622-
mbfl_free(filt->opaque);
622+
efree(filt->opaque);
623623
}
624624

625625
mbfl_filt_conv_common_dtor(filt);

ext/mbstring/libmbfl/filters/mbfilter_htmlent.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,15 @@ static const char html_entity_chars[] = "#0123456789abcdefghijklmnopqrstuvwxyzAB
161161
void mbfl_filt_conv_html_dec_ctor(mbfl_convert_filter *filter)
162162
{
163163
filter->status = 0;
164-
filter->opaque = mbfl_malloc(html_enc_buffer_size+1);
164+
filter->opaque = emalloc(html_enc_buffer_size+1);
165165
}
166166

167167
void mbfl_filt_conv_html_dec_dtor(mbfl_convert_filter *filter)
168168
{
169169
filter->status = 0;
170170
if (filter->opaque)
171171
{
172-
mbfl_free((void*)filter->opaque);
172+
efree((void*)filter->opaque);
173173
}
174174
filter->opaque = NULL;
175175
}
@@ -310,6 +310,6 @@ int mbfl_filt_conv_html_dec_flush(mbfl_convert_filter *filter)
310310
void mbfl_filt_conv_html_dec_copy(mbfl_convert_filter *src, mbfl_convert_filter *dest)
311311
{
312312
*dest = *src;
313-
dest->opaque = mbfl_malloc(html_enc_buffer_size+1);
313+
dest->opaque = emalloc(html_enc_buffer_size+1);
314314
memcpy(dest->opaque, src->opaque, html_enc_buffer_size+1);
315315
}

ext/mbstring/libmbfl/filters/mbfilter_tl_jisx0201_jisx0208.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
*
2323
*/
2424

25-
#include "mbfl_allocators.h"
2625
#include "mbfilter_tl_jisx0201_jisx0208.h"
2726
#include "translit_kana_jisx0201_jisx0208.h"
2827

ext/mbstring/libmbfl/mbfl/mbfilter.c

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ mbfl_buffer_converter_new(
124124
const mbfl_encoding *to,
125125
size_t buf_initsz)
126126
{
127-
mbfl_buffer_converter *convd = mbfl_malloc(sizeof(mbfl_buffer_converter));
127+
mbfl_buffer_converter *convd = emalloc(sizeof(mbfl_buffer_converter));
128128
convd->from = from;
129129
convd->to = to;
130130

@@ -147,7 +147,7 @@ mbfl_buffer_converter_new(
147147
}
148148
}
149149
if (convd->filter1 == NULL) {
150-
mbfl_free(convd);
150+
efree(convd);
151151
return NULL;
152152
}
153153

@@ -168,7 +168,7 @@ mbfl_buffer_converter_delete(mbfl_buffer_converter *convd)
168168
mbfl_convert_filter_delete(convd->filter2);
169169
}
170170
mbfl_memory_device_clear(&convd->device);
171-
mbfl_free((void*)convd);
171+
efree((void*)convd);
172172
}
173173
}
174174

@@ -368,8 +368,8 @@ mbfl_encoding_detector_new(const mbfl_encoding **elist, int elistsz, int strict)
368368
}
369369

370370
/* allocate */
371-
identd = mbfl_malloc(sizeof(mbfl_encoding_detector));
372-
identd->filter_list = mbfl_calloc(elistsz, sizeof(mbfl_identify_filter *));
371+
identd = emalloc(sizeof(mbfl_encoding_detector));
372+
identd->filter_list = ecalloc(elistsz, sizeof(mbfl_identify_filter *));
373373

374374
/* create filters */
375375
i = 0;
@@ -403,9 +403,9 @@ mbfl_encoding_detector_delete(mbfl_encoding_detector *identd)
403403
i--;
404404
mbfl_identify_filter_delete(identd->filter_list[i]);
405405
}
406-
mbfl_free((void *)identd->filter_list);
406+
efree((void *)identd->filter_list);
407407
}
408-
mbfl_free((void *)identd);
408+
efree((void *)identd);
409409
}
410410
}
411411

@@ -557,7 +557,7 @@ mbfl_identify_encoding(mbfl_string *string, const mbfl_encoding **elist, int eli
557557
const mbfl_encoding *encoding;
558558

559559
/* flist is an array of mbfl_identify_filter instances */
560-
flist = mbfl_calloc(elistsz, sizeof(mbfl_identify_filter));
560+
flist = ecalloc(elistsz, sizeof(mbfl_identify_filter));
561561

562562
num = 0;
563563
if (elist != NULL) {
@@ -624,7 +624,7 @@ mbfl_identify_encoding(mbfl_string *string, const mbfl_encoding **elist, int eli
624624
mbfl_identify_filter_cleanup(&flist[i]);
625625
}
626626

627-
mbfl_free((void *)flist);
627+
efree((void *)flist);
628628

629629
return encoding;
630630
}
@@ -1108,7 +1108,7 @@ mbfl_substr(
11081108
/* allocate memory and copy */
11091109
n = end - start;
11101110
result->len = 0;
1111-
result->val = w = (unsigned char*)mbfl_malloc(n + 1);
1111+
result->val = w = (unsigned char*)emalloc(n + 1);
11121112
result->len = n;
11131113
memcpy(w, string->val + start, n);
11141114
w[n] = '\0';
@@ -1256,7 +1256,7 @@ mbfl_strcut(
12561256

12571257
/* allocate memory and copy string */
12581258
sz = end - start;
1259-
w = mbfl_calloc(sz + 8, sizeof(unsigned char));
1259+
w = ecalloc(sz + 8, sizeof(unsigned char));
12601260

12611261
memcpy(w, start, sz);
12621262
w[sz] = '\0';
@@ -1686,7 +1686,7 @@ mbfl_ja_jp_hantozen(
16861686
}
16871687
next_filter = decoder;
16881688

1689-
param = mbfl_malloc(sizeof(mbfl_filt_tl_jisx0201_jisx0208_param));
1689+
param = emalloc(sizeof(mbfl_filt_tl_jisx0201_jisx0208_param));
16901690
param->mode = mode;
16911691

16921692
tl_filter = mbfl_convert_filter_new2(
@@ -1695,7 +1695,7 @@ mbfl_ja_jp_hantozen(
16951695
(int(*)(void*))next_filter->filter_flush,
16961696
next_filter);
16971697
if (tl_filter == NULL) {
1698-
mbfl_free(param);
1698+
efree(param);
16991699
goto out;
17001700
}
17011701

@@ -1729,7 +1729,7 @@ mbfl_ja_jp_hantozen(
17291729
out:
17301730
if (tl_filter != NULL) {
17311731
if (tl_filter->opaque != NULL) {
1732-
mbfl_free(tl_filter->opaque);
1732+
efree(tl_filter->opaque);
17331733
}
17341734
mbfl_convert_filter_delete(tl_filter);
17351735
}
@@ -1922,7 +1922,7 @@ mime_header_encoder_new(
19221922
return NULL;
19231923
}
19241924

1925-
pe = mbfl_malloc(sizeof(struct mime_header_encoder_data));
1925+
pe = emalloc(sizeof(struct mime_header_encoder_data));
19261926
mbfl_memory_device_init(&pe->outdev, 0, 0);
19271927
mbfl_memory_device_init(&pe->tmpdev, 0, 0);
19281928
pe->prevpos = 0;
@@ -2003,7 +2003,7 @@ mime_header_encoder_delete(struct mime_header_encoder_data *pe)
20032003
mbfl_convert_filter_delete(pe->encod_filter_backup);
20042004
mbfl_memory_device_clear(&pe->outdev);
20052005
mbfl_memory_device_clear(&pe->tmpdev);
2006-
mbfl_free((void*)pe);
2006+
efree((void*)pe);
20072007
}
20082008
}
20092009

@@ -2265,7 +2265,7 @@ mime_header_decoder_result(struct mime_header_decoder_data *pd, mbfl_string *res
22652265
struct mime_header_decoder_data*
22662266
mime_header_decoder_new(const mbfl_encoding *outcode)
22672267
{
2268-
struct mime_header_decoder_data *pd = mbfl_malloc(sizeof(struct mime_header_decoder_data));
2268+
struct mime_header_decoder_data *pd = emalloc(sizeof(struct mime_header_decoder_data));
22692269

22702270
mbfl_memory_device_init(&pd->outdev, 0, 0);
22712271
mbfl_memory_device_init(&pd->tmpdev, 0, 0);
@@ -2297,7 +2297,7 @@ mime_header_decoder_delete(struct mime_header_decoder_data *pd)
22972297
mbfl_convert_filter_delete(pd->deco_filter);
22982298
mbfl_memory_device_clear(&pd->outdev);
22992299
mbfl_memory_device_clear(&pd->tmpdev);
2300-
mbfl_free((void*)pd);
2300+
efree((void*)pd);
23012301
}
23022302
}
23032303

ext/mbstring/libmbfl/mbfl/mbfilter.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,10 @@
8585
#ifndef MBFL_MBFILTER_H
8686
#define MBFL_MBFILTER_H
8787

88+
#include "zend.h"
89+
8890
#include "mbfl_defs.h"
8991
#include "mbfl_consts.h"
90-
#include "mbfl_allocators.h"
9192
#include "mbfl_encoding.h"
9293
#include "mbfl_language.h"
9394
#include "mbfl_string.h"

ext/mbstring/libmbfl/mbfl/mbfl_allocators.c

Lines changed: 0 additions & 73 deletions
This file was deleted.

ext/mbstring/libmbfl/mbfl/mbfl_allocators.h

Lines changed: 0 additions & 54 deletions
This file was deleted.

0 commit comments

Comments
 (0)