Skip to content

Make ruby workable with CALC_EXACT_MALLOC_SIZE set to 1 #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions dln_find.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,6 @@ char *dln_argv0;
# include <strings.h>
#endif

#ifndef xmalloc
void *xmalloc();
void *xcalloc();
void *xrealloc();
#endif

#define free(x) xfree(x)

#include <stdio.h>
#if defined(_WIN32)
#include "missing/file.h"
Expand Down
1 change: 1 addition & 0 deletions ext/dl/cfunc.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <ruby.h>
#include <errno.h>
#include <ruby/util.h>
#include "dl.h"

VALUE rb_cDLCFunc;
Expand Down
6 changes: 3 additions & 3 deletions ext/readline/readline.c
Original file line number Diff line number Diff line change
Expand Up @@ -672,10 +672,10 @@ readline_attempted_completion_function(const char *text, int start, int end)
matches = RARRAY_LEN(ary);
if (matches == 0)
return NULL;
result = ALLOC_N(char *, matches + 2);
result = (char**)malloc((matches + 2)*sizeof(char*));
for (i = 0; i < matches; i++) {
temp = rb_obj_as_string(RARRAY_PTR(ary)[i]);
result[i + 1] = ALLOC_N(char, RSTRING_LEN(temp) + 1);
result[i + 1] = (char*)malloc(RSTRING_LEN(temp) + 1);
strcpy(result[i + 1], RSTRING_PTR(temp));
}
result[matches + 1] = NULL;
Expand Down Expand Up @@ -707,7 +707,7 @@ readline_attempted_completion_function(const char *text, int start, int end)
if (low > si) low = si;
i++;
}
result[0] = ALLOC_N(char, low + 1);
result[0] = (char*)malloc(low + 1);
strncpy(result[0], result[1], low);
result[0][low] = '\0';
}
Expand Down
2 changes: 1 addition & 1 deletion ext/syslog/syslog.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ static VALUE mSyslog_close(VALUE self)

closelog();

free((void *)syslog_ident);
xfree((void *)syslog_ident);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syslog_ident is allocated by malloc in strdup, so this should be wrong

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strdup is overwritten by #include "ruby/util.h" on top of syslog.c

syslog_ident = NULL;
syslog_options = syslog_facility = syslog_mask = -1;
syslog_opened = 0;
Expand Down
19 changes: 18 additions & 1 deletion gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ rb_objspace_free(rb_objspace_t *objspace)
struct gc_list *list, *next;
for (list = global_List; list; list = next) {
next = list->next;
free(list);
ruby_xfree(list);
}
}
if (objspace->heap.sorted) {
Expand Down Expand Up @@ -908,6 +908,23 @@ ruby_xfree(void *x)
vm_xfree(&rb_objspace, x);
}

/* Mimic ruby_xmalloc, but need not rb_objspace.
* should return pointer suitable for ruby_xfree
*/
void *
ruby_mimmalloc(size_t size)
{
void *mem;
#if CALC_EXACT_MALLOC_SIZE
size += sizeof(size_t);
#endif
mem = malloc(size);
#if CALC_EXACT_MALLOC_SIZE
((size_t *)mem)[0] = size;
mem = (size_t *)mem + 1;
#endif
return mem;
}

/*
* call-seq:
Expand Down
1 change: 1 addition & 0 deletions internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ void Init_File(void);

/* gc.c */
void Init_heap(void);
void *ruby_mimmalloc(size_t size);

/* inits.c */
void rb_call_inits(void);
Expand Down
2 changes: 1 addition & 1 deletion process.c
Original file line number Diff line number Diff line change
Expand Up @@ -2031,7 +2031,7 @@ run_exec_dup2(VALUE ary, VALUE save, char *errmsg, size_t errmsg_buflen)
} *pairs = 0;

n = RARRAY_LEN(ary);
pairs = (struct fd_pair *)malloc(sizeof(struct fd_pair) * n);
pairs = (struct fd_pair *)xmalloc(sizeof(struct fd_pair) * n);
if (pairs == NULL) {
ERRMSG("malloc");
return -1;
Expand Down
4 changes: 0 additions & 4 deletions regint.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,6 @@
/* escape other system UChar definition */
#ifndef RUBY_DEFINES_H
#include "ruby/ruby.h"
#undef xmalloc
#undef xrealloc
#undef xcalloc
#undef xfree
#endif
#ifdef ONIG_ESCAPE_UCHAR_COLLISION
#undef ONIG_ESCAPE_UCHAR_COLLISION
Expand Down
4 changes: 2 additions & 2 deletions vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -2198,8 +2198,8 @@ void
Init_BareVM(void)
{
/* VM bootstrap: phase 1 */
rb_vm_t * vm = malloc(sizeof(*vm));
rb_thread_t * th = malloc(sizeof(*th));
rb_vm_t * vm = ruby_mimmalloc(sizeof(*vm));
rb_thread_t * th = ruby_mimmalloc(sizeof(*th));
if (!vm || !th) {
fprintf(stderr, "[FATAL] failed to allocate memory\n");
exit(EXIT_FAILURE);
Expand Down