Skip to content

Commit

Permalink
Consolidate memory allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
ChinYikMing committed Dec 25, 2023
1 parent 48ed780 commit 9193105
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,18 +195,30 @@ cache_t *cache_create(int size_bits)
cache_size = 1 << size_bits;
for (int i = 0; i < THRESHOLD; i++) {
cache->lists[i] = malloc(sizeof(struct list_head));
if (!cache->lists[i]) {
for (int j = 0; j < i; j++) {
free(cache->lists[j]);
}
return NULL;
}
INIT_LIST_HEAD(cache->lists[i]);
}

cache->map = malloc(sizeof(hashtable_t));
if (!cache->map) {
for (int i = 0; i < THRESHOLD; i++) {
free(cache->lists[i]);
}
free(cache->lists);
free(cache);
return NULL;
}
cache->map->ht_list_head = malloc(cache_size * sizeof(struct hlist_head));
if (!cache->map->ht_list_head) {
free(cache->map);
for (int i = 0; i < THRESHOLD; i++) {
free(cache->lists[i]);
}
free(cache->lists);
free(cache);
return NULL;
Expand Down
3 changes: 3 additions & 0 deletions src/elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* "LICENSE" for information on usage and redistribution of this file.
*/

#include <assert.h>
#include <stdlib.h>
#include <string.h>

Expand Down Expand Up @@ -70,6 +71,7 @@ struct elf_internal {
elf_t *elf_new(void)
{
elf_t *e = malloc(sizeof(elf_t));
assert(e);
e->hdr = NULL;
e->raw_size = 0;
e->symbols = map_init(int, char *, map_cmp_uint);
Expand Down Expand Up @@ -342,6 +344,7 @@ bool elf_open(elf_t *e, const char *input)
/* allocate memory */
free(e->raw_data);
e->raw_data = malloc(e->raw_size);
assert(e->raw_data);

/* read data into memory */
const size_t r = fread(e->raw_data, 1, e->raw_size, f);
Expand Down
7 changes: 6 additions & 1 deletion src/emulate.c
Original file line number Diff line number Diff line change
Expand Up @@ -632,8 +632,10 @@ static void block_translate(riscv_t *rv, block_t *block)
#if RV32_HAS(EXT_C)
|| ir->opcode == rv_insn_cjalr || ir->opcode == rv_insn_cjr
#endif
)
) {
ir->branch_table = calloc(1, sizeof(branch_history_table_t));
assert(ir->branch_table);
}
break;
}

Expand All @@ -659,6 +661,7 @@ static void block_translate(riscv_t *rv, block_t *block)
if (count > 1) { \
ir->opcode = IIF(RW)(rv_insn_fuse4, rv_insn_fuse3); \
ir->fuse = malloc(count * sizeof(opcode_fuse_t)); \
assert(ir->fuse); \
ir->imm2 = count; \
memcpy(ir->fuse, ir, sizeof(opcode_fuse_t)); \
ir->impl = dispatch_table[ir->opcode]; \
Expand Down Expand Up @@ -849,6 +852,7 @@ static void match_pattern(riscv_t *rv, block_t *block)
if (count > 1) {
ir->opcode = rv_insn_fuse1;
ir->fuse = malloc(count * sizeof(opcode_fuse_t));
assert(ir->fuse);
ir->imm2 = count;
memcpy(ir->fuse, ir, sizeof(opcode_fuse_t));
ir->impl = dispatch_table[ir->opcode];
Expand Down Expand Up @@ -887,6 +891,7 @@ static void match_pattern(riscv_t *rv, block_t *block)
}
if (count > 1) {
ir->fuse = malloc(count * sizeof(opcode_fuse_t));
assert(ir->fuse);
memcpy(ir->fuse, ir, sizeof(opcode_fuse_t));
ir->opcode = rv_insn_fuse7;
ir->imm2 = count;
Expand Down
2 changes: 2 additions & 0 deletions src/mpool.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ static void *mpool_extend(mpool_t *mp)
if (!p)
return NULL;
area_t *new_area = malloc(sizeof(area_t));
if (!new_area)
return NULL;
new_area->mapped = p;
new_area->next = NULL;
size_t chunk_count = pool_size / (sizeof(memchunk_t) + mp->chunk_size);
Expand Down
2 changes: 2 additions & 0 deletions src/riscv.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ static void block_map_init(block_map_t *map, const uint8_t bits)
map->block_capacity = 1 << bits;
map->size = 0;
map->map = calloc(map->block_capacity, sizeof(struct block *));
assert(map->map);
}

/* clear all block in the block map */
Expand Down Expand Up @@ -113,6 +114,7 @@ riscv_t *rv_create(const riscv_io_t *io,
assert(io);

riscv_t *rv = calloc(1, sizeof(riscv_t));
assert(rv);

/* copy over the IO interface */
memcpy(&rv->io, io, sizeof(riscv_io_t));
Expand Down

1 comment on commit 9193105

@jserv
Copy link
Contributor

@jserv jserv commented on 9193105 Dec 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmarks

Benchmark suite Current: 9193105 Previous: 406a4dc Ratio
Dhrystone 1670.66 Average DMIPS over 10 runs 1800 Average DMIPS over 10 runs 1.08
Coremark 1519.987 Average iterations/sec over 10 runs 1510.077 Average iterations/sec over 10 runs 0.99

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.