Compare commits

..

4 Commits

6 changed files with 19 additions and 149 deletions

View File

@ -215,7 +215,7 @@ void intr_handleintr(IntrStackFrame *frame) {
kprintf("ERROR %s, 0x%lX\n", exceptions[frame->trapnum], frame->errnum); kprintf("ERROR %s, 0x%lX\n", exceptions[frame->trapnum], frame->errnum);
intr_dumpframe(frame); intr_dumpframe(frame);
backtrace((BackTraceFrame *)frame->regs.rbp); backtrace((BackTraceFrame *)frame->regs.rbp);
if (frame->cs == UCODE) { if (frame->cs == (UCODE | 0x3)) {
kprintf("killed pid %ld %s\n", PROCS.current->pid, PROCS.current->name); kprintf("killed pid %ld %s\n", PROCS.current->pid, PROCS.current->name);
proc_killself(); proc_killself();
proc_sched((void *)frame); proc_sched((void *)frame);

View File

@ -13,7 +13,7 @@
#define PROC_NAME_MAX 0x100 #define PROC_NAME_MAX 0x100
#define PROC_STACKBLOCKS (1024*1) #define PROC_STACKBLOCKS (256)
#define PROC_STACKSIZE (PROC_STACKBLOCKS * BITMAP_BLOCK_SIZE) #define PROC_STACKSIZE (PROC_STACKBLOCKS * BITMAP_BLOCK_SIZE)
#define PROC_MAX 0x100 // max amount of processes #define PROC_MAX 0x100 // max amount of processes

View File

@ -11,8 +11,9 @@
(lst)->data = umalloc(sizeof(*(lst)->data) * (lst)->capacity); \ (lst)->data = umalloc(sizeof(*(lst)->data) * (lst)->capacity); \
} else { \ } else { \
if ((lst)->count == (lst)->capacity) { \ if ((lst)->count == (lst)->capacity) { \
size_t __oldcap = (lst)->capacity; \
(lst)->capacity *= 2; \ (lst)->capacity *= 2; \
(lst)->data = urealloc((lst)->data, sizeof(*(lst)->data) * (lst)->capacity); \ (lst)->data = urealloc((lst)->data, sizeof(*(lst)->data) * __oldcap, sizeof(*(lst)->data) * (lst)->capacity); \
} \ } \
} \ } \
(lst)->data[(lst)->count++] = (d); \ (lst)->data[(lst)->count++] = (d); \

View File

@ -9,147 +9,7 @@
#include <util/util.h> #include <util/util.h>
#include <assert.h> #include <assert.h>
#define ARENA_SIZE (1<<20)
#define BLOCK_MAGIC 0xDEADBEEF
typedef struct UmArena {
struct UmArena *next;
uintptr_t mem;
uintptr_t base;
size_t cursor;
int64_t balance;
} __attribute__((aligned(16))) UmArena;
typedef struct {
uint32_t magic;
size_t size;
uint8_t data[];
} __attribute__((aligned(16))) UmBlock;
static UmArena *ARENAS = NULL;
UmArena *um_newarena(void) {
size_t arenasize = ARENA_SIZE;
uint8_t *mem = NULL;
int32_t err = mman_map(NULL, arenasize, MMAN_MAP_PF_RW, 0, &mem);
if (mem == NULL || err != E_OK) {
return NULL;
}
UmArena *arena = (UmArena *)mem;
arena->base = (uintptr_t)mem;
arena->mem = ((uintptr_t)mem + sizeof(UmArena) + 15) & ~0xF;
arena->cursor = 0;
arena->balance = 0;
LL_APPEND(ARENAS, arena);
return arena;
}
void *umalloc(size_t size) { void *umalloc(size_t size) {
UmArena *usable = NULL;
size = (size + 15) & ~(size_t)0xF;
UmArena *arena, *arenatmp;
LL_FOREACH_SAFE(ARENAS, arena, arenatmp) {
if (arena->cursor + sizeof(UmBlock) + size <= ARENA_SIZE - ((uintptr_t)arena->mem - arena->base)) {
usable = arena;
break;
}
}
if (usable == NULL) {
usable = um_newarena();
}
if (usable == NULL) {
return NULL;
}
uintptr_t addr = usable->mem + usable->cursor;
UmBlock *block = (UmBlock *)addr;
block->size = size;
block->magic = BLOCK_MAGIC;
usable->cursor += sizeof(UmBlock) + size;
usable->balance += size;
string_memset(block->data, 0, size);
return block->data;
}
void ufree(void *ptr_) {
if (ptr_ == NULL) {
return;
}
uintptr_t ptr = (uintptr_t)ptr_;
UmArena *freeable = NULL;
size_t size = 0;
UmArena *arena, *arenatmp;
LL_FOREACH_SAFE(ARENAS, arena, arenatmp) {
if (ptr >= arena->mem && ptr < arena->mem + arena->cursor) {
UmBlock *block = (UmBlock *)(ptr - sizeof(UmBlock));
if (((uintptr_t)block->data != ptr)
|| (block->magic != BLOCK_MAGIC)
|| (block->size == 0)
|| (block->size > ARENA_SIZE)) {
return;
}
string_memset(block->data, 0xDD, block->size);
block->magic = 0;
arena->balance -= block->size;
ASSERT(arena->balance >= 0, "umalloc: imbalance after free\n");
if (arena->balance == 0) {
LL_REMOVE(ARENAS, arena);
mman_unmap((void *)arena->base);
}
return;
}
}
}
void *urealloc(void *ptr, size_t newsize) {
void *new;
UmBlock *block = (UmBlock *)(ptr - sizeof(UmBlock));
if (block->magic != BLOCK_MAGIC || block->data != ptr) {
goto err;
}
if (!ptr) {
new = umalloc(newsize);
if (!new) {
goto err;
}
} else {
if (block->size < newsize) {
new = umalloc(newsize);
if (!new) {
goto err;
}
string_memcpy(new, ptr, block->size);
ufree(ptr);
} else {
new = ptr;
}
}
return new;
err:
return NULL;
}
void *umallocbig(size_t size) {
size_t allocsz = ALIGN_UP(size, 4096); size_t allocsz = ALIGN_UP(size, 4096);
uint8_t *out; uint8_t *out;
int32_t ret = mman_map(NULL, allocsz, MMAN_MAP_PF_RW, 0, &out); int32_t ret = mman_map(NULL, allocsz, MMAN_MAP_PF_RW, 0, &out);
@ -159,7 +19,18 @@ void *umallocbig(size_t size) {
return out; return out;
} }
void ufreebig(void *addr) { void ufree(void *addr) {
if (addr != NULL) if (addr != NULL)
mman_unmap(addr); mman_unmap(addr);
} }
void *urealloc(void *addr, size_t oldsize, size_t newsize) {
void *tmp = umalloc(newsize);
if (tmp == NULL) {
return NULL;
}
string_memset(tmp, 0, newsize);
string_memcpy(tmp, addr, oldsize);
ufree(addr);
return tmp;
}

View File

@ -6,8 +6,6 @@
void *umalloc(size_t size); void *umalloc(size_t size);
void ufree(void *ptr_); void ufree(void *ptr_);
void *urealloc(void *ptr, size_t newsize); void *urealloc(void *ptr, size_t oldsize, size_t newsize);
void *umallocbig(size_t size);
void ufreebig(void *addr);
#endif // ULIB_UMALLOC_UMALLOC_H_ #endif // ULIB_UMALLOC_UMALLOC_H_

View File

@ -233,11 +233,11 @@ bool interp_runstring(char *string, InterpResult **res, bool interactive) {
goto next; goto next;
} }
} else if (cmdtk->type == TOK_MISC) { } else if (cmdtk->type == TOK_MISC) {
size_t argslen1; size_t argslen1 = 0;
Token *argtk, *argtktmp; Token *argtk, *argtktmp;
LL_FOREACH_SAFE_IDX(cmdtk->next, argtk, argtktmp, argslen1); LL_FOREACH_SAFE_IDX(cmdtk->next, argtk, argtktmp, argslen1);
size_t i; size_t i = 0;
char **args1 = (char **)umalloc(sizeof(char *) * argslen1); char **args1 = (char **)umalloc(sizeof(char *) * argslen1);
LL_FOREACH_SAFE_IDX(cmdtk->next, argtk, argtktmp, i) { LL_FOREACH_SAFE_IDX(cmdtk->next, argtk, argtktmp, i) {
args1[i] = (char *)umalloc(PROC_ARG_MAX); args1[i] = (char *)umalloc(PROC_ARG_MAX);