diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2019-06-23 10:51:34 +0900 |
|---|---|---|
| committer | GitHub <[email protected]> | 2019-06-23 10:51:34 +0900 |
| commit | af9637e5458aca4484f684cdfb37509201960709 (patch) | |
| tree | dfa09256645d26b579d24da25ae9a9cfef7e61a9 /mrbgems/mruby-struct/src/struct.c | |
| parent | 36f1c26d9da7fd916264c7150b2e088955084c42 (diff) | |
| parent | a76da32567f9c6f8aea5c697f3ec68f31b99fb16 (diff) | |
| download | mruby-af9637e5458aca4484f684cdfb37509201960709.tar.gz mruby-af9637e5458aca4484f684cdfb37509201960709.zip | |
Merge pull request #4523 from dearblue/use-stack
Use stack memory for small name of Struct members
Diffstat (limited to 'mrbgems/mruby-struct/src/struct.c')
| -rw-r--r-- | mrbgems/mruby-struct/src/struct.c | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/mrbgems/mruby-struct/src/struct.c b/mrbgems/mruby-struct/src/struct.c index 44822a03e..e1f64df55 100644 --- a/mrbgems/mruby-struct/src/struct.c +++ b/mrbgems/mruby-struct/src/struct.c @@ -123,18 +123,29 @@ mrb_struct_ref(mrb_state *mrb, mrb_value obj) static mrb_sym mrb_id_attrset(mrb_state *mrb, mrb_sym id) { +#define STACKED_ALLOC_MAX 32 +#define STACKED_STRING_MAX (STACKED_ALLOC_MAX - 1) /* '=' character */ + const char *name; char *buf; mrb_int len; mrb_sym mid; + char stacked[STACKED_ALLOC_MAX]; name = mrb_sym2name_len(mrb, id, &len); - buf = (char *)mrb_malloc(mrb, (size_t)len+1); + if (len > STACKED_STRING_MAX) { + buf = (char *)mrb_malloc(mrb, (size_t)len+1); + } + else { + buf = stacked; + } memcpy(buf, name, (size_t)len); buf[len] = '='; mid = mrb_intern(mrb, buf, len+1); - mrb_free(mrb, buf); + if (buf != stacked) { + mrb_free(mrb, buf); + } return mid; } |
