Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

malloc.h 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 size_t usedmem;
  21. extern size_t maxusedmem;
  22. extern void * __malloc alloc_memblock ( size_t size, size_t align,
  23. size_t offset );
  24. extern void free_memblock ( void *ptr, size_t size );
  25. extern void mpopulate ( void *start, size_t len );
  26. extern void mdumpfree ( void );
  27. /**
  28. * Allocate memory for DMA
  29. *
  30. * @v size Requested size
  31. * @v align Physical alignment
  32. * @v offset Offset from physical alignment
  33. * @ret ptr Memory, or NULL
  34. *
  35. * Allocates physically-aligned memory for DMA.
  36. *
  37. * @c align must be a power of two. @c size may not be zero.
  38. */
  39. static inline void * __malloc malloc_dma_offset ( size_t size,
  40. size_t phys_align,
  41. size_t offset ) {
  42. void * ptr = alloc_memblock ( size, phys_align, offset );
  43. if ( ptr && size )
  44. VALGRIND_MALLOCLIKE_BLOCK ( ptr, size, 0, 0 );
  45. return ptr;
  46. }
  47. /**
  48. * Allocate memory for DMA
  49. *
  50. * @v size Requested size
  51. * @v align Physical alignment
  52. * @ret ptr Memory, or NULL
  53. *
  54. * Allocates physically-aligned memory for DMA.
  55. *
  56. * @c align must be a power of two. @c size may not be zero.
  57. */
  58. static inline void * __malloc malloc_dma ( size_t size, size_t phys_align ) {
  59. return malloc_dma_offset ( size, phys_align, 0 );
  60. }
  61. /**
  62. * Free memory allocated with malloc_dma()
  63. *
  64. * @v ptr Memory allocated by malloc_dma(), or NULL
  65. * @v size Size of memory, as passed to malloc_dma()
  66. *
  67. * Memory allocated with malloc_dma() can only be freed with
  68. * free_dma(); it cannot be freed with the standard free().
  69. *
  70. * If @c ptr is NULL, no action is taken.
  71. */
  72. static inline void free_dma ( void *ptr, size_t size ) {
  73. VALGRIND_FREELIKE_BLOCK ( ptr, 0 );
  74. free_memblock ( ptr, size );
  75. }
  76. /** A cache discarder */
  77. struct cache_discarder {
  78. /**
  79. * Discard some cached data
  80. *
  81. * @ret discarded Number of cached items discarded
  82. */
  83. unsigned int ( * discard ) ( void );
  84. };
  85. /** Cache discarder table */
  86. #define CACHE_DISCARDERS __table ( struct cache_discarder, "cache_discarders" )
  87. /** Declare a cache discarder */
  88. #define __cache_discarder( cost ) __table_entry ( CACHE_DISCARDERS, cost )
  89. /** @defgroup cache_cost Cache discarder costs
  90. *
  91. * @{
  92. */
  93. #define CACHE_CHEAP 01 /**< Items with a low replacement cost */
  94. #define CACHE_NORMAL 02 /**< Items with a normal replacement cost */
  95. #define CACHE_EXPENSIVE 03 /**< Items with a high replacement cost */
  96. /** @} */
  97. #endif /* _IPXE_MALLOC_H */