balloc.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. * Adds to a size_t with overflow detection.
  95. *
  96. * @param s pointer to a size_t to add to
  97. * @param add number to add
  98. * @return 1 on success, 0 on failure
  99. */
  100. static int BSizeAdd (size_t *s, size_t add);
  101. /**
  102. * Aligns a size_t upwards with overflow detection.
  103. *
  104. * @param s pointer to a size_t to align
  105. * @param align alignment value. Must be >0.
  106. * @return 1 on success, 0 on failure
  107. */
  108. static int BSizeAlign (size_t *s, size_t align);
  109. void * BAlloc (size_t bytes)
  110. {
  111. if (bytes == 0) {
  112. return malloc(1);
  113. }
  114. return malloc(bytes);
  115. }
  116. void BFree (void *m)
  117. {
  118. free(m);
  119. }
  120. void * BAllocSize (bsize_t bytes)
  121. {
  122. if (bytes.is_overflow) {
  123. return NULL;
  124. }
  125. return BAlloc(bytes.value);
  126. }
  127. void * BAllocArray (size_t count, size_t bytes)
  128. {
  129. if (count == 0 || bytes == 0) {
  130. return malloc(1);
  131. }
  132. if (count > SIZE_MAX / bytes) {
  133. return NULL;
  134. }
  135. return BAlloc(count * bytes);
  136. }
  137. void * BAllocArray2 (size_t count2, size_t count1, size_t bytes)
  138. {
  139. if (count2 == 0 || count1 == 0 || bytes == 0) {
  140. return malloc(1);
  141. }
  142. if (count1 > SIZE_MAX / bytes) {
  143. return NULL;
  144. }
  145. if (count2 > SIZE_MAX / (count1 * bytes)) {
  146. return NULL;
  147. }
  148. return BAlloc(count2 * (count1 * bytes));
  149. }
  150. int BSizeAdd (size_t *s, size_t add)
  151. {
  152. ASSERT(s)
  153. if (add > SIZE_MAX - *s) {
  154. return 0;
  155. }
  156. *s += add;
  157. return 1;
  158. }
  159. int BSizeAlign (size_t *s, size_t align)
  160. {
  161. ASSERT(s)
  162. ASSERT(align > 0)
  163. size_t mod = *s % align;
  164. if (mod > 0) {
  165. if (align - mod > SIZE_MAX - *s) {
  166. return 0;
  167. }
  168. *s += align - mod;
  169. }
  170. return 1;
  171. }
  172. #endif