gc-inline.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #ifndef SCM_GC_INLINE_H
  2. #define SCM_GC_INLINE_H
  3. /* Copyright 1995-1996,1998-2004,2006-2014,2018-2019
  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. static inline size_t
  38. scm_inline_gc_bytes_to_freelist_index (size_t bytes)
  39. {
  40. return (bytes - 1U) / SCM_INLINE_GC_GRANULE_BYTES;
  41. }
  42. static inline size_t
  43. scm_inline_gc_freelist_object_size (size_t idx)
  44. {
  45. return (idx + 1U) * SCM_INLINE_GC_GRANULE_BYTES;
  46. }
  47. /* The values of these must match the internal POINTERLESS and NORMAL
  48. definitions in libgc, for which unfortunately there are no external
  49. definitions. Alack. */
  50. typedef enum scm_inline_gc_kind
  51. {
  52. SCM_INLINE_GC_KIND_POINTERLESS,
  53. SCM_INLINE_GC_KIND_NORMAL
  54. } scm_inline_gc_kind;
  55. static inline void *
  56. scm_inline_gc_alloc (void **freelist, size_t idx, scm_inline_gc_kind kind)
  57. {
  58. void *head = *freelist;
  59. if (SCM_UNLIKELY (!head))
  60. {
  61. size_t bytes = scm_inline_gc_freelist_object_size (idx);
  62. GC_generic_malloc_many (bytes, kind, freelist);
  63. head = *freelist;
  64. if (SCM_UNLIKELY (!head))
  65. return (*GC_get_oom_fn ()) (bytes);
  66. }
  67. *freelist = *(void **)(head);
  68. return head;
  69. }
  70. static inline void *
  71. scm_inline_gc_malloc_pointerless (scm_thread *thread, size_t bytes)
  72. {
  73. size_t idx = scm_inline_gc_bytes_to_freelist_index (bytes);
  74. if (SCM_UNLIKELY (idx >= SCM_INLINE_GC_FREELIST_COUNT))
  75. return GC_malloc_atomic (bytes);
  76. return scm_inline_gc_alloc
  77. (&thread->pointerless_freelists[idx], idx, SCM_INLINE_GC_KIND_POINTERLESS);
  78. }
  79. static inline void *
  80. scm_inline_gc_malloc (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 (bytes);
  85. return scm_inline_gc_alloc
  86. (&thread->freelists[idx], idx, SCM_INLINE_GC_KIND_NORMAL);
  87. }
  88. static inline void *
  89. scm_inline_gc_malloc_words (scm_thread *thread, size_t words)
  90. {
  91. return scm_inline_gc_malloc (thread, words * sizeof (void *));
  92. }
  93. static inline void *
  94. scm_inline_gc_malloc_pointerless_words (scm_thread *thread, size_t words)
  95. {
  96. return scm_inline_gc_malloc_pointerless (thread, words * sizeof (void *));
  97. }
  98. static inline SCM
  99. scm_inline_cell (scm_thread *thread, scm_t_bits car, scm_t_bits cdr)
  100. {
  101. SCM cell = SCM_PACK_POINTER (scm_inline_gc_malloc_words (thread, 2));
  102. SCM_GC_SET_CELL_WORD (cell, 0, car);
  103. SCM_GC_SET_CELL_WORD (cell, 1, cdr);
  104. return cell;
  105. }
  106. static inline SCM
  107. scm_inline_double_cell (scm_thread *thread, scm_t_bits car, scm_t_bits cbr,
  108. scm_t_bits ccr, scm_t_bits cdr)
  109. {
  110. SCM cell = SCM_PACK_POINTER (scm_inline_gc_malloc_words (thread, 4));
  111. SCM_GC_SET_CELL_WORD (cell, 0, car);
  112. SCM_GC_SET_CELL_WORD (cell, 1, cbr);
  113. SCM_GC_SET_CELL_WORD (cell, 2, ccr);
  114. SCM_GC_SET_CELL_WORD (cell, 3, cdr);
  115. return cell;
  116. }
  117. static inline SCM
  118. scm_inline_words (scm_thread *thread, scm_t_bits car, uint32_t n_words)
  119. {
  120. SCM obj = SCM_PACK_POINTER (scm_inline_gc_malloc_words (thread, n_words));
  121. SCM_GC_SET_CELL_WORD (obj, 0, car);
  122. return obj;
  123. }
  124. static inline SCM
  125. scm_inline_cons (scm_thread *thread, SCM x, SCM y)
  126. {
  127. return scm_inline_cell (thread, SCM_UNPACK (x), SCM_UNPACK (y));
  128. }
  129. #endif /* SCM_GC_INLINE_H */