Skip to content

Commit

Permalink
Merge pull request #11 from cs233/fix-memory-leaks
Browse files Browse the repository at this point in the history
Fixed memory leaks
  • Loading branch information
Nydauron authored Apr 24, 2023
2 parents 8f5eccf + 1d6fa3c commit 1907687
Show file tree
Hide file tree
Showing 21 changed files with 219 additions and 119 deletions.
6 changes: 2 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ add_subdirectory(spim/CPU)

target_link_libraries(wasm spim)

target_compile_options(wasm PRIVATE -I. -I$CPU -Wall -pedantic -Wextra -Wunused -Wno-write-strings -x c++)
target_compile_options(wasm PRIVATE -pthread -I. -I$CPU -Wall -pedantic -Wextra -Wunused -Wno-write-strings -x c++)
target_link_options(wasm PRIVATE -pthread)

if (EMSCRIPTEN)
target_link_options(wasm PRIVATE
-pthread
-lm
-lembind
"SHELL:-s EXIT_RUNTIME=0"
Expand Down Expand Up @@ -108,7 +108,6 @@ if (EMSCRIPTEN)
endif()

target_compile_options(wasm PRIVATE
-pthread
"SHELL:-s NO_DISABLE_EXCEPTION_CATCHING"
)

Expand All @@ -117,7 +116,6 @@ if (EMSCRIPTEN)
COMMAND mv wasm.* ../${DIST_DIR}/
)
else()
target_link_options(wasm PRIVATE "-pthread")
if (CMAKE_BUILD_TYPE IN_LIST DEBUG_PROFILES)
target_link_options(wasm PRIVATE "${EXTRA_DEBUG_LINKER_FLAGS}")
endif()
Expand Down
2 changes: 1 addition & 1 deletion spim/CPU/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ BISON_TARGET(MyParser parser.y ${CMAKE_CURRENT_BINARY_DIR}/parser_yacc.cpp
find_package(FLEX 2.6.4 REQUIRED)
FLEX_TARGET(MyLexxer scanner.l ${CMAKE_CURRENT_BINARY_DIR}/lex.yy.cpp)

add_compile_options(-Wall -pedantic -Wextra -Wunused -Wno-write-strings -Wno-narrowing -x c++)
add_compile_options(-pthread -Wall -pedantic -Wextra -Wunused -Wno-write-strings -Wno-narrowing -x c++)

include_directories(${CMAKE_CURRENT_BINARY_DIR})
add_library(spim STATIC ${Spim_SOURCES} ${BISON_MyParser_OUTPUTS} ${FLEX_MyLexxer_OUTPUTS} )
Expand Down
9 changes: 9 additions & 0 deletions spim/CPU/consts.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef CONSTS_H
#define CONSTS_H

constexpr int K = 1024;


#define BYTES_PER_WORD 4 /* On the MIPS processor */

#endif
14 changes: 14 additions & 0 deletions spim/CPU/image.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
#include "inst.h"
#include "spim.h"
#include "spim-utils.h"
#include "image.h"
#include "sym-tbl.h"

#include <iostream>

MIPSImage::MIPSImage(int ctx) : ctx(ctx), std_out(ctx, std::cout), std_err(ctx, std::cerr) {}

MIPSImage::~MIPSImage() {
initialize_symbol_table(*this);
for (auto it = labels_to_free.begin(); it != labels_to_free.end(); it ++) {
free((*it)->name);
free(*it);
}
}

int MIPSImage::get_ctx() const {
return ctx;
}
Expand All @@ -22,6 +32,10 @@ label **MIPSImage::get_label_hash_table() {
return label_hash_table;
}

void MIPSImage::push_label_to_free_vector(label *label) {
labels_to_free.push_back(label);
}

label *MIPSImage::get_local_labels() {
return local_labels;
}
Expand Down
5 changes: 4 additions & 1 deletion spim/CPU/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,23 @@ class MIPSImage {

std::unordered_map<mem_addr, breakpoint> bkpt_map;
// std::unordered_map<mem_addr, label> labels;
label *local_labels = NULL;
label *local_labels = NULL; // No allocs occur here
label *label_hash_table[LABEL_HASH_TABLE_SIZE] = {0};
std::vector<label *> labels_to_free;

MIPSImagePrintStream std_out;
MIPSImagePrintStream std_err;
public:
MIPSImage(int ctx);
~MIPSImage();

int get_ctx() const;

mem_image_t &mem_image();
reg_image_t &reg_image();
label **get_label_hash_table();
label *get_local_labels();
void push_label_to_free_vector(label *);
void set_local_labels(label *);
const mem_image_t &memview_image() const;
const reg_image_t &regview_image() const;
Expand Down
11 changes: 7 additions & 4 deletions spim/CPU/inst.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,9 @@ i_type_inst_full_word (MIPSImage &img, int opcode, int rt, int rs, imm_expr *exp
{
r_type_inst (img, Y_ADDU_OP, 1, 1, rs);
}
i_type_inst_free (img, opcode, rt, 1, lower_bits_of_expr (img, const_imm_expr (img, low)));
imm_expr *const_expr = const_imm_expr (img, low);
i_type_inst_free (img, opcode, rt, 1, lower_bits_of_expr (img, const_expr));
free(const_expr);
}
else
{
Expand Down Expand Up @@ -577,7 +579,9 @@ free_inst (instruction *inst)
/* Don't free the breakpoint insructions since we only have one. */
{
if (EXPR (inst))
free (EXPR (inst));
free (EXPR (inst));
if (SOURCE(inst))
free (SOURCE(inst));
free (inst);
}
}
Expand Down Expand Up @@ -664,7 +668,6 @@ inst_to_string(MIPSImage &img, mem_addr addr)
return "";
}

ss_init (&ss);
format_an_inst (img, &ss, inst, addr);
return ss_to_string (img, &ss);
}
Expand Down Expand Up @@ -1194,7 +1197,7 @@ make_addr_expr (MIPSImage &img, int offs, char *sym, int reg_no)
else
{
expr->reg_no = (unsigned char)reg_no;
expr->imm = make_imm_expr (img, offs, (sym ? str_copy (img, sym) : sym), false);
expr->imm = make_imm_expr (img, offs, sym, false);
}
return (expr);
}
Expand Down
4 changes: 2 additions & 2 deletions spim/CPU/instruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ typedef struct inst_s
} r_t;

int32 encoding;
imm_expr *expr;
char *source_line;
imm_expr *expr = 0;
char *source_line = 0;
} instruction;

#endif
18 changes: 11 additions & 7 deletions spim/CPU/mem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,12 @@





/* Local functions: */

static mem_word bad_mem_read (MIPSImage &img, mem_addr addr, int mask);
static void bad_mem_write (MIPSImage &img, mem_addr addr, mem_word value, int mask);
static instruction *bad_text_read (MIPSImage &img, mem_addr addr);
static void bad_text_write (MIPSImage &img, mem_addr addr, instruction *inst);
static void free_instructions (instruction **inst, int n);
static mem_word read_memory_mapped_IO (MIPSImage &img, mem_addr addr);
static void write_memory_mapped_IO (MIPSImage &img, mem_addr addr, mem_word value);

Expand Down Expand Up @@ -175,7 +172,7 @@ void mem_dump_profile(MIPSImage &img) {
mem_image_t &mem_image = img.mem_image();

str_stream ss;
ss_init(&ss);

FILE *file = NULL;

// TODO: need to standardize this for multiple contexts
Expand Down Expand Up @@ -227,7 +224,7 @@ void mem_dump_profile(MIPSImage &img) {

/* Free the storage used by the old instructions in memory. */

static void
void
free_instructions (instruction **inst, int n)
{
for ( ; n > 0; n --, inst ++)
Expand Down Expand Up @@ -421,10 +418,17 @@ void
set_mem_inst(MIPSImage &img, mem_addr addr, instruction* inst)
{
img.mem_image().text_modified = true;
if ((addr >= TEXT_BOT) && (addr < img.mem_image().text_top) && !(addr & 0x3))
if ((addr >= TEXT_BOT) && (addr < img.mem_image().text_top) && !(addr & 0x3)) {
if (img.mem_image().text_seg [(addr - TEXT_BOT) >> 2]) {
free_inst(img.mem_image().text_seg [(addr - TEXT_BOT) >> 2]);
}
img.mem_image().text_seg [(addr - TEXT_BOT) >> 2] = inst;
else if ((addr >= K_TEXT_BOT) && (addr < img.mem_image().k_text_top) && !(addr & 0x3))
} else if ((addr >= K_TEXT_BOT) && (addr < img.mem_image().k_text_top) && !(addr & 0x3)) {
if (img.mem_image().k_text_seg [(addr - K_TEXT_BOT) >> 2]) {
free_inst(img.mem_image().k_text_seg [(addr - K_TEXT_BOT) >> 2]);
}
img.mem_image().k_text_seg [(addr - K_TEXT_BOT) >> 2] = inst;
}
else
bad_text_write (img, addr, inst);
}
Expand Down
25 changes: 0 additions & 25 deletions spim/CPU/mem.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,31 +41,6 @@

#include "image.h"

/* The text boundaries. */
#define TEXT_BOT ((mem_addr) 0x400000)
/* Amount to grow text segment when we run out of space for instructions. */
#define TEXT_CHUNK_SIZE 4096

/* The data boundaries. */
#define DATA_BOT ((mem_addr) 0x10000000)

/* The stack boundaries. */
/* Exclusive, but include 4K at top of stack. */
#define STACK_TOP ((mem_addr) 0x80000000)

/* The kernel text boundaries. */
#define K_TEXT_BOT ((mem_addr) 0x80000000)

/* The Kernel data boundaries. */
#define K_DATA_BOT ((mem_addr) 0x90000000)

/* Memory-mapped IO area: */
#define MM_IO_BOT ((mem_addr) 0xffff0000)
#define MM_IO_TOP ((mem_addr) 0xffffffff)

#define SPECIAL_BOT ((mem_addr) 0xfffe0000)
#define SPECIAL_TOP ((mem_addr) 0xffff0000)




Expand Down
54 changes: 53 additions & 1 deletion spim/CPU/mem_image.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,45 @@
#ifndef MEM_IMAGE_H
#define MEM_IMAGE_H

#include "consts.h"
#include "types.h"
#include "instruction.h"

#include <stdlib.h>

/* Type of contents of a memory word. */

typedef int32 /*@alt unsigned int @*/ mem_word;

#define BYTE_TYPE signed char

/* The text boundaries. */
#define TEXT_BOT ((mem_addr) 0x400000)
/* Amount to grow text segment when we run out of space for instructions. */
#define TEXT_CHUNK_SIZE 4096

/* The data boundaries. */
#define DATA_BOT ((mem_addr) 0x10000000)

/* The stack boundaries. */
/* Exclusive, but include 4K at top of stack. */
#define STACK_TOP ((mem_addr) 0x80000000)

/* The kernel text boundaries. */
#define K_TEXT_BOT ((mem_addr) 0x80000000)

/* The Kernel data boundaries. */
#define K_DATA_BOT ((mem_addr) 0x90000000)

/* Memory-mapped IO area: */
#define MM_IO_BOT ((mem_addr) 0xffff0000)
#define MM_IO_TOP ((mem_addr) 0xffffffff)

#define SPECIAL_BOT ((mem_addr) 0xfffe0000)
#define SPECIAL_TOP ((mem_addr) 0xffff0000)

void free_instructions (instruction **inst, int n);

typedef struct memimage {
/* The text segment. */
instruction **text_seg = 0;
Expand Down Expand Up @@ -48,6 +78,28 @@ typedef struct memimage {
mem_addr k_data_top = 0;

char* prof_file_name = 0;

~memimage() {
free_instructions(text_seg, (text_top - TEXT_BOT) / BYTES_PER_WORD);
free_instructions(k_text_seg, (k_text_top - K_TEXT_BOT) / BYTES_PER_WORD);
if (text_seg)
free(text_seg);
if (text_prof)
free(text_prof);
if (data_seg)
free(data_seg);
if (stack_seg)
free(stack_seg);
if (special_seg)
free(special_seg);
if (k_text_seg)
free(k_text_seg);
if (k_text_prof)
free(k_text_prof);
if (k_data_seg)
free(k_data_seg);

}
} mem_image_t;

#endif
#endif
Loading

0 comments on commit 1907687

Please sign in to comment.