1
0

alloc.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #ifndef _ALLOC_H_
  2. #define _ALLOC_H_
  3. /*
  4. * alloc supplies three ingredients to the test framework that are all
  5. * related to the support of dynamic memory allocation.
  6. *
  7. * The first is a set of alloc function wrappers for malloc and its
  8. * friends. Using wrappers allows test code and common code to use the
  9. * same interface for memory allocation at all stages, even though the
  10. * implementations may change with the stage, e.g. pre/post paging.
  11. *
  12. * The second is a set of implementations for the alloc function
  13. * interfaces. These implementations are named early_*, as they can be
  14. * used almost immediately by the test framework.
  15. *
  16. * The third is a very simple physical memory allocator, which the
  17. * early_* alloc functions build on.
  18. *
  19. * Copyright (C) 2014, Red Hat Inc, Andrew Jones <drjones@redhat.com>
  20. *
  21. * This work is licensed under the terms of the GNU LGPL, version 2.
  22. */
  23. #include "libcflat.h"
  24. struct alloc_ops {
  25. void *(*malloc)(size_t size);
  26. void *(*calloc)(size_t nmemb, size_t size);
  27. void (*free)(void *ptr);
  28. void *(*memalign)(size_t alignment, size_t size);
  29. };
  30. /*
  31. * alloc_ops is initialized to early_alloc_ops
  32. */
  33. extern struct alloc_ops *alloc_ops;
  34. static inline void *malloc(size_t size)
  35. {
  36. assert(alloc_ops && alloc_ops->malloc);
  37. return alloc_ops->malloc(size);
  38. }
  39. static inline void *calloc(size_t nmemb, size_t size)
  40. {
  41. assert(alloc_ops && alloc_ops->calloc);
  42. return alloc_ops->calloc(nmemb, size);
  43. }
  44. static inline void free(void *ptr)
  45. {
  46. assert(alloc_ops && alloc_ops->free);
  47. alloc_ops->free(ptr);
  48. }
  49. static inline void *memalign(size_t alignment, size_t size)
  50. {
  51. assert(alloc_ops && alloc_ops->memalign);
  52. return alloc_ops->memalign(alignment, size);
  53. }
  54. /*
  55. * phys_alloc is a very simple allocator which allows physical memory
  56. * to be partitioned into regions until all memory is allocated.
  57. *
  58. * Note: This is such a simple allocator that there is no way to free
  59. * a region. For more complicated memory management a single region
  60. * can be allocated, but then have its memory managed by a more
  61. * sophisticated allocator, e.g. a page allocator.
  62. */
  63. #define DEFAULT_MINIMUM_ALIGNMENT 32
  64. /*
  65. * phys_alloc_init creates the initial free memory region of size @size
  66. * at @base. The minimum alignment is set to DEFAULT_MINIMUM_ALIGNMENT.
  67. */
  68. extern void phys_alloc_init(phys_addr_t base, phys_addr_t size);
  69. /*
  70. * phys_alloc_set_minimum_alignment sets the minimum alignment to
  71. * @align.
  72. */
  73. extern void phys_alloc_set_minimum_alignment(phys_addr_t align);
  74. /*
  75. * phys_alloc_aligned returns the base address of a region of size @size,
  76. * where the address is aligned to @align, or INVALID_PHYS_ADDR if there
  77. * isn't enough free memory to satisfy the request.
  78. */
  79. extern phys_addr_t phys_alloc_aligned(phys_addr_t size, phys_addr_t align);
  80. /*
  81. * phys_zalloc_aligned is like phys_alloc_aligned, but zeros the memory
  82. * before returning the address.
  83. */
  84. extern phys_addr_t phys_zalloc_aligned(phys_addr_t size, phys_addr_t align);
  85. /*
  86. * phys_alloc returns the base address of a region of size @size, or
  87. * INVALID_PHYS_ADDR if there isn't enough free memory to satisfy the
  88. * request.
  89. */
  90. extern phys_addr_t phys_alloc(phys_addr_t size);
  91. /*
  92. * phys_zalloc is like phys_alloc, but zeros the memory before returning.
  93. */
  94. extern phys_addr_t phys_zalloc(phys_addr_t size);
  95. /*
  96. * phys_alloc_show outputs all currently allocated regions with the
  97. * following format
  98. * <start_addr>-<end_addr> [<USED|FREE>]
  99. */
  100. extern void phys_alloc_show(void);
  101. #endif /* _ALLOC_H_ */