malloca.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* Safe automatic memory allocation.
  2. Copyright (C) 2003, 2006-2007, 2009-2012 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 Lesser General Public License as published by
  6. the Free Software Foundation; either version 2, 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 Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser 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. #define _GL_USE_STDLIB_ALLOC 1
  16. #include <config.h>
  17. /* Specification. */
  18. #include "malloca.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; char room[MAGIC_SIZE]; };
  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. struct header { void *next; char room[HEADER_SIZE - sizeof (struct preliminary_header) + MAGIC_SIZE]; };
  45. verify (HEADER_SIZE == sizeof (struct header));
  46. /* We make the hash table quite big, so that during lookups the probability
  47. of empty hash buckets is quite high. There is no need to make the hash
  48. table resizable, because when the hash table gets filled so much that the
  49. lookup becomes slow, it means that the application has memory leaks. */
  50. #define HASH_TABLE_SIZE 257
  51. static void * mmalloca_results[HASH_TABLE_SIZE];
  52. #endif
  53. void *
  54. mmalloca (size_t n)
  55. {
  56. #if HAVE_ALLOCA
  57. /* Allocate one more word, that serves as an indicator for malloc()ed
  58. memory, so that freea() of an alloca() result is fast. */
  59. size_t nplus = n + HEADER_SIZE;
  60. if (nplus >= n)
  61. {
  62. char *p = (char *) malloc (nplus);
  63. if (p != NULL)
  64. {
  65. size_t slot;
  66. p += HEADER_SIZE;
  67. /* Put a magic number into the indicator word. */
  68. ((int *) p)[-1] = MAGIC_NUMBER;
  69. /* Enter p into the hash table. */
  70. slot = (unsigned long) p % HASH_TABLE_SIZE;
  71. ((struct header *) (p - HEADER_SIZE))->next = mmalloca_results[slot];
  72. mmalloca_results[slot] = p;
  73. return p;
  74. }
  75. }
  76. /* Out of memory. */
  77. return NULL;
  78. #else
  79. # if !MALLOC_0_IS_NONNULL
  80. if (n == 0)
  81. n = 1;
  82. # endif
  83. return malloc (n);
  84. #endif
  85. }
  86. #if HAVE_ALLOCA
  87. void
  88. freea (void *p)
  89. {
  90. /* mmalloca() may have returned NULL. */
  91. if (p != NULL)
  92. {
  93. /* Attempt to quickly distinguish the mmalloca() result - which has
  94. a magic indicator word - and the alloca() result - which has an
  95. uninitialized indicator word. It is for this test that sa_increment
  96. additional bytes are allocated in the alloca() case. */
  97. if (((int *) p)[-1] == MAGIC_NUMBER)
  98. {
  99. /* Looks like a mmalloca() result. To see whether it really is one,
  100. perform a lookup in the hash table. */
  101. size_t slot = (unsigned long) p % HASH_TABLE_SIZE;
  102. void **chain = &mmalloca_results[slot];
  103. for (; *chain != NULL;)
  104. {
  105. if (*chain == p)
  106. {
  107. /* Found it. Remove it from the hash table and free it. */
  108. char *p_begin = (char *) p - HEADER_SIZE;
  109. *chain = ((struct header *) p_begin)->next;
  110. free (p_begin);
  111. return;
  112. }
  113. chain = &((struct header *) ((char *) *chain - HEADER_SIZE))->next;
  114. }
  115. }
  116. /* At this point, we know it was not a mmalloca() result. */
  117. }
  118. }
  119. #endif