2020#include <string.h>
2121#include <mruby/internal.h>
2222
23+ #define mrbc_malloc (s ) mrb_basic_alloc_func(NULL,(s))
24+ #define mrbc_realloc (p ,s ) mrb_basic_alloc_func((p),(s))
25+ #define mrbc_free (p ) mrb_basic_alloc_func((p),0)
26+
2327#ifndef MRB_CODEGEN_LEVEL_MAX
2428#define MRB_CODEGEN_LEVEL_MAX 256
2529#endif
@@ -159,15 +163,6 @@ codegen_palloc(codegen_scope *s, size_t len)
159163 return p ;
160164}
161165
162- static void *
163- codegen_realloc (codegen_scope * s , void * p , size_t len )
164- {
165- p = mrb_realloc_simple (s -> mrb , p , len );
166-
167- if (!p && len > 0 ) codegen_error (s , "mrb_realloc" );
168- return p ;
169- }
170-
171166static void
172167check_no_ext_ops (codegen_scope * s , uint16_t a , uint16_t b )
173168{
@@ -195,9 +190,9 @@ emit_B(codegen_scope *s, uint32_t pc, uint8_t i)
195190 else {
196191 s -> icapa *= 2 ;
197192 }
198- s -> iseq = (mrb_code * )codegen_realloc ( s , s -> iseq , sizeof (mrb_code )* s -> icapa );
193+ s -> iseq = (mrb_code * )mrbc_realloc ( s -> iseq , sizeof (mrb_code )* s -> icapa );
199194 if (s -> lines ) {
200- s -> lines = (uint16_t * )codegen_realloc ( s , s -> lines , sizeof (uint16_t )* s -> icapa );
195+ s -> lines = (uint16_t * )mrbc_realloc ( s -> lines , sizeof (uint16_t )* s -> icapa );
201196 }
202197 }
203198 if (s -> lines ) {
@@ -805,11 +800,11 @@ realloc_pool_str(codegen_scope *s, mrb_irep_pool *p, mrb_int len)
805800{
806801 char * str ;
807802 if ((p -> tt & 3 ) == IREP_TT_SSTR ) {
808- str = (char * )codegen_realloc ( s , NULL , len + 1 );
803+ str = (char * )mrbc_malloc ( len + 1 );
809804 }
810805 else {
811806 str = (char * )p -> u .str ;
812- str = (char * )codegen_realloc ( s , str , len + 1 );
807+ str = (char * )mrbc_realloc ( str , len + 1 );
813808 }
814809 p -> tt = (uint32_t )(len <<2 | IREP_TT_STR );
815810 str [len ] = '\0' ;
@@ -820,7 +815,7 @@ static void
820815free_pool_str (codegen_scope * s , mrb_irep_pool * p )
821816{
822817 if ((p -> tt & 3 ) != IREP_TT_SSTR ) {
823- codegen_realloc ( s , (char * )p -> u .str , 0 );
818+ mrbc_free ( (char * )p -> u .str );
824819 }
825820 p -> u .str = NULL ;
826821 s -> irep -> plen -- ;
@@ -1095,7 +1090,7 @@ lit_pool_extend(codegen_scope *s)
10951090{
10961091 if (s -> irep -> plen == s -> pcapa ) {
10971092 s -> pcapa *= 2 ;
1098- s -> pool = (mrb_irep_pool * )codegen_realloc ( s , s -> pool , sizeof (mrb_irep_pool )* s -> pcapa );
1093+ s -> pool = (mrb_irep_pool * )mrbc_realloc ( s -> pool , sizeof (mrb_irep_pool )* s -> pcapa );
10991094 }
11001095
11011096 return & s -> pool [s -> irep -> plen ++ ];
@@ -1125,7 +1120,7 @@ new_litbint(codegen_scope *s, const char *p, int base)
11251120
11261121 char * buf ;
11271122 pv -> tt = IREP_TT_BIGINT ;
1128- buf = (char * )codegen_realloc ( s , NULL , plen + 3 );
1123+ buf = (char * )mrbc_malloc ( plen + 3 );
11291124 buf [0 ] = (char )plen ;
11301125 buf [1 ] = base ;
11311126 memcpy (buf + 2 , p , plen );
@@ -1172,7 +1167,7 @@ new_lit_str2(codegen_scope *s, const char *str1, mrb_int len1, const char *str2,
11721167 else {
11731168 char * p ;
11741169 pool -> tt = (uint32_t )(len <<2 ) | IREP_TT_STR ;
1175- p = (char * )codegen_realloc ( s , NULL , len + 1 );
1170+ p = (char * )mrbc_malloc ( len + 1 );
11761171 memcpy (p , str1 , len1 );
11771172 if (str2 ) memcpy (p + len1 , str2 , len2 );
11781173 p [len ] = '\0' ;
@@ -1266,7 +1261,7 @@ new_sym(codegen_scope *s, mrb_sym sym)
12661261 if (s -> scapa > 0xffff ) {
12671262 codegen_error (s , "too many symbols" );
12681263 }
1269- s -> syms = (mrb_sym * )codegen_realloc ( s , s -> syms , sizeof (mrb_sym )* s -> scapa );
1264+ s -> syms = (mrb_sym * )mrbc_realloc ( s -> syms , sizeof (mrb_sym )* s -> scapa );
12701265 }
12711266 s -> syms [s -> irep -> slen ] = sym ;
12721267 return s -> irep -> slen ++ ;
@@ -3968,7 +3963,7 @@ scope_add_irep(codegen_scope *s)
39683963 s -> irep = irep = mrb_add_irep (s -> mrb );
39693964 if (prev -> irep -> rlen == prev -> rcapa ) {
39703965 prev -> rcapa *= 2 ;
3971- prev -> reps = (mrb_irep * * )codegen_realloc ( s , prev -> reps , sizeof (mrb_irep * )* prev -> rcapa );
3966+ prev -> reps = (mrb_irep * * )mrbc_realloc ( prev -> reps , sizeof (mrb_irep * )* prev -> rcapa );
39723967 }
39733968 prev -> reps [prev -> irep -> rlen ] = irep ;
39743969 prev -> irep -> rlen ++ ;
@@ -3998,16 +3993,16 @@ scope_new(mrb_state *mrb, codegen_scope *prev, node *nlv)
39983993 scope_add_irep (s );
39993994
40003995 s -> rcapa = 8 ;
4001- s -> reps = (mrb_irep * * )mrb_malloc ( mrb , sizeof (mrb_irep * )* s -> rcapa );
3996+ s -> reps = (mrb_irep * * )mrbc_malloc ( sizeof (mrb_irep * )* s -> rcapa );
40023997
40033998 s -> icapa = 1024 ;
4004- s -> iseq = (mrb_code * )mrb_malloc ( mrb , sizeof (mrb_code )* s -> icapa );
3999+ s -> iseq = (mrb_code * )mrbc_malloc ( sizeof (mrb_code )* s -> icapa );
40054000
40064001 s -> pcapa = 32 ;
4007- s -> pool = (mrb_irep_pool * )mrb_malloc ( mrb , sizeof (mrb_irep_pool )* s -> pcapa );
4002+ s -> pool = (mrb_irep_pool * )mrbc_malloc ( sizeof (mrb_irep_pool )* s -> pcapa );
40084003
40094004 s -> scapa = 256 ;
4010- s -> syms = (mrb_sym * )mrb_malloc ( mrb , sizeof (mrb_sym )* s -> scapa );
4005+ s -> syms = (mrb_sym * )mrbc_malloc ( sizeof (mrb_sym )* s -> scapa );
40114006
40124007 s -> lv = nlv ;
40134008 s -> sp += node_len (nlv )+ 1 ; /* add self */
@@ -4017,7 +4012,7 @@ scope_new(mrb_state *mrb, codegen_scope *prev, node *nlv)
40174012 node * n = nlv ;
40184013 size_t i = 0 ;
40194014
4020- s -> irep -> lv = lv = (mrb_sym * )mrb_malloc ( mrb , sizeof (mrb_sym )* (s -> nlocals - 1 ));
4015+ s -> irep -> lv = lv = (mrb_sym * )mrbc_malloc ( sizeof (mrb_sym )* (s -> nlocals - 1 ));
40214016 for (i = 0 , n = nlv ; n ; i ++ ,n = n -> cdr ) {
40224017 lv [i ] = lv_name (n );
40234018 }
@@ -4027,7 +4022,7 @@ scope_new(mrb_state *mrb, codegen_scope *prev, node *nlv)
40274022
40284023 s -> filename_sym = prev -> filename_sym ;
40294024 if (s -> filename_sym ) {
4030- s -> lines = (uint16_t * )mrb_malloc ( mrb , sizeof (short )* s -> icapa );
4025+ s -> lines = (uint16_t * )mrbc_malloc ( sizeof (short )* s -> icapa );
40314026 }
40324027 s -> lineno = prev -> lineno ;
40334028
@@ -4059,7 +4054,7 @@ scope_finish(codegen_scope *s)
40594054 irep -> flags = 0 ;
40604055 if (s -> iseq ) {
40614056 size_t catchsize = sizeof (struct mrb_irep_catch_handler ) * irep -> clen ;
4062- irep -> iseq = (const mrb_code * )codegen_realloc ( s , s -> iseq , sizeof (mrb_code )* s -> pc + catchsize );
4057+ irep -> iseq = (const mrb_code * )mrbc_realloc ( s -> iseq , sizeof (mrb_code )* s -> pc + catchsize );
40634058 irep -> ilen = s -> pc ;
40644059 if (irep -> clen > 0 ) {
40654060 memcpy ((void * )(irep -> iseq + irep -> ilen ), s -> catch_table , catchsize );
@@ -4068,11 +4063,11 @@ scope_finish(codegen_scope *s)
40684063 else {
40694064 irep -> clen = 0 ;
40704065 }
4071- mrb_free ( s -> mrb , s -> catch_table );
4066+ mrbc_free ( s -> catch_table );
40724067 s -> catch_table = NULL ;
4073- irep -> pool = (const mrb_irep_pool * )codegen_realloc ( s , s -> pool , sizeof (mrb_irep_pool )* irep -> plen );
4074- irep -> syms = (const mrb_sym * )codegen_realloc ( s , s -> syms , sizeof (mrb_sym )* irep -> slen );
4075- irep -> reps = (const mrb_irep * * )codegen_realloc ( s , s -> reps , sizeof (mrb_irep * )* irep -> rlen );
4068+ irep -> pool = (const mrb_irep_pool * )mrbc_realloc ( s -> pool , sizeof (mrb_irep_pool )* irep -> plen );
4069+ irep -> syms = (const mrb_sym * )mrbc_realloc ( s -> syms , sizeof (mrb_sym )* irep -> slen );
4070+ irep -> reps = (const mrb_irep * * )mrbc_realloc ( s -> reps , sizeof (mrb_irep * )* irep -> rlen );
40764071 if (s -> filename_sym ) {
40774072 mrb_sym fname = mrb_parser_get_filename (s -> parser , s -> filename_index );
40784073 const char * filename = mrb_sym_name_len (s -> mrb , fname , NULL );
@@ -4177,7 +4172,7 @@ static int
41774172catch_handler_new (codegen_scope * s )
41784173{
41794174 size_t newsize = sizeof (struct mrb_irep_catch_handler ) * (s -> irep -> clen + 1 );
4180- s -> catch_table = (struct mrb_irep_catch_handler * )codegen_realloc ( s , (void * )s -> catch_table , newsize );
4175+ s -> catch_table = (struct mrb_irep_catch_handler * )mrbc_realloc ( (void * )s -> catch_table , newsize );
41814176 return s -> irep -> clen ++ ;
41824177}
41834178
0 commit comments