Simplify error handling if malloc fails.

There's no way to reliable handle out of memory situation in an
emulator. So just bail out by raising assertion.
This commit is contained in:
Radosław Kujawa 2018-08-05 23:45:44 +02:00
parent 0896956337
commit 2cc9d86c64
8 changed files with 12 additions and 19 deletions

View File

@ -75,11 +75,9 @@ assemble_single_buf(uint8_t **buf, uint8_t *bsize, char *mnemonic, addressing_t
}
*bsize = id.size;
*buf = GC_MALLOC(id.size);
if(*buf == NULL) {
rk65c02_log(LOG_ERROR, "Error allocating assembly buffer.");
return false;
}
assert(*buf != NULL);
/* fill the buffer */
memset(*buf, 0, id.size);

View File

@ -35,6 +35,7 @@ bus_device_add(bus_t *b, device_t *d, uint16_t addr)
}
dm = (device_mapping_t *) GC_MALLOC(sizeof(device_mapping_t));
assert(dm != NULL);
dm->dev = d;
dm->addr = addr;

View File

@ -46,10 +46,7 @@ debug_trace_savestate(rk65c02emu_t *e, uint16_t address, instrdef_t *id,
trace_t *tr;
tr = (trace_t *) GC_MALLOC(sizeof(trace_t));
if (tr == NULL) {
fprintf(stderr, "Error allocating trace structure.\n");
return;
}
assert(tr != NULL);
tr->address = address;

View File

@ -68,9 +68,11 @@ device_fb_init()
d->write_1 = device_fb_write_1;
f = GC_MALLOC(sizeof(struct fb_state));
assert(f != NULL);
d->aux = f;
f->cram = GC_MALLOC(FB_AS_SIZE);
assert(f->cram != NULL);
memset(d->aux, 0, FB_AS_SIZE);
return d;

View File

@ -41,7 +41,6 @@ device_ram_init(uint16_t size)
device_t *d;
d = (device_t *) GC_MALLOC(sizeof(device_t));
assert(d != NULL);
d->name = "RAM";
@ -52,6 +51,8 @@ device_ram_init(uint16_t size)
d->finish = NULL;
d->aux = GC_MALLOC(size);
assert(d->aux != NULL);
memset(d->aux, 0, size);
return d;

View File

@ -89,6 +89,7 @@ device_serial_init()
d->finish = device_serial_finish;
dp = (struct device_serial_priv *) GC_MALLOC(sizeof(struct device_serial_priv));
assert(dp != NULL);
d->aux = dp;
if (mkfifo(txpipepath, S_IRUSR | S_IWUSR) != 0) {

View File

@ -73,11 +73,7 @@ instruction_string_get(instruction_t *i)
char *str;
str = GC_MALLOC(INSTR_STR_LEN);
if (str == NULL) {
rk65c02_log(LOG_CRIT, "Error allocating memory for buffer: %s.",
strerror(errno));
return NULL;
}
assert(str != NULL);
memset(str, 0, INSTR_STR_LEN);
id = instruction_decode(i->opcode);

View File

@ -25,6 +25,7 @@ rk65c02_load_rom(const char *path, uint16_t load_addr, bus_t *b)
if (b == NULL) {
b = GC_MALLOC(sizeof(bus_t));
assert(b != NULL);
*b = bus_init_with_default_devs();
}
@ -215,11 +216,7 @@ rk65c02_regs_string_get(reg_state_t regs)
/* XXX: string allocation to a separate utility function? */
str = GC_MALLOC(REGS_STR_LEN);
if (str == NULL) {
rk65c02_log(LOG_CRIT, "Error allocating memory for buffer: %s.",
strerror(errno));
return NULL;
}
assert(str != NULL);
memset(str, 0, REGS_STR_LEN);
snprintf(str, REGS_STR_LEN, "A: %X X: %X Y: %X PC: %X SP: %X P: %c%c%c%c%c%c%c%c",