balloc.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /**
  2. * @file balloc.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Memory allocation functions.
  32. */
  33. #ifndef BADVPN_MISC_BALLOC_H
  34. #define BADVPN_MISC_BALLOC_H
  35. #include <stddef.h>
  36. #include <stdint.h>
  37. #include <stdlib.h>
  38. #include <limits.h>
  39. #include <misc/debug.h>
  40. #include <misc/bsize.h>
  41. #include <misc/maxalign.h>
  42. /**
  43. * Allocates memory.
  44. *
  45. * @param bytes number of bytes to allocate.
  46. * @return a non-NULL pointer to the memory, or NULL on failure.
  47. * The memory allocated can be freed using {@link BFree}.
  48. */
  49. static void * BAlloc (size_t bytes);
  50. /**
  51. * Frees memory.
  52. *
  53. * @param m memory to free. Must have been obtained with {@link BAlloc},
  54. * {@link BAllocArray}, or {@link BAllocArray2}. May be NULL;
  55. * in this case, this function does nothing.
  56. */
  57. static void BFree (void *m);
  58. /**
  59. * Allocates memory, with size given as a {@link bsize_t}.
  60. *
  61. * @param bytes number of bytes to allocate. If the size is overflow,
  62. * this function will return NULL.
  63. * @return a non-NULL pointer to the memory, or NULL on failure.
  64. * The memory allocated can be freed using {@link BFree}.
  65. */
  66. static void * BAllocSize (bsize_t bytes);
  67. /**
  68. * Allocates memory for an array.
  69. * A check is first done to make sure the multiplication doesn't overflow;
  70. * otherwise, this is equivalent to {@link BAlloc}(count * bytes).
  71. * This may be slightly faster if 'bytes' is constant, because a division
  72. * with 'bytes' is performed.
  73. *
  74. * @param count number of elements.
  75. * @param bytes size of one array element.
  76. * @return a non-NULL pointer to the memory, or NULL on failure.
  77. * The memory allocated can be freed using {@link BFree}.
  78. */
  79. static void * BAllocArray (size_t count, size_t bytes);
  80. /**
  81. * Reallocates memory that was allocated using one of the allocation
  82. * functions in this file. On success, the memory may be moved to a
  83. * different address, leaving the old address invalid.
  84. *
  85. * @param mem pointer to an existing memory block. May be NULL, in which
  86. * case this is equivalent to {@link BAllocArray}.
  87. * @param count number of elements for reallocation
  88. * @param bytes size of one array element for reallocation
  89. * @return a non-NULL pointer to the address of the reallocated memory
  90. * block, or NULL on failure. On failure, the original memory
  91. * block is left intact.
  92. */
  93. static void * BReallocArray (void *mem, size_t count, size_t bytes);
  94. /**
  95. * Allocates memory for a two-dimensional array.
  96. *
  97. * Checks are first done to make sure the multiplications don't overflow;
  98. * otherwise, this is equivalent to {@link BAlloc}((count2 * (count1 * bytes)).
  99. *
  100. * @param count2 number of elements in one dimension.
  101. * @param count1 number of elements in the other dimension.
  102. * @param bytes size of one array element.
  103. * @return a non-NULL pointer to the memory, or NULL on failure.
  104. * The memory allocated can be freed using {@link BFree}.
  105. */
  106. static void * BAllocArray2 (size_t count2, size_t count1, size_t bytes);
  107. /**
  108. * Adds to a size_t with overflow detection.
  109. *
  110. * @param s pointer to a size_t to add to
  111. * @param add number to add
  112. * @return 1 on success, 0 on failure
  113. */
  114. static int BSizeAdd (size_t *s, size_t add);
  115. /**
  116. * Aligns a size_t upwards with overflow detection.
  117. *
  118. * @param s pointer to a size_t to align
  119. * @param align alignment value. Must be >0.
  120. * @return 1 on success, 0 on failure
  121. */
  122. static int BSizeAlign (size_t *s, size_t align);
  123. void * BAlloc (size_t bytes)
  124. {
  125. if (bytes == 0) {
  126. return malloc(1);
  127. }
  128. return malloc(bytes);
  129. }
  130. void BFree (void *m)
  131. {
  132. free(m);
  133. }
  134. void * BAllocSize (bsize_t bytes)
  135. {
  136. if (bytes.is_overflow) {
  137. return NULL;
  138. }
  139. return BAlloc(bytes.value);
  140. }
  141. void * BAllocArray (size_t count, size_t bytes)
  142. {
  143. if (count == 0 || bytes == 0) {
  144. return malloc(1);
  145. }
  146. if (count > SIZE_MAX / bytes) {
  147. return NULL;
  148. }
  149. return BAlloc(count * bytes);
  150. }
  151. void * BReallocArray (void *mem, size_t count, size_t bytes)
  152. {
  153. if (count == 0 || bytes == 0) {
  154. return realloc(mem, 1);
  155. }
  156. if (count > SIZE_MAX / bytes) {
  157. return NULL;
  158. }
  159. return realloc(mem, count * bytes);
  160. }
  161. void * BAllocArray2 (size_t count2, size_t count1, size_t bytes)
  162. {
  163. if (count2 == 0 || count1 == 0 || bytes == 0) {
  164. return malloc(1);
  165. }
  166. if (count1 > SIZE_MAX / bytes) {
  167. return NULL;
  168. }
  169. if (count2 > SIZE_MAX / (count1 * bytes)) {
  170. return NULL;
  171. }
  172. return BAlloc(count2 * (count1 * bytes));
  173. }
  174. int BSizeAdd (size_t *s, size_t add)
  175. {
  176. ASSERT(s)
  177. if (add > SIZE_MAX - *s) {
  178. return 0;
  179. }
  180. *s += add;
  181. return 1;
  182. }
  183. int BSizeAlign (size_t *s, size_t align)
  184. {
  185. ASSERT(s)
  186. ASSERT(align > 0)
  187. size_t mod = *s % align;
  188. if (mod > 0) {
  189. if (align - mod > SIZE_MAX - *s) {
  190. return 0;
  191. }
  192. *s += align - mod;
  193. }
  194. return 1;
  195. }
  196. #endif