balloc.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * @file balloc.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * Memory allocation functions.
  25. */
  26. #ifndef BADVPN_MISC_BALLOC_H
  27. #define BADVPN_MISC_BALLOC_H
  28. #include <stddef.h>
  29. #include <inttypes.h>
  30. #include <stdlib.h>
  31. #include <misc/debug.h>
  32. #include <misc/bsize.h>
  33. /**
  34. * Allocates memory.
  35. *
  36. * @param bytes number of bytes to allocate.
  37. * @return a non-NULL pointer to the memory, or NULL on failure.
  38. * The memory allocated can be freed using {@link BFree}.
  39. */
  40. static void * BAlloc (size_t bytes);
  41. /**
  42. * Frees memory.
  43. *
  44. * @param m memory to free. Must have been obtained with {@link BAlloc},
  45. * {@link BAllocArray}, or {@link BAllocArray2}. May be NULL;
  46. * in this case, this function does nothing.
  47. */
  48. static void BFree (void *m);
  49. /**
  50. * Allocates memory, with size given as a {@link bsize_t}.
  51. *
  52. * @param bytes number of bytes to allocate. If the size is overflow,
  53. * this function will return NULL.
  54. * @return a non-NULL pointer to the memory, or NULL on failure.
  55. * The memory allocated can be freed using {@link BFree}.
  56. */
  57. static void * BAllocSize (bsize_t bytes);
  58. /**
  59. * Allocates memory for an array.
  60. * A check is first done to make sure the multiplication doesn't overflow;
  61. * otherwise, this is equivalent to {@link BAlloc}(count * bytes).
  62. *
  63. * @param count number of elements.
  64. * @param bytes size of one array element.
  65. * @return a non-NULL pointer to the memory, or NULL on failure.
  66. * The memory allocated can be freed using {@link BFree}.
  67. */
  68. static void * BAllocArray (size_t count, size_t bytes);
  69. /**
  70. * Allocates memory for a two-dimensional array.
  71. *
  72. * Checks are first done to make sure the multiplications don't overflow;
  73. * otherwise, this is equivalent to {@link BAlloc}((count2 * (count1 * bytes)).
  74. *
  75. * @param count2 number of elements in one dimension.
  76. * @param count1 number of elements in the other dimension.
  77. * @param bytes size of one array element.
  78. * @return a non-NULL pointer to the memory, or NULL on failure.
  79. * The memory allocated can be freed using {@link BFree}.
  80. */
  81. static void * BAllocArray2 (size_t count2, size_t count1, size_t bytes);
  82. void * BAlloc (size_t bytes)
  83. {
  84. if (bytes == 0) {
  85. return malloc(1);
  86. }
  87. return malloc(bytes);
  88. }
  89. void BFree (void *m)
  90. {
  91. free(m);
  92. }
  93. void * BAllocSize (bsize_t bytes)
  94. {
  95. if (bytes.is_overflow) {
  96. return NULL;
  97. }
  98. return BAlloc(bytes.value);
  99. }
  100. void * BAllocArray (size_t count, size_t bytes)
  101. {
  102. if (count == 0 || bytes == 0) {
  103. return malloc(1);
  104. }
  105. if (count > SIZE_MAX / bytes) {
  106. return NULL;
  107. }
  108. return BAlloc(count * bytes);
  109. }
  110. void * BAllocArray2 (size_t count2, size_t count1, size_t bytes)
  111. {
  112. if (count2 == 0 || count1 == 0 || bytes == 0) {
  113. return malloc(1);
  114. }
  115. if (count1 > SIZE_MAX / bytes) {
  116. return NULL;
  117. }
  118. if (count2 > SIZE_MAX / (count1 * bytes)) {
  119. return NULL;
  120. }
  121. return BAlloc(count2 * (count1 * bytes));
  122. }
  123. #endif