iPXE
malloc.h
Go to the documentation of this file.
00001 #ifndef _IPXE_MALLOC_H
00002 #define _IPXE_MALLOC_H
00003 
00004 #include <stdint.h>
00005 
00006 /** @file
00007  *
00008  * Dynamic memory allocation
00009  *
00010  */
00011 
00012 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00013 
00014 /*
00015  * Prototypes for the standard functions (malloc() et al) are in
00016  * stdlib.h.  Include <ipxe/malloc.h> only if you need the
00017  * non-standard functions, such as malloc_dma().
00018  *
00019  */
00020 #include <stdlib.h>
00021 #include <ipxe/tables.h>
00022 #include <valgrind/memcheck.h>
00023 
00024 extern size_t freemem;
00025 extern size_t usedmem;
00026 extern size_t maxusedmem;
00027 
00028 extern void * __malloc alloc_memblock ( size_t size, size_t align,
00029                                         size_t offset );
00030 extern void free_memblock ( void *ptr, size_t size );
00031 extern void mpopulate ( void *start, size_t len );
00032 extern void mdumpfree ( void );
00033 
00034 /**
00035  * Allocate memory for DMA
00036  *
00037  * @v size              Requested size
00038  * @v align             Physical alignment
00039  * @v offset            Offset from physical alignment
00040  * @ret ptr             Memory, or NULL
00041  *
00042  * Allocates physically-aligned memory for DMA.
00043  *
00044  * @c align must be a power of two.  @c size may not be zero.
00045  */
00046 static inline void * __malloc malloc_dma_offset ( size_t size,
00047                                                   size_t phys_align,
00048                                                   size_t offset ) {
00049         void * ptr = alloc_memblock ( size, phys_align, offset );
00050         if ( ptr && size )
00051                 VALGRIND_MALLOCLIKE_BLOCK ( ptr, size, 0, 0 );
00052         return ptr;
00053 }
00054 
00055 /**
00056  * Allocate memory for DMA
00057  *
00058  * @v size              Requested size
00059  * @v align             Physical alignment
00060  * @ret ptr             Memory, or NULL
00061  *
00062  * Allocates physically-aligned memory for DMA.
00063  *
00064  * @c align must be a power of two.  @c size may not be zero.
00065  */
00066 static inline void * __malloc malloc_dma ( size_t size, size_t phys_align ) {
00067         return malloc_dma_offset ( size, phys_align, 0 );
00068 }
00069 
00070 /**
00071  * Free memory allocated with malloc_dma()
00072  *
00073  * @v ptr               Memory allocated by malloc_dma(), or NULL
00074  * @v size              Size of memory, as passed to malloc_dma()
00075  *
00076  * Memory allocated with malloc_dma() can only be freed with
00077  * free_dma(); it cannot be freed with the standard free().
00078  *
00079  * If @c ptr is NULL, no action is taken.
00080  */
00081 static inline void free_dma ( void *ptr, size_t size ) {
00082         VALGRIND_FREELIKE_BLOCK ( ptr, 0 );
00083         free_memblock ( ptr, size );
00084 }
00085 
00086 /** A cache discarder */
00087 struct cache_discarder {
00088         /**
00089          * Discard some cached data
00090          *
00091          * @ret discarded       Number of cached items discarded
00092          */
00093         unsigned int ( * discard ) ( void );
00094 };
00095 
00096 /** Cache discarder table */
00097 #define CACHE_DISCARDERS __table ( struct cache_discarder, "cache_discarders" )
00098 
00099 /** Declare a cache discarder */
00100 #define __cache_discarder( cost ) __table_entry ( CACHE_DISCARDERS, cost )
00101 
00102 /** @defgroup cache_cost Cache discarder costs
00103  *
00104  * @{
00105  */
00106 
00107 #define CACHE_CHEAP     01      /**< Items with a low replacement cost */
00108 #define CACHE_NORMAL    02      /**< Items with a normal replacement cost */
00109 #define CACHE_EXPENSIVE 03      /**< Items with a high replacement cost */
00110 
00111 /** @} */
00112 
00113 #endif /* _IPXE_MALLOC_H */