mm_private.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2010 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef GRUB_MM_PRIVATE_H
  19. #define GRUB_MM_PRIVATE_H 1
  20. #include <grub/mm.h>
  21. #include <grub/misc.h>
  22. /* For context, see kern/mm.c */
  23. /* Magic words. */
  24. #define GRUB_MM_FREE_MAGIC 0x2d3c2808
  25. #define GRUB_MM_ALLOC_MAGIC 0x6db08fa4
  26. /* A header describing a block of memory - either allocated or free */
  27. typedef struct grub_mm_header
  28. {
  29. /*
  30. * The 'next' free block in this region's circular free list.
  31. * Only meaningful if the block is free.
  32. */
  33. struct grub_mm_header *next;
  34. /* The block size, not in bytes but the number of cells of
  35. * GRUB_MM_ALIGN bytes. Includes the header cell.
  36. */
  37. grub_size_t size;
  38. /* either free or alloc magic, depending on the block type. */
  39. grub_size_t magic;
  40. /* pad to cell size: see the top of kern/mm.c. */
  41. #if GRUB_CPU_SIZEOF_VOID_P == 4
  42. char padding[4];
  43. #elif GRUB_CPU_SIZEOF_VOID_P == 8
  44. char padding[8];
  45. #else
  46. # error "unknown word size"
  47. #endif
  48. }
  49. *grub_mm_header_t;
  50. #if GRUB_CPU_SIZEOF_VOID_P == 4
  51. # define GRUB_MM_ALIGN_LOG2 4
  52. #elif GRUB_CPU_SIZEOF_VOID_P == 8
  53. # define GRUB_MM_ALIGN_LOG2 5
  54. #endif
  55. #define GRUB_MM_ALIGN (1 << GRUB_MM_ALIGN_LOG2)
  56. /* A region from which we can make allocations. */
  57. typedef struct grub_mm_region
  58. {
  59. /* The first free block in this region. */
  60. struct grub_mm_header *first;
  61. /*
  62. * The next region in the linked list of regions. Regions are initially
  63. * sorted in order of increasing size, but can grow, in which case the
  64. * ordering may not be preserved.
  65. */
  66. struct grub_mm_region *next;
  67. /*
  68. * A grub_mm_region will always be aligned to cell size. The pre-size is
  69. * the number of bytes we were given but had to skip in order to get that
  70. * alignment.
  71. */
  72. grub_size_t pre_size;
  73. /*
  74. * Likewise, the post-size is the number of bytes we wasted at the end
  75. * of the allocation because it wasn't a multiple of GRUB_MM_ALIGN
  76. */
  77. grub_size_t post_size;
  78. /* How many bytes are in this region? (free and allocated) */
  79. grub_size_t size;
  80. /* pad to a multiple of cell size */
  81. char padding[3 * GRUB_CPU_SIZEOF_VOID_P];
  82. }
  83. *grub_mm_region_t;
  84. #ifndef GRUB_MACHINE_EMU
  85. extern grub_mm_region_t EXPORT_VAR (grub_mm_base);
  86. #endif
  87. static inline void
  88. grub_mm_size_sanity_check (void) {
  89. /* Ensure we preserve alignment when doing h = (grub_mm_header_t) (r + 1). */
  90. COMPILE_TIME_ASSERT ((sizeof (struct grub_mm_region) %
  91. sizeof (struct grub_mm_header)) == 0);
  92. /*
  93. * GRUB_MM_ALIGN is supposed to represent cell size, and a mm_header is
  94. * supposed to be 1 cell.
  95. */
  96. COMPILE_TIME_ASSERT (sizeof (struct grub_mm_header) == GRUB_MM_ALIGN);
  97. }
  98. #endif