You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

malloc.h 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #ifndef _IPXE_MALLOC_H
  2. #define _IPXE_MALLOC_H
  3. #include <stdint.h>
  4. /** @file
  5. *
  6. * Dynamic memory allocation
  7. *
  8. */
  9. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  10. /*
  11. * Prototypes for the standard functions (malloc() et al) are in
  12. * stdlib.h. Include <ipxe/malloc.h> only if you need the
  13. * non-standard functions, such as malloc_dma().
  14. *
  15. */
  16. #include <stdlib.h>
  17. #include <ipxe/tables.h>
  18. #include <valgrind/memcheck.h>
  19. extern size_t freemem;
  20. extern void * __malloc alloc_memblock ( size_t size, size_t align,
  21. size_t offset );
  22. extern void free_memblock ( void *ptr, size_t size );
  23. extern void mpopulate ( void *start, size_t len );
  24. extern void mdumpfree ( void );
  25. /**
  26. * Allocate memory for DMA
  27. *
  28. * @v size Requested size
  29. * @v align Physical alignment
  30. * @v offset Offset from physical alignment
  31. * @ret ptr Memory, or NULL
  32. *
  33. * Allocates physically-aligned memory for DMA.
  34. *
  35. * @c align must be a power of two. @c size may not be zero.
  36. */
  37. static inline void * __malloc malloc_dma_offset ( size_t size,
  38. size_t phys_align,
  39. size_t offset ) {
  40. void * ptr = alloc_memblock ( size, phys_align, offset );
  41. if ( ptr && size )
  42. VALGRIND_MALLOCLIKE_BLOCK ( ptr, size, 0, 0 );
  43. return ptr;
  44. }
  45. /**
  46. * Allocate memory for DMA
  47. *
  48. * @v size Requested size
  49. * @v align Physical alignment
  50. * @ret ptr Memory, or NULL
  51. *
  52. * Allocates physically-aligned memory for DMA.
  53. *
  54. * @c align must be a power of two. @c size may not be zero.
  55. */
  56. static inline void * __malloc malloc_dma ( size_t size, size_t phys_align ) {
  57. return malloc_dma_offset ( size, phys_align, 0 );
  58. }
  59. /**
  60. * Free memory allocated with malloc_dma()
  61. *
  62. * @v ptr Memory allocated by malloc_dma(), or NULL
  63. * @v size Size of memory, as passed to malloc_dma()
  64. *
  65. * Memory allocated with malloc_dma() can only be freed with
  66. * free_dma(); it cannot be freed with the standard free().
  67. *
  68. * If @c ptr is NULL, no action is taken.
  69. */
  70. static inline void free_dma ( void *ptr, size_t size ) {
  71. VALGRIND_FREELIKE_BLOCK ( ptr, 0 );
  72. free_memblock ( ptr, size );
  73. }
  74. /** A cache discarder */
  75. struct cache_discarder {
  76. /**
  77. * Discard some cached data
  78. *
  79. * @ret discarded Number of cached items discarded
  80. */
  81. unsigned int ( * discard ) ( void );
  82. };
  83. /** Cache discarder table */
  84. #define CACHE_DISCARDERS __table ( struct cache_discarder, "cache_discarders" )
  85. /** Declare a cache discarder */
  86. #define __cache_discarder( cost ) __table_entry ( CACHE_DISCARDERS, cost )
  87. /** @defgroup cache_cost Cache discarder costs
  88. *
  89. * @{
  90. */
  91. #define CACHE_CHEAP 01 /**< Items with a low replacement cost */
  92. #define CACHE_NORMAL 02 /**< Items with a normal replacement cost */
  93. #define CACHE_EXPENSIVE 03 /**< Items with a high replacement cost */
  94. /** @} */
  95. #endif /* _IPXE_MALLOC_H */