From 9290f1fe99c91ba6c57dec956ff73d1741d81b45 Mon Sep 17 00:00:00 2001 From: Zheng Da Date: Tue, 17 Nov 2009 10:29:23 +0100 Subject: Add DDEKit headers. --- libddekit/include/ddekit/memory.h | 144 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 libddekit/include/ddekit/memory.h (limited to 'libddekit/include/ddekit/memory.h') diff --git a/libddekit/include/ddekit/memory.h b/libddekit/include/ddekit/memory.h new file mode 100644 index 00000000..051a4d9e --- /dev/null +++ b/libddekit/include/ddekit/memory.h @@ -0,0 +1,144 @@ +/* + * \brief Memory subsystem + * \author Thomas Friebel + * \author Christian Helmuth + * \date 2006-11-03 + */ + +#ifndef _ddekit_memory_h +#define _ddekit_memory_h + + +/******************* + ** Slab facility ** + *******************/ + +struct ddekit_slab; + +/** + * Store user pointer in slab cache + * + * \param slab pointer to slab cache + * \param data user pointer + */ +void ddekit_slab_set_data(struct ddekit_slab * slab, void *data); + +/** + * Read user pointer from slab cache + * + * \param slab pointer to slab cache + * + * \return stored user pointer or 0 + */ +void *ddekit_slab_get_data(struct ddekit_slab * slab); + +/** + * Allocate slab in slab cache + * + * \param slab pointer to slab cache + * + * \return pointer to allocated slab + */ +void *ddekit_slab_alloc(struct ddekit_slab * slab); + +/** + * Deallocate slab in slab cache + * + * \param slab pointer to slab cache + * \param objp pointer to allocated slab + */ +void ddekit_slab_free(struct ddekit_slab * slab, void *objp); + +/** + * Setup page cache for all slabs + * + * \param pages maximal number of memory pages + * + * If 'pages' is too low, memory pages may be given back to the memory server + * (dm_phys) and just to be allocated again later. This hits performance (but + * saves memory). Increase 'pages' to avoid this thrashing-like effect. + * + * If the maximal number of unused pages is exceeded, subsequent deallocation + * will be freed at the memory server. This page cache caches pages from all + * slabs. + */ +void ddekit_slab_setup_page_cache(unsigned pages); + +/** + * Destroy slab cache + * + * \param slab pointer to slab cache structure + */ +void ddekit_slab_destroy(struct ddekit_slab * slab); + +/** + * Initialize slab cache + * + * \param size size of cache objects + * \param contiguous make this slab use physically contiguous memory + * + * \return pointer to new slab cache or 0 on error + */ +struct ddekit_slab * ddekit_slab_init(unsigned size, int contiguous); + + +/********************** + ** Memory allocator ** + **********************/ + +/** + * Allocate large memory block + * + * \param size block size + * \return pointer to new memory block + * + * Allocations via this allocator may be slow (because memory servers are + * involved) and should be used only for large (i.e., > page size) blocks. If + * allocations/deallocations are relatively dynamic this may not be what you + * want. + * + * Allocated blocks have valid virt->phys mappings and are physically + * contiguous. + */ +void *ddekit_large_malloc(int size); + +/** + * Free large memory block + * + * \param p pointer to memory block + */ +void ddekit_large_free(void *p); + +/** FIXME + * contig_malloc() is the lowest-level allocator interface one could implement. + * we should consider to provide vmalloc() too. */ +void *ddekit_contig_malloc( + unsigned long size, + unsigned long low, unsigned long high, + unsigned long alignment, unsigned long boundary +); + + +/***************************** + ** Simple memory allocator ** + *****************************/ + +/** + * Allocate memory block via simple allocator + * + * \param size block size + * \return pointer to new memory block + * + * The blocks allocated via this allocator CANNOT be used for DMA or other + * device operations, i.e., there exists no virt->phys mapping. + */ +void *ddekit_simple_malloc(unsigned size); + +/** + * Free memory block via simple allocator + * + * \param p pointer to memory block + */ +void ddekit_simple_free(void *p); + +#endif -- cgit v1.2.3 From 9341952a7cea0d39b871ecdb6ac153fc820524c8 Mon Sep 17 00:00:00 2001 From: Zheng Da Date: Mon, 7 Dec 2009 02:25:32 +0100 Subject: Make ddekit_simple_malloc/free inline. --- libddekit/Makefile | 2 +- libddekit/include/ddekit/memory.h | 13 +++++++++-- libddekit/malloc.c | 49 --------------------------------------- libddekit/memory.c | 3 +++ 4 files changed, 15 insertions(+), 52 deletions(-) delete mode 100644 libddekit/malloc.c (limited to 'libddekit/include/ddekit/memory.h') diff --git a/libddekit/Makefile b/libddekit/Makefile index 018d67c9..f2e52f25 100644 --- a/libddekit/Makefile +++ b/libddekit/Makefile @@ -19,7 +19,7 @@ dir := libddekit makemode := library libname = libddekit -SRCS= condvar.c init.c interrupt.c lock.c malloc.c memory.c \ +SRCS= condvar.c init.c interrupt.c lock.c memory.c \ pci.c pgtab.c printf.c resources.c list.c \ thread.c timer.c kmem.c LCLHDRS = include/ddekit/condvar.h include/ddekit/lock.h \ diff --git a/libddekit/include/ddekit/memory.h b/libddekit/include/ddekit/memory.h index 051a4d9e..2c573d8f 100644 --- a/libddekit/include/ddekit/memory.h +++ b/libddekit/include/ddekit/memory.h @@ -123,6 +123,9 @@ void *ddekit_contig_malloc( ** Simple memory allocator ** *****************************/ +#include +#include "ddekit/inline.h" + /** * Allocate memory block via simple allocator * @@ -132,13 +135,19 @@ void *ddekit_contig_malloc( * The blocks allocated via this allocator CANNOT be used for DMA or other * device operations, i.e., there exists no virt->phys mapping. */ -void *ddekit_simple_malloc(unsigned size); +static INLINE void *ddekit_simple_malloc(unsigned size) +{ + return malloc (size); +} /** * Free memory block via simple allocator * * \param p pointer to memory block */ -void ddekit_simple_free(void *p); +static INLINE void ddekit_simple_free(void *p) +{ + free (p); +} #endif diff --git a/libddekit/malloc.c b/libddekit/malloc.c deleted file mode 100644 index a30cd7b7..00000000 --- a/libddekit/malloc.c +++ /dev/null @@ -1,49 +0,0 @@ -/* - Copyright (C) 2009 Free Software Foundation, Inc. - Written by Zheng Da. - - This file is part of the GNU Hurd. - - The GNU Hurd is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2, or (at your option) - any later version. - - The GNU Hurd is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with the GNU Hurd; see the file COPYING. If not, write to - the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ - -/** - * Allocate memory block via simple allocator - * - * \param size block size - * \return pointer to new memory block - * - * The blocks allocated via this allocator CANNOT be used for DMA or other - * device operations, i.e., there exists no virt->phys mapping. - * - * Each chunk stores its size in the first word for free() to work. - */ - -#include - -void *ddekit_simple_malloc(unsigned size) -{ - return malloc (size); -} - - -/** - * Free memory block via simple allocator - * - * \param p pointer to memory block - */ -void ddekit_simple_free(void *p) -{ - free (p); -} diff --git a/libddekit/memory.c b/libddekit/memory.c index 1ffb15b0..781a4bae 100644 --- a/libddekit/memory.c +++ b/libddekit/memory.c @@ -160,6 +160,9 @@ struct ddekit_slab * ddekit_slab_init(unsigned size, int contiguous) * Free large block of memory * * This is no useful for allocation < page size. + * + * TODO The freed memory can be cached and will be still accessible ( + * no page fault when accessed). I hope it won't caused any troubles. */ void ddekit_large_free(void *objp) { -- cgit v1.2.3 From 0ea79b053d4a853d7bdd3363ccb6c132b2f7ee5c Mon Sep 17 00:00:00 2001 From: Zheng Da Date: Tue, 29 Dec 2009 20:48:00 +0100 Subject: Don't include C library headers files in our header files. --- libddekit/Makefile | 2 +- libddekit/c_headers.h | 7 ++++++ libddekit/include/ddekit/memory.h | 13 ++--------- libddekit/include/ddekit/panic.h | 2 +- libddekit/include/ddekit/printf.h | 2 +- libddekit/malloc.c | 49 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 61 insertions(+), 14 deletions(-) create mode 100644 libddekit/c_headers.h create mode 100644 libddekit/malloc.c (limited to 'libddekit/include/ddekit/memory.h') diff --git a/libddekit/Makefile b/libddekit/Makefile index f2e52f25..018d67c9 100644 --- a/libddekit/Makefile +++ b/libddekit/Makefile @@ -19,7 +19,7 @@ dir := libddekit makemode := library libname = libddekit -SRCS= condvar.c init.c interrupt.c lock.c memory.c \ +SRCS= condvar.c init.c interrupt.c lock.c malloc.c memory.c \ pci.c pgtab.c printf.c resources.c list.c \ thread.c timer.c kmem.c LCLHDRS = include/ddekit/condvar.h include/ddekit/lock.h \ diff --git a/libddekit/c_headers.h b/libddekit/c_headers.h new file mode 100644 index 00000000..6141e900 --- /dev/null +++ b/libddekit/c_headers.h @@ -0,0 +1,7 @@ +#ifndef __C_HEADERS_H__ +#define __C_HEADERS_H__ + +#include +#include + +#endif diff --git a/libddekit/include/ddekit/memory.h b/libddekit/include/ddekit/memory.h index 2c573d8f..051a4d9e 100644 --- a/libddekit/include/ddekit/memory.h +++ b/libddekit/include/ddekit/memory.h @@ -123,9 +123,6 @@ void *ddekit_contig_malloc( ** Simple memory allocator ** *****************************/ -#include -#include "ddekit/inline.h" - /** * Allocate memory block via simple allocator * @@ -135,19 +132,13 @@ void *ddekit_contig_malloc( * The blocks allocated via this allocator CANNOT be used for DMA or other * device operations, i.e., there exists no virt->phys mapping. */ -static INLINE void *ddekit_simple_malloc(unsigned size) -{ - return malloc (size); -} +void *ddekit_simple_malloc(unsigned size); /** * Free memory block via simple allocator * * \param p pointer to memory block */ -static INLINE void ddekit_simple_free(void *p) -{ - free (p); -} +void ddekit_simple_free(void *p); #endif diff --git a/libddekit/include/ddekit/panic.h b/libddekit/include/ddekit/panic.h index 11c46ebb..f036ab3e 100644 --- a/libddekit/include/ddekit/panic.h +++ b/libddekit/include/ddekit/panic.h @@ -3,7 +3,7 @@ /** \defgroup DDEKit_util */ -#include +#include "c_headers.h" /** Panic - print error message and enter the kernel debugger. * \ingroup DDEKit_util diff --git a/libddekit/include/ddekit/printf.h b/libddekit/include/ddekit/printf.h index 35b0dfa1..aa086c71 100644 --- a/libddekit/include/ddekit/printf.h +++ b/libddekit/include/ddekit/printf.h @@ -1,7 +1,7 @@ #ifndef _ddekit_print_h #define _ddekit_print_h -#include +#include "c_headers.h" /** Print message. * \ingroup DDEKit_util diff --git a/libddekit/malloc.c b/libddekit/malloc.c new file mode 100644 index 00000000..a30cd7b7 --- /dev/null +++ b/libddekit/malloc.c @@ -0,0 +1,49 @@ +/* + Copyright (C) 2009 Free Software Foundation, Inc. + Written by Zheng Da. + + This file is part of the GNU Hurd. + + The GNU Hurd is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + The GNU Hurd is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with the GNU Hurd; see the file COPYING. If not, write to + the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ + +/** + * Allocate memory block via simple allocator + * + * \param size block size + * \return pointer to new memory block + * + * The blocks allocated via this allocator CANNOT be used for DMA or other + * device operations, i.e., there exists no virt->phys mapping. + * + * Each chunk stores its size in the first word for free() to work. + */ + +#include + +void *ddekit_simple_malloc(unsigned size) +{ + return malloc (size); +} + + +/** + * Free memory block via simple allocator + * + * \param p pointer to memory block + */ +void ddekit_simple_free(void *p) +{ + free (p); +} -- cgit v1.2.3