gc-inline.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* classes: h_files */
  2. #ifndef SCM_GC_INLINE_H
  3. #define SCM_GC_INLINE_H
  4. /* Copyright (C) 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2006,
  5. * 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public License
  9. * as published by the Free Software Foundation; either version 3 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. */
  22. /* Much of this file was copied from gc_inline.h, from the BDW
  23. * collector. Its copyright notice is:
  24. *
  25. * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
  26. * Copyright (c) 1991-1995 by Xerox Corporation. All rights reserved.
  27. * Copyright (c) 2005 Hewlett-Packard Development Company, L.P.
  28. *
  29. * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
  30. * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
  31. *
  32. * Permission is hereby granted to use or copy this program
  33. * for any purpose, provided the above notices are retained on all copies.
  34. * Permission to modify the code and to distribute modified code is granted,
  35. * provided the above notices are retained, and a notice that the code was
  36. * modified is included with the above copyright notice.
  37. */
  38. #include "libguile/__scm.h"
  39. #include "libguile/gc.h"
  40. #include "libguile/bdw-gc.h"
  41. #include "libguile/threads.h"
  42. #include <gc/gc_inline.h> /* GC_generic_malloc_many */
  43. #define SCM_INLINE_GC_GRANULE_WORDS 2
  44. #define SCM_INLINE_GC_GRANULE_BYTES \
  45. (sizeof(void *) * SCM_INLINE_GC_GRANULE_WORDS)
  46. /* A freelist set contains SCM_INLINE_GC_FREELIST_COUNT pointers to
  47. singly linked lists of objects of different sizes, the ith one
  48. containing objects i + 1 granules in size. This setting of
  49. SCM_INLINE_GC_FREELIST_COUNT will hold freelists for allocations of
  50. up to 256 bytes. */
  51. #define SCM_INLINE_GC_FREELIST_COUNT (256U / SCM_INLINE_GC_GRANULE_BYTES)
  52. static inline size_t
  53. scm_inline_gc_bytes_to_freelist_index (size_t bytes)
  54. {
  55. return (bytes - 1U) / SCM_INLINE_GC_GRANULE_BYTES;
  56. }
  57. static inline size_t
  58. scm_inline_gc_freelist_object_size (size_t idx)
  59. {
  60. return (idx + 1U) * SCM_INLINE_GC_GRANULE_BYTES;
  61. }
  62. /* The values of these must match the internal POINTERLESS and NORMAL
  63. definitions in libgc, for which unfortunately there are no external
  64. definitions. Alack. */
  65. typedef enum scm_inline_gc_kind
  66. {
  67. SCM_INLINE_GC_KIND_POINTERLESS,
  68. SCM_INLINE_GC_KIND_NORMAL
  69. } scm_inline_gc_kind;
  70. static inline void *
  71. scm_inline_gc_alloc (void **freelist, size_t idx, scm_inline_gc_kind kind)
  72. {
  73. void *head = *freelist;
  74. if (SCM_UNLIKELY (!head))
  75. {
  76. size_t bytes = scm_inline_gc_freelist_object_size (idx);
  77. GC_generic_malloc_many (bytes, kind, freelist);
  78. head = *freelist;
  79. if (SCM_UNLIKELY (!head))
  80. return (*GC_get_oom_fn ()) (bytes);
  81. }
  82. *freelist = *(void **)(head);
  83. return head;
  84. }
  85. static inline void *
  86. scm_inline_gc_malloc_pointerless (scm_i_thread *thread, size_t bytes)
  87. {
  88. size_t idx = scm_inline_gc_bytes_to_freelist_index (bytes);
  89. if (SCM_UNLIKELY (idx >= SCM_INLINE_GC_FREELIST_COUNT))
  90. return GC_malloc_atomic (bytes);
  91. return scm_inline_gc_alloc
  92. (&thread->pointerless_freelists[idx], idx, SCM_INLINE_GC_KIND_POINTERLESS);
  93. }
  94. static inline void *
  95. scm_inline_gc_malloc (scm_i_thread *thread, size_t bytes)
  96. {
  97. size_t idx = scm_inline_gc_bytes_to_freelist_index (bytes);
  98. if (SCM_UNLIKELY (idx >= SCM_INLINE_GC_FREELIST_COUNT))
  99. return GC_malloc (bytes);
  100. return scm_inline_gc_alloc
  101. (&thread->freelists[idx], idx, SCM_INLINE_GC_KIND_NORMAL);
  102. }
  103. static inline void *
  104. scm_inline_gc_malloc_words (scm_i_thread *thread, size_t words)
  105. {
  106. return scm_inline_gc_malloc (thread, words * sizeof (void *));
  107. }
  108. static inline SCM
  109. scm_inline_cell (scm_i_thread *thread, scm_t_bits car, scm_t_bits cdr)
  110. {
  111. SCM cell = SCM_PACK_POINTER (scm_inline_gc_malloc_words (thread, 2));
  112. SCM_GC_SET_CELL_WORD (cell, 0, car);
  113. SCM_GC_SET_CELL_WORD (cell, 1, cdr);
  114. return cell;
  115. }
  116. static inline SCM
  117. scm_inline_double_cell (scm_i_thread *thread, scm_t_bits car, scm_t_bits cbr,
  118. scm_t_bits ccr, scm_t_bits cdr)
  119. {
  120. SCM cell = SCM_PACK_POINTER (scm_inline_gc_malloc_words (thread, 4));
  121. SCM_GC_SET_CELL_WORD (cell, 0, car);
  122. SCM_GC_SET_CELL_WORD (cell, 1, cbr);
  123. SCM_GC_SET_CELL_WORD (cell, 2, ccr);
  124. SCM_GC_SET_CELL_WORD (cell, 3, cdr);
  125. return cell;
  126. }
  127. static inline SCM
  128. scm_inline_words (scm_i_thread *thread, scm_t_bits car, scm_t_uint32 n_words)
  129. {
  130. SCM obj = SCM_PACK_POINTER (scm_inline_gc_malloc_words (thread, n_words));
  131. SCM_GC_SET_CELL_WORD (obj, 0, car);
  132. return obj;
  133. }
  134. static inline SCM
  135. scm_inline_cons (scm_i_thread *thread, SCM x, SCM y)
  136. {
  137. return scm_inline_cell (thread, SCM_UNPACK (x), SCM_UNPACK (y));
  138. }
  139. #endif /* SCM_GC_INLINE_H */
  140. /*
  141. Local Variables:
  142. c-file-style: "gnu"
  143. End:
  144. */