gc-inline.h 5.0 KB

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