Skip to content

Commit

Permalink
Integrate TLSF memory pool
Browse files Browse the repository at this point in the history
  • Loading branch information
qwe661234 committed Apr 9, 2023
1 parent 57069ea commit 6936a4f
Show file tree
Hide file tree
Showing 4 changed files with 978 additions and 15 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ OBJS := \
emulate.o \
riscv.o \
elf.o \
tlsf.o \
cache.o \
$(OBJS_EXT) \
main.o
Expand Down
28 changes: 13 additions & 15 deletions src/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,27 @@
#include <string.h>

#include "io.h"
#include "tlsf.h"

#define POOL_SIZE 1 << 22

static const uint32_t mask_lo = 0xffff;
static const uint32_t mask_hi = ~(0xffff);

char pool[POOL_SIZE];

memory_t *memory_new()
{
memory_t *m = malloc(sizeof(memory_t));
memset(m->chunks, 0, sizeof(m->chunks));
init_memory_pool(POOL_SIZE, pool);
memory_t *m = calloc_ex(1, sizeof(memory_t), pool);
return m;
}

void memory_delete(memory_t *m)
{
if (!m)
return;

for (uint32_t i = 0; i < (sizeof(m->chunks) / sizeof(chunk_t *)); i++) {
chunk_t *c = m->chunks[i];
if (c)
free(c);
}
free(m);
destroy_memory_pool(pool);
}

void memory_read(memory_t *m, uint8_t *dst, uint32_t addr, uint32_t size)
Expand Down Expand Up @@ -109,7 +108,7 @@ void memory_write(memory_t *m, uint32_t addr, const uint8_t *src, uint32_t size)
uint32_t x = p >> 16;
chunk_t *c = m->chunks[x];
if (!c) {
c = calloc(1, sizeof(chunk_t));
c = malloc_ex(sizeof(chunk_t), pool);
m->chunks[x] = c;
}
c->data[p & 0xffff] = src[i];
Expand All @@ -121,7 +120,7 @@ void memory_write_w(memory_t *m, uint32_t addr, const uint8_t *src)
const uint32_t addr_lo = addr & mask_lo, addr_hi = addr >> 16;
chunk_t *c = m->chunks[addr_hi];
if (unlikely(!c)) {
c = calloc(1, sizeof(chunk_t));
c = calloc_ex(1, sizeof(chunk_t), pool);
m->chunks[addr_hi] = c;
}
*(uint32_t *) (c->data + addr_lo) = *(const uint32_t *) src;
Expand All @@ -132,7 +131,7 @@ void memory_write_s(memory_t *m, uint32_t addr, const uint8_t *src)
const uint32_t addr_lo = addr & mask_lo, addr_hi = addr >> 16;
chunk_t *c = m->chunks[addr_hi];
if (unlikely(!c)) {
c = calloc(1, sizeof(chunk_t));
c = calloc_ex(1, sizeof(chunk_t), pool);
m->chunks[addr_hi] = c;
}
*(uint16_t *) (c->data + addr_lo) = *(const uint16_t *) src;
Expand All @@ -143,7 +142,7 @@ void memory_write_b(memory_t *m, uint32_t addr, const uint8_t *src)
const uint32_t addr_lo = addr & mask_lo, addr_hi = addr >> 16;
chunk_t *c = m->chunks[addr_hi];
if (unlikely(!c)) {
c = calloc(1, sizeof(chunk_t));
c = calloc_ex(1, sizeof(chunk_t), pool);
m->chunks[addr_hi] = c;
}
c->data[addr_lo] = src[0];
Expand All @@ -156,8 +155,7 @@ void memory_fill(memory_t *m, uint32_t addr, uint32_t size, uint8_t val)
uint32_t x = p >> 16;
chunk_t *c = m->chunks[x];
if (!c) {
c = malloc(sizeof(chunk_t));
memset(c->data, 0, sizeof(c->data));
c = calloc_ex(1, sizeof(chunk_t), pool);
m->chunks[x] = c;
}
c->data[p & 0xffff] = val;
Expand Down
Loading

0 comments on commit 6936a4f

Please sign in to comment.