alloc.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #ifndef ALLOC_H
  2. #define ALLOC_H
  3. #include <stdio.h>
  4. #include <stdbool.h>
  5. /**
  6. * alloc_init - initialize a pool of memory for the allocator.
  7. * @pool: the contiguous bytes for the allocator to use
  8. * @poolsize: the size of the pool
  9. *
  10. * This stores all the setup state required to perform allocation within the
  11. * pool (there is no external state). Any previous contents of @pool is
  12. * discarded.
  13. *
  14. * The same @pool and @poolsize arguments must be handed to the other alloc
  15. * functions after this.
  16. *
  17. * If the pool is too small for meaningful allocations, alloc_get will fail.
  18. *
  19. * Example:
  20. * void *pool = malloc(32*1024*1024);
  21. * if (!pool)
  22. * err(1, "Failed to allocate 32MB");
  23. * alloc_init(pool, 32*1024*1024);
  24. */
  25. void alloc_init(void *pool, unsigned long poolsize);
  26. /**
  27. * alloc_get - allocate some memory from the pool
  28. * @pool: the contiguous bytes for the allocator to use
  29. * @poolsize: the size of the pool
  30. * @size: the size of the desired allocation
  31. * @align: the alignment of the desired allocation (0 or power of 2)
  32. *
  33. * This is "malloc" within an initialized pool.
  34. *
  35. * It will return a unique pointer within the pool (ie. between @pool
  36. * and @pool+@poolsize) which meets the alignment requirements of
  37. * @align. Note that the alignment is relative to the start of the pool,
  38. * so of @pool is not aligned, the pointer won't be either.
  39. *
  40. * Returns NULL if there is no contiguous room.
  41. *
  42. * Example:
  43. * #include <ccan/alignof/alignof.h>
  44. * ...
  45. * double *d = alloc_get(pool, 32*1024*1024,
  46. * sizeof(*d), ALIGNOF(*d));
  47. * if (!d)
  48. * err(1, "Failed to allocate a double");
  49. */
  50. void *alloc_get(void *pool, unsigned long poolsize,
  51. unsigned long size, unsigned long align);
  52. /**
  53. * alloc_free - free some allocated memory from the pool
  54. * @pool: the contiguous bytes for the allocator to use
  55. * @poolsize: the size of the pool
  56. * @p: the non-NULL pointer returned from alloc_get.
  57. *
  58. * This is "free" within an initialized pool. A pointer should only be
  59. * freed once, and must be a pointer returned from a successful alloc_get()
  60. * call.
  61. *
  62. * Example:
  63. * alloc_free(pool, 32*1024*1024, d);
  64. */
  65. void alloc_free(void *pool, unsigned long poolsize, void *free);
  66. /**
  67. * alloc_size - get the actual size allocated by alloc_get
  68. * @pool: the contiguous bytes for the allocator to use
  69. * @poolsize: the size of the pool
  70. * @p: the non-NULL pointer returned from alloc_get.
  71. *
  72. * alloc_get() may overallocate, in which case you may use the extra
  73. * space exactly as if you had asked for it.
  74. *
  75. * The return value will always be at least the @size passed to alloc_get().
  76. *
  77. * Example:
  78. * printf("Allocating a double actually got me %lu bytes\n",
  79. * alloc_size(pool, 32*1024*1024, d));
  80. */
  81. unsigned long alloc_size(void *pool, unsigned long poolsize, void *p);
  82. /**
  83. * alloc_check - check the integrity of the allocation pool
  84. * @pool: the contiguous bytes for the allocator to use
  85. * @poolsize: the size of the pool
  86. *
  87. * alloc_check() can be used for debugging suspected pool corruption. It may
  88. * be quite slow, but provides some assistance for hard-to-find overruns or
  89. * double-frees. Unlike the rest of the code, it will not crash on corrupted
  90. * pools.
  91. *
  92. * There is an internal function check_fail() which this calls on failure which
  93. * is useful for placing breakpoints and gaining more insight into the type
  94. * of the corruption detected.
  95. *
  96. * Example:
  97. * #include <assert.h>
  98. *
  99. * ...
  100. * assert(alloc_check(pool, 32*1024*1024));
  101. */
  102. bool alloc_check(void *pool, unsigned long poolsize);
  103. /**
  104. * alloc_visualize - dump information about the allocation pool
  105. * @pool: the contiguous bytes for the allocator to use
  106. * @poolsize: the size of the pool
  107. *
  108. * When debugging the allocator itself, it's often useful to see how
  109. * the pool is being used. alloc_visualize() does that, but makes
  110. * assumptions about correctness (like the rest of the code) so if you
  111. * suspect corruption call alloc_check() first.
  112. *
  113. * Example:
  114. * #include <stdio.h>
  115. *
  116. * double *d = alloc_get(pool, 32*1024*1024,
  117. * sizeof(*d), ALIGNOF(*d));
  118. * if (!d) {
  119. * fprintf(stderr, "Allocation failed!\n");
  120. * if (!alloc_check(pool, 32*1024*1024))
  121. * errx(1, "Allocation pool is corrupt");
  122. * alloc_visualize(stderr, pool, 32*1024*1024));
  123. * exit(1);
  124. * }
  125. */
  126. void alloc_visualize(FILE *out, void *pool, unsigned long poolsize);
  127. #endif /* ALLOC_H */