mirror of
https://github.com/ipxe/ipxe
synced 2025-12-19 19:49:45 +03:00
Updated memory allocator to improve support for unaligned or partially
aligned blocks. Moved header to include/malloc.h, since we now also provide the POSIX-like malloc()/free() pair. Not yet tested.
This commit is contained in:
@@ -1,36 +0,0 @@
|
||||
#ifndef _GPXE_MALLOC_H
|
||||
#define _GPXE_MALLOC_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/** @file
|
||||
*
|
||||
* Memory allocation
|
||||
*
|
||||
*/
|
||||
|
||||
extern void * gmalloc ( size_t size );
|
||||
extern void gfree ( void *ptr, size_t size );
|
||||
extern void gmpopulate ( void *start, size_t len );
|
||||
|
||||
/**
|
||||
* Allocate cleared memory
|
||||
*
|
||||
* @v size Requested size
|
||||
* @ret ptr Allocated memory
|
||||
*
|
||||
* Allocate memory as per gmalloc(), and zero it.
|
||||
*
|
||||
* Note that gmalloc() and gcalloc() are identical, in the interests
|
||||
* of reducing code size. Callers should not, however, rely on
|
||||
* gmalloc() clearing memory, since this behaviour may change in
|
||||
* future.
|
||||
*/
|
||||
static inline void * gcalloc ( size_t size ) {
|
||||
return gmalloc ( size );
|
||||
}
|
||||
|
||||
/* Debug function; not compiled in by default */
|
||||
void gdumpfree ( void );
|
||||
|
||||
#endif /* _GPXE_MALLOC_H */
|
||||
66
src/include/malloc.h
Normal file
66
src/include/malloc.h
Normal file
@@ -0,0 +1,66 @@
|
||||
#ifndef _MALLOC_H
|
||||
#define _MALLOC_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/** @file
|
||||
*
|
||||
* Dynamic memory allocation
|
||||
*
|
||||
*/
|
||||
|
||||
extern void * alloc_memblock ( size_t size, size_t align );
|
||||
extern void free_memblock ( void *ptr, size_t size );
|
||||
extern void * malloc ( size_t size );
|
||||
extern void free ( void *ptr );
|
||||
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 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Allocate cleared memory
|
||||
*
|
||||
* @v nmemb Number of members
|
||||
* @v size Size of each member
|
||||
* @ret ptr Allocated memory
|
||||
*
|
||||
* Allocate memory as per malloc(), and zero it.
|
||||
*
|
||||
* Note that malloc() and calloc() are identical, in the interests of
|
||||
* reducing code size. Callers should not, however, rely on malloc()
|
||||
* clearing memory, since this behaviour may change in future.
|
||||
*/
|
||||
static inline void * calloc ( size_t nmemb, size_t size ) {
|
||||
return malloc ( nmemb * size );
|
||||
}
|
||||
|
||||
#endif /* _MALLOC_H */
|
||||
Reference in New Issue
Block a user