eh_alloc.cc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. // -*- C++ -*- Allocate exception objects.
  2. // Copyright (C) 2001-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of GCC.
  5. //
  6. // GCC is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation; either version 3, or (at your option)
  9. // any later version.
  10. //
  11. // GCC is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // Under Section 7 of GPL version 3, you are granted additional
  17. // permissions described in the GCC Runtime Library Exception, version
  18. // 3.1, as published by the Free Software Foundation.
  19. // You should have received a copy of the GNU General Public License and
  20. // a copy of the GCC Runtime Library Exception along with this program;
  21. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  22. // <http://www.gnu.org/licenses/>.
  23. // This is derived from the C++ ABI for IA-64. Where we diverge
  24. // for cross-architecture compatibility are noted with "@@@".
  25. #include <bits/c++config.h>
  26. #include <cstdlib>
  27. #if _GLIBCXX_HOSTED
  28. #include <cstring>
  29. #endif
  30. #include <climits>
  31. #include <exception>
  32. #include "unwind-cxx.h"
  33. #include <ext/concurrence.h>
  34. #include <new>
  35. #if _GLIBCXX_HOSTED
  36. using std::free;
  37. using std::malloc;
  38. using std::memset;
  39. #else
  40. // In a freestanding environment, these functions may not be available
  41. // -- but for now, we assume that they are.
  42. extern "C" void *malloc (std::size_t);
  43. extern "C" void free(void *);
  44. extern "C" void *memset (void *, int, std::size_t);
  45. #endif
  46. using namespace __cxxabiv1;
  47. // ??? How to control these parameters.
  48. // Guess from the size of basic types how large a buffer is reasonable.
  49. // Note that the basic c++ exception header has 13 pointers and 2 ints,
  50. // so on a system with PSImode pointers we're talking about 56 bytes
  51. // just for overhead.
  52. #if INT_MAX == 32767
  53. # define EMERGENCY_OBJ_SIZE 128
  54. # define EMERGENCY_OBJ_COUNT 16
  55. #elif !defined (_GLIBCXX_LLP64) && LONG_MAX == 2147483647
  56. # define EMERGENCY_OBJ_SIZE 512
  57. # define EMERGENCY_OBJ_COUNT 32
  58. #else
  59. # define EMERGENCY_OBJ_SIZE 1024
  60. # define EMERGENCY_OBJ_COUNT 64
  61. #endif
  62. #ifndef __GTHREADS
  63. # undef EMERGENCY_OBJ_COUNT
  64. # define EMERGENCY_OBJ_COUNT 4
  65. #endif
  66. namespace __gnu_cxx
  67. {
  68. void __freeres();
  69. }
  70. namespace
  71. {
  72. // A fixed-size heap, variable size object allocator
  73. class pool
  74. {
  75. public:
  76. pool();
  77. _GLIBCXX_NODISCARD void *allocate (std::size_t);
  78. void free (void *);
  79. bool in_pool (void *);
  80. private:
  81. struct free_entry {
  82. std::size_t size;
  83. free_entry *next;
  84. };
  85. struct allocated_entry {
  86. std::size_t size;
  87. char data[] __attribute__((aligned));
  88. };
  89. // A single mutex controlling emergency allocations.
  90. __gnu_cxx::__mutex emergency_mutex;
  91. // The free-list
  92. free_entry *first_free_entry;
  93. // The arena itself - we need to keep track of these only
  94. // to implement in_pool.
  95. char *arena;
  96. std::size_t arena_size;
  97. friend void __gnu_cxx::__freeres();
  98. };
  99. pool::pool()
  100. {
  101. // Allocate the arena - we could add a GLIBCXX_EH_ARENA_SIZE environment
  102. // to make this tunable.
  103. arena_size = (EMERGENCY_OBJ_SIZE * EMERGENCY_OBJ_COUNT
  104. + EMERGENCY_OBJ_COUNT * sizeof (__cxa_dependent_exception));
  105. arena = (char *)malloc (arena_size);
  106. if (!arena)
  107. {
  108. // If the allocation failed go without an emergency pool.
  109. arena_size = 0;
  110. first_free_entry = NULL;
  111. return;
  112. }
  113. // Populate the free-list with a single entry covering the whole arena
  114. first_free_entry = reinterpret_cast <free_entry *> (arena);
  115. new (first_free_entry) free_entry;
  116. first_free_entry->size = arena_size;
  117. first_free_entry->next = NULL;
  118. }
  119. void *pool::allocate (std::size_t size)
  120. {
  121. __gnu_cxx::__scoped_lock sentry(emergency_mutex);
  122. // We need an additional size_t member plus the padding to
  123. // ensure proper alignment of data.
  124. size += offsetof (allocated_entry, data);
  125. // And we need to at least hand out objects of the size of
  126. // a freelist entry.
  127. if (size < sizeof (free_entry))
  128. size = sizeof (free_entry);
  129. // And we need to align objects we hand out to the maximum
  130. // alignment required on the target (this really aligns the
  131. // tail which will become a new freelist entry).
  132. size = ((size + __alignof__ (allocated_entry::data) - 1)
  133. & ~(__alignof__ (allocated_entry::data) - 1));
  134. // Search for an entry of proper size on the freelist.
  135. free_entry **e;
  136. for (e = &first_free_entry;
  137. *e && (*e)->size < size;
  138. e = &(*e)->next)
  139. ;
  140. if (!*e)
  141. return NULL;
  142. allocated_entry *x;
  143. if ((*e)->size - size >= sizeof (free_entry))
  144. {
  145. // Split block if it is too large.
  146. free_entry *f = reinterpret_cast <free_entry *>
  147. (reinterpret_cast <char *> (*e) + size);
  148. std::size_t sz = (*e)->size;
  149. free_entry *next = (*e)->next;
  150. new (f) free_entry;
  151. f->next = next;
  152. f->size = sz - size;
  153. x = reinterpret_cast <allocated_entry *> (*e);
  154. new (x) allocated_entry;
  155. x->size = size;
  156. *e = f;
  157. }
  158. else
  159. {
  160. // Exact size match or too small overhead for a free entry.
  161. std::size_t sz = (*e)->size;
  162. free_entry *next = (*e)->next;
  163. x = reinterpret_cast <allocated_entry *> (*e);
  164. new (x) allocated_entry;
  165. x->size = sz;
  166. *e = next;
  167. }
  168. return &x->data;
  169. }
  170. void pool::free (void *data)
  171. {
  172. __gnu_cxx::__scoped_lock sentry(emergency_mutex);
  173. allocated_entry *e = reinterpret_cast <allocated_entry *>
  174. (reinterpret_cast <char *> (data) - offsetof (allocated_entry, data));
  175. std::size_t sz = e->size;
  176. if (!first_free_entry
  177. || (reinterpret_cast <char *> (e) + sz
  178. < reinterpret_cast <char *> (first_free_entry)))
  179. {
  180. // If the free list is empty or the entry is before the
  181. // first element and cannot be merged with it add it as
  182. // the first free entry.
  183. free_entry *f = reinterpret_cast <free_entry *> (e);
  184. new (f) free_entry;
  185. f->size = sz;
  186. f->next = first_free_entry;
  187. first_free_entry = f;
  188. }
  189. else if (reinterpret_cast <char *> (e) + sz
  190. == reinterpret_cast <char *> (first_free_entry))
  191. {
  192. // Check if we can merge with the first free entry being right
  193. // after us.
  194. free_entry *f = reinterpret_cast <free_entry *> (e);
  195. new (f) free_entry;
  196. f->size = sz + first_free_entry->size;
  197. f->next = first_free_entry->next;
  198. first_free_entry = f;
  199. }
  200. else
  201. {
  202. // Else search for a free item we can merge with at its end.
  203. free_entry **fe;
  204. for (fe = &first_free_entry;
  205. (*fe)->next
  206. && (reinterpret_cast <char *> ((*fe)->next)
  207. > reinterpret_cast <char *> (e) + sz);
  208. fe = &(*fe)->next)
  209. ;
  210. // If we can merge the next block into us do so and continue
  211. // with the cases below.
  212. if (reinterpret_cast <char *> (e) + sz
  213. == reinterpret_cast <char *> ((*fe)->next))
  214. {
  215. sz += (*fe)->next->size;
  216. (*fe)->next = (*fe)->next->next;
  217. }
  218. if (reinterpret_cast <char *> (*fe) + (*fe)->size
  219. == reinterpret_cast <char *> (e))
  220. // Merge with the freelist entry.
  221. (*fe)->size += sz;
  222. else
  223. {
  224. // Else put it after it which keeps the freelist sorted.
  225. free_entry *f = reinterpret_cast <free_entry *> (e);
  226. new (f) free_entry;
  227. f->size = sz;
  228. f->next = (*fe)->next;
  229. (*fe)->next = f;
  230. }
  231. }
  232. }
  233. bool pool::in_pool (void *ptr)
  234. {
  235. char *p = reinterpret_cast <char *> (ptr);
  236. return (p > arena
  237. && p < arena + arena_size);
  238. }
  239. pool emergency_pool;
  240. }
  241. namespace __gnu_cxx
  242. {
  243. void
  244. __freeres()
  245. {
  246. if (emergency_pool.arena)
  247. {
  248. ::free(emergency_pool.arena);
  249. emergency_pool.arena = 0;
  250. }
  251. }
  252. }
  253. extern "C" void *
  254. __cxxabiv1::__cxa_allocate_exception(std::size_t thrown_size) _GLIBCXX_NOTHROW
  255. {
  256. void *ret;
  257. thrown_size += sizeof (__cxa_refcounted_exception);
  258. ret = malloc (thrown_size);
  259. if (!ret)
  260. ret = emergency_pool.allocate (thrown_size);
  261. if (!ret)
  262. std::terminate ();
  263. memset (ret, 0, sizeof (__cxa_refcounted_exception));
  264. return (void *)((char *)ret + sizeof (__cxa_refcounted_exception));
  265. }
  266. extern "C" void
  267. __cxxabiv1::__cxa_free_exception(void *vptr) _GLIBCXX_NOTHROW
  268. {
  269. char *ptr = (char *) vptr - sizeof (__cxa_refcounted_exception);
  270. if (emergency_pool.in_pool (ptr))
  271. emergency_pool.free (ptr);
  272. else
  273. free (ptr);
  274. }
  275. extern "C" __cxa_dependent_exception*
  276. __cxxabiv1::__cxa_allocate_dependent_exception() _GLIBCXX_NOTHROW
  277. {
  278. __cxa_dependent_exception *ret;
  279. ret = static_cast<__cxa_dependent_exception*>
  280. (malloc (sizeof (__cxa_dependent_exception)));
  281. if (!ret)
  282. ret = static_cast <__cxa_dependent_exception*>
  283. (emergency_pool.allocate (sizeof (__cxa_dependent_exception)));
  284. if (!ret)
  285. std::terminate ();
  286. memset (ret, 0, sizeof (__cxa_dependent_exception));
  287. return ret;
  288. }
  289. extern "C" void
  290. __cxxabiv1::__cxa_free_dependent_exception
  291. (__cxa_dependent_exception *vptr) _GLIBCXX_NOTHROW
  292. {
  293. if (emergency_pool.in_pool (vptr))
  294. emergency_pool.free (vptr);
  295. else
  296. free (vptr);
  297. }