Combine rb_imemo_tmpbuf_auto_free_pointer and rb_imemo_tmpbuf_new

This commit is contained in:
Peter Zhu 2025-09-13 14:10:57 -04:00
parent 70210acab0
commit b0ce1fd549
5 changed files with 11 additions and 22 deletions

2
dir.c
View File

@ -1480,7 +1480,7 @@ rb_dir_getwd_ospath(void)
VALUE cwd; VALUE cwd;
VALUE path_guard; VALUE path_guard;
path_guard = rb_imemo_tmpbuf_auto_free_pointer(); path_guard = rb_imemo_tmpbuf_new();
path = ruby_getcwd(); path = ruby_getcwd();
rb_imemo_tmpbuf_set_ptr(path_guard, path); rb_imemo_tmpbuf_set_ptr(path_guard, path);
#ifdef __APPLE__ #ifdef __APPLE__

15
imemo.c
View File

@ -48,23 +48,14 @@ rb_imemo_new(enum imemo_type type, VALUE v0, size_t size)
return (VALUE)obj; return (VALUE)obj;
} }
static rb_imemo_tmpbuf_t *
rb_imemo_tmpbuf_new(void)
{
return IMEMO_NEW(rb_imemo_tmpbuf_t, imemo_tmpbuf, 0);
}
void * void *
rb_alloc_tmp_buffer_with_count(volatile VALUE *store, size_t size, size_t cnt) rb_alloc_tmp_buffer_with_count(volatile VALUE *store, size_t size, size_t cnt)
{ {
void *ptr;
rb_imemo_tmpbuf_t *tmpbuf;
/* Keep the order; allocate an empty imemo first then xmalloc, to /* Keep the order; allocate an empty imemo first then xmalloc, to
* get rid of potential memory leak */ * get rid of potential memory leak */
tmpbuf = rb_imemo_tmpbuf_new(); rb_imemo_tmpbuf_t *tmpbuf = (rb_imemo_tmpbuf_t *)rb_imemo_tmpbuf_new();
*store = (VALUE)tmpbuf; *store = (VALUE)tmpbuf;
ptr = ruby_xmalloc(size); void *ptr = ruby_xmalloc(size);
tmpbuf->ptr = ptr; tmpbuf->ptr = ptr;
tmpbuf->cnt = cnt; tmpbuf->cnt = cnt;
@ -97,7 +88,7 @@ rb_free_tmp_buffer(volatile VALUE *store)
rb_imemo_tmpbuf_t * rb_imemo_tmpbuf_t *
rb_imemo_tmpbuf_parser_heap(void *buf, rb_imemo_tmpbuf_t *old_heap, size_t cnt) rb_imemo_tmpbuf_parser_heap(void *buf, rb_imemo_tmpbuf_t *old_heap, size_t cnt)
{ {
rb_imemo_tmpbuf_t *tmpbuf = rb_imemo_tmpbuf_new(); rb_imemo_tmpbuf_t *tmpbuf = (rb_imemo_tmpbuf_t *)rb_imemo_tmpbuf_new();
tmpbuf->ptr = buf; tmpbuf->ptr = buf;
tmpbuf->next = old_heap; tmpbuf->next = old_heap;
tmpbuf->cnt = cnt; tmpbuf->cnt = cnt;

View File

@ -138,10 +138,8 @@ static inline enum imemo_type imemo_type(VALUE imemo);
static inline int imemo_type_p(VALUE imemo, enum imemo_type imemo_type); static inline int imemo_type_p(VALUE imemo, enum imemo_type imemo_type);
static inline bool imemo_throw_data_p(VALUE imemo); static inline bool imemo_throw_data_p(VALUE imemo);
static inline struct vm_ifunc *rb_vm_ifunc_proc_new(rb_block_call_func_t func, const void *data); static inline struct vm_ifunc *rb_vm_ifunc_proc_new(rb_block_call_func_t func, const void *data);
static inline VALUE rb_imemo_tmpbuf_auto_free_pointer(void);
static inline void *RB_IMEMO_TMPBUF_PTR(VALUE v); static inline void *RB_IMEMO_TMPBUF_PTR(VALUE v);
static inline void *rb_imemo_tmpbuf_set_ptr(VALUE v, void *ptr); static inline void *rb_imemo_tmpbuf_set_ptr(VALUE v, void *ptr);
static inline VALUE rb_imemo_tmpbuf_auto_free_pointer_new_from_an_RString(VALUE str);
static inline void MEMO_V1_SET(struct MEMO *m, VALUE v); static inline void MEMO_V1_SET(struct MEMO *m, VALUE v);
static inline void MEMO_V2_SET(struct MEMO *m, VALUE v); static inline void MEMO_V2_SET(struct MEMO *m, VALUE v);
@ -201,7 +199,7 @@ rb_vm_ifunc_proc_new(rb_block_call_func_t func, const void *data)
} }
static inline VALUE static inline VALUE
rb_imemo_tmpbuf_auto_free_pointer(void) rb_imemo_tmpbuf_new(void)
{ {
return rb_imemo_new(imemo_tmpbuf, 0, sizeof(rb_imemo_tmpbuf_t)); return rb_imemo_new(imemo_tmpbuf, 0, sizeof(rb_imemo_tmpbuf_t));
} }
@ -220,7 +218,7 @@ rb_imemo_tmpbuf_set_ptr(VALUE v, void *ptr)
} }
static inline VALUE static inline VALUE
rb_imemo_tmpbuf_auto_free_pointer_new_from_an_RString(VALUE str) rb_imemo_tmpbuf_new_from_an_RString(VALUE str)
{ {
const void *src; const void *src;
VALUE imemo; VALUE imemo;
@ -230,7 +228,7 @@ rb_imemo_tmpbuf_auto_free_pointer_new_from_an_RString(VALUE str)
StringValue(str); StringValue(str);
/* create tmpbuf to keep the pointer before xmalloc */ /* create tmpbuf to keep the pointer before xmalloc */
imemo = rb_imemo_tmpbuf_auto_free_pointer(); imemo = rb_imemo_tmpbuf_new();
tmpbuf = (rb_imemo_tmpbuf_t *)imemo; tmpbuf = (rb_imemo_tmpbuf_t *)imemo;
len = RSTRING_LEN(str); len = RSTRING_LEN(str);
src = RSTRING_PTR(str); src = RSTRING_PTR(str);

View File

@ -2635,7 +2635,7 @@ rb_exec_fillarg(VALUE prog, int argc, VALUE *argv, VALUE env, VALUE opthash, VAL
} }
rb_str_buf_cat(argv_str, (char *)&null, sizeof(null)); /* terminator for execve. */ rb_str_buf_cat(argv_str, (char *)&null, sizeof(null)); /* terminator for execve. */
eargp->invoke.cmd.argv_str = eargp->invoke.cmd.argv_str =
rb_imemo_tmpbuf_auto_free_pointer_new_from_an_RString(argv_str); rb_imemo_tmpbuf_new_from_an_RString(argv_str);
} }
RB_GC_GUARD(execarg_obj); RB_GC_GUARD(execarg_obj);
} }
@ -2726,7 +2726,7 @@ open_func(void *ptr)
static void static void
rb_execarg_allocate_dup2_tmpbuf(struct rb_execarg *eargp, long len) rb_execarg_allocate_dup2_tmpbuf(struct rb_execarg *eargp, long len)
{ {
VALUE tmpbuf = rb_imemo_tmpbuf_auto_free_pointer(); VALUE tmpbuf = rb_imemo_tmpbuf_new();
rb_imemo_tmpbuf_set_ptr(tmpbuf, ruby_xmalloc(run_exec_dup2_tmpbuf_size(len))); rb_imemo_tmpbuf_set_ptr(tmpbuf, ruby_xmalloc(run_exec_dup2_tmpbuf_size(len)));
eargp->dup2_tmpbuf = tmpbuf; eargp->dup2_tmpbuf = tmpbuf;
} }
@ -2830,7 +2830,7 @@ rb_execarg_parent_start1(VALUE execarg_obj)
p = NULL; p = NULL;
rb_str_buf_cat(envp_str, (char *)&p, sizeof(p)); rb_str_buf_cat(envp_str, (char *)&p, sizeof(p));
eargp->envp_str = eargp->envp_str =
rb_imemo_tmpbuf_auto_free_pointer_new_from_an_RString(envp_str); rb_imemo_tmpbuf_new_from_an_RString(envp_str);
eargp->envp_buf = envp_buf; eargp->envp_buf = envp_buf;
/* /*

2
util.c
View File

@ -529,7 +529,7 @@ ruby_strdup(const char *str)
char * char *
ruby_getcwd(void) ruby_getcwd(void)
{ {
VALUE guard = rb_imemo_tmpbuf_auto_free_pointer(); VALUE guard = rb_imemo_tmpbuf_new();
int size = 200; int size = 200;
char *buf = xmalloc(size); char *buf = xmalloc(size);