Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Change malloc to mrb_malloc

  • Loading branch information...
commit 55d94e95a3f582b773a5e9691261f362b55560e3 1 parent f0b0f13
@masuidrive masuidrive authored
View
24 src/cfunc_call.c
@@ -79,8 +79,8 @@ cfunc_call(mrb_state *mrb, mrb_value self)
}
}
- args = malloc(sizeof(ffi_type*) * margc);
- values = malloc(sizeof(void*) * margc);
+ args = mrb_malloc(mrb, sizeof(ffi_type*) * margc);
+ values = mrb_malloc(mrb, sizeof(void*) * margc);
mrb_sym sym_to_ffi_value = mrb_intern(mrb, "to_ffi_value");
mrb_value nil_ary[1];
@@ -108,10 +108,10 @@ cfunc_call(mrb_state *mrb, mrb_value self)
if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, margc, result_type, args) == FFI_OK) {
void *result;
if(result_type->size > sizeof(long)) {
- result = malloc(result_type->size);
+ result = mrb_malloc(mrb, result_type->size);
}
else if(result_type->size) {
- result = malloc(sizeof(long));
+ result = mrb_malloc(mrb, sizeof(long));
}
else {
result = NULL;
@@ -129,8 +129,8 @@ cfunc_call(mrb_state *mrb, mrb_value self)
}
cfunc_call_exit:
- free(values);
- free(args);
+ mrb_free(mrb, values);
+ mrb_free(mrb, args);
return mresult;
}
@@ -164,8 +164,8 @@ cfunc_libcall(mrb_state *mrb, mrb_value self)
}
}
- args = malloc(sizeof(ffi_type*) * margc);
- values = malloc(sizeof(void*) * margc);
+ args = mrb_malloc(mrb, sizeof(ffi_type*) * margc);
+ values = mrb_malloc(mrb, sizeof(void*) * margc);
mrb_sym sym_to_ffi_value = mrb_intern(mrb, "to_ffi_value");
mrb_value nil_ary[1];
@@ -193,10 +193,10 @@ cfunc_libcall(mrb_state *mrb, mrb_value self)
if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, margc, result_type, args) == FFI_OK) {
void *result;
if(result_type->size > sizeof(long)) {
- result = malloc(result_type->size);
+ result = mrb_malloc(mrb, result_type->size);
}
else if(result_type->size) {
- result = malloc(sizeof(long));
+ result = mrb_malloc(mrb, sizeof(long));
}
else {
result = NULL;
@@ -214,8 +214,8 @@ cfunc_libcall(mrb_state *mrb, mrb_value self)
}
cfunc_call_exit:
- free(values);
- free(args);
+ mrb_free(mrb, values);
+ mrb_free(mrb, args);
return mresult;
}
View
22 src/cfunc_closure.c
@@ -29,10 +29,10 @@ cfunc_closure_destructor(mrb_state *mrb, void *p_)
if (p->closure) {
ffi_closure_free(p->closure);
}
- free(p->arg_types);
- free(p->arg_ffi_types);
- free(p->cif);
- free(p);
+ mrb_free(mrb, p->arg_types);
+ mrb_free(mrb, p->arg_ffi_types);
+ mrb_free(mrb, p->cif);
+ mrb_free(mrb, p);
}
static void
@@ -57,7 +57,7 @@ cfunc_closure_initialize(mrb_state *mrb, mrb_value self)
struct cfunc_closure_data *data;
data = mrb_get_datatype(mrb, self, &cfunc_closure_data_type);
if (!data) {
- data = malloc(sizeof(struct cfunc_closure_data));
+ data = mrb_malloc(mrb, sizeof(struct cfunc_closure_data));
}
data->refer = 0;
data->autofree = 0;
@@ -75,8 +75,8 @@ cfunc_closure_initialize(mrb_state *mrb, mrb_value self)
ffi_type *return_ffi_type = rclass_to_mrb_ffi_type(mrb, mrb_class_ptr(rettype_mrb))->ffi_type_value;
data->return_type = rettype_mrb;
- data->arg_ffi_types = malloc(sizeof(ffi_type*) * data->argc);
- data->arg_types = malloc(sizeof(mrb_value) * data->argc);
+ data->arg_ffi_types = mrb_malloc(mrb, sizeof(ffi_type*) * data->argc);
+ data->arg_types = mrb_malloc(mrb, sizeof(mrb_value) * data->argc);
int i;
for (i = 0; i < data->argc; ++i) {
data->arg_types[i] = mrb_ary_ref(mrb, args_mrb, i);
@@ -87,7 +87,7 @@ cfunc_closure_initialize(mrb_state *mrb, mrb_value self)
void *closure_pointer = NULL;
data->closure = ffi_closure_alloc(sizeof(ffi_closure) + sizeof(void*), &closure_pointer);
- data->cif = malloc(sizeof(ffi_cif));
+ data->cif = mrb_malloc(mrb, sizeof(ffi_cif));
if (data->closure) {
if (ffi_prep_cif(data->cif, FFI_DEFAULT_ABI, data->argc, return_ffi_type, data->arg_ffi_types) == FFI_OK) {
@@ -111,11 +111,11 @@ cfunc_closure_call_binding(ffi_cif *cif, void *ret, void **args, void *self_)
int ai = mrb_gc_arena_save(data->mrb);
- mrb_value *ary = malloc(sizeof(mrb_value) * data->argc);
+ mrb_value *ary = mrb_malloc(data->mrb, sizeof(mrb_value) * data->argc);
int i;
for (i = 0; i < data->argc; ++i) {
// TODO: I felt too much consume memory
- void *p = malloc(data->arg_ffi_types[i]->size);
+ void *p = mrb_malloc(data->mrb, data->arg_ffi_types[i]->size);
memcpy(p, args[i], data->arg_ffi_types[i]->size);
mrb_value pointer = cfunc_pointer_new_with_pointer(data->mrb, p, true);
ary[i] = mrb_funcall(data->mrb, data->arg_types[i], "refer", 1, pointer);
@@ -123,7 +123,7 @@ cfunc_closure_call_binding(ffi_cif *cif, void *ret, void **args, void *self_)
mrb_value block = mrb_iv_get(data->mrb, self, mrb_intern(data->mrb, "@block"));
mrb_value result = mrb_funcall_argv(data->mrb, block, mrb_intern(data->mrb, "call"), data->argc, ary);
- free(ary);
+ mrb_free(data->mrb, ary);
mrb_value ret_pointer = cfunc_pointer_new_with_pointer(data->mrb, ret, false);
mrb_funcall(data->mrb, data->return_type, "set", 2, ret_pointer, result);
View
18 src/cfunc_pointer.c
@@ -26,9 +26,9 @@ cfunc_pointer_destructor(mrb_state *mrb, void *p)
{
struct cfunc_type_data *data = (struct cfunc_type_data *)p;
if(data->autofree) {
- free(get_cfunc_pointer_data(data));
+ mrb_free(mrb, get_cfunc_pointer_data(data));
}
- free(p);
+ mrb_free(mrb, p);
}
@@ -62,14 +62,14 @@ void set_cfunc_pointer_data(struct cfunc_type_data *data, void *p)
mrb_value
cfunc_pointer_class_malloc(mrb_state *mrb, mrb_value klass)
{
- struct cfunc_type_data *data = malloc(sizeof(struct cfunc_type_data));
+ struct cfunc_type_data *data = mrb_malloc(mrb, sizeof(struct cfunc_type_data));
data->refer = false;
data->autofree = false;
mrb_int alloc_size;
mrb_get_args(mrb, "i", &alloc_size);
- set_cfunc_pointer_data(data, malloc(alloc_size));
+ set_cfunc_pointer_data(data, mrb_malloc(mrb, alloc_size));
return mrb_obj_value(Data_Wrap_Struct(mrb, mrb_class_ptr(klass), &cfunc_pointer_data_type, data));
}
@@ -78,7 +78,7 @@ cfunc_pointer_class_malloc(mrb_state *mrb, mrb_value klass)
mrb_value
cfunc_pointer_new_with_pointer(mrb_state *mrb, void *p, bool autofree)
{
- struct cfunc_type_data *data = malloc(sizeof(struct cfunc_type_data));
+ struct cfunc_type_data *data = mrb_malloc(mrb, sizeof(struct cfunc_type_data));
data->refer = false;
data->autofree = autofree;
@@ -93,7 +93,7 @@ mrb_value
cfunc_pointer_refer(mrb_state *mrb, mrb_value klass)
{
struct RClass *c = mrb_class_ptr(klass);
- struct cfunc_type_data *data = malloc(sizeof(struct cfunc_type_data));
+ struct cfunc_type_data *data = mrb_malloc(mrb, sizeof(struct cfunc_type_data));
data->refer = true;
data->autofree = false;
@@ -114,7 +114,7 @@ cfunc_pointer_initialize(mrb_state *mrb, mrb_value self)
struct cfunc_type_data *data;
data = mrb_get_datatype(mrb, self, &cfunc_pointer_data_type);
if (!data) {
- data = malloc(sizeof(struct cfunc_type_data));
+ data = mrb_malloc(mrb, sizeof(struct cfunc_type_data));
DATA_PTR(self) = data;
DATA_TYPE(self) = &cfunc_pointer_data_type;
}
@@ -141,7 +141,7 @@ cfunc_pointer_realloc(mrb_state *mrb, mrb_value self)
mrb_int alloc_size;
mrb_get_args(mrb, "i", &alloc_size);
- set_cfunc_pointer_data(data, realloc(get_cfunc_pointer_data(data), alloc_size));
+ set_cfunc_pointer_data(data, mrb_realloc(mrb, get_cfunc_pointer_data(data), alloc_size));
return self;
}
@@ -152,7 +152,7 @@ cfunc_pointer_free(mrb_state *mrb, mrb_value self)
{
struct cfunc_type_data *data = DATA_PTR(self);
- free(get_cfunc_pointer_data(data));
+ mrb_free(mrb, get_cfunc_pointer_data(data));
data->autofree = false;
set_cfunc_pointer_data(data, NULL);
View
44 src/cfunc_rubyvm.c
@@ -63,7 +63,7 @@ struct queue_task {
struct task_arg* mrb_value_to_task_arg(mrb_state *mrb, mrb_value v)
{
- struct task_arg *arg = malloc(sizeof(struct task_arg));
+ struct task_arg *arg = mrb_malloc(mrb, sizeof(struct task_arg));
arg->tt = mrb_type(v);
switch (mrb_type(v)) {
@@ -80,7 +80,7 @@ struct task_arg* mrb_value_to_task_arg(mrb_state *mrb, mrb_value v)
case MRB_TT_SYMBOL:
{
const char* name = mrb_sym2name_len(mrb, v.value.sym, &arg->value.string.len);
- arg->value.string.ptr = malloc(arg->value.string.len+1);
+ arg->value.string.ptr = mrb_malloc(mrb, arg->value.string.len+1);
memcpy(arg->value.string.ptr, name, arg->value.string.len+1);
}
break;
@@ -89,7 +89,7 @@ struct task_arg* mrb_value_to_task_arg(mrb_state *mrb, mrb_value v)
{
struct RString *str = mrb_str_ptr(v);
arg->value.string.len = str->len;
- arg->value.string.ptr = malloc(arg->value.string.len+1);
+ arg->value.string.ptr = mrb_malloc(mrb, arg->value.string.len+1);
memcpy(arg->value.string.ptr, str->ptr, arg->value.string.len+1);
}
break;
@@ -99,7 +99,7 @@ struct task_arg* mrb_value_to_task_arg(mrb_state *mrb, mrb_value v)
struct RArray *ary = mrb_ary_ptr(v);
arg->value.array.len = ary->len;
- arg->value.array.ptr = malloc(ary->len * sizeof(struct task_arg));
+ arg->value.array.ptr = mrb_malloc(mrb, ary->len * sizeof(struct task_arg));
int i;
for(i=0; i<ary->len; i++) {
@@ -162,7 +162,7 @@ mrb_value task_arg_to_mrb_value(mrb_state *mrb, struct task_arg* arg)
}
void
-free_task_arg(struct task_arg* arg)
+free_task_arg(mrb_state *mrb, struct task_arg* arg)
{
if(!arg) {
return;
@@ -170,16 +170,16 @@ free_task_arg(struct task_arg* arg)
switch (arg->tt) {
case MRB_TT_SYMBOL:
case MRB_TT_STRING:
- free(arg->value.string.ptr);
+ mrb_free(mrb, arg->value.string.ptr);
break;
case MRB_TT_ARRAY:
{
int i;
for(i=0; i<arg->value.array.len; i++) {
- free_task_arg(arg->value.array.ptr[i]);
+ free_task_arg(mrb, arg->value.array.ptr[i]);
}
- free(arg->value.array.ptr);
+ mrb_free(mrb, arg->value.array.ptr);
}
break;
@@ -190,18 +190,18 @@ free_task_arg(struct task_arg* arg)
void
-free_queue_task(struct queue_task* task)
+free_queue_task(mrb_state *mrb, struct queue_task* task)
{
task->refcount--;
if(task->refcount < 1) {
int i;
for(i=0; i<task->args_len; ++i) {
- free_task_arg(task->args[i]);
+ free_task_arg(mrb, task->args[i]);
}
- free(task->args);
- free(task->result);
- free(task->name);
- free(task);
+ mrb_free(mrb, task->args);
+ mrb_free(mrb, task->result);
+ mrb_free(mrb, task->name);
+ mrb_free(mrb, task);
}
}
@@ -224,7 +224,7 @@ const struct mrb_data_type cfunc_rubyvm_data_type = {
static void
cfunc_rubyvm_task_data_destructor(mrb_state *mrb, void *p)
{
- free_queue_task((struct queue_task*)p);
+ free_queue_task(mrb, (struct queue_task*)p);
};
@@ -264,7 +264,7 @@ cfunc_rubyvm_open(void *args)
mrb_sym taskname = mrb_intern(mrb, task->name);
int args_len = task->args_len;
- mrb_value *args = malloc(sizeof(struct task_arg) * task->args_len);
+ mrb_value *args = mrb_malloc(mrb, sizeof(struct task_arg) * task->args_len);
int i;
for(i=0; i<task->args_len; ++i) {
args[i] = task_arg_to_mrb_value(data->state, task->args[i]);
@@ -277,8 +277,8 @@ cfunc_rubyvm_open(void *args)
task->status = queue_task_finished;
pthread_cond_signal(&task->sync_cond);
- free(args);
- free_queue_task(task);
+ mrb_free(mrb, args);
+ free_queue_task(mrb, task);
}
return NULL;
@@ -294,7 +294,7 @@ cfunc_rubyvm_dispatch(mrb_state *mrb, mrb_value self)
int args_len;
mrb_get_args(mrb, "o*", &name_obj, &args, &args_len);
- struct queue_task *task = malloc(sizeof(struct queue_task));
+ struct queue_task *task = mrb_malloc(mrb, sizeof(struct queue_task));
task->refcount = 2;
task->result = NULL;
task->status = queue_task_queued;
@@ -304,11 +304,11 @@ cfunc_rubyvm_dispatch(mrb_state *mrb, mrb_value self)
const char* name = mrb_string_value_ptr(mrb, name_obj);
int name_len = strlen(name);
- task->name = malloc(name_len+1);
+ task->name = mrb_malloc(mrb, name_len+1);
strncpy(task->name, name, name_len+1);
task->args_len = args_len;
- task->args = malloc(sizeof(struct task_arg) * task->args_len);
+ task->args = mrb_malloc(mrb, sizeof(struct task_arg) * task->args_len);
int i;
for(i=0; i<args_len; ++i) {
task->args[i] = mrb_value_to_task_arg(mrb, args[i]);
@@ -359,7 +359,7 @@ cfunc_rubyvm_class_thread(mrb_state *mrb, mrb_value klass)
{
// init bindle data with RubyVM object
struct RClass *c = mrb_class_ptr(klass);
- struct cfunc_rubyvm_data *data = malloc(sizeof(struct cfunc_rubyvm_data));
+ struct cfunc_rubyvm_data *data = mrb_malloc(mrb, sizeof(struct cfunc_rubyvm_data));
mrb_value self = mrb_obj_value((struct RObject *)Data_Wrap_Struct(mrb, c, &cfunc_rubyvm_data_type, data));
// load script
View
6 src/cfunc_struct.c
@@ -53,11 +53,11 @@ cfunc_struct_define_struct(mrb_state *mrb, mrb_value klass)
mrb_get_args(mrb, "A", &elements_mrb);
struct RArray *elements = mrb_ary_ptr(elements_mrb);
- ffi_type *tm_type = malloc(sizeof(ffi_type));
+ ffi_type *tm_type = mrb_malloc(mrb, sizeof(ffi_type));
tm_type->type = FFI_TYPE_STRUCT;
tm_type->size = tm_type->alignment = 0;
- ffi_type **tm_type_elements = malloc(sizeof(ffi_type*) * (elements->len + 1));
+ ffi_type **tm_type_elements = mrb_malloc(mrb, sizeof(ffi_type*) * (elements->len + 1));
int i;
for(i = 0; i < elements->len; ++i) {
tm_type_elements[i] = rclass_to_mrb_ffi_type(mrb, mrb_class_ptr(elements->ptr[i]))->ffi_type_value;
@@ -65,7 +65,7 @@ cfunc_struct_define_struct(mrb_state *mrb, mrb_value klass)
tm_type_elements[i] = NULL;
tm_type->elements = tm_type_elements;
- struct mrb_ffi_type *mft = malloc(sizeof(struct mrb_ffi_type));
+ struct mrb_ffi_type *mft = mrb_malloc(mrb, sizeof(struct mrb_ffi_type));
mft->name = mrb_class_name(mrb, mrb_class_ptr(klass));
mft->ffi_type_value = tm_type;
mft->mrb_to_c = &cfunc_type_ffi_struct_mrb_to_c;
View
6 src/cfunc_type.c
@@ -28,7 +28,7 @@ cfunc_type_destructor(mrb_state *mrb, void *p)
{
struct cfunc_type_data *data = (struct cfunc_type_data*)p;
if(data->autofree) {
- free(data->value._pointer);
+ mrb_free(mrb, data->value._pointer);
}
mrb_free(mrb, p);
}
@@ -76,7 +76,7 @@ static mrb_value
cfunc_type_class_refer(mrb_state *mrb, mrb_value klass)
{
struct RClass *c = mrb_class_ptr(klass);
- struct cfunc_type_data *data = malloc(sizeof(struct cfunc_type_data));
+ struct cfunc_type_data *data = mrb_malloc(mrb, sizeof(struct cfunc_type_data));
data->autofree = false;
mrb_value pointer;
@@ -97,7 +97,7 @@ cfunc_type_initialize(mrb_state *mrb, mrb_value self)
struct cfunc_type_data *data;
data = mrb_get_datatype(mrb, self, &cfunc_type_data);
if (!data) {
- data = malloc(sizeof(struct cfunc_type_data));
+ data = mrb_malloc(mrb, sizeof(struct cfunc_type_data));
data->value._uint64 = 0;
}
data->autofree = false;
Please sign in to comment.
Something went wrong with that request. Please try again.