9os

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 8be45eb61d70f166d9863d2685a51f113d65aa0e
parent 6e2d2bb546697770a84c56da2c52aceecd3ac571
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Sun, 27 Sep 2020 14:39:54 +0200

os9: Make alloc() a generic function

This function be generic and we can have only one implementation.

Change-Id: I38c969130882e72518a63f39070a929439bc0fff

Diffstat:
Minclude/os9/os9.h | 9+++++----
Msrc/os9/Makefile | 1+
Asrc/os9/alloc.c | 34++++++++++++++++++++++++++++++++++
Msrc/os9/arch/arm64/arch.h | 4----
Msrc/os9/arch/arm64/main.c | 33---------------------------------
Msrc/os9/hosted/main.c | 19-------------------
6 files changed, 40 insertions(+), 60 deletions(-)

diff --git a/include/os9/os9.h b/include/os9/os9.h @@ -23,6 +23,10 @@ #define ENABLE 1 #define DISABLE 0 +#define KiB 1024u +#define MiB (1024u * KiB) +#define GiB (1024ul * MiB) + typedef struct context Context; typedef struct proc Proc; typedef struct task Task; @@ -113,15 +117,12 @@ extern noreturn void halt(void); extern noreturn void panic(const char *msg); extern noreturn void swtch(Context *ctx); extern noreturn void trap(Context *ctx); -extern noreturn void badcmd(int error); -extern void svc(int numsvc); extern int debug(void); extern void idev(void); -extern void dumpregs(Context *ctx, int fd); +extern void *alloc(size_t size); /* architectural functions */ extern Context *getctx(Context *ctx); -extern void *alloc(size_t size); extern void interrupt(int); extern uint8_t inm8(void *addr); extern uint16_t inm16(void *addr); diff --git a/src/os9/Makefile b/src/os9/Makefile @@ -5,6 +5,7 @@ include $(PROJECTDIR)/scripts/rules.mk OBJS =\ dlang.o\ sched.o\ + alloc.o\ dev/builtin.o\ DIRS =\ diff --git a/src/os9/alloc.c b/src/os9/alloc.c @@ -0,0 +1,34 @@ +#include <os9/os9.h> + +#define HEAPSIZ (512 *KiB) + +union bucket { + long long ll; + uintptr_t up; +}; + +void * +alloc(size_t size) +{ + static union bucket heap[HEAPSIZ / sizeof(union bucket)]; + static union bucket *base = heap; + static int lock; + size_t n; + union bucket *bp; + + if (size == 0) { + lock = 1; + return NULL; + } + n = size-1 / sizeof(union bucket) + 1; + + if (lock) + panic("alloc with lock"); + + bp = base; + if (&bp[size] > &heap[HEAPSIZ]) + panic("out of memory"); + base += n; + + return bp; +} diff --git a/src/os9/arch/arm64/arch.h b/src/os9/arch/arm64/arch.h @@ -60,10 +60,6 @@ enum barrier_type { #define T0SZ(x) ((x) << 0) /* MMU */ -#define KiB 1024u -#define MiB (1024u * KiB) -#define GiB (1024ul * MiB) - #define UXN(x) ((pte_t) (x) << 54) #define PXN(x) ((pte_t) (x) << 53) #define CONT(x) ((pte_t) (x) << 52) diff --git a/src/os9/arch/arm64/main.c b/src/os9/arch/arm64/main.c @@ -11,42 +11,9 @@ #include "sysreg.h" #include "arch.h" -#define HEAPSIZ (512 *KiB) - #define DEVICE 7 #define NORMAL 0 -union bucket { - long long ll; - uintptr_t up; -}; - -void * -alloc(size_t size) -{ - static union bucket heap[HEAPSIZ / sizeof(union bucket)]; - static union bucket *base = heap; - static int lock; - size_t n; - union bucket *bp; - - if (size == 0) { - lock = 1; - return NULL; - } - n = size-1 / sizeof(union bucket) + 1; - - if (lock) - panic("alloc with lock"); - - bp = base; - if (&bp[size] > &heap[HEAPSIZ]) - panic("out of memory"); - base += n; - - return bp; -} - static unsigned long long firstmap(Mach *m) { diff --git a/src/os9/hosted/main.c b/src/os9/hosted/main.c @@ -16,25 +16,6 @@ panic(const char *msg) longjmp(recover, 1); } -void * -alloc(size_t size) -{ - static int lock; - void *p; - - if (size == 0) { - lock = 1; - return NULL; - } - - if (lock) - panic("alloc"); - - if ((p = malloc(size)) == NULL) - panic("alloc"); - return p; -} - static void imach(void) {