malloca.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* Safe automatic memory allocation.
  2. Copyright (C) 2003, 2006-2007, 2009-2013 Free Software Foundation, Inc.
  3. Written by Bruno Haible <bruno@clisp.org>, 2003.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, see <http://www.gnu.org/licenses/>. */
  14. #define _GL_USE_STDLIB_ALLOC 1
  15. #include <config.h>
  16. /* Specification. */
  17. #include "malloca.h"
  18. #include <stdint.h>
  19. #include "verify.h"
  20. /* The speed critical point in this file is freea() applied to an alloca()
  21. result: it must be fast, to match the speed of alloca(). The speed of
  22. mmalloca() and freea() in the other case are not critical, because they
  23. are only invoked for big memory sizes. */
  24. #if HAVE_ALLOCA
  25. /* Store the mmalloca() results in a hash table. This is needed to reliably
  26. distinguish a mmalloca() result and an alloca() result.
  27. Although it is possible that the same pointer is returned by alloca() and
  28. by mmalloca() at different times in the same application, it does not lead
  29. to a bug in freea(), because:
  30. - Before a pointer returned by alloca() can point into malloc()ed memory,
  31. the function must return, and once this has happened the programmer must
  32. not call freea() on it anyway.
  33. - Before a pointer returned by mmalloca() can point into the stack, it
  34. must be freed. The only function that can free it is freea(), and
  35. when freea() frees it, it also removes it from the hash table. */
  36. #define MAGIC_NUMBER 0x1415fb4a
  37. #define MAGIC_SIZE sizeof (int)
  38. /* This is how the header info would look like without any alignment
  39. considerations. */
  40. struct preliminary_header { void *next; int magic; };
  41. /* But the header's size must be a multiple of sa_alignment_max. */
  42. #define HEADER_SIZE \
  43. (((sizeof (struct preliminary_header) + sa_alignment_max - 1) / sa_alignment_max) * sa_alignment_max)
  44. union header {
  45. void *next;
  46. struct {
  47. char room[HEADER_SIZE - MAGIC_SIZE];
  48. int word;
  49. } magic;
  50. };
  51. verify (HEADER_SIZE == sizeof (union header));
  52. /* We make the hash table quite big, so that during lookups the probability
  53. of empty hash buckets is quite high. There is no need to make the hash
  54. table resizable, because when the hash table gets filled so much that the
  55. lookup becomes slow, it means that the application has memory leaks. */
  56. #define HASH_TABLE_SIZE 257
  57. static void * mmalloca_results[HASH_TABLE_SIZE];
  58. #endif
  59. void *
  60. mmalloca (size_t n)
  61. {
  62. #if HAVE_ALLOCA
  63. /* Allocate one more word, that serves as an indicator for malloc()ed
  64. memory, so that freea() of an alloca() result is fast. */
  65. size_t nplus = n + HEADER_SIZE;
  66. if (nplus >= n)
  67. {
  68. void *p = malloc (nplus);
  69. if (p != NULL)
  70. {
  71. size_t slot;
  72. union header *h = p;
  73. p = h + 1;
  74. /* Put a magic number into the indicator word. */
  75. h->magic.word = MAGIC_NUMBER;
  76. /* Enter p into the hash table. */
  77. slot = (uintptr_t) p % HASH_TABLE_SIZE;
  78. h->next = mmalloca_results[slot];
  79. mmalloca_results[slot] = p;
  80. return p;
  81. }
  82. }
  83. /* Out of memory. */
  84. return NULL;
  85. #else
  86. # if !MALLOC_0_IS_NONNULL
  87. if (n == 0)
  88. n = 1;
  89. # endif
  90. return malloc (n);
  91. #endif
  92. }
  93. #if HAVE_ALLOCA
  94. void
  95. freea (void *p)
  96. {
  97. /* mmalloca() may have returned NULL. */
  98. if (p != NULL)
  99. {
  100. /* Attempt to quickly distinguish the mmalloca() result - which has
  101. a magic indicator word - and the alloca() result - which has an
  102. uninitialized indicator word. It is for this test that sa_increment
  103. additional bytes are allocated in the alloca() case. */
  104. if (((int *) p)[-1] == MAGIC_NUMBER)
  105. {
  106. /* Looks like a mmalloca() result. To see whether it really is one,
  107. perform a lookup in the hash table. */
  108. size_t slot = (uintptr_t) p % HASH_TABLE_SIZE;
  109. void **chain = &mmalloca_results[slot];
  110. for (; *chain != NULL;)
  111. {
  112. union header *h = p;
  113. if (*chain == p)
  114. {
  115. /* Found it. Remove it from the hash table and free it. */
  116. union header *p_begin = h - 1;
  117. *chain = p_begin->next;
  118. free (p_begin);
  119. return;
  120. }
  121. h = *chain;
  122. chain = &h[-1].next;
  123. }
  124. }
  125. /* At this point, we know it was not a mmalloca() result. */
  126. }
  127. }
  128. #endif