malloca.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* Safe automatic memory allocation.
  2. Copyright (C) 2003, 2006-2007 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, write to the Free Software Foundation,
  14. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  15. #include <config.h>
  16. /* Specification. */
  17. #include "malloca.h"
  18. /* The speed critical point in this file is freea() applied to an alloca()
  19. result: it must be fast, to match the speed of alloca(). The speed of
  20. mmalloca() and freea() in the other case are not critical, because they
  21. are only invoked for big memory sizes. */
  22. #if HAVE_ALLOCA
  23. /* Store the mmalloca() results in a hash table. This is needed to reliably
  24. distinguish a mmalloca() result and an alloca() result.
  25. Although it is possible that the same pointer is returned by alloca() and
  26. by mmalloca() at different times in the same application, it does not lead
  27. to a bug in freea(), because:
  28. - Before a pointer returned by alloca() can point into malloc()ed memory,
  29. the function must return, and once this has happened the programmer must
  30. not call freea() on it anyway.
  31. - Before a pointer returned by mmalloca() can point into the stack, it
  32. must be freed. The only function that can free it is freea(), and
  33. when freea() frees it, it also removes it from the hash table. */
  34. #define MAGIC_NUMBER 0x1415fb4a
  35. #define MAGIC_SIZE sizeof (int)
  36. /* This is how the header info would look like without any alignment
  37. considerations. */
  38. struct preliminary_header { void *next; char room[MAGIC_SIZE]; };
  39. /* But the header's size must be a multiple of sa_alignment_max. */
  40. #define HEADER_SIZE \
  41. (((sizeof (struct preliminary_header) + sa_alignment_max - 1) / sa_alignment_max) * sa_alignment_max)
  42. struct header { void *next; char room[HEADER_SIZE - sizeof (struct preliminary_header) + MAGIC_SIZE]; };
  43. /* Verify that HEADER_SIZE == sizeof (struct header). */
  44. typedef int verify1[2 * (HEADER_SIZE == sizeof (struct header)) - 1];
  45. /* We make the hash table quite big, so that during lookups the probability
  46. of empty hash buckets is quite high. There is no need to make the hash
  47. table resizable, because when the hash table gets filled so much that the
  48. lookup becomes slow, it means that the application has memory leaks. */
  49. #define HASH_TABLE_SIZE 257
  50. static void * mmalloca_results[HASH_TABLE_SIZE];
  51. #endif
  52. void *
  53. mmalloca (size_t n)
  54. {
  55. #if HAVE_ALLOCA
  56. /* Allocate one more word, that serves as an indicator for malloc()ed
  57. memory, so that freea() of an alloca() result is fast. */
  58. size_t nplus = n + HEADER_SIZE;
  59. if (nplus >= n)
  60. {
  61. char *p = (char *) malloc (nplus);
  62. if (p != NULL)
  63. {
  64. size_t slot;
  65. p += HEADER_SIZE;
  66. /* Put a magic number into the indicator word. */
  67. ((int *) p)[-1] = MAGIC_NUMBER;
  68. /* Enter p into the hash table. */
  69. slot = (unsigned long) p % HASH_TABLE_SIZE;
  70. ((struct header *) (p - HEADER_SIZE))->next = mmalloca_results[slot];
  71. mmalloca_results[slot] = p;
  72. return p;
  73. }
  74. }
  75. /* Out of memory. */
  76. return NULL;
  77. #else
  78. # if !MALLOC_0_IS_NONNULL
  79. if (n == 0)
  80. n = 1;
  81. # endif
  82. return malloc (n);
  83. #endif
  84. }
  85. #if HAVE_ALLOCA
  86. void
  87. freea (void *p)
  88. {
  89. /* mmalloca() may have returned NULL. */
  90. if (p != NULL)
  91. {
  92. /* Attempt to quickly distinguish the mmalloca() result - which has
  93. a magic indicator word - and the alloca() result - which has an
  94. uninitialized indicator word. It is for this test that sa_increment
  95. additional bytes are allocated in the alloca() case. */
  96. if (((int *) p)[-1] == MAGIC_NUMBER)
  97. {
  98. /* Looks like a mmalloca() result. To see whether it really is one,
  99. perform a lookup in the hash table. */
  100. size_t slot = (unsigned long) p % HASH_TABLE_SIZE;
  101. void **chain = &mmalloca_results[slot];
  102. for (; *chain != NULL;)
  103. {
  104. if (*chain == p)
  105. {
  106. /* Found it. Remove it from the hash table and free it. */
  107. char *p_begin = (char *) p - HEADER_SIZE;
  108. *chain = ((struct header *) p_begin)->next;
  109. free (p_begin);
  110. return;
  111. }
  112. chain = &((struct header *) ((char *) *chain - HEADER_SIZE))->next;
  113. }
  114. }
  115. /* At this point, we know it was not a mmalloca() result. */
  116. }
  117. }
  118. #endif