1
0
mirror of https://github.com/rkujawa/rk65c02.git synced 2025-03-02 23:29:23 +00:00

Use boehm gc to manage memory.

I'm less likely to fuck this up now.
This commit is contained in:
Radosław Kujawa 2018-03-26 12:36:47 +02:00
parent a98c8f1018
commit ae3e782b74
11 changed files with 30 additions and 35 deletions

View File

@ -10,6 +10,7 @@
#include <sys/types.h>
#include <gc/gc.h>
#include <utlist.h>
#include "bus.h"
@ -26,7 +27,7 @@ bus_device_add(bus_t *b, device_t *d, uint16_t addr)
{
device_mapping_t *dm;
dm = (device_mapping_t *) malloc(sizeof(device_mapping_t));
dm = (device_mapping_t *) GC_MALLOC(sizeof(device_mapping_t));
dm->dev = d;
/* TODO: check if addr + size is not bigger than RK65C02_BUS_SIZE */
@ -186,7 +187,5 @@ void
bus_finish(bus_t *t)
{
assert(t != NULL);
/* TODO: foreach devices free 'em */
}

View File

@ -1,5 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <gc/gc.h>
#include <utlist.h>
#include "rk65c02.h"
@ -33,9 +35,6 @@ debug_trace_print_all(rk65c02emu_t *e)
rk65c02_log(LOG_TRACE, "%X: %s\t%s", tr->address, instrstr,
regsstr);
free(instrstr);
free(regsstr);
}
}
@ -46,7 +45,7 @@ debug_trace_savestate(rk65c02emu_t *e, uint16_t address, instrdef_t *id,
{
trace_t *tr;
tr = (trace_t *) malloc(sizeof(trace_t));
tr = (trace_t *) GC_MALLOC(sizeof(trace_t));
if (tr == NULL) {
fprintf(stderr, "Error allocating trace structure.\n");
return;
@ -86,7 +85,7 @@ debug_breakpoint_add(rk65c02emu_t *e, uint16_t address)
{
breakpoint_t *bp;
bp = (breakpoint_t *) malloc(sizeof(breakpoint_t));
bp = (breakpoint_t *) GC_MALLOC(sizeof(breakpoint_t));
if (bp == NULL)
return false;

View File

@ -3,6 +3,8 @@
#include <assert.h>
#include <string.h>
#include <gc/gc.h>
#include "bus.h"
#include "device.h"
@ -55,7 +57,7 @@ device_fb_init()
device_t *d;
struct fb_state *f;
d = (device_t *) malloc(sizeof(device_t));
d = (device_t *) GC_MALLOC(sizeof(device_t));
assert(d != NULL);
@ -65,10 +67,10 @@ device_fb_init()
d->read_1 = device_fb_read_1;
d->write_1 = device_fb_write_1;
f = malloc(sizeof(struct fb_state));
f = GC_MALLOC(sizeof(struct fb_state));
d->aux = f;
f->cram = malloc(FB_AS_SIZE);
f->cram = GC_MALLOC(FB_AS_SIZE);
memset(d->aux, 0, FB_AS_SIZE);
return d;
@ -81,7 +83,5 @@ device_fb_finish(device_t *d)
f = d->aux;
free(f->cram);
free(f);
}

View File

@ -3,6 +3,8 @@
#include <assert.h>
#include <string.h>
#include <gc/gc.h>
#include "bus.h"
#include "device.h"
@ -38,7 +40,7 @@ device_ram_init(uint16_t size)
{
device_t *d;
d = (device_t *) malloc(sizeof(device_t));
d = (device_t *) GC_MALLOC(sizeof(device_t));
assert(d != NULL);
@ -48,7 +50,7 @@ device_ram_init(uint16_t size)
d->read_1 = device_ram_read_1;
d->write_1 = device_ram_write_1;
d->aux = malloc(size);
d->aux = GC_MALLOC(size);
memset(d->aux, 0, size);
return d;
@ -57,6 +59,5 @@ device_ram_init(uint16_t size)
void
device_ram_finish(device_t *d)
{
free(d->aux);
}

View File

@ -7,6 +7,8 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <gc/gc.h>
#include "bus.h"
#include "device.h"
@ -74,7 +76,7 @@ device_serial_init()
device_t *d;
struct device_serial_priv *dp;
d = (device_t *) malloc(sizeof(device_t));
d = (device_t *) GC_MALLOC(sizeof(device_t));
assert(d != NULL);
@ -84,7 +86,7 @@ device_serial_init()
d->read_1 = device_serial_read_1;
d->write_1 = device_serial_write_1;
dp = (struct device_serial_priv *) malloc(sizeof(struct device_serial_priv));
dp = (struct device_serial_priv *) GC_MALLOC(sizeof(struct device_serial_priv));
d->aux = dp;
if (mkfifo(txpipepath, S_IRUSR | S_IWUSR) != 0) {
@ -115,8 +117,6 @@ device_serial_finish(device_t *d)
unlink(txpipepath);
unlink(rxpipepath);
free(d->aux);
// XXX?
}

View File

@ -7,6 +7,8 @@
#include <assert.h>
#include <string.h>
#include <gc/gc.h>
#include "bus.h"
#include "rk65c02.h"
#include "65c02isa.h"
@ -61,8 +63,6 @@ instruction_print(instruction_t *i)
str = instruction_string_get(i);
printf("%s", str);
free(str);
}
char *
@ -72,7 +72,7 @@ instruction_string_get(instruction_t *i)
instrdef_t id;
char *str;
str = malloc(INSTR_STR_LEN);
str = GC_MALLOC(INSTR_STR_LEN);
if (str == NULL) {
rk65c02_log(LOG_CRIT, "Error allocating memory for buffer: %s.",
strerror(errno));
@ -164,7 +164,6 @@ assemble_single(assembler_t *a, const char *mnemonic, addressing_t mode, uint8_t
return rv;
rv = bus_load_buf(a->bus, a->pc, asmbuf, bsize);
free(asmbuf);
a->pc += bsize;
return rv;
@ -205,7 +204,7 @@ assemble_single_buf(uint8_t **buf, uint8_t *bsize, const char *mnemonic, address
}
*bsize = id.size;
*buf = malloc(id.size);
*buf = GC_MALLOC(id.size);
if(*buf == NULL) {
rk65c02_log(LOG_ERROR, "Error allocating assembly buffer.");
return false;

View File

@ -7,6 +7,8 @@
#include <assert.h>
#include <string.h>
#include <gc/gc.h>
#include "bus.h"
#include "instruction.h"
#include "rk65c02.h"
@ -195,7 +197,6 @@ rk65c02_dump_regs(reg_state_t regs)
printf ("%s\n", str);
free(str);
}
char *
@ -205,7 +206,7 @@ rk65c02_regs_string_get(reg_state_t regs)
char *str;
/* XXX: string allocation to a separate utility function? */
str = malloc(REGS_STR_LEN);
str = GC_MALLOC(REGS_STR_LEN);
if (str == NULL) {
rk65c02_log(LOG_CRIT, "Error allocating memory for buffer: %s.",
strerror(errno));

View File

@ -1,5 +1,5 @@
CFLAGS=-Wall -I../src -g
LDFLAGS=-latf-c
LDFLAGS=-latf-c -lgc
RK6502LIB=../src/librk65c02.a
VASM=vasm6502_std
VASMFLAGS=-Fbin -c02

View File

@ -28,20 +28,17 @@ ATF_TC_BODY(assemble_single_buf, tc)
ATF_REQUIRE(assemble_single_buf_implied(&asmbuf, &bsize, "nop"));
ATF_CHECK(asmbuf[0] == 0xEA); /* check if nop really */
ATF_REQUIRE(bus_load_buf(&b, caddr, asmbuf, bsize));
free(asmbuf);
caddr += bsize;
ATF_REQUIRE(assemble_single_buf(&asmbuf, &bsize, "lda", IMMEDIATE, 0xAA, 0));
ATF_CHECK(asmbuf[0] == 0xA9); /* check if lda really */
ATF_CHECK(asmbuf[1] == 0xAA); /* check the operand */
ATF_REQUIRE(bus_load_buf(&b, caddr, asmbuf, bsize));
free(asmbuf);
caddr += bsize;
ATF_REQUIRE(assemble_single_buf_implied(&asmbuf, &bsize, "stp"));
ATF_CHECK(asmbuf[0] == 0xDB); /* check if stp really */
ATF_REQUIRE(bus_load_buf(&b, caddr, asmbuf, bsize));
free(asmbuf);
caddr += bsize;
rk65c02_start(&e);

View File

@ -87,17 +87,14 @@ ATF_TC_BODY(intr_rti, tc)
ATF_REQUIRE(assemble_single_buf_implied(&asmbuf, &bsize, "nop"));
ATF_REQUIRE(bus_load_buf(&b, israsmpc, asmbuf, bsize));
free(asmbuf);
israsmpc += bsize;
ATF_REQUIRE(assemble_single_buf_implied(&asmbuf, &bsize, "rti"));
ATF_REQUIRE(bus_load_buf(&b, israsmpc, asmbuf, bsize));
free(asmbuf);
israsmpc += bsize;
ATF_REQUIRE(assemble_single_buf_implied(&asmbuf, &bsize, "nop"));
ATF_REQUIRE(bus_load_buf(&b, ROM_LOAD_ADDR, asmbuf, bsize));
free(asmbuf);
/* There's a return address and saved processor flags on stack. */
e.regs.SP = 0xFF;

View File

@ -2,7 +2,9 @@
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <atf-c.h>
#include <gc/gc.h>
#include "bus.h"
#include "rk65c02.h"
@ -15,7 +17,7 @@ rom_path(const char *name, const atf_tc_t *tc)
char *rompath;
const char *srcdir;
rompath = malloc(PATH_MAX);
rompath = GC_MALLOC(PATH_MAX);
srcdir = atf_tc_get_config_var(tc, "srcdir");
strcpy(rompath, srcdir);