diff options
| author | Masaki Muranaka <[email protected]> | 2013-01-09 12:03:16 +0900 |
|---|---|---|
| committer | Masaki Muranaka <[email protected]> | 2013-01-09 12:19:39 +0900 |
| commit | 0b337accad67e8855a574e8524b5a1ceed162bd3 (patch) | |
| tree | cea31f11c1470c4bebf1e874d01aa8dd4fc7b48b /src | |
| parent | b5830aed17f0623175a4befc7d39a21b217992e7 (diff) | |
| download | mruby-0b337accad67e8855a574e8524b5a1ceed162bd3.tar.gz mruby-0b337accad67e8855a574e8524b5a1ceed162bd3.zip | |
Extract each expresion in "if" statements. It is for maintainability.
Diffstat (limited to 'src')
| -rw-r--r-- | src/dump.c | 36 | ||||
| -rw-r--r-- | src/load.c | 36 |
2 files changed, 48 insertions, 24 deletions
diff --git a/src/dump.c b/src/dump.c index c32703a17..d26372ee3 100644 --- a/src/dump.c +++ b/src/dump.c @@ -353,7 +353,8 @@ write_pool_block(mrb_state *mrb, mrb_irep *irep, char *buf, int type) uint16_t len =0; buf_size = MRB_DUMP_DEFAULT_STR_LEN; - if ((char_buf = (char *)mrb_malloc(mrb, buf_size)) == NULL) + char_buf = (char *)mrb_malloc(mrb, buf_size); + if (char_buf == NULL) goto error_exit; buf += uint32_dump((uint32_t)irep->plen, buf, type); /* number of pool */ @@ -376,7 +377,8 @@ write_pool_block(mrb_state *mrb, mrb_irep *irep, char *buf, int type) len = str_dump_len(RSTRING_PTR(str), RSTRING_LEN(str), type); if (len > buf_size - 1) { buf_size = len + 1; - if ((char_buf = (char *)mrb_realloc(mrb, char_buf, buf_size)) == NULL) + char_buf = (char *)mrb_realloc(mrb, char_buf, buf_size); + if (char_buf == NULL) goto error_exit; memset(char_buf, 0, buf_size); } @@ -389,7 +391,8 @@ write_pool_block(mrb_state *mrb, mrb_irep *irep, char *buf, int type) len = str_dump_len(RSTRING_PTR(str), RSTRING_LEN(str), type); if ( len > buf_size - 1) { buf_size = len + 1; - if ((char_buf = mrb_realloc(mrb, char_buf, buf_size)) == NULL) + char_buf = mrb_realloc(mrb, char_buf, buf_size); + if (char_buf == NULL) goto error_exit; memset(char_buf, 0, buf_size); } @@ -423,7 +426,8 @@ write_syms_block(mrb_state *mrb, mrb_irep *irep, char *buf, int type) uint16_t buf_size =0; buf_size = MRB_DUMP_DEFAULT_STR_LEN; - if ((char_buf = (char *)mrb_malloc(mrb, buf_size)) == NULL) + char_buf = (char *)mrb_malloc(mrb, buf_size); + if (char_buf == NULL) goto error_exit; buf += uint32_dump((uint32_t)irep->slen, buf, type); /* number of symbol */ @@ -439,7 +443,8 @@ write_syms_block(mrb_state *mrb, mrb_irep *irep, char *buf, int type) nlen = str_dump_len((char*)name, len, type); if ( nlen > buf_size - 1) { buf_size = nlen + 1; - if ((char_buf = (char *)mrb_realloc(mrb, char_buf, buf_size)) == NULL) + char_buf = (char *)mrb_realloc(mrb, char_buf, buf_size); + if (char_buf == NULL) goto error_exit; } memset(char_buf, 0, buf_size); @@ -475,7 +480,8 @@ calc_crc_section(mrb_state *mrb, mrb_irep *irep, uint16_t *crc, int section) default: return MRB_DUMP_GENERAL_FAILURE; } - if ((buf = (char *)mrb_calloc(mrb, 1, buf_size)) == NULL) + buf = (char *)mrb_calloc(mrb, 1, buf_size); + if (buf == NULL) return MRB_DUMP_GENERAL_FAILURE; buf_top = buf; @@ -599,7 +605,8 @@ write_irep_record(mrb_state *mrb, int irep_no, char* bin, uint32_t *rlen, int ty default: break; } - if ((rc = calc_crc_section(mrb, irep, &crc, section)) != 0) + rc = calc_crc_section(mrb, irep, &crc, section); + if (rc != 0) return rc; bin += uint16_dump(crc, bin, type); /* crc */ @@ -624,10 +631,12 @@ dump_irep_record(mrb_state *mrb, int irep_no, FILE* fp, uint32_t *rlen) if (irep_record_size == 0) return MRB_DUMP_GENERAL_FAILURE; - if ((buf = (char *)mrb_calloc(mrb, 1, irep_record_size)) == NULL) + buf = (char *)mrb_calloc(mrb, 1, irep_record_size); + if (buf == NULL) return MRB_DUMP_GENERAL_FAILURE; - if ((rc = write_irep_record(mrb, irep_no, buf, rlen, DUMP_TYPE_HEX)) != MRB_DUMP_OK) { + rc = write_irep_record(mrb, irep_no, buf, rlen, DUMP_TYPE_HEX); + if (rc != MRB_DUMP_OK) { rc = MRB_DUMP_GENERAL_FAILURE; goto error_exit; } @@ -657,7 +666,8 @@ mrb_write_irep(mrb_state *mrb, int top, char *bin) bin += sizeof(rite_binary_header) + MRB_DUMP_SIZE_OF_SHORT/* crc */; for (irep_no=top; irep_no<mrb->irep_len; irep_no++) { - if ((rc = write_irep_record(mrb, irep_no, bin, &rlen, DUMP_TYPE_BIN)) != 0) + rc = write_irep_record(mrb, irep_no, bin, &rlen, DUMP_TYPE_BIN); + if (rc != 0) return rc; bin += (rlen + DUMP_SIZE(MRB_DUMP_SIZE_OF_LONG, DUMP_TYPE_BIN)); @@ -685,7 +695,8 @@ mrb_dump_irep(mrb_state *mrb, int top, FILE* fp) return MRB_DUMP_WRITE_FAULT; for (irep_no=top; irep_no<mrb->irep_len; irep_no++) { - if ((rc = dump_irep_record(mrb, irep_no, fp, &rlen)) != 0) + rc = dump_irep_record(mrb, irep_no, fp, &rlen); + if (rc != 0) return rc; rbds += rlen; @@ -716,7 +727,8 @@ mrb_bdump_irep(mrb_state *mrb, int n, FILE *f,const char *initname) buf_size += get_irep_record_size(mrb, irep_no, DUMP_TYPE_BIN); buf_size += MRB_DUMP_SIZE_OF_LONG; /* end of file */ - if ((buf = (char *)mrb_malloc(mrb, buf_size)) == NULL) + buf = (char *)mrb_malloc(mrb, buf_size); + if (buf == NULL) return MRB_DUMP_GENERAL_FAILURE; rc = mrb_write_irep(mrb, n, buf); diff --git a/src/load.c b/src/load.c index 2b5f58001..f434fdd74 100644 --- a/src/load.c +++ b/src/load.c @@ -144,7 +144,8 @@ load_rite_irep_record(mrb_state *mrb, RiteFILE* rfp, unsigned char* dst, uint32_ uint16_t buf_size =0; buf_size = MRB_DUMP_DEFAULT_STR_LEN; - if ((char_buf = (char *)mrb_malloc(mrb, buf_size)) == NULL) + char_buf = (char *)mrb_malloc(mrb, buf_size); + if (char_buf == NULL) goto error_exit; pStart = dst; @@ -193,7 +194,8 @@ load_rite_irep_record(mrb_state *mrb, RiteFILE* rfp, unsigned char* dst, uint32_ if ( pdl > buf_size - 1) { buf_size = pdl + 1; - if ((char_buf = (char *)mrb_realloc(mrb, char_buf, buf_size)) == NULL) + char_buf = (char *)mrb_realloc(mrb, char_buf, buf_size); + if (char_buf == NULL) goto error_exit; } memset(char_buf, '\0', buf_size); @@ -220,7 +222,8 @@ load_rite_irep_record(mrb_state *mrb, RiteFILE* rfp, unsigned char* dst, uint32_ if ( snl > buf_size - 1) { buf_size = snl + 1; - if ((char_buf = (char *)mrb_realloc(mrb, char_buf, buf_size)) == NULL) + char_buf = (char *)mrb_realloc(mrb, char_buf, buf_size); + if (char_buf == NULL) goto error_exit; } memset(char_buf, '\0', buf_size); @@ -259,11 +262,13 @@ mrb_read_irep_file(mrb_state *mrb, FILE* fp) rfp = &ritefp; //Read File Header Section - if ((ret = load_rite_header(fp, &bin_header, hcrc)) != MRB_DUMP_OK) + 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); - if ((rite_dst = (unsigned char *)mrb_malloc(mrb, len)) == NULL) + rite_dst = (unsigned char *)mrb_malloc(mrb, len); + if (rite_dst == NULL) return MRB_DUMP_GENERAL_FAILURE; dst = rite_dst; @@ -277,7 +282,8 @@ mrb_read_irep_file(mrb_state *mrb, FILE* fp) for (i=0; i<len; i++) { rite_fgets(rfp, hex8, sizeof(hex8), TRUE); //record len dst += hex_to_bin32(dst, hex8); - if ((ret = load_rite_irep_record(mrb, rfp, dst, &rlen)) != MRB_DUMP_OK) //irep info + ret = load_rite_irep_record(mrb, rfp, dst, &rlen); + if (ret != MRB_DUMP_OK) //irep info goto error_exit; dst += rlen; } @@ -362,7 +368,8 @@ read_rite_irep_record(mrb_state *mrb, unsigned char *src, uint32_t* len) irep->ilen = bin_to_uint32(src); //iseq length src += MRB_DUMP_SIZE_OF_LONG; if (irep->ilen > 0) { - if ((irep->iseq = (mrb_code *)mrb_malloc(mrb, sizeof(mrb_code) * irep->ilen)) == NULL) { + irep->iseq = (mrb_code *)mrb_malloc(mrb, sizeof(mrb_code) * irep->ilen); + if (irep->iseq == NULL) { ret = MRB_DUMP_GENERAL_FAILURE; goto error_exit; } @@ -397,7 +404,8 @@ read_rite_irep_record(mrb_state *mrb, unsigned char *src, uint32_t* len) if (pdl > bufsize - 1) { mrb_free(mrb, buf); bufsize = pdl + 1; - if ((buf = (char *)mrb_malloc(mrb, bufsize)) == NULL) { + buf = (char *)mrb_malloc(mrb, bufsize); + if (buf == NULL) { ret = MRB_DUMP_GENERAL_FAILURE; goto error_exit; } @@ -448,7 +456,8 @@ read_rite_irep_record(mrb_state *mrb, unsigned char *src, uint32_t* len) irep->slen = bin_to_uint32(src); //syms length src += MRB_DUMP_SIZE_OF_LONG; if (irep->slen > 0) { - if ((irep->syms = (mrb_sym *)mrb_malloc(mrb, sizeof(mrb_sym) * irep->slen)) == NULL) { + irep->syms = (mrb_sym *)mrb_malloc(mrb, sizeof(mrb_sym) * irep->slen); + if (irep->syms == NULL) { ret = MRB_DUMP_INVALID_IREP; goto error_exit; } @@ -469,7 +478,8 @@ read_rite_irep_record(mrb_state *mrb, unsigned char *src, uint32_t* len) if (snl > bufsize - 1) { mrb_free(mrb, buf); bufsize = snl + 1; - if ((buf = (char *)mrb_malloc(mrb, bufsize)) == NULL) { + buf = (char *)mrb_malloc(mrb, bufsize); + if (buf == NULL) { ret = MRB_DUMP_GENERAL_FAILURE; goto error_exit; } @@ -510,7 +520,8 @@ mrb_read_irep(mrb_state *mrb, const char *bin) sirep = mrb->irep_len; //Read File Header Section - if ((nirep = read_rite_header(mrb, src, &bin_header)) < 0) + nirep = read_rite_header(mrb, src, &bin_header); + if (nirep < 0) return nirep; src += sizeof(bin_header) + MRB_DUMP_SIZE_OF_SHORT; //header + crc @@ -518,7 +529,8 @@ mrb_read_irep(mrb_state *mrb, const char *bin) //Read Binary Data Section for (n=0,i=sirep; n<nirep; n++,i++) { src += MRB_DUMP_SIZE_OF_LONG; //record ren - if ((ret = read_rite_irep_record(mrb, src, &len)) != MRB_DUMP_OK) + ret = read_rite_irep_record(mrb, src, &len); + if (ret != MRB_DUMP_OK) goto error_exit; src += len; } |
