9os

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

commit b0f359791a7e87e08f84505af359dbabbac58018
parent 2530b27878b32447a48cd62f914762f3400521c6
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Sun, 25 Oct 2020 20:54:16 +0100

os9: Fix allocb() and alloc()

Change-Id: I736335207f7cbcf62c7779836cb94618e9b15594

Diffstat:
Minclude/os9/os9.h | 3---
Msrc/os9/alloc.c | 44++++++++++++++++++++++++++++----------------
Msrc/os9/hosted/main.c | 2--
3 files changed, 28 insertions(+), 21 deletions(-)

diff --git a/include/os9/os9.h b/include/os9/os9.h @@ -20,9 +20,6 @@ #define NR_MPOINTS 4 #define NR_CHANS 20 -#define NR_BUFFERS 512 -#define HEAPSIZ (4 * PAGESIZE) - #define KiB 1024u #define MiB (1024u * KiB) #define GiB (1024ul * MiB) diff --git a/src/os9/alloc.c b/src/os9/alloc.c @@ -2,28 +2,37 @@ #include <string.h> +#ifndef NR_BUFFERS +#define NR_BUFFERS 512 +#endif + +#ifndef HEAPSIZ +#define HEAPSIZ 4 +#endif + union bucket { long long ll; uintptr_t up; }; -extern char buffertab[NR_BUFFERS][PAGESIZE]; +char buffertab[NR_BUFFERS][PAGESIZE]; void * allocb(int n) { - void *addr; + void *addr = NULL; static unsigned long npages; static mutex_t m; lock(&m); - if (npages + n>= NR_BUFFERS) - panic("out of memory"); + if (n >= NR_BUFFERS - npages) + goto end; addr= buffertab[npages]; npages += n; +end: unlock(&m); - return memset(addr, 0, PAGESIZE); + return addr; } void * @@ -31,27 +40,30 @@ alloc(size_t size) { static int lockalloc; size_t n; - union bucket *bp; - static union bucket *base, *heap; + static size_t used; + static union bucket *heap; static mutex_t m; + union bucket *bp = NULL; + lock(&m); if (size == 0) { lockalloc = 1; - return NULL; + goto end; } if (lockalloc) - panic("alloc with lock"); + goto end; - lock(&m); - if (!base) - base = heap = allocb(HEAPSIZ/ PAGESIZE); + if (!heap) + heap = allocb(HEAPSIZ); n = (size-1) / sizeof(union bucket) + 1; - bp = base; - if (&bp[size] > &heap[HEAPSIZ]) - panic("out of memory"); - base += n; + size = n * sizeof(union bucket); + if (used > SIZE_MAX - size) + goto end; + bp = heap + used; + used += size; +end: unlock(&m); return bp; diff --git a/src/os9/hosted/main.c b/src/os9/hosted/main.c @@ -11,8 +11,6 @@ noreturn void longjmp(jmp_buf env, int val); Chan *console; -char buffertab[NR_BUFFERS][PAGESIZE]; - Mach mach; int