balloc.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. * Allocates memory for a two-dimensional array.
  82. *
  83. * Checks are first done to make sure the multiplications don't overflow;
  84. * otherwise, this is equivalent to {@link BAlloc}((count2 * (count1 * bytes)).
  85. *
  86. * @param count2 number of elements in one dimension.
  87. * @param count1 number of elements in the other dimension.
  88. * @param bytes size of one array element.
  89. * @return a non-NULL pointer to the memory, or NULL on failure.
  90. * The memory allocated can be freed using {@link BFree}.
  91. */
  92. static void * BAllocArray2 (size_t count2, size_t count1, size_t bytes);
  93. /**
  94. * Allocates memory for two arrays as a continuous block.
  95. * Unused bytes are inserted to make the second array aligned for any kind
  96. * of object.
  97. * The 'count' and 'bytes' arguments are interchangeable; for performance
  98. * reasons, try to have the 'bytes' arguments constant.
  99. *
  100. * @param count1 number of elements for first array
  101. * @param bytes1 size of element for first array
  102. * @param count2 number of elements for second array
  103. * @param bytes2 size of element for second array
  104. * @param out2 *out2 will receive the pointer to the second array, if successful.
  105. * Must not be NULL.
  106. * @return on success, returns the pointer to the first array and sets *out2;
  107. * the memory can be freed by calling {@link BFree} on the returned pointer.
  108. * On failure, returns NULL.
  109. */
  110. static void * BAllocTwoArrays (size_t count1, size_t bytes1, size_t count2, size_t bytes2, void **out2);
  111. /**
  112. * Allocates memory for three arrays as a continuous block.
  113. * Unused bytes are inserted to make the second and third arrays aligned for any
  114. * kind of object.
  115. * The 'count' and 'bytes' arguments are interchangeable; for performance
  116. * reasons, try to have the 'bytes' arguments constant.
  117. *
  118. * @param count1 number of elements for first array
  119. * @param bytes1 size of element for first array
  120. * @param count2 number of elements for second array
  121. * @param bytes2 size of element for second array
  122. * @param count3 number of elements for third array
  123. * @param bytes3 size of element for third array
  124. * @param out2 *out2 will receive the pointer to the second array, if successful.
  125. * Must not be NULL.
  126. * @param out3 *out3 will receive the pointer to the third array, if successful.
  127. * Must not be NULL.
  128. * @return on success, returns the pointer to the first array and sets *out2 and
  129. * *out3; the memory can be freed by calling {@link BFree} on the returned
  130. * pointer. On failure, returns NULL.
  131. */
  132. static void * BAllocThreeArrays (size_t count1, size_t bytes1, size_t count2, size_t bytes2, size_t count3, size_t bytes3, void **out2, void **out3);
  133. void * BAlloc (size_t bytes)
  134. {
  135. if (bytes == 0) {
  136. return malloc(1);
  137. }
  138. return malloc(bytes);
  139. }
  140. void BFree (void *m)
  141. {
  142. free(m);
  143. }
  144. void * BAllocSize (bsize_t bytes)
  145. {
  146. if (bytes.is_overflow) {
  147. return NULL;
  148. }
  149. return BAlloc(bytes.value);
  150. }
  151. void * BAllocArray (size_t count, size_t bytes)
  152. {
  153. if (count == 0 || bytes == 0) {
  154. return malloc(1);
  155. }
  156. if (count > SIZE_MAX / bytes) {
  157. return NULL;
  158. }
  159. return BAlloc(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. void * BAllocTwoArrays (size_t count1, size_t bytes1, size_t count2, size_t bytes2, void **out2)
  175. {
  176. ASSERT(out2)
  177. // first array
  178. if (bytes1 > 0 && count1 > SIZE_MAX / bytes1) {
  179. return NULL;
  180. }
  181. size_t s = count1 * bytes1;
  182. // alignment for second array
  183. size_t mod = s % BMAX_ALIGN;
  184. if (mod > 0) {
  185. if (BMAX_ALIGN - mod > SIZE_MAX - s) {
  186. return NULL;
  187. }
  188. s += BMAX_ALIGN - mod;
  189. }
  190. // second array
  191. size_t pos2 = s;
  192. if (bytes2 > 0 && count2 > (SIZE_MAX - s) / bytes2) {
  193. return NULL;
  194. }
  195. s += count2 * bytes2;
  196. void *arr = BAlloc(s);
  197. if (arr) {
  198. *out2 = (char *)arr + pos2;
  199. }
  200. return arr;
  201. }
  202. void * BAllocThreeArrays (size_t count1, size_t bytes1, size_t count2, size_t bytes2, size_t count3, size_t bytes3, void **out2, void **out3)
  203. {
  204. ASSERT(out2)
  205. ASSERT(out2)
  206. // first array
  207. if (bytes1 > 0 && count1 > SIZE_MAX / bytes1) {
  208. return NULL;
  209. }
  210. size_t s = count1 * bytes1;
  211. // alignment for second array
  212. size_t mod = s % BMAX_ALIGN;
  213. if (mod > 0) {
  214. if (BMAX_ALIGN - mod > SIZE_MAX - s) {
  215. return NULL;
  216. }
  217. s += BMAX_ALIGN - mod;
  218. }
  219. // second array
  220. size_t pos2 = s;
  221. if (bytes2 > 0 && count2 > (SIZE_MAX - s) / bytes2) {
  222. return NULL;
  223. }
  224. s += count2 * bytes2;
  225. // alignment for third array
  226. mod = s % BMAX_ALIGN;
  227. if (mod > 0) {
  228. if (BMAX_ALIGN - mod > SIZE_MAX - s) {
  229. return NULL;
  230. }
  231. s += BMAX_ALIGN - mod;
  232. }
  233. // third array
  234. size_t pos3 = s;
  235. if (bytes3 > 0 && count3 > (SIZE_MAX - s) / bytes3) {
  236. return NULL;
  237. }
  238. s += count3 * bytes3;
  239. void *arr = BAlloc(s);
  240. if (arr) {
  241. *out2 = (char *)arr + pos2;
  242. *out3 = (char *)arr + pos3;
  243. }
  244. return arr;
  245. }
  246. #endif