static uint32_t get_pool_block_size(mrb_state *mrb, mrb_irep *irep, int type) { uint32_t size = 0; int pool_no; mrb_value str; char buf[32]; size += MRB_DUMP_SIZE_OF_LONG; /* plen */ size += irep->plen; /* tt(n) */ size += irep->plen * MRB_DUMP_SIZE_OF_SHORT; /* len(n) */ size += MRB_DUMP_SIZE_OF_SHORT; /* crc */ size = DUMP_SIZE(size, type); for (pool_no = 0; pool_no < irep->plen; pool_no++) { uint16_t nlen =0; int len; switch (mrb_type(irep->pool[pool_no])) { case MRB_TT_FIXNUM: len = mrb_int_to_str( buf, mrb_fixnum(irep->pool[pool_no])); size += (uint32_t)len; break; case MRB_TT_FLOAT: len = mrb_float_to_str( buf, mrb_float(irep->pool[pool_no])); size += (uint32_t)len; break; case MRB_TT_STRING: str = mrb_string_value( mrb, &irep->pool[pool_no]); nlen = str_dump_len(RSTRING_PTR(str), RSTRING_LEN(str), type); size += nlen; break; #ifdef ENABLE_REGEXP case MRB_TT_REGEX: str = mrb_reg_to_s(mrb, irep->pool[pool_no]); nlen = str_dump_len(RSTRING_PTR(str), RSTRING_LEN(str), type); size += nlen; break; #endif default: break; } } return size; }
static uint32_t get_pool_block_size(mrb_state *mrb, mrb_irep *irep, int type) { uint32_t size = 0; int pool_no; mrb_value str; char buf[32]; size += MRB_DUMP_SIZE_OF_LONG; /* plen */ size += irep->plen * sizeof(char); /* tt(n) */ size += irep->plen * MRB_DUMP_SIZE_OF_SHORT; /* len(n) */ size += MRB_DUMP_SIZE_OF_SHORT; /* crc */ size = DUMP_SIZE(size, type); for (pool_no = 0; pool_no < irep->plen; pool_no++) { uint16_t nlen =0; switch (irep->pool[pool_no].tt) { case MRB_TT_FIXNUM: sprintf( buf, "%d", irep->pool[pool_no].value.i); size += strlen(buf); break; case MRB_TT_FLOAT: sprintf( buf, "%.16e", irep->pool[pool_no].value.f); size += strlen(buf); break; case MRB_TT_STRING: str = mrb_string_value( mrb, &irep->pool[pool_no]); nlen = str_dump_len(RSTRING_PTR(str), RSTRING_LEN(str), type); size += nlen; break; #ifdef INCLUDE_REGEXP case MRB_TT_REGEX: str = mrb_reg_to_s(mrb, irep->pool[pool_no]); nlen = str_dump_len(RSTRING_PTR(str), RSTRING_LEN(str), type); size += nlen; break; #endif default: break; } } return size; }
static int write_pool_block(mrb_state *mrb, mrb_irep *irep, char *buf, int type) { int pool_no; mrb_value str; char *buf_top = buf; char *char_buf; uint16_t buf_size =0; uint16_t len =0; int result; buf_size = MRB_DUMP_DEFAULT_STR_LEN; char_buf = (char *)mrb_malloc(mrb, buf_size); if (char_buf == NULL) { result = MRB_DUMP_GENERAL_FAILURE; goto error_exit; } buf += uint32_dump((uint32_t)irep->plen, buf, type); /* number of pool */ for (pool_no = 0; pool_no < irep->plen; pool_no++) { buf += uint8_dump(mrb_type(irep->pool[pool_no]), buf, type); /* data type */ memset(char_buf, 0, buf_size); switch (mrb_type(irep->pool[pool_no])) { case MRB_TT_FIXNUM: len = mrb_int_to_str(char_buf, mrb_fixnum(irep->pool[pool_no])); break; case MRB_TT_FLOAT: len = mrb_float_to_str(char_buf, mrb_float(irep->pool[pool_no])); break; case MRB_TT_STRING: str = irep->pool[pool_no]; len = str_dump_len(RSTRING_PTR(str), RSTRING_LEN(str), type); if (len > buf_size - 1) { buf_size = len + 1; char_buf = (char *)mrb_realloc(mrb, char_buf, buf_size); if (char_buf == NULL) { result = MRB_DUMP_GENERAL_FAILURE; goto error_exit; } memset(char_buf, 0, buf_size); } str_dump(RSTRING_PTR(str), char_buf, RSTRING_LEN(str), type); break; #ifdef ENABLE_REGEXP case MRB_TT_REGEX: str = mrb_reg_to_s(mrb, irep->pool[pool_no]); len = str_dump_len(RSTRING_PTR(str), RSTRING_LEN(str), type); if ( len > buf_size - 1) { buf_size = len + 1; char_buf = mrb_realloc(mrb, char_buf, buf_size); if (char_buf == NULL) { result = MRB_DUMP_GENERAL_FAILURE; goto error_exit; } memset(char_buf, 0, buf_size); } str_dump(RSTRING_PTR(str), char_buf, RSTRING_LEN(str), type); break; #endif default: buf += uint16_dump(0, buf, type); /* data length = 0 */ continue; } buf += uint16_dump(len, buf, type); /* data length */ memcpy(buf, char_buf, len); buf += len; } result = (int)(buf - buf_top); error_exit: mrb_free(mrb, char_buf); return result; }
static int write_pool_block(mrb_state *mrb, mrb_irep *irep, char *buf, int type) { int pool_no; mrb_value str; char *buf_top = buf; char *char_buf; uint16_t buf_size =0; buf_size = MRB_DUMP_DEFAULT_STR_LEN; if ((char_buf = mrb_malloc(mrb, buf_size)) == 0) goto error_exit; buf += uint32_dump((uint32_t)irep->plen, buf, type); /* number of pool */ for (pool_no = 0; pool_no < irep->plen; pool_no++) { uint16_t nlen =0; buf += uint8_dump(irep->pool[pool_no].tt, buf, type); /* data type */ memset(char_buf, 0, buf_size); switch (irep->pool[pool_no].tt) { case MRB_TT_FIXNUM: sprintf(char_buf, "%d", irep->pool[pool_no].value.i); break; case MRB_TT_FLOAT: sprintf(char_buf, "%.16e", irep->pool[pool_no].value.f); break; case MRB_TT_STRING: str = mrb_string_value( mrb, &irep->pool[pool_no]); nlen = str_dump_len(RSTRING_PTR(str), RSTRING_LEN(str), type); if ( nlen > buf_size - 1) { buf_size = nlen + 1; if ((char_buf = mrb_realloc(mrb, char_buf, buf_size)) == 0) goto error_exit; memset(char_buf, 0, buf_size); } str_dump(RSTRING_PTR(str), char_buf, RSTRING_LEN(str), type); break; #ifdef INCLUDE_REGEXP case MRB_TT_REGEX: str = mrb_reg_to_s(mrb, irep->pool[pool_no]); nlen = str_dump_len(RSTRING_PTR(str), RSTRING_LEN(str), type); if ( nlen > buf_size - 1) { buf_size = nlen + 1; if ((char_buf = mrb_realloc(mrb, char_buf, buf_size)) == 0) goto error_exit; memset(char_buf, 0, buf_size); } str_dump(RSTRING_PTR(str), char_buf, RSTRING_LEN(str), type); break; #endif default: buf += uint16_dump(0, buf, type); /* data length = 0 */ continue; } buf += uint16_dump((uint16_t)strlen(char_buf), buf, type); /* data length */ memcpy(buf, char_buf, strlen(char_buf)); buf += strlen(char_buf); } error_exit: if (char_buf) mrb_free(mrb, char_buf); return (int)(buf - buf_top); }