balloc.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. * Changes the size of a memory block. On success, the memory block
  60. * may be moved to a different address.
  61. *
  62. * @param m pointer to a memory block obtained from {@link BAlloc}
  63. * or other functions in this group. If this is NULL, the
  64. * call is equivalent to {@link BAlloc}(bytes).
  65. * @param bytes new size of the memory block
  66. * @return new pointer to the memory block, or NULL on failure
  67. */
  68. static void * BRealloc (void *m, size_t bytes);
  69. /**
  70. * Allocates memory, with size given as a {@link bsize_t}.
  71. *
  72. * @param bytes number of bytes to allocate. If the size is overflow,
  73. * this function will return NULL.
  74. * @return a non-NULL pointer to the memory, or NULL on failure.
  75. * The memory allocated can be freed using {@link BFree}.
  76. */
  77. static void * BAllocSize (bsize_t bytes);
  78. /**
  79. * Allocates memory for an array.
  80. * A check is first done to make sure the multiplication doesn't overflow;
  81. * otherwise, this is equivalent to {@link BAlloc}(count * bytes).
  82. * This may be slightly faster if 'bytes' is constant, because a division
  83. * with 'bytes' is performed.
  84. *
  85. * @param count number of elements.
  86. * @param bytes size of one array element.
  87. * @return a non-NULL pointer to the memory, or NULL on failure.
  88. * The memory allocated can be freed using {@link BFree}.
  89. */
  90. static void * BAllocArray (size_t count, size_t bytes);
  91. /**
  92. * Reallocates memory that was allocated using one of the allocation
  93. * functions in this file. On success, the memory may be moved to a
  94. * different address, leaving the old address invalid.
  95. *
  96. * @param mem pointer to an existing memory block. May be NULL, in which
  97. * case this is equivalent to {@link BAllocArray}.
  98. * @param count number of elements for reallocation
  99. * @param bytes size of one array element for reallocation
  100. * @return a non-NULL pointer to the address of the reallocated memory
  101. * block, or NULL on failure. On failure, the original memory
  102. * block is left intact.
  103. */
  104. static void * BReallocArray (void *mem, size_t count, size_t bytes);
  105. /**
  106. * Allocates memory for a two-dimensional array.
  107. *
  108. * Checks are first done to make sure the multiplications don't overflow;
  109. * otherwise, this is equivalent to {@link BAlloc}((count2 * (count1 * bytes)).
  110. *
  111. * @param count2 number of elements in one dimension.
  112. * @param count1 number of elements in the other dimension.
  113. * @param bytes size of one array element.
  114. * @return a non-NULL pointer to the memory, or NULL on failure.
  115. * The memory allocated can be freed using {@link BFree}.
  116. */
  117. static void * BAllocArray2 (size_t count2, size_t count1, size_t bytes);
  118. /**
  119. * Adds to a size_t with overflow detection.
  120. *
  121. * @param s pointer to a size_t to add to
  122. * @param add number to add
  123. * @return 1 on success, 0 on failure
  124. */
  125. static int BSizeAdd (size_t *s, size_t add);
  126. /**
  127. * Aligns a size_t upwards with overflow detection.
  128. *
  129. * @param s pointer to a size_t to align
  130. * @param align alignment value. Must be >0.
  131. * @return 1 on success, 0 on failure
  132. */
  133. static int BSizeAlign (size_t *s, size_t align);
  134. void * BAlloc (size_t bytes)
  135. {
  136. if (bytes == 0) {
  137. return malloc(1);
  138. }
  139. return malloc(bytes);
  140. }
  141. void BFree (void *m)
  142. {
  143. free(m);
  144. }
  145. void * BRealloc (void *m, size_t bytes)
  146. {
  147. if (bytes == 0) {
  148. return realloc(m, 1);
  149. }
  150. return realloc(m, bytes);
  151. }
  152. void * BAllocSize (bsize_t bytes)
  153. {
  154. if (bytes.is_overflow) {
  155. return NULL;
  156. }
  157. return BAlloc(bytes.value);
  158. }
  159. void * BAllocArray (size_t count, size_t bytes)
  160. {
  161. if (count == 0 || bytes == 0) {
  162. return malloc(1);
  163. }
  164. if (count > SIZE_MAX / bytes) {
  165. return NULL;
  166. }
  167. return BAlloc(count * bytes);
  168. }
  169. void * BReallocArray (void *mem, size_t count, size_t bytes)
  170. {
  171. if (count == 0 || bytes == 0) {
  172. return realloc(mem, 1);
  173. }
  174. if (count > SIZE_MAX / bytes) {
  175. return NULL;
  176. }
  177. return realloc(mem, count * bytes);
  178. }
  179. void * BAllocArray2 (size_t count2, size_t count1, size_t bytes)
  180. {
  181. if (count2 == 0 || count1 == 0 || bytes == 0) {
  182. return malloc(1);
  183. }
  184. if (count1 > SIZE_MAX / bytes) {
  185. return NULL;
  186. }
  187. if (count2 > SIZE_MAX / (count1 * bytes)) {
  188. return NULL;
  189. }
  190. return BAlloc(count2 * (count1 * bytes));
  191. }
  192. int BSizeAdd (size_t *s, size_t add)
  193. {
  194. ASSERT(s)
  195. if (add > SIZE_MAX - *s) {
  196. return 0;
  197. }
  198. *s += add;
  199. return 1;
  200. }
  201. int BSizeAlign (size_t *s, size_t align)
  202. {
  203. ASSERT(s)
  204. ASSERT(align > 0)
  205. size_t mod = *s % align;
  206. if (mod > 0) {
  207. if (align - mod > SIZE_MAX - *s) {
  208. return 0;
  209. }
  210. *s += align - mod;
  211. }
  212. return 1;
  213. }
  214. #endif