mrb_value mrb_mraa_i2c_read(mrb_state *mrb, mrb_value self){ mraa_i2c_context i2c; mrb_int length; uint8_t *rbuf; mrb_int num_of_read; int ai, i; mrb_value mrv_rbuf; Data_Get_Struct(mrb, self, &mrb_mraa_i2c_ctx_type, i2c); mrb_get_args(mrb, "i", &length); rbuf = (uint8_t*)mrb_malloc(mrb, sizeof(uint8_t) * length); memset(rbuf, 0, sizeof(uint8_t) * length); num_of_read = mraa_i2c_read(i2c, rbuf, length); mrv_rbuf = mrb_ary_new_capa(mrb, num_of_read); ai = mrb_gc_arena_save(mrb); for (i = 0; i < num_of_read; i++){ mrb_ary_push(mrb, mrv_rbuf, mrb_fixnum_value(rbuf[i])); mrb_gc_arena_restore(mrb, ai); } mrb_free(mrb, rbuf); return mrb_assoc_new(mrb, mrv_rbuf, mrb_fixnum_value(num_of_read)); }
static void ary_modify(mrb_state *mrb, struct RArray *a) { if (a->flags & MRB_ARY_SHARED) { mrb_shared_array *shared = a->aux.shared; if (shared->refcnt == 1 && a->ptr == shared->ptr) { a->ptr = shared->ptr; a->aux.capa = a->len; mrb_free(mrb, shared); } else { mrb_value *ptr, *p; mrb_int len; p = a->ptr; len = a->len * sizeof(mrb_value); ptr = (mrb_value *)mrb_malloc(mrb, len); if (p) { array_copy(ptr, p, a->len); } a->ptr = ptr; a->aux.capa = a->len; mrb_ary_decref(mrb, shared); } a->flags &= ~MRB_ARY_SHARED; } }
/* Initializes a time by setting the amount of milliseconds since the epoch.*/ static mrb_value mrb_time_initialize(mrb_state *mrb, mrb_value self) { mrb_int ayear = 0, amonth = 1, aday = 1, ahour = 0, amin = 0, asec = 0, ausec = 0; int n; struct mrb_time *tm; tm = (struct mrb_time*)DATA_PTR(self); if (tm) { mrb_free(mrb, tm); } DATA_TYPE(self) = &mrb_time_type; DATA_PTR(self) = NULL; n = mrb_get_args(mrb, "|iiiiiii", &ayear, &amonth, &aday, &ahour, &amin, &asec, &ausec); if (n == 0) { tm = current_mrb_time(mrb); } else { tm = time_mktime(mrb, ayear, amonth, aday, ahour, amin, asec, ausec, MRB_TIMEZONE_LOCAL); } DATA_PTR(self) = tm; return self; }
mrb_value mrb_mraa_i2c_write(mrb_state *mrb, mrb_value self){ mraa_i2c_context i2c; mrb_value mrv_wbuf; mrb_int length; mraa_result_t result; uint8_t *wbuf; mrb_int argc; Data_Get_Struct(mrb, self, &mrb_mraa_i2c_ctx_type, i2c); argc = mrb_get_args(mrb, "o|i", &mrv_wbuf, &length); result = MRAA_ERROR_INVALID_PARAMETER; if (mrb_array_p(mrv_wbuf)){ int i; if (argc == 1){ length = RARRAY_LEN(mrv_wbuf); } wbuf = (uint8_t *)mrb_malloc(mrb, sizeof(uint8_t) * length); memset(wbuf, 0, sizeof(uint8_t) * length); for (i = 0; i < length; i++){ wbuf[i] = mrb_fixnum(mrb_ary_ref(mrb, mrv_wbuf, i)); } result = mraa_i2c_write(i2c, wbuf, length); mrb_free(mrb, wbuf); } return mrb_fixnum_value(result); }
static mrb_value mrb_str_reverse_bang(mrb_state *mrb, mrb_value str) { int utf8_len = mrb_utf8_strlen(str); if (utf8_len > 1) { int len = RSTRING_LEN(str); char *buf = (char *)mrb_malloc(mrb, len); unsigned char* p = (unsigned char*)buf; unsigned char* e = (unsigned char*)buf + len; unsigned char* r = (unsigned char*)RSTRING_END(str); memcpy(buf, RSTRING_PTR(str), len); mrb_str_modify(mrb, mrb_str_ptr(str)); while (p<e) { int clen = utf8len(p); r -= clen; memcpy(r, p, clen); p += clen; } mrb_free(mrb, buf); } return str; }
static void mrb_cp_mat2x2_free(mrb_state* mrb, void* ptr) { if (ptr) { mrb_free(mrb, ptr); } }
static mrb_value mrb_random_init(mrb_state *mrb, mrb_value self) { mrb_value seed; mt_state *t; DATA_TYPE(self) = &mt_state_type; DATA_PTR(self) = NULL; /* avoid memory leaks */ t = (mt_state*)DATA_PTR(self); if (t) { mrb_free(mrb, t); } t = (mt_state *)mrb_malloc(mrb, sizeof(mt_state)); t->mti = N + 1; seed = get_opt(mrb); seed = mrb_random_mt_srand(mrb, t, seed); if (mrb_nil_p(seed)) { t->has_seed = FALSE; } else { mrb_assert(mrb_fixnum_p(seed)); t->has_seed = TRUE; t->seed = mrb_fixnum(seed); } DATA_PTR(self) = t; return self; }
int mrb_dump_irep_cfunc(mrb_state *mrb, size_t start_index, int debug_info, FILE *fp, const char *initname) { uint8_t *bin = NULL; size_t bin_size = 0, bin_idx = 0; int result; if (fp == NULL || initname == NULL || !is_valid_c_symbol_name(initname)) { return MRB_DUMP_INVALID_ARGUMENT; } result = mrb_dump_irep(mrb, start_index, debug_info, &bin, &bin_size); if (result == MRB_DUMP_OK) { fprintf(fp, "#include <stdint.h>\n"); // for uint8_t under at least Darwin fprintf(fp, "const uint8_t %s[] = {", initname); while (bin_idx < bin_size) { if (bin_idx % 16 == 0 ) fputs("\n", fp); fprintf(fp, "0x%02x,", bin[bin_idx++]); } fputs("\n};\n", fp); } mrb_free(mrb, bin); return result; }
void mrb_free_backtrace(mrb_state *mrb) { mrb->backtrace.exc = 0; mrb->backtrace.n = 0; mrb->backtrace.n_allocated = 0; mrb_free(mrb, mrb->backtrace.entries); }
static void lib_hmac_free(mrb_state *mrb, void *ptr) { struct mrb_hmac *hmac = ptr; memset(&hmac->ctx, 0, sizeof(hmac->ctx)); mrb_free(mrb, hmac); }
/* :nodoc: */ static void rb_lmc_cache_destructor(mrb_state *mrb, void *p) { rb_lmc_handle_t *h; lmc_error_t e; h = (rb_lmc_handle_t *)p; if (!h || (h->open == 0) || !h->lmc) return; if (!local_memcache_free(h->lmc, &e)) { mrb_free(mrb, p); rb_lmc_raise_exception(mrb, &e); return; } mrb_free(mrb, p); }
static void mrb_shared_sample_buffer_unref(mrb_state *mrb, mrb_shared_sample_buffer *b) { if (!b) return; b->refcnt -= 1; if (b->refcnt <= 0) { mrb_free(mrb, b); } }
static void mrb_uv_dlfree(mrb_state *mrb, void *p) { if (p) { uv_dlclose((uv_lib_t*)p); mrb_free(mrb, p); } }
/********************************************************* * UV::Mutex *********************************************************/ void mrb_uv_mutex_free(mrb_state *mrb, void *p) { if (p) { uv_mutex_destroy((uv_mutex_t*)p); mrb_free(mrb, p); } }
static void mrb_uv_cond_free(mrb_state *mrb, void* p) { if (p) { uv_cond_destroy((uv_cond_t*)p); mrb_free(mrb, p); } }
static void mrb_uv_key_free(mrb_state *mrb, void* p) { if (p) { uv_key_delete((uv_key_t*)p); mrb_free(mrb, p); } }
void mrb_uv_sem_free(mrb_state *mrb, void *p) { if(!p) { return; } uv_sem_destroy((uv_sem_t*)p); mrb_free(mrb, p); }
static void free_breakpoint( mrb_state *mrb, mrb_debug_breakpoint *bp ) { switch(bp->type) { case MRB_DEBUG_BPTYPE_LINE: mrb_free(mrb, (void*)bp->point.linepoint.file); break; case MRB_DEBUG_BPTYPE_METHOD: mrb_free(mrb, (void*)bp->point.methodpoint.method_name); if(bp->point.methodpoint.class_name != NULL) { mrb_free(mrb, (void*)bp->point.methodpoint.class_name); } break; default: break; } }
/********************************************************* * UV::Barrier *********************************************************/ static void mrb_uv_barrier_free(mrb_state *mrb, void *p) { if(p) { uv_barrier_destroy((uv_barrier_t*)p); mrb_free(mrb, p); } }
static mrb_value mrb_uv_barrier_destroy(mrb_state *mrb, mrb_value self) { uv_barrier_destroy((uv_barrier_t*)mrb_uv_get_ptr(mrb, self, &barrier_type)); mrb_free(mrb, DATA_PTR(self)); DATA_PTR(self) = NULL; return self; }
static void destroy_hook(mrb_state *mrb, void* ptr) { DataClass *obj = reinterpret_cast<DataClass*>(ptr); obj->destroy(mrb); obj->~DataClass(); mrb_free(mrb, obj); }
static void mrb_sample_buffer_destroy(mrb_state *mrb, void *x) { mrb_sample_buffer *b = x; if (b) { mrb_shared_sample_buffer_unref(mrb, b->shared); mrb_free(mrb, b); } }
void mrb_uv_dlclose(mrb_state *mrb, mrb_value dl) { uv_lib_t *lib = (uv_lib_t*)mrb_uv_get_ptr(mrb, dl, &dl_type); uv_dlclose(lib); mrb_free(mrb, DATA_PTR(dl)); DATA_PTR(dl) = NULL; }
void mrb_gc_destroy(mrb_state *mrb, mrb_gc *gc) { free_heap(mrb, gc); #ifndef MRB_GC_FIXED_ARENA mrb_free(mrb, gc->arena); #endif }
static void mrb_uv_rwlock_free(mrb_state *mrb, void *p) { if (p) { uv_rwlock_destroy((uv_rwlock_t*)p); mrb_free(mrb, p); } }
int mrb_read_irep_file(mrb_state *mrb, FILE* fp) { int ret, i; uint32_t len, rlen = 0; unsigned char hex8[8], hcrc[4]; unsigned char *dst, *rite_dst = NULL; rite_binary_header bin_header; RiteFILE ritefp = { 0 }; RiteFILE *rfp; if ((mrb == NULL) || (fp == NULL)) { return MRB_DUMP_INVALID_ARGUMENT; } ritefp.fp = fp; rfp = &ritefp; //Read File Header Section ret = load_rite_header(fp, &bin_header, hcrc); if (ret != MRB_DUMP_OK) return ret; len = sizeof(rite_binary_header) + bin_to_uint32(bin_header.rbds); rite_dst = (unsigned char *)mrb_malloc(mrb, len); if (rite_dst == NULL) return MRB_DUMP_GENERAL_FAILURE; dst = rite_dst; memset(dst, 0x00, len); *(rite_binary_header *)dst = bin_header; dst += sizeof(rite_binary_header); dst += hex_to_bin16(dst, hcrc); //Read Binary Data Section len = bin_to_uint16(bin_header.nirep); for (i=0; i<len; i++) { rite_fgets(rfp, hex8, sizeof(hex8), TRUE); //record len dst += hex_to_bin32(dst, hex8); ret = load_rite_irep_record(mrb, rfp, dst, &rlen); if (ret != MRB_DUMP_OK) //irep info goto error_exit; dst += rlen; } rite_fgets(rfp, hex8, sizeof(hex8), TRUE); //dummy record len hex_to_bin32(dst, hex8); /* dst += hex_to_bin32(dst, hex8); */ if (0 != hex_to_uint32(hex8)) { ret = MRB_DUMP_INVALID_IREP; goto error_exit; } if (ret == MRB_DUMP_OK) ret = mrb_read_irep(mrb, (char*)rite_dst); error_exit: mrb_free(mrb, rite_dst); return ret; }
static mrb_value mrb_sdl2_mixer_chunk_initialize(mrb_state *mrb, mrb_value self) { Mix_Chunk *chunk = NULL; mrb_sdl2_mixer_chunk_data_t *data = (mrb_sdl2_mixer_chunk_data_t*)DATA_PTR(self); if (NULL == data) { data = (mrb_sdl2_mixer_chunk_data_t*)mrb_malloc(mrb, sizeof(mrb_sdl2_mixer_chunk_data_t)); if (NULL == data) { mrb_raise(mrb, E_RUNTIME_ERROR, "insufficient memory."); } data->chunk = NULL; } SDL_RWops *rwops; bool str = false; if (1 == mrb->c->ci->argc) { mrb_value v; mrb_get_args(mrb, "o", &v); if (mrb_type(v) == MRB_TT_STRING) { rwops = SDL_RWFromFile(RSTRING_PTR(v), "r"); str = true; } else if (mrb_type(v) == MRB_TT_OBJECT) rwops = mrb_sdl2_rwops_get_ptr(mrb, v); chunk = Mix_LoadWAV_RW(rwops, 0); } else { mrb_free(mrb, data); mrb_raise(mrb, E_ARGUMENT_ERROR, "wrong number of arguments."); } if (NULL == chunk) { mrb_free(mrb, data); if (str) SDL_FreeRW(rwops); mruby_sdl2_raise_error(mrb); } if (str) SDL_FreeRW(rwops); data->chunk = chunk; DATA_PTR(self) = data; DATA_TYPE(self) = &mrb_sdl2_mixer_chunk_data_type; return self; }
static void mrb_io_free(mrb_state *mrb, void *ptr) { struct mrb_io *io = (struct mrb_io *)ptr; if (io != NULL) { fptr_finalize(mrb, io, TRUE); mrb_free(mrb, io); } }
static void mrb_buffer_free(mrb_state *mrb, void *ptr) { struct buffer *bp; bp = (struct buffer *)ptr; if (bfind(bp->b_bname, FALSE) == NULL) { mrb_free(mrb, ptr); } }
void mrb_debug_context_free(mrb_state *mrb) { if (_debug_context) { mrb_debug_delete_break_all(mrb, _debug_context); mrb_free(mrb, _debug_context); _debug_context = NULL; } }