Dump/load 16 bits for `ilen` and `slen` in `irep`.
Those types are `uint16_t` in definition. Also we no longer need padding
for `iseq`.
MODIFY
src/dump.c
src/dump.c
@@ -28,17 +28,6 @@ static size_t get_irep_record_size_1(mrb_state *mrb, const mrb_irep *irep);
# error This code cannot be built on your environment.
#endif
-static size_t
-write_padding(uint8_t *buf)
-{
- const size_t align = MRB_DUMP_ALIGNMENT;
- size_t pad_len = -(intptr_t)buf & (align-1);
- if (pad_len > 0) {
- memset(buf, 0, pad_len);
- }
- return pad_len;
-}
-
static size_t
get_irep_header_size(mrb_state *mrb)
{
@@ -69,9 +58,8 @@ get_iseq_block_size(mrb_state *mrb, const mrb_irep *irep)
{
size_t size = 0;
- size += sizeof(uint32_t); /* ilen */
- size += sizeof(uint32_t); /* max padding */
- size += sizeof(uint32_t) * irep->ilen; /* iseq(n) */
+ size += sizeof(uint16_t); /* ilen */
+ size += irep->ilen * sizeof(mrb_code); /* iseq(n) */
return size;
}
@@ -81,8 +69,7 @@ write_iseq_block(mrb_state *mrb, const mrb_irep *irep, uint8_t *buf, uint8_t fla
{
uint8_t *cur = buf;
- cur += uint32_to_bin(irep->ilen, cur); /* number of opcode */
- cur += write_padding(cur);
+ cur += uint16_to_bin(irep->ilen, cur); /* number of opcode */
memcpy(cur, irep->iseq, irep->ilen * sizeof(mrb_code));
cur += irep->ilen * sizeof(mrb_code);
@@ -239,7 +226,7 @@ get_syms_block_size(mrb_state *mrb, const mrb_irep *irep)
int sym_no;
mrb_int len;
- size += sizeof(uint32_t); /* slen */
+ size += sizeof(uint16_t); /* slen */
for (sym_no = 0; sym_no < irep->slen; sym_no++) {
size += sizeof(uint16_t); /* snl(n) */
if (irep->syms[sym_no] != 0) {
@@ -258,7 +245,7 @@ write_syms_block(mrb_state *mrb, const mrb_irep *irep, uint8_t *buf)
uint8_t *cur = buf;
const char *name;
- cur += uint32_to_bin(irep->slen, cur); /* number of symbol */
+ cur += uint16_to_bin(irep->slen, cur); /* number of symbol */
for (sym_no = 0; sym_no < irep->slen; sym_no++) {
if (irep->syms[sym_no] != 0) {

MODIFY
src/load.c
src/load.c
#define SIZE_ERROR_MUL(nmemb, size) ((size_t)(nmemb) > SIZE_MAX / (size))
-static size_t
-skip_padding(const uint8_t *buf)
-{
- const size_t align = MRB_DUMP_ALIGNMENT;
- return -(intptr_t)buf & (align-1);
-}
-
static size_t
offset_crc_body(void)
{
@@ -109,9 +102,8 @@ read_irep_record_1(mrb_state *mrb, const uint8_t *bin, size_t *len, uint8_t flag
/* ISEQ BLOCK (and CATCH HANDLER TABLE BLOCK) */
irep->clen = bin_to_uint16(src); /* number of catch handler */
src += sizeof(uint16_t);
- irep->ilen = (uint16_t)bin_to_uint32(src);
- src += sizeof(uint32_t);
- src += skip_padding(src);
+ irep->ilen = bin_to_uint16(src);
+ src += sizeof(uint16_t);
if (irep->ilen > 0) {
size_t data_len = sizeof(mrb_code) * irep->ilen +
@@ -212,8 +204,8 @@ read_irep_record_1(mrb_state *mrb, const uint8_t *bin, size_t *len, uint8_t flag
}
/* SYMS BLOCK */
- irep->slen = (uint16_t)bin_to_uint32(src); /* syms length */
- src += sizeof(uint32_t);
+ irep->slen = bin_to_uint16(src); /* syms length */
+ src += sizeof(uint16_t);
if (irep->slen > 0) {
if (SIZE_ERROR_MUL(irep->slen, sizeof(mrb_sym))) {
return NULL;

Dump/load 16 bits for `ilen` and `slen` in `irep`.

Those types are `uint16_t` in definition. Also we no longer need padding
for `iseq`.