balloc.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 <inttypes.h>
  37. #include <stdlib.h>
  38. #include <misc/debug.h>
  39. #include <misc/bsize.h>
  40. /**
  41. * Allocates memory.
  42. *
  43. * @param bytes number of bytes to allocate.
  44. * @return a non-NULL pointer to the memory, or NULL on failure.
  45. * The memory allocated can be freed using {@link BFree}.
  46. */
  47. static void * BAlloc (size_t bytes);
  48. /**
  49. * Frees memory.
  50. *
  51. * @param m memory to free. Must have been obtained with {@link BAlloc},
  52. * {@link BAllocArray}, or {@link BAllocArray2}. May be NULL;
  53. * in this case, this function does nothing.
  54. */
  55. static void BFree (void *m);
  56. /**
  57. * Allocates memory, with size given as a {@link bsize_t}.
  58. *
  59. * @param bytes number of bytes to allocate. If the size is overflow,
  60. * this function will return NULL.
  61. * @return a non-NULL pointer to the memory, or NULL on failure.
  62. * The memory allocated can be freed using {@link BFree}.
  63. */
  64. static void * BAllocSize (bsize_t bytes);
  65. /**
  66. * Allocates memory for an array.
  67. * A check is first done to make sure the multiplication doesn't overflow;
  68. * otherwise, this is equivalent to {@link BAlloc}(count * bytes).
  69. *
  70. * @param count number of elements.
  71. * @param bytes size of one array element.
  72. * @return a non-NULL pointer to the memory, or NULL on failure.
  73. * The memory allocated can be freed using {@link BFree}.
  74. */
  75. static void * BAllocArray (size_t count, size_t bytes);
  76. /**
  77. * Allocates memory for a two-dimensional array.
  78. *
  79. * Checks are first done to make sure the multiplications don't overflow;
  80. * otherwise, this is equivalent to {@link BAlloc}((count2 * (count1 * bytes)).
  81. *
  82. * @param count2 number of elements in one dimension.
  83. * @param count1 number of elements in the other dimension.
  84. * @param bytes size of one array element.
  85. * @return a non-NULL pointer to the memory, or NULL on failure.
  86. * The memory allocated can be freed using {@link BFree}.
  87. */
  88. static void * BAllocArray2 (size_t count2, size_t count1, size_t bytes);
  89. void * BAlloc (size_t bytes)
  90. {
  91. if (bytes == 0) {
  92. return malloc(1);
  93. }
  94. return malloc(bytes);
  95. }
  96. void BFree (void *m)
  97. {
  98. free(m);
  99. }
  100. void * BAllocSize (bsize_t bytes)
  101. {
  102. if (bytes.is_overflow) {
  103. return NULL;
  104. }
  105. return BAlloc(bytes.value);
  106. }
  107. void * BAllocArray (size_t count, size_t bytes)
  108. {
  109. if (count == 0 || bytes == 0) {
  110. return malloc(1);
  111. }
  112. if (count > SIZE_MAX / bytes) {
  113. return NULL;
  114. }
  115. return BAlloc(count * bytes);
  116. }
  117. void * BAllocArray2 (size_t count2, size_t count1, size_t bytes)
  118. {
  119. if (count2 == 0 || count1 == 0 || bytes == 0) {
  120. return malloc(1);
  121. }
  122. if (count1 > SIZE_MAX / bytes) {
  123. return NULL;
  124. }
  125. if (count2 > SIZE_MAX / (count1 * bytes)) {
  126. return NULL;
  127. }
  128. return BAlloc(count2 * (count1 * bytes));
  129. }
  130. #endif