@@ -33,6 +33,7 @@ typedef struct {
3333
3434typedef struct {
3535 char * buffer_value ;
36+ hb_allocator_T allocator ;
3637} buffer_args_T ;
3738
3839static VALUE parse_convert_body (VALUE arg ) {
@@ -76,7 +77,8 @@ static VALUE buffer_to_string_body(VALUE arg) {
7677static VALUE buffer_cleanup (VALUE arg ) {
7778 buffer_args_T * args = (buffer_args_T * ) arg ;
7879
79- if (args -> buffer_value != NULL ) { free (args -> buffer_value ); }
80+ hb_allocator_dealloc (& args -> allocator , args -> buffer_value );
81+ hb_allocator_destroy (& args -> allocator );
8082
8183 return Qnil ;
8284}
@@ -151,9 +153,12 @@ static VALUE Herb_extract_ruby(int argc, VALUE* argv, VALUE self) {
151153 rb_scan_args (argc , argv , "1:" , & source , & options );
152154
153155 char * string = (char * ) check_string (source );
154- hb_buffer_T output ;
155156
156- if (!hb_buffer_init (& output , strlen (string ))) { return Qnil ; }
157+ hb_allocator_T allocator ;
158+ if (!hb_allocator_init (& allocator , HB_ALLOCATOR_ARENA )) { return Qnil ; }
159+
160+ hb_buffer_T output ;
161+ if (!hb_buffer_init (& output , strlen (string ), & allocator )) { return Qnil ; }
157162
158163 herb_extract_ruby_options_T extract_options = HERB_EXTRACT_RUBY_DEFAULT_OPTIONS ;
159164
@@ -173,32 +178,25 @@ static VALUE Herb_extract_ruby(int argc, VALUE* argv, VALUE self) {
173178 if (!NIL_P (preserve_positions_value )) { extract_options .preserve_positions = RTEST (preserve_positions_value ); }
174179 }
175180
176- hb_allocator_T allocator ;
177- if (!hb_allocator_init (& allocator , HB_ALLOCATOR_ARENA )) { return Qnil ; }
178-
179181 herb_extract_ruby_to_buffer_with_options (string , & output , & extract_options , & allocator );
180182
181- hb_allocator_destroy (& allocator );
182-
183- buffer_args_T args = { .buffer_value = output .value };
183+ buffer_args_T args = { .buffer_value = output .value , .allocator = allocator };
184184
185185 return rb_ensure (buffer_to_string_body , (VALUE ) & args , buffer_cleanup , (VALUE ) & args );
186186}
187187
188188static VALUE Herb_extract_html (VALUE self , VALUE source ) {
189189 char * string = (char * ) check_string (source );
190- hb_buffer_T output ;
191-
192- if (!hb_buffer_init (& output , strlen (string ))) { return Qnil ; }
193190
194191 hb_allocator_T allocator ;
195192 if (!hb_allocator_init (& allocator , HB_ALLOCATOR_ARENA )) { return Qnil ; }
196193
197- herb_extract_html_to_buffer (string , & output , & allocator );
194+ hb_buffer_T output ;
195+ if (!hb_buffer_init (& output , strlen (string ), & allocator )) { return Qnil ; }
198196
199- hb_allocator_destroy ( & allocator );
197+ herb_extract_html_to_buffer ( string , & output , & allocator );
200198
201- buffer_args_T args = { .buffer_value = output .value };
199+ buffer_args_T args = { .buffer_value = output .value , . allocator = allocator };
202200
203201 return rb_ensure (buffer_to_string_body , (VALUE ) & args , buffer_cleanup , (VALUE ) & args );
204202}
@@ -304,36 +302,36 @@ static VALUE Herb_leak_check(VALUE self, VALUE source) {
304302 }
305303
306304 {
307- hb_buffer_T output ;
308- if (!hb_buffer_init (& output , strlen (string ))) { return Qnil ; }
309-
310305 hb_allocator_T allocator ;
311306 if (!hb_allocator_init (& allocator , HB_ALLOCATOR_TRACKING )) { return Qnil ; }
312307
308+ hb_buffer_T output ;
309+ if (!hb_buffer_init (& output , strlen (string ), & allocator )) { return Qnil ; }
310+
313311 herb_extract_ruby_options_T extract_options = HERB_EXTRACT_RUBY_DEFAULT_OPTIONS ;
314312 herb_extract_ruby_to_buffer_with_options (string , & output , & extract_options , & allocator );
315313
316314 hb_allocator_tracking_stats_T * stats = hb_allocator_tracking_stats (& allocator );
317315 rb_hash_aset (result , ID2SYM (rb_intern ("extract_ruby" )), make_tracking_hash (stats ));
318316
317+ hb_buffer_free (& output );
319318 hb_allocator_destroy (& allocator );
320- free (output .value );
321319 }
322320
323321 {
324- hb_buffer_T output ;
325- if (!hb_buffer_init (& output , strlen (string ))) { return Qnil ; }
326-
327322 hb_allocator_T allocator ;
328323 if (!hb_allocator_init (& allocator , HB_ALLOCATOR_TRACKING )) { return Qnil ; }
329324
325+ hb_buffer_T output ;
326+ if (!hb_buffer_init (& output , strlen (string ), & allocator )) { return Qnil ; }
327+
330328 herb_extract_html_to_buffer (string , & output , & allocator );
331329
332330 hb_allocator_tracking_stats_T * stats = hb_allocator_tracking_stats (& allocator );
333331 rb_hash_aset (result , ID2SYM (rb_intern ("extract_html" )), make_tracking_hash (stats ));
334332
333+ hb_buffer_free (& output );
335334 hb_allocator_destroy (& allocator );
336- free (output .value );
337335 }
338336
339337 return result ;
0 commit comments