/libs/headers/gc/new_gc_alloc.h

http://github.com/nddrylliog/ooc · C++ Header · 484 lines · 332 code · 78 blank · 74 comment · 27 complexity · 0f7d1d7f914fad5cfa9fc6d24a2083b5 MD5 · raw file

  1. /*
  2. * Copyright (c) 1996-1998 by Silicon Graphics. All rights reserved.
  3. *
  4. * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
  5. * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
  6. *
  7. * Permission is hereby granted to use or copy this program
  8. * for any purpose, provided the above notices are retained on all copies.
  9. * Permission to modify the code and to distribute modified code is granted,
  10. * provided the above notices are retained, and a notice that the code was
  11. * modified is included with the above copyright notice.
  12. */
  13. //
  14. // This is a revision of gc_alloc.h for SGI STL versions > 3.0
  15. // Unlike earlier versions, it supplements the standard "alloc.h"
  16. // instead of replacing it.
  17. //
  18. // This is sloppy about variable names used in header files.
  19. // It also doesn't yet understand the new header file names or
  20. // namespaces.
  21. //
  22. // This assumes the collector has been compiled with -DATOMIC_UNCOLLECTABLE.
  23. // The user should also consider -DREDIRECT_MALLOC=GC_uncollectable_malloc,
  24. // to ensure that object allocated through malloc are traced.
  25. //
  26. // Some of this could be faster in the explicit deallocation case.
  27. // In particular, we spend too much time clearing objects on the
  28. // free lists. That could be avoided.
  29. //
  30. // This uses template classes with static members, and hence does not work
  31. // with g++ 2.7.2 and earlier.
  32. //
  33. // Unlike its predecessor, this one simply defines
  34. // gc_alloc
  35. // single_client_gc_alloc
  36. // traceable_alloc
  37. // single_client_traceable_alloc
  38. //
  39. // It does not redefine alloc. Nor does it change the default allocator,
  40. // though the user may wish to do so. (The argument against changing
  41. // the default allocator is that it may introduce subtle link compatibility
  42. // problems. The argument for changing it is that the usual default
  43. // allocator is usually a very bad choice for a garbage collected environment.)
  44. //
  45. // This code assumes that the collector itself has been compiled with a
  46. // compiler that defines __STDC__ .
  47. //
  48. #ifndef GC_ALLOC_H
  49. #include "gc.h"
  50. #if (__GNUC__ < 3)
  51. # include <stack> // A more portable way to get stl_alloc.h .
  52. #else
  53. # include <bits/stl_alloc.h>
  54. # ifndef __STL_BEGIN_NAMESPACE
  55. # define __STL_BEGIN_NAMESPACE namespace std {
  56. # define __STL_END_NAMESPACE };
  57. # endif
  58. #ifndef __STL_USE_STD_ALLOCATORS
  59. #define __STL_USE_STD_ALLOCATORS
  60. #endif
  61. #endif
  62. /* A hack to deal with gcc 3.1. If you are using gcc3.1 and later, */
  63. /* you should probably really use gc_allocator.h instead. */
  64. #if defined (__GNUC__) && \
  65. (__GNUC__ > 3 || (__GNUC__ == 3 && (__GNUC_MINOR__ >= 1)))
  66. # define simple_alloc __simple_alloc
  67. #endif
  68. #define GC_ALLOC_H
  69. #include <stddef.h>
  70. #include <string.h>
  71. // The following need to match collector data structures.
  72. // We can't include gc_priv.h, since that pulls in way too much stuff.
  73. // This should eventually be factored out into another include file.
  74. extern "C" {
  75. extern void ** const GC_objfreelist_ptr;
  76. extern void ** const GC_aobjfreelist_ptr;
  77. extern void ** const GC_uobjfreelist_ptr;
  78. extern void ** const GC_auobjfreelist_ptr;
  79. extern void GC_incr_bytes_allocd(size_t bytes);
  80. extern void GC_incr_mem_freed(size_t words); /* FIXME: use bytes */
  81. extern char * GC_generic_malloc_words_small(size_t word, int kind);
  82. /* FIXME: Doesn't exist anymore. */
  83. }
  84. // Object kinds; must match PTRFREE, NORMAL, UNCOLLECTABLE, and
  85. // AUNCOLLECTABLE in gc_priv.h.
  86. enum { GC_PTRFREE = 0, GC_NORMAL = 1, GC_UNCOLLECTABLE = 2,
  87. GC_AUNCOLLECTABLE = 3 };
  88. enum { GC_max_fast_bytes = 255 };
  89. enum { GC_bytes_per_word = sizeof(char *) };
  90. enum { GC_byte_alignment = 8 };
  91. enum { GC_word_alignment = GC_byte_alignment/GC_bytes_per_word };
  92. inline void * &GC_obj_link(void * p)
  93. { return *reinterpret_cast<void **>(p); }
  94. // Compute a number of words >= n+1 bytes.
  95. // The +1 allows for pointers one past the end.
  96. inline size_t GC_round_up(size_t n)
  97. {
  98. return ((n + GC_byte_alignment)/GC_byte_alignment)*GC_word_alignment;
  99. }
  100. // The same but don't allow for extra byte.
  101. inline size_t GC_round_up_uncollectable(size_t n)
  102. {
  103. return ((n + GC_byte_alignment - 1)/GC_byte_alignment)*GC_word_alignment;
  104. }
  105. template <int dummy>
  106. class GC_aux_template {
  107. public:
  108. // File local count of allocated words. Occasionally this is
  109. // added into the global count. A separate count is necessary since the
  110. // real one must be updated with a procedure call.
  111. static size_t GC_bytes_recently_allocd;
  112. // Same for uncollectable mmory. Not yet reflected in either
  113. // GC_bytes_recently_allocd or GC_non_gc_bytes.
  114. static size_t GC_uncollectable_bytes_recently_allocd;
  115. // Similar counter for explicitly deallocated memory.
  116. static size_t GC_bytes_recently_freed;
  117. // Again for uncollectable memory.
  118. static size_t GC_uncollectable_bytes_recently_freed;
  119. static void * GC_out_of_line_malloc(size_t nwords, int kind);
  120. };
  121. template <int dummy>
  122. size_t GC_aux_template<dummy>::GC_bytes_recently_allocd = 0;
  123. template <int dummy>
  124. size_t GC_aux_template<dummy>::GC_uncollectable_bytes_recently_allocd = 0;
  125. template <int dummy>
  126. size_t GC_aux_template<dummy>::GC_bytes_recently_freed = 0;
  127. template <int dummy>
  128. size_t GC_aux_template<dummy>::GC_uncollectable_bytes_recently_freed = 0;
  129. template <int dummy>
  130. void * GC_aux_template<dummy>::GC_out_of_line_malloc(size_t nwords, int kind)
  131. {
  132. GC_bytes_recently_allocd += GC_uncollectable_bytes_recently_allocd;
  133. GC_non_gc_bytes +=
  134. GC_uncollectable_bytes_recently_allocd;
  135. GC_uncollectable_bytes_recently_allocd = 0;
  136. GC_bytes_recently_freed += GC_uncollectable_bytes_recently_freed;
  137. GC_non_gc_bytes -= GC_uncollectable_bytes_recently_freed;
  138. GC_uncollectable_bytes_recently_freed = 0;
  139. GC_incr_bytes_allocd(GC_bytes_recently_allocd);
  140. GC_bytes_recently_allocd = 0;
  141. GC_incr_mem_freed(GC_bytes_per_word(GC_bytes_recently_freed));
  142. GC_bytes_recently_freed = 0;
  143. return GC_generic_malloc_words_small(nwords, kind);
  144. }
  145. typedef GC_aux_template<0> GC_aux;
  146. // A fast, single-threaded, garbage-collected allocator
  147. // We assume the first word will be immediately overwritten.
  148. // In this version, deallocation is not a noop, and explicit
  149. // deallocation is likely to help performance.
  150. template <int dummy>
  151. class single_client_gc_alloc_template {
  152. public:
  153. static void * allocate(size_t n)
  154. {
  155. size_t nwords = GC_round_up(n);
  156. void ** flh;
  157. void * op;
  158. if (n > GC_max_fast_bytes) return GC_malloc(n);
  159. flh = GC_objfreelist_ptr + nwords;
  160. if (0 == (op = *flh)) {
  161. return GC_aux::GC_out_of_line_malloc(nwords, GC_NORMAL);
  162. }
  163. *flh = GC_obj_link(op);
  164. GC_aux::GC_bytes_recently_allocd += nwords * GC_bytes_per_word;
  165. return op;
  166. }
  167. static void * ptr_free_allocate(size_t n)
  168. {
  169. size_t nwords = GC_round_up(n);
  170. void ** flh;
  171. void * op;
  172. if (n > GC_max_fast_bytes) return GC_malloc_atomic(n);
  173. flh = GC_aobjfreelist_ptr + nwords;
  174. if (0 == (op = *flh)) {
  175. return GC_aux::GC_out_of_line_malloc(nwords, GC_PTRFREE);
  176. }
  177. *flh = GC_obj_link(op);
  178. GC_aux::GC_bytes_recently_allocd += nwords * GC_bytes_per_word;
  179. return op;
  180. }
  181. static void deallocate(void *p, size_t n)
  182. {
  183. size_t nwords = GC_round_up(n);
  184. void ** flh;
  185. if (n > GC_max_fast_bytes) {
  186. GC_free(p);
  187. } else {
  188. flh = GC_objfreelist_ptr + nwords;
  189. GC_obj_link(p) = *flh;
  190. memset(reinterpret_cast<char *>(p) + GC_bytes_per_word, 0,
  191. GC_bytes_per_word * (nwords - 1));
  192. *flh = p;
  193. GC_aux::GC_bytes_recently_freed += nwords * GC_bytes_per_word;
  194. }
  195. }
  196. static void ptr_free_deallocate(void *p, size_t n)
  197. {
  198. size_t nwords = GC_round_up(n);
  199. void ** flh;
  200. if (n > GC_max_fast_bytes) {
  201. GC_free(p);
  202. } else {
  203. flh = GC_aobjfreelist_ptr + nwords;
  204. GC_obj_link(p) = *flh;
  205. *flh = p;
  206. GC_aux::GC_bytes_recently_freed += nwords * GC_bytes_per_word;
  207. }
  208. }
  209. };
  210. typedef single_client_gc_alloc_template<0> single_client_gc_alloc;
  211. // Once more, for uncollectable objects.
  212. template <int dummy>
  213. class single_client_traceable_alloc_template {
  214. public:
  215. static void * allocate(size_t n)
  216. {
  217. size_t nwords = GC_round_up_uncollectable(n);
  218. void ** flh;
  219. void * op;
  220. if (n > GC_max_fast_bytes) return GC_malloc_uncollectable(n);
  221. flh = GC_uobjfreelist_ptr + nwords;
  222. if (0 == (op = *flh)) {
  223. return GC_aux::GC_out_of_line_malloc(nwords, GC_UNCOLLECTABLE);
  224. }
  225. *flh = GC_obj_link(op);
  226. GC_aux::GC_uncollectable_bytes_recently_allocd +=
  227. nwords * GC_bytes_per_word;
  228. return op;
  229. }
  230. static void * ptr_free_allocate(size_t n)
  231. {
  232. size_t nwords = GC_round_up_uncollectable(n);
  233. void ** flh;
  234. void * op;
  235. if (n > GC_max_fast_bytes) return GC_malloc_atomic_uncollectable(n);
  236. flh = GC_auobjfreelist_ptr + nwords;
  237. if (0 == (op = *flh)) {
  238. return GC_aux::GC_out_of_line_malloc(nwords, GC_AUNCOLLECTABLE);
  239. }
  240. *flh = GC_obj_link(op);
  241. GC_aux::GC_uncollectable_bytes_recently_allocd +=
  242. nwords * GC_bytes_per_word;
  243. return op;
  244. }
  245. static void deallocate(void *p, size_t n)
  246. {
  247. size_t nwords = GC_round_up_uncollectable(n);
  248. void ** flh;
  249. if (n > GC_max_fast_bytes) {
  250. GC_free(p);
  251. } else {
  252. flh = GC_uobjfreelist_ptr + nwords;
  253. GC_obj_link(p) = *flh;
  254. *flh = p;
  255. GC_aux::GC_uncollectable_bytes_recently_freed +=
  256. nwords * GC_bytes_per_word;
  257. }
  258. }
  259. static void ptr_free_deallocate(void *p, size_t n)
  260. {
  261. size_t nwords = GC_round_up_uncollectable(n);
  262. void ** flh;
  263. if (n > GC_max_fast_bytes) {
  264. GC_free(p);
  265. } else {
  266. flh = GC_auobjfreelist_ptr + nwords;
  267. GC_obj_link(p) = *flh;
  268. *flh = p;
  269. GC_aux::GC_uncollectable_bytes_recently_freed +=
  270. nwords * GC_bytes_per_word;
  271. }
  272. }
  273. };
  274. typedef single_client_traceable_alloc_template<0> single_client_traceable_alloc;
  275. template < int dummy >
  276. class gc_alloc_template {
  277. public:
  278. static void * allocate(size_t n) { return GC_malloc(n); }
  279. static void * ptr_free_allocate(size_t n)
  280. { return GC_malloc_atomic(n); }
  281. static void deallocate(void *, size_t) { }
  282. static void ptr_free_deallocate(void *, size_t) { }
  283. };
  284. typedef gc_alloc_template < 0 > gc_alloc;
  285. template < int dummy >
  286. class traceable_alloc_template {
  287. public:
  288. static void * allocate(size_t n) { return GC_malloc_uncollectable(n); }
  289. static void * ptr_free_allocate(size_t n)
  290. { return GC_malloc_atomic_uncollectable(n); }
  291. static void deallocate(void *p, size_t) { GC_free(p); }
  292. static void ptr_free_deallocate(void *p, size_t) { GC_free(p); }
  293. };
  294. typedef traceable_alloc_template < 0 > traceable_alloc;
  295. // We want to specialize simple_alloc so that it does the right thing
  296. // for all pointerfree types. At the moment there is no portable way to
  297. // even approximate that. The following approximation should work for
  298. // SGI compilers, and recent versions of g++.
  299. # define __GC_SPECIALIZE(T,alloc) \
  300. class simple_alloc<T, alloc> { \
  301. public: \
  302. static T *allocate(size_t n) \
  303. { return 0 == n? 0 : \
  304. reinterpret_cast<T*>(alloc::ptr_free_allocate(n * sizeof (T))); } \
  305. static T *allocate(void) \
  306. { return reinterpret_cast<T*>(alloc::ptr_free_allocate(sizeof (T))); } \
  307. static void deallocate(T *p, size_t n) \
  308. { if (0 != n) alloc::ptr_free_deallocate(p, n * sizeof (T)); } \
  309. static void deallocate(T *p) \
  310. { alloc::ptr_free_deallocate(p, sizeof (T)); } \
  311. };
  312. __STL_BEGIN_NAMESPACE
  313. __GC_SPECIALIZE(char, gc_alloc)
  314. __GC_SPECIALIZE(int, gc_alloc)
  315. __GC_SPECIALIZE(unsigned, gc_alloc)
  316. __GC_SPECIALIZE(float, gc_alloc)
  317. __GC_SPECIALIZE(double, gc_alloc)
  318. __GC_SPECIALIZE(char, traceable_alloc)
  319. __GC_SPECIALIZE(int, traceable_alloc)
  320. __GC_SPECIALIZE(unsigned, traceable_alloc)
  321. __GC_SPECIALIZE(float, traceable_alloc)
  322. __GC_SPECIALIZE(double, traceable_alloc)
  323. __GC_SPECIALIZE(char, single_client_gc_alloc)
  324. __GC_SPECIALIZE(int, single_client_gc_alloc)
  325. __GC_SPECIALIZE(unsigned, single_client_gc_alloc)
  326. __GC_SPECIALIZE(float, single_client_gc_alloc)
  327. __GC_SPECIALIZE(double, single_client_gc_alloc)
  328. __GC_SPECIALIZE(char, single_client_traceable_alloc)
  329. __GC_SPECIALIZE(int, single_client_traceable_alloc)
  330. __GC_SPECIALIZE(unsigned, single_client_traceable_alloc)
  331. __GC_SPECIALIZE(float, single_client_traceable_alloc)
  332. __GC_SPECIALIZE(double, single_client_traceable_alloc)
  333. __STL_END_NAMESPACE
  334. #ifdef __STL_USE_STD_ALLOCATORS
  335. __STL_BEGIN_NAMESPACE
  336. template <class _Tp>
  337. struct _Alloc_traits<_Tp, gc_alloc >
  338. {
  339. static const bool _S_instanceless = true;
  340. typedef simple_alloc<_Tp, gc_alloc > _Alloc_type;
  341. typedef __allocator<_Tp, gc_alloc > allocator_type;
  342. };
  343. inline bool operator==(const gc_alloc&,
  344. const gc_alloc&)
  345. {
  346. return true;
  347. }
  348. inline bool operator!=(const gc_alloc&,
  349. const gc_alloc&)
  350. {
  351. return false;
  352. }
  353. template <class _Tp>
  354. struct _Alloc_traits<_Tp, single_client_gc_alloc >
  355. {
  356. static const bool _S_instanceless = true;
  357. typedef simple_alloc<_Tp, single_client_gc_alloc > _Alloc_type;
  358. typedef __allocator<_Tp, single_client_gc_alloc > allocator_type;
  359. };
  360. inline bool operator==(const single_client_gc_alloc&,
  361. const single_client_gc_alloc&)
  362. {
  363. return true;
  364. }
  365. inline bool operator!=(const single_client_gc_alloc&,
  366. const single_client_gc_alloc&)
  367. {
  368. return false;
  369. }
  370. template <class _Tp>
  371. struct _Alloc_traits<_Tp, traceable_alloc >
  372. {
  373. static const bool _S_instanceless = true;
  374. typedef simple_alloc<_Tp, traceable_alloc > _Alloc_type;
  375. typedef __allocator<_Tp, traceable_alloc > allocator_type;
  376. };
  377. inline bool operator==(const traceable_alloc&,
  378. const traceable_alloc&)
  379. {
  380. return true;
  381. }
  382. inline bool operator!=(const traceable_alloc&,
  383. const traceable_alloc&)
  384. {
  385. return false;
  386. }
  387. template <class _Tp>
  388. struct _Alloc_traits<_Tp, single_client_traceable_alloc >
  389. {
  390. static const bool _S_instanceless = true;
  391. typedef simple_alloc<_Tp, single_client_traceable_alloc > _Alloc_type;
  392. typedef __allocator<_Tp, single_client_traceable_alloc > allocator_type;
  393. };
  394. inline bool operator==(const single_client_traceable_alloc&,
  395. const single_client_traceable_alloc&)
  396. {
  397. return true;
  398. }
  399. inline bool operator!=(const single_client_traceable_alloc&,
  400. const single_client_traceable_alloc&)
  401. {
  402. return false;
  403. }
  404. __STL_END_NAMESPACE
  405. #endif /* __STL_USE_STD_ALLOCATORS */
  406. #endif /* GC_ALLOC_H */