From 48a2b96d2b8228a1c7dd49425fccd8b1202bf905 Mon Sep 17 00:00:00 2001 From: normal Date: Fri, 25 Jul 2014 21:34:35 +0000 Subject: introduce ZALLOC{,_N} to replace ALLOC{,_N}+MEMZERO use Using calloc where possible reduces code and binary sizes. * include/ruby/ruby.h (ZALLOC, ZALLOC_N): implement (Data_Make_Struct, TypedData_Make_Struct): ZALLOC replaces ALLOC+memset * compile.c (iseq_seq_sequence): ZALLOC_N replaces ALLOC_N+MEMZERO * cont.c (fiber_t_alloc): ZALLOC replaces ALLOC+MEMZERO * io.c (rb_io_reopen): ditto * iseq.c (prepare_iseq_build): ditto * parse.y (new_args_tail_gen, parser_new, ripper_s_allocate): ditto * re.c (match_alloc): ditto * variable.c (rb_const_set): ditto * ext/socket/raddrinfo.c (get_addrinfo): ditto * ext/strscan/strscan.c (strscan_s_allocate): ditto * gc.c (rb_objspace_alloc): calloc replaces malloc+MEMZERO git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@46952 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 16 ++++++++++++++++ compile.c | 3 +-- cont.c | 3 +-- ext/socket/raddrinfo.c | 3 +-- ext/strscan/strscan.c | 3 +-- gc.c | 6 ++---- include/ruby/ruby.h | 8 ++++---- io.c | 3 +-- iseq.c | 3 +-- parse.y | 9 +++------ re.c | 3 +-- variable.c | 3 +-- 12 files changed, 33 insertions(+), 30 deletions(-) diff --git a/ChangeLog b/ChangeLog index 116929d9db..f23d7c95eb 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,19 @@ +Sat Jul 26 05:58:35 2014 Eric Wong + + * include/ruby/ruby.h (ZALLOC, ZALLOC_N): implement + (Data_Make_Struct, TypedData_Make_Struct): + ZALLOC replaces ALLOC+memset + * compile.c (iseq_seq_sequence): ZALLOC_N replaces ALLOC_N+MEMZERO + * cont.c (fiber_t_alloc): ZALLOC replaces ALLOC+MEMZERO + * io.c (rb_io_reopen): ditto + * iseq.c (prepare_iseq_build): ditto + * parse.y (new_args_tail_gen, parser_new, ripper_s_allocate): ditto + * re.c (match_alloc): ditto + * variable.c (rb_const_set): ditto + * ext/socket/raddrinfo.c (get_addrinfo): ditto + * ext/strscan/strscan.c (strscan_s_allocate): ditto + * gc.c (rb_objspace_alloc): calloc replaces malloc+MEMZERO + Sat Jul 26 05:54:54 2014 Eric Wong * symbol.c (dsymbol_check): remove unneeded semi-colon diff --git a/compile.c b/compile.c index 051b369fc1..6af1dfda58 100644 --- a/compile.c +++ b/compile.c @@ -1441,8 +1441,7 @@ iseq_set_sequence(rb_iseq_t *iseq, LINK_ANCHOR *anchor) /* make instruction sequence */ generated_iseq = ALLOC_N(VALUE, pos); line_info_table = ALLOC_N(struct iseq_line_info_entry, k); - iseq->is_entries = ALLOC_N(union iseq_inline_storage_entry, iseq->is_size); - MEMZERO(iseq->is_entries, union iseq_inline_storage_entry, iseq->is_size); + iseq->is_entries = ZALLOC_N(union iseq_inline_storage_entry, iseq->is_size); iseq->callinfo_entries = ALLOC_N(rb_call_info_t, iseq->callinfo_size); /* MEMZERO(iseq->callinfo_entries, rb_call_info_t, iseq->callinfo_size); */ diff --git a/cont.c b/cont.c index 3df3aea22c..18a3373de2 100644 --- a/cont.c +++ b/cont.c @@ -1162,8 +1162,7 @@ fiber_t_alloc(VALUE fibval) } THREAD_MUST_BE_RUNNING(th); - fib = ALLOC(rb_fiber_t); - memset(fib, 0, sizeof(rb_fiber_t)); + fib = ZALLOC(rb_fiber_t); fib->cont.self = fibval; fib->cont.type = FIBER_CONTEXT; cont_init(&fib->cont, th); diff --git a/ext/socket/raddrinfo.c b/ext/socket/raddrinfo.c index 1ec03eb489..ceed9c3ac1 100644 --- a/ext/socket/raddrinfo.c +++ b/ext/socket/raddrinfo.c @@ -718,8 +718,7 @@ get_addrinfo(VALUE self) static rb_addrinfo_t * alloc_addrinfo() { - rb_addrinfo_t *rai = ALLOC(rb_addrinfo_t); - memset(rai, 0, sizeof(rb_addrinfo_t)); + rb_addrinfo_t *rai = ZALLOC(rb_addrinfo_t); rai->inspectname = Qnil; rai->canonname = Qnil; return rai; diff --git a/ext/strscan/strscan.c b/ext/strscan/strscan.c index ad0c527972..fd61b544b3 100644 --- a/ext/strscan/strscan.c +++ b/ext/strscan/strscan.c @@ -199,8 +199,7 @@ strscan_s_allocate(VALUE klass) { struct strscanner *p; - p = ALLOC(struct strscanner); - MEMZERO(p, struct strscanner, 1); + p = ZALLOC(struct strscanner); CLEAR_MATCH_STATUS(p); onig_region_init(&(p->regs)); p->str = Qnil; diff --git a/gc.c b/gc.c index ae3f570ad1..f67caa62d7 100644 --- a/gc.c +++ b/gc.c @@ -920,8 +920,7 @@ RVALUE_DEMOTE_FROM_OLD(rb_objspace_t *objspace, VALUE obj) rb_objspace_t * rb_objspace_alloc(void) { - rb_objspace_t *objspace = malloc(sizeof(rb_objspace_t)); - memset(objspace, 0, sizeof(*objspace)); + rb_objspace_t *objspace = calloc(1, sizeof(rb_objspace_t)); ruby_gc_stress = ruby_initial_gc_stress; malloc_limit = gc_params.malloc_limit_min; @@ -1087,13 +1086,12 @@ heap_page_allocate(rb_objspace_t *objspace) } /* assign heap_page entry */ - page = (struct heap_page *)malloc(sizeof(struct heap_page)); + page = (struct heap_page *)calloc(1, sizeof(struct heap_page)); if (page == 0) { aligned_free(page_body); during_gc = 0; rb_memerror(); } - MEMZERO((void*)page, struct heap_page, 1); page->body = page_body; diff --git a/include/ruby/ruby.h b/include/ruby/ruby.h index 7bb322868e..472e1e417a 100644 --- a/include/ruby/ruby.h +++ b/include/ruby/ruby.h @@ -1000,8 +1000,7 @@ void *rb_check_typeddata(VALUE, const rb_data_type_t *); rb_data_object_alloc((klass),(sval),(RUBY_DATA_FUNC)(mark),(RUBY_DATA_FUNC)(free)) #define Data_Make_Struct(klass,type,mark,free,sval) (\ - (sval) = ALLOC(type),\ - memset((sval), 0, sizeof(type)),\ + (sval) = ZALLOC(type),\ Data_Wrap_Struct((klass),(mark),(free),(sval))\ ) @@ -1009,8 +1008,7 @@ void *rb_check_typeddata(VALUE, const rb_data_type_t *); rb_data_typed_object_alloc((klass),(sval),(data_type)) #define TypedData_Make_Struct(klass, type, data_type, sval) (\ - (sval) = ALLOC(type),\ - memset((sval), 0, sizeof(type)),\ + (sval) = ZALLOC(type),\ TypedData_Wrap_Struct((klass),(data_type),(sval))\ ) @@ -1270,6 +1268,8 @@ rb_num2char_inline(VALUE x) #define ALLOC_N(type,n) ((type*)xmalloc2((n),sizeof(type))) #define ALLOC(type) ((type*)xmalloc(sizeof(type))) +#define ZALLOC_N(type,n) ((type*)xcalloc((n),sizeof(type))) +#define ZALLOC(type) (ZALLOC_N(type,1)) #define REALLOC_N(var,type,n) ((var)=(type*)xrealloc2((char*)(var),(n),sizeof(type))) #define ALLOCA_N(type,n) ((type*)alloca(sizeof(type)*(n))) diff --git a/io.c b/io.c index 10dfa6f680..f2a59bab77 100644 --- a/io.c +++ b/io.c @@ -6725,8 +6725,7 @@ rb_io_reopen(int argc, VALUE *argv, VALUE file) rb_io_taint_check(file); fptr = RFILE(file)->fptr; if (!fptr) { - fptr = RFILE(file)->fptr = ALLOC(rb_io_t); - MEMZERO(fptr, rb_io_t, 1); + fptr = RFILE(file)->fptr = ZALLOC(rb_io_t); } if (!NIL_P(nmode) || !NIL_P(opt)) { diff --git a/iseq.c b/iseq.c index b3ff7fd558..fdfd3b5f13 100644 --- a/iseq.c +++ b/iseq.c @@ -286,8 +286,7 @@ prepare_iseq_build(rb_iseq_t *iseq, * iseq->cached_special_block = 0; */ - iseq->compile_data = ALLOC(struct iseq_compile_data); - MEMZERO(iseq->compile_data, struct iseq_compile_data, 1); + iseq->compile_data = ZALLOC(struct iseq_compile_data); RB_OBJ_WRITE(iseq->self, &iseq->compile_data->err_info, Qnil); RB_OBJ_WRITE(iseq->self, &iseq->compile_data->mark_ary, rb_ary_tmp_new(3)); diff --git a/parse.y b/parse.y index 882a282137..925c1a540b 100644 --- a/parse.y +++ b/parse.y @@ -9511,8 +9511,7 @@ new_args_tail_gen(struct parser_params *parser, NODE *k, ID kr, ID b) NODE *node; int check = 0; - args = ALLOC(struct rb_args_info); - MEMZERO(args, struct rb_args_info, 1); + args = ZALLOC(struct rb_args_info); node = NEW_NODE(NODE_ARGS, 0, 0, args); args->block_arg = b; @@ -10244,8 +10243,7 @@ parser_new(void) { struct parser_params *p; - p = ALLOC_N(struct parser_params, 1); - MEMZERO(p, struct parser_params, 1); + p = ZALLOC(struct parser_params); parser_initialize(p); return p; } @@ -10665,8 +10663,7 @@ ripper_s_allocate(VALUE klass) struct parser_params *p; VALUE self; - p = ALLOC_N(struct parser_params, 1); - MEMZERO(p, struct parser_params, 1); + p = ZALLOC(struct parser_params); self = TypedData_Wrap_Struct(klass, &parser_data_type, p); p->value = self; return self; diff --git a/re.c b/re.c index 299d3198ff..869fe2e673 100644 --- a/re.c +++ b/re.c @@ -872,8 +872,7 @@ match_alloc(VALUE klass) match->str = 0; match->rmatch = 0; match->regexp = 0; - match->rmatch = ALLOC(struct rmatch); - MEMZERO(match->rmatch, struct rmatch, 1); + match->rmatch = ZALLOC(struct rmatch); return (VALUE)match; } diff --git a/variable.c b/variable.c index 975fcf3ab2..2ed69d770d 100644 --- a/variable.c +++ b/variable.c @@ -2213,8 +2213,7 @@ rb_const_set(VALUE klass, ID id, VALUE val) rb_clear_constant_cache(); - ce = ALLOC(rb_const_entry_t); - MEMZERO(ce, rb_const_entry_t, 1); + ce = ZALLOC(rb_const_entry_t); ce->flag = visibility; ce->line = rb_sourceline(); st_insert(RCLASS_CONST_TBL(klass), (st_data_t)id, (st_data_t)ce); -- cgit v1.2.3