voron

experimental ARM OS
git clone git://git.2f30.org/voron
Log | Files | Refs | README | LICENSE

commit a52444902a369f3d17dfe45d9f3fca6a7415f340
parent 4965eb05948f3bd624b854b4656765a9959c33f2
Author: sin <sin@2f30.org>
Date:   Wed, 17 Jul 2013 10:57:06 +0100

Rename arenas to pool

Diffstat:
Dinclude/arenas.h | 11-----------
Ainclude/pool.h | 11+++++++++++
Dkernel/arenas.c | 107-------------------------------------------------------------------------------
Akernel/pool.c | 107+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 118 insertions(+), 118 deletions(-)

diff --git a/include/arenas.h b/include/arenas.h @@ -1,11 +0,0 @@ -#ifndef __ARENAS_H -#define __ARENAS_H - -typedef int arena_id; - -struct arenas *arenas_init(void); -void arenas_free(struct arenas *arenas); -void *arena_alloc(struct arenas *arenas, size_t n, arena_id id); -void arena_free(struct arenas *arenas, arena_id id); - -#endif /* __ARENAS_H */ diff --git a/include/pool.h b/include/pool.h @@ -0,0 +1,11 @@ +#ifndef __POOL_H +#define __POOL_H + +typedef int arena_id; + +struct pool *pool_init(void); +void pool_free(struct pool *pool); +void *arena_alloc(struct pool *pool, size_t n, arena_id id); +void arena_free(struct pool *pool, arena_id id); + +#endif /* __POOL_H */ diff --git a/kernel/arenas.c b/kernel/arenas.c @@ -1,107 +0,0 @@ -#include <kernel.h> -#include <mmu.h> -#include <alloc.h> -#include <spinlock.h> -#include <arenas.h> - -struct arena { - arena_id id; - void *mem; - size_t siz; - int used; -}; - -struct arenas { - struct arena *arena; - size_t siz; -}; - -static int ndebug = 0; -static spinlock_t arenas_lock = SPINLOCK_INIT; - -struct arenas * -arenas_init(void) -{ - struct arenas *arenas; - - arenas = kmalloc(sizeof(struct arenas)); - if (!arenas) - return NULL; - arenas->arena = NULL; - arenas->siz = 0; - return arenas; -} - -void -arenas_free(struct arenas *arenas) -{ - struct arena *arena; - size_t i; - - spinlock_lock(&arenas_lock); - for (i = 0; i < arenas->siz; i++) { - arena = &arenas->arena[i]; - kfree(arena->mem); - } - kfree(arenas->arena); - kfree(arenas); - spinlock_unlock(&arenas_lock); -} - -void * -arena_alloc(struct arenas *arenas, size_t n, arena_id id) -{ - struct arena *tmp; - struct arena *arena; - size_t i; - - spinlock_lock(&arenas_lock); - for (i = 0; i < arenas->siz; i++) { - arena = &arenas->arena[i]; - if (arena->id == id) { - if (ndebug > 0) - kprintf("Found possible unused arena:%d\n", arena->id); - if (!arena->used && arena->siz >= n) { - if (ndebug > 0) - kprintf("Found unused arena:%d of size %zu bytes\n", - arena->id, arena->siz); - arena->used = 1; - spinlock_unlock(&arenas_lock); - return arena->mem; - } - } - } - - if (ndebug > 0) - kprintf("Allocating new arena:%d of size %zu bytes\n", - id, n); - - tmp = krealloc(arenas->arena, sizeof(struct arena) * (arenas->siz + 1)); - if (!tmp) - panic("out of memory"); - arenas->arena = tmp; - arena = &arenas->arena[arenas->siz]; - arena->id = id; - arena->mem = kmalloc(n); - if (!arena->mem) - panic("out of memory"); - arena->siz = n; - arenas->siz++; - spinlock_unlock(&arenas_lock); - return arena->mem; -} - -void -arena_free(struct arenas *arenas, arena_id id) -{ - struct arena *arena; - size_t i; - - spinlock_lock(&arenas_lock); - for (i = 0; i < arenas->siz; i++) { - arena = &arenas->arena[i]; - if (arena->id == id) - arena->used = 0; - } - spinlock_unlock(&arenas_lock); -} diff --git a/kernel/pool.c b/kernel/pool.c @@ -0,0 +1,107 @@ +#include <kernel.h> +#include <mmu.h> +#include <alloc.h> +#include <spinlock.h> +#include <pool.h> + +struct arena { + arena_id id; + void *mem; + size_t siz; + int used; +}; + +struct pool { + struct arena *arena; + size_t siz; +}; + +static int ndebug = 0; +static spinlock_t pool_lock = SPINLOCK_INIT; + +struct pool * +pool_init(void) +{ + struct pool *pool; + + pool = kmalloc(sizeof(struct pool)); + if (!pool) + return NULL; + pool->arena = NULL; + pool->siz = 0; + return pool; +} + +void +pool_free(struct pool *pool) +{ + struct arena *arena; + size_t i; + + spinlock_lock(&pool_lock); + for (i = 0; i < pool->siz; i++) { + arena = &pool->arena[i]; + kfree(arena->mem); + } + kfree(pool->arena); + kfree(pool); + spinlock_unlock(&pool_lock); +} + +void * +arena_alloc(struct pool *pool, size_t n, arena_id id) +{ + struct arena *tmp; + struct arena *arena; + size_t i; + + spinlock_lock(&pool_lock); + for (i = 0; i < pool->siz; i++) { + arena = &pool->arena[i]; + if (arena->id == id) { + if (ndebug > 0) + kprintf("Found possible unused arena:%d\n", arena->id); + if (!arena->used && arena->siz >= n) { + if (ndebug > 0) + kprintf("Found unused arena:%d of size %zu bytes\n", + arena->id, arena->siz); + arena->used = 1; + spinlock_unlock(&pool_lock); + return arena->mem; + } + } + } + + if (ndebug > 0) + kprintf("Allocating new arena:%d of size %zu bytes\n", + id, n); + + tmp = krealloc(pool->arena, sizeof(struct arena) * (pool->siz + 1)); + if (!tmp) + panic("out of memory"); + pool->arena = tmp; + arena = &pool->arena[pool->siz]; + arena->id = id; + arena->mem = kmalloc(n); + if (!arena->mem) + panic("out of memory"); + arena->siz = n; + pool->siz++; + spinlock_unlock(&pool_lock); + return arena->mem; +} + +void +arena_free(struct pool *pool, arena_id id) +{ + struct arena *arena; + size_t i; + + spinlock_lock(&pool_lock); + for (i = 0; i < pool->siz; i++) { + arena = &pool->arena[i]; + if (arena->id == id) + arena->used = 0; + } + spinlock_unlock(&pool_lock); +}