balloc.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. /**
  33. * Allocates memory.
  34. *
  35. * @param bytes number of bytes to allocate.
  36. * @return a non-NULL pointer to the memory, or NULL on failure.
  37. * The memory allocated can be freed using {@link BFree}.
  38. */
  39. static void * BAlloc (size_t bytes);
  40. /**
  41. * Frees memory.
  42. *
  43. * @param m memory to free. Must have been obtained with {@link BAlloc},
  44. * {@link BAllocArray}, or {@link BAllocArray2}. May be NULL;
  45. * in this case, this function does nothing.
  46. */
  47. static void BFree (void *m);
  48. /**
  49. * Allocates memory for an array.
  50. * A check is first done to make sure the multiplication doesn't overflow;
  51. * otherwise, this is equivalent to {@link BAlloc}(count * bytes).
  52. *
  53. * @param count number of elements.
  54. * @param bytes size of one array element.
  55. * @return a non-NULL pointer to the memory, or NULL on failure.
  56. * The memory allocated can be freed using {@link BFree}.
  57. */
  58. static void * BAllocArray (size_t count, size_t bytes);
  59. /**
  60. * Allocates memory for a two-dimensional array.
  61. *
  62. * Checks are first done to make sure the multiplications don't overflow;
  63. * otherwise, this is equivalent to {@link BAlloc}((count2 * (count1 * bytes)).
  64. *
  65. * @param count2 number of elements in one dimension.
  66. * @param count1 number of elements in the other dimension.
  67. * @param bytes size of one array element.
  68. * @return a non-NULL pointer to the memory, or NULL on failure.
  69. * The memory allocated can be freed using {@link BFree}.
  70. */
  71. static void * BAllocArray2 (size_t count2, size_t count1, size_t bytes);
  72. void * BAlloc (size_t bytes)
  73. {
  74. if (bytes == 0) {
  75. return malloc(1);
  76. }
  77. return malloc(bytes);
  78. }
  79. void BFree (void *m)
  80. {
  81. free(m);
  82. }
  83. void * BAllocArray (size_t count, size_t bytes)
  84. {
  85. if (count == 0 || bytes == 0) {
  86. return malloc(1);
  87. }
  88. if (count > SIZE_MAX / bytes) {
  89. return NULL;
  90. }
  91. return BAlloc(count * bytes);
  92. }
  93. void * BAllocArray2 (size_t count2, size_t count1, size_t bytes)
  94. {
  95. if (count2 == 0 || count1 == 0 || bytes == 0) {
  96. return malloc(1);
  97. }
  98. if (count1 > SIZE_MAX / bytes) {
  99. return NULL;
  100. }
  101. if (count2 > SIZE_MAX / (count1 * bytes)) {
  102. return NULL;
  103. }
  104. return BAlloc(count2 * (count1 * bytes));
  105. }
  106. #endif