Esempio n. 1
0
MRB_API void*
mrb_realloc(mrb_state *mrb, void *p, size_t len)
{
  void *p2;

  p2 = mrb_realloc_simple(mrb, p, len);
  if (!p2 && len) {
    if (mrb->out_of_memory) {
      /* mrb_panic(mrb); */
    }
    else {
      mrb->out_of_memory = TRUE;
      mrb_exc_raise(mrb, mrb_obj_value(mrb->nomem_err));
    }
  }
  else {
    mrb->out_of_memory = FALSE;
  }

  return p2;
}
Esempio n. 2
0
File: gc.c Progetto: anehing/mruby
void*
mrb_realloc(mrb_state *mrb, void *p, size_t len)
{
  void *p2;

  p2 = mrb_realloc_simple(mrb, p, len);
  if (!p2 && len) {
    if (mrb->out_of_memory) {
      /* mrb_panic(mrb); */
    }
    else {
      mrb->out_of_memory = TRUE;
      mrb_raise(mrb, E_RUNTIME_ERROR, "Out of memory");
    }
  }
  else {
    mrb->out_of_memory = FALSE;
  }

  return p2;
}
Esempio n. 3
0
File: gc.c Progetto: anehing/mruby
void*
mrb_malloc_simple(mrb_state *mrb, size_t len)
{
  return mrb_realloc_simple(mrb, 0, len);
}
Esempio n. 4
0
mrb_irep*
mrb_read_irep_file(mrb_state *mrb, FILE* fp)
{
  mrb_irep *irep = NULL;
  int result;
  uint8_t *buf;
  uint16_t crc, crcwk = 0;
  size_t section_size = 0;
  size_t nbytes;
  struct rite_section_header section_header;
  long fpos;
  size_t block_size = 1 << 14;
  const uint8_t block_fallback_count = 4;
  int i;
  const size_t buf_size = sizeof(struct rite_binary_header);

  if ((mrb == NULL) || (fp == NULL)) {
    return NULL;
  }

  /* You don't need use SIZE_ERROR as buf_size is enough small. */
  buf = (uint8_t*)mrb_malloc(mrb, buf_size);
  if (fread(buf, buf_size, 1, fp) == 0) {
    mrb_free(mrb, buf);
    return NULL;
  }
  result = read_binary_header(buf, NULL, &crc);
  mrb_free(mrb, buf);
  if (result != MRB_DUMP_OK) {
    return NULL;
  }

  /* verify CRC */
  fpos = ftell(fp);
  /* You don't need use SIZE_ERROR as block_size is enough small. */
  for (i = 0; i < block_fallback_count; i++,block_size >>= 1){
    buf = (uint8_t*)mrb_realloc_simple(mrb, 0, block_size);
    if (buf) break;
  }
  if (!buf) {
    return NULL;
  }
  fseek(fp, offset_crc_body(), SEEK_SET);
  while ((nbytes = fread(buf, 1, block_size, fp)) > 0) {
    crcwk = calc_crc_16_ccitt(buf, nbytes, crcwk);
  }
  mrb_free(mrb, buf);
  if (nbytes == 0 && ferror(fp)) {
    return NULL;
  }
  if (crcwk != crc) {
    return NULL;
  }
  fseek(fp, fpos + section_size, SEEK_SET);

  /* read sections */
  do {
    fpos = ftell(fp);
    if (fread(&section_header, sizeof(struct rite_section_header), 1, fp) == 0) {
      return NULL;
    }
    section_size = (size_t)bin_to_uint32(section_header.section_size);

    if (memcmp(section_header.section_identify, RITE_SECTION_IREP_IDENTIFIER, sizeof(section_header.section_identify)) == 0) {
      fseek(fp, fpos, SEEK_SET);
      irep = read_section_irep_file(mrb, fp);
      if (!irep) return NULL;
    }
    else if (memcmp(section_header.section_identify, RITE_SECTION_LINENO_IDENTIFIER, sizeof(section_header.section_identify)) == 0) {
      if (!irep) return NULL;   /* corrupted data */
      fseek(fp, fpos, SEEK_SET);
      result = read_section_lineno_file(mrb, fp, irep);
      if (result < MRB_DUMP_OK) return NULL;
    }
    else if (memcmp(section_header.section_identify, RITE_SECTION_DEBUG_IDENTIFIER, sizeof(section_header.section_identify)) == 0) {
      if (!irep) return NULL;   /* corrupted data */
      else {
        uint8_t* const bin = (uint8_t*)mrb_malloc(mrb, section_size);

        fseek(fp, fpos, SEEK_SET);
        if (fread((char*)bin, section_size, 1, fp) != 1) {
          mrb_free(mrb, bin);
          return NULL;
        }
        result = read_section_debug(mrb, bin, irep, TRUE);
        mrb_free(mrb, bin);
      }
      if (result < MRB_DUMP_OK) return NULL;
    }

    fseek(fp, fpos + section_size, SEEK_SET);
  } while (memcmp(section_header.section_identify, RITE_BINARY_EOF, sizeof(section_header.section_identify)) != 0);

  return irep;
}