From 6d4e37cf42a16c1e0b2393efe4902a2030907dbe Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Thu, 18 Jan 2007 20:11:04 +0000 Subject: Move include/malloc.h to include/gpxe/malloc.h, since everything in there is now gPXE-specific. (The standard malloc() et al have been in stdlib.h for a while). Add free memory counter. --- src/core/heap.c | 2 +- src/core/malloc.c | 11 ++++++++- src/include/gpxe/malloc.h | 57 +++++++++++++++++++++++++++++++++++++++++++++++ src/include/malloc.h | 55 --------------------------------------------- src/net/pkbuff.c | 2 +- 5 files changed, 69 insertions(+), 58 deletions(-) create mode 100644 src/include/gpxe/malloc.h delete mode 100644 src/include/malloc.h diff --git a/src/core/heap.c b/src/core/heap.c index f2c7e80b6..4afd270b1 100644 --- a/src/core/heap.c +++ b/src/core/heap.c @@ -1,4 +1,4 @@ -#include +#include #include /** diff --git a/src/core/malloc.c b/src/core/malloc.c index db2b500c9..448080b4f 100644 --- a/src/core/malloc.c +++ b/src/core/malloc.c @@ -22,7 +22,7 @@ #include #include #include -#include +#include /** @file * @@ -69,6 +69,9 @@ struct autosized_block { /** List of free memory blocks */ static LIST_HEAD ( free_blocks ); +/** Total amount of free memory */ +size_t freemem; + /** * Allocate a memory block * @@ -134,6 +137,9 @@ void * alloc_memblock ( size_t size, size_t align ) { */ if ( pre_size < MIN_MEMBLOCK_SIZE ) list_del ( &pre->list ); + /* Update total free memory */ + freemem -= size; + /* Return allocated block */ DBG ( "Allocated [%p,%p)\n", block, ( ( ( void * ) block ) + size ) ); return block; @@ -206,6 +212,9 @@ void free_memblock ( void *ptr, size_t size ) { freeing->size += block->size; list_del ( &block->list ); } + + /* Update free memory counter */ + freemem += size; } /** diff --git a/src/include/gpxe/malloc.h b/src/include/gpxe/malloc.h new file mode 100644 index 000000000..0e18f6a35 --- /dev/null +++ b/src/include/gpxe/malloc.h @@ -0,0 +1,57 @@ +#ifndef _GPXE_MALLOC_H +#define _GPXE_MALLOC_H + +#include + +/** @file + * + * Dynamic memory allocation + * + */ + +/* + * Prototypes for the standard functions (malloc() et al) are in + * stdlib.h. Include only if you need the + * non-standard functions, such as malloc_dma(). + * + */ +#include + +extern size_t freemem; + +extern void * alloc_memblock ( size_t size, size_t align ); +extern void free_memblock ( void *ptr, size_t size ); +extern void mpopulate ( void *start, size_t len ); +extern void mdumpfree ( void ); + +/** + * Allocate memory for DMA + * + * @v size Requested size + * @v align Physical alignment + * @ret ptr Memory, or NULL + * + * Allocates physically-aligned memory for DMA. + * + * @c align must be a power of two. @c size may not be zero. + */ +static inline void * malloc_dma ( size_t size, size_t phys_align ) { + return alloc_memblock ( size, phys_align ); +} + +/** + * Free memory allocated with malloc_dma() + * + * @v ptr Memory allocated by malloc_dma(), or NULL + * @v size Size of memory, as passed to malloc_dma() + * + * Memory allocated with malloc_dma() can only be freed with + * free_dma(); it cannot be freed with the standard free(). + * + * If @c ptr is NULL, no action is taken. + */ +static inline void free_dma ( void *ptr, size_t size ) { + free_memblock ( ptr, size ); +} + +#endif /* _GPXE_MALLOC_H */ diff --git a/src/include/malloc.h b/src/include/malloc.h deleted file mode 100644 index b14ec78c8..000000000 --- a/src/include/malloc.h +++ /dev/null @@ -1,55 +0,0 @@ -#ifndef _MALLOC_H -#define _MALLOC_H - -#include - -/** @file - * - * Dynamic memory allocation - * - */ - -/* - * Prototypes for the standard functions (malloc() et al) are in - * stdlib.h. Include only if you need the non-standard - * functions, such as malloc_dma(). - * - */ -#include - -extern void * alloc_memblock ( size_t size, size_t align ); -extern void free_memblock ( void *ptr, size_t size ); -extern void mpopulate ( void *start, size_t len ); -extern void mdumpfree ( void ); - -/** - * Allocate memory for DMA - * - * @v size Requested size - * @v align Physical alignment - * @ret ptr Memory, or NULL - * - * Allocates physically-aligned memory for DMA. - * - * @c align must be a power of two. @c size may not be zero. - */ -static inline void * malloc_dma ( size_t size, size_t phys_align ) { - return alloc_memblock ( size, phys_align ); -} - -/** - * Free memory allocated with malloc_dma() - * - * @v ptr Memory allocated by malloc_dma(), or NULL - * @v size Size of memory, as passed to malloc_dma() - * - * Memory allocated with malloc_dma() can only be freed with - * free_dma(); it cannot be freed with the standard free(). - * - * If @c ptr is NULL, no action is taken. - */ -static inline void free_dma ( void *ptr, size_t size ) { - free_memblock ( ptr, size ); -} - -#endif /* _MALLOC_H */ diff --git a/src/net/pkbuff.c b/src/net/pkbuff.c index ac3088301..63bc95f0f 100644 --- a/src/net/pkbuff.c +++ b/src/net/pkbuff.c @@ -17,7 +17,7 @@ */ #include -#include +#include #include /** @file -- cgit v1.2.3-55-g7522