Skip to content
Snippets Groups Projects
Commit b984e82d authored by Toby Murray's avatar Toby Murray
Browse files

Dynamically allocate 'memory' and 'regs'

parent aab955d5
No related branches found
No related tags found
No related merge requests found
......@@ -45,18 +45,28 @@ const char INSTRUCTION_JZ[] = "jz";
/* we force building with -fwrapv to ensure that signed overflow is defined
to wrap around */
int32_t memory[MEMORY_SIZE];
int32_t regs[NUM_REGS];
/* we dynamically allocate memory and regs since AdddressSanitizer appears
* to more reliably catch out of bounds memory accesses for heap allocated
* buffers than for global variables */
int32_t * memory = NULL;
int32_t * regs = NULL;
unsigned int count = 0; /* counts number of instructions executed so far */
static void machine_init(void){
memset(memory,0,sizeof(memory));
memset(regs,0,sizeof(regs));
memory = malloc(sizeof(int32_t)*MEMORY_SIZE);
regs = malloc(sizeof(int32_t)*NUM_REGS);
memset(memory,0,sizeof(int32_t)*MEMORY_SIZE);
memset(regs,0,sizeof(int32_t)*NUM_REGS);
count = 0;
}
static void machine_free(void){
free(memory);
free(regs);
}
static void do_add(unsigned int dest, unsigned int src1, unsigned int src2)
{
regs[dest] = regs[src1] + regs[src2];
......@@ -539,6 +549,7 @@ int run_machine(const char * const filename, int cycles){
const int res = read_program(filename);
if (res < 0){
fprintf(stderr,"Error reading program from %s\n",filename);
machine_free();
return 1;
}
......@@ -548,10 +559,12 @@ int run_machine(const char * const filename, int cycles){
int32_t result;
if (execute(numLines,cycles,&result) != 0){
fprintf(stderr,"Program %s didn't execute successfully!\n",filename);
machine_free();
return 1;
}
printf("Program %s executed successfully. Result: %d\n",filename,result);
machine_free();
return 0;
}
......@@ -45,18 +45,28 @@ const char INSTRUCTION_JZ[] = "jz";
/* we force building with -fwrapv to ensure that signed overflow is defined
to wrap around */
int32_t memory[MEMORY_SIZE];
int32_t regs[NUM_REGS];
/* we dynamically allocate memory and regs since AdddressSanitizer appears
* to more reliably catch out of bounds memory accesses for heap allocated
* buffers than for global variables */
int32_t * memory = NULL;
int32_t * regs = NULL;
unsigned int count = 0; /* counts number of instructions executed so far */
static void machine_init(void){
memset(memory,0,sizeof(memory));
memset(regs,0,sizeof(regs));
memory = malloc(sizeof(int32_t)*MEMORY_SIZE);
regs = malloc(sizeof(int32_t)*NUM_REGS);
memset(memory,0,sizeof(int32_t)*MEMORY_SIZE);
memset(regs,0,sizeof(int32_t)*NUM_REGS);
count = 0;
}
static void machine_free(void){
free(memory);
free(regs);
}
static void do_add(unsigned int dest, unsigned int src1, unsigned int src2)
{
regs[dest] = regs[src1] + regs[src2];
......@@ -539,6 +549,7 @@ int run_machine(const char * const filename, int cycles){
const int res = read_program(filename);
if (res < 0){
fprintf(stderr,"Error reading program from %s\n",filename);
machine_free();
return 1;
}
......@@ -548,10 +559,12 @@ int run_machine(const char * const filename, int cycles){
int32_t result;
if (execute(numLines,cycles,&result) != 0){
fprintf(stderr,"Program %s didn't execute successfully!\n",filename);
machine_free();
return 1;
}
printf("Program %s executed successfully. Result: %d\n",filename,result);
machine_free();
return 0;
}
......@@ -45,18 +45,28 @@ const char INSTRUCTION_JZ[] = "jz";
/* we force building with -fwrapv to ensure that signed overflow is defined
to wrap around */
int32_t memory[MEMORY_SIZE];
int32_t regs[NUM_REGS];
/* we dynamically allocate memory and regs since AdddressSanitizer appears
* to more reliably catch out of bounds memory accesses for heap allocated
* buffers than for global variables */
int32_t * memory = NULL;
int32_t * regs = NULL;
unsigned int count = 0; /* counts number of instructions executed so far */
static void machine_init(void){
memset(memory,0,sizeof(memory));
memset(regs,0,sizeof(regs));
memory = malloc(sizeof(int32_t)*MEMORY_SIZE);
regs = malloc(sizeof(int32_t)*NUM_REGS);
memset(memory,0,sizeof(int32_t)*MEMORY_SIZE);
memset(regs,0,sizeof(int32_t)*NUM_REGS);
count = 0;
}
static void machine_free(void){
free(memory);
free(regs);
}
static void do_add(unsigned int dest, unsigned int src1, unsigned int src2)
{
regs[dest] = regs[src1] + regs[src2];
......@@ -539,6 +549,7 @@ int run_machine(const char * const filename, int cycles){
const int res = read_program(filename);
if (res < 0){
fprintf(stderr,"Error reading program from %s\n",filename);
machine_free();
return 1;
}
......@@ -548,10 +559,12 @@ int run_machine(const char * const filename, int cycles){
int32_t result;
if (execute(numLines,cycles,&result) != 0){
fprintf(stderr,"Program %s didn't execute successfully!\n",filename);
machine_free();
return 1;
}
printf("Program %s executed successfully. Result: %d\n",filename,result);
machine_free();
return 0;
}
......@@ -45,18 +45,28 @@ const char INSTRUCTION_JZ[] = "jz";
/* we force building with -fwrapv to ensure that signed overflow is defined
to wrap around */
int32_t memory[MEMORY_SIZE];
int32_t regs[NUM_REGS];
/* we dynamically allocate memory and regs since AdddressSanitizer appears
* to more reliably catch out of bounds memory accesses for heap allocated
* buffers than for global variables */
int32_t * memory = NULL;
int32_t * regs = NULL;
unsigned int count = 0; /* counts number of instructions executed so far */
static void machine_init(void){
memset(memory,0,sizeof(memory));
memset(regs,0,sizeof(regs));
memory = malloc(sizeof(int32_t)*MEMORY_SIZE);
regs = malloc(sizeof(int32_t)*NUM_REGS);
memset(memory,0,sizeof(int32_t)*MEMORY_SIZE);
memset(regs,0,sizeof(int32_t)*NUM_REGS);
count = 0;
}
static void machine_free(void){
free(memory);
free(regs);
}
static void do_add(unsigned int dest, unsigned int src1, unsigned int src2)
{
regs[dest] = regs[src1] + regs[src2];
......@@ -539,6 +549,7 @@ int run_machine(const char * const filename, int cycles){
const int res = read_program(filename);
if (res < 0){
fprintf(stderr,"Error reading program from %s\n",filename);
machine_free();
return 1;
}
......@@ -548,10 +559,12 @@ int run_machine(const char * const filename, int cycles){
int32_t result;
if (execute(numLines,cycles,&result) != 0){
fprintf(stderr,"Program %s didn't execute successfully!\n",filename);
machine_free();
return 1;
}
printf("Program %s executed successfully. Result: %d\n",filename,result);
machine_free();
return 0;
}
......@@ -45,18 +45,28 @@ const char INSTRUCTION_JZ[] = "jz";
/* we force building with -fwrapv to ensure that signed overflow is defined
to wrap around */
int32_t memory[MEMORY_SIZE];
int32_t regs[NUM_REGS];
/* we dynamically allocate memory and regs since AdddressSanitizer appears
* to more reliably catch out of bounds memory accesses for heap allocated
* buffers than for global variables */
int32_t * memory = NULL;
int32_t * regs = NULL;
unsigned int count = 0; /* counts number of instructions executed so far */
static void machine_init(void){
memset(memory,0,sizeof(memory));
memset(regs,0,sizeof(regs));
memory = malloc(sizeof(int32_t)*MEMORY_SIZE);
regs = malloc(sizeof(int32_t)*NUM_REGS);
memset(memory,0,sizeof(int32_t)*MEMORY_SIZE);
memset(regs,0,sizeof(int32_t)*NUM_REGS);
count = 0;
}
static void machine_free(void){
free(memory);
free(regs);
}
static void do_add(unsigned int dest, unsigned int src1, unsigned int src2)
{
regs[dest] = regs[src1] + regs[src2];
......@@ -539,6 +549,7 @@ int run_machine(const char * const filename, int cycles){
const int res = read_program(filename);
if (res < 0){
fprintf(stderr,"Error reading program from %s\n",filename);
machine_free();
return 1;
}
......@@ -548,10 +559,12 @@ int run_machine(const char * const filename, int cycles){
int32_t result;
if (execute(numLines,cycles,&result) != 0){
fprintf(stderr,"Program %s didn't execute successfully!\n",filename);
machine_free();
return 1;
}
printf("Program %s executed successfully. Result: %d\n",filename,result);
machine_free();
return 0;
}
......@@ -45,18 +45,28 @@ const char INSTRUCTION_JZ[] = "jz";
/* we force building with -fwrapv to ensure that signed overflow is defined
to wrap around */
int32_t memory[MEMORY_SIZE];
int32_t regs[NUM_REGS];
/* we dynamically allocate memory and regs since AdddressSanitizer appears
* to more reliably catch out of bounds memory accesses for heap allocated
* buffers than for global variables */
int32_t * memory = NULL;
int32_t * regs = NULL;
unsigned int count = 0; /* counts number of instructions executed so far */
static void machine_init(void){
memset(memory,0,sizeof(memory));
memset(regs,0,sizeof(regs));
memory = malloc(sizeof(int32_t)*MEMORY_SIZE);
regs = malloc(sizeof(int32_t)*NUM_REGS);
memset(memory,0,sizeof(int32_t)*MEMORY_SIZE);
memset(regs,0,sizeof(int32_t)*NUM_REGS);
count = 0;
}
static void machine_free(void){
free(memory);
free(regs);
}
static void do_add(unsigned int dest, unsigned int src1, unsigned int src2)
{
regs[dest] = regs[src1] + regs[src2];
......@@ -539,6 +549,7 @@ int run_machine(const char * const filename, int cycles){
const int res = read_program(filename);
if (res < 0){
fprintf(stderr,"Error reading program from %s\n",filename);
machine_free();
return 1;
}
......@@ -548,10 +559,12 @@ int run_machine(const char * const filename, int cycles){
int32_t result;
if (execute(numLines,cycles,&result) != 0){
fprintf(stderr,"Program %s didn't execute successfully!\n",filename);
machine_free();
return 1;
}
printf("Program %s executed successfully. Result: %d\n",filename,result);
machine_free();
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment