bytevectors.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #ifndef SCM_BYTEVECTORS_H
  2. #define SCM_BYTEVECTORS_H
  3. /* Copyright 2009, 2011, 2018, 2023
  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. #include <libguile/error.h>
  18. #include "libguile/gc.h"
  19. #include "libguile/uniform.h"
  20. /* R6RS bytevectors. */
  21. /* The size in words of the bytevector header (type tag and flags, length,
  22. and pointer to the underlying buffer). */
  23. #define SCM_BYTEVECTOR_HEADER_SIZE 4U
  24. #define SCM_BYTEVECTOR_LENGTH(_bv) \
  25. ((size_t) SCM_CELL_WORD_1 (_bv))
  26. #define SCM_BYTEVECTOR_CONTENTS(_bv) \
  27. ((signed char *) SCM_CELL_WORD_2 (_bv))
  28. #define SCM_BYTEVECTOR_PARENT(_bv) \
  29. (SCM_CELL_OBJECT_3 (_bv))
  30. SCM_API SCM scm_endianness_big;
  31. SCM_API SCM scm_endianness_little;
  32. SCM_API SCM scm_c_make_bytevector (size_t);
  33. SCM_API int scm_is_bytevector (SCM);
  34. SCM_API size_t scm_c_bytevector_length (SCM);
  35. SCM_API uint8_t scm_c_bytevector_ref (SCM, size_t);
  36. SCM_API void scm_c_bytevector_set_x (SCM, size_t, uint8_t);
  37. SCM_API SCM scm_make_bytevector (SCM, SCM);
  38. SCM_API SCM scm_bytevector_slice (SCM, SCM, SCM);
  39. SCM_API SCM scm_native_endianness (void);
  40. SCM_API SCM scm_bytevector_p (SCM);
  41. SCM_API SCM scm_bytevector_length (SCM);
  42. SCM_API SCM scm_bytevector_eq_p (SCM, SCM);
  43. SCM_API SCM scm_bytevector_fill_x (SCM, SCM);
  44. SCM_API SCM scm_bytevector_copy_x (SCM, SCM, SCM, SCM, SCM);
  45. SCM_API SCM scm_bytevector_copy (SCM);
  46. SCM_API SCM scm_uniform_array_to_bytevector (SCM);
  47. SCM_API SCM scm_bytevector_to_u8_list (SCM);
  48. SCM_API SCM scm_u8_list_to_bytevector (SCM);
  49. SCM_API SCM scm_uint_list_to_bytevector (SCM, SCM, SCM);
  50. SCM_API SCM scm_bytevector_to_uint_list (SCM, SCM, SCM);
  51. SCM_API SCM scm_sint_list_to_bytevector (SCM, SCM, SCM);
  52. SCM_API SCM scm_bytevector_to_sint_list (SCM, SCM, SCM);
  53. SCM_API SCM scm_bytevector_u16_native_ref (SCM, SCM);
  54. SCM_API SCM scm_bytevector_s16_native_ref (SCM, SCM);
  55. SCM_API SCM scm_bytevector_u32_native_ref (SCM, SCM);
  56. SCM_API SCM scm_bytevector_s32_native_ref (SCM, SCM);
  57. SCM_API SCM scm_bytevector_u64_native_ref (SCM, SCM);
  58. SCM_API SCM scm_bytevector_s64_native_ref (SCM, SCM);
  59. SCM_API SCM scm_bytevector_u8_ref (SCM, SCM);
  60. SCM_API SCM scm_bytevector_s8_ref (SCM, SCM);
  61. SCM_API SCM scm_bytevector_uint_ref (SCM, SCM, SCM, SCM);
  62. SCM_API SCM scm_bytevector_sint_ref (SCM, SCM, SCM, SCM);
  63. SCM_API SCM scm_bytevector_u16_ref (SCM, SCM, SCM);
  64. SCM_API SCM scm_bytevector_s16_ref (SCM, SCM, SCM);
  65. SCM_API SCM scm_bytevector_u32_ref (SCM, SCM, SCM);
  66. SCM_API SCM scm_bytevector_s32_ref (SCM, SCM, SCM);
  67. SCM_API SCM scm_bytevector_u64_ref (SCM, SCM, SCM);
  68. SCM_API SCM scm_bytevector_s64_ref (SCM, SCM, SCM);
  69. SCM_API SCM scm_bytevector_u16_native_set_x (SCM, SCM, SCM);
  70. SCM_API SCM scm_bytevector_s16_native_set_x (SCM, SCM, SCM);
  71. SCM_API SCM scm_bytevector_u32_native_set_x (SCM, SCM, SCM);
  72. SCM_API SCM scm_bytevector_s32_native_set_x (SCM, SCM, SCM);
  73. SCM_API SCM scm_bytevector_u64_native_set_x (SCM, SCM, SCM);
  74. SCM_API SCM scm_bytevector_s64_native_set_x (SCM, SCM, SCM);
  75. SCM_API SCM scm_bytevector_u8_set_x (SCM, SCM, SCM);
  76. SCM_API SCM scm_bytevector_s8_set_x (SCM, SCM, SCM);
  77. SCM_API SCM scm_bytevector_uint_set_x (SCM, SCM, SCM, SCM, SCM);
  78. SCM_API SCM scm_bytevector_sint_set_x (SCM, SCM, SCM, SCM, SCM);
  79. SCM_API SCM scm_bytevector_u16_set_x (SCM, SCM, SCM, SCM);
  80. SCM_API SCM scm_bytevector_s16_set_x (SCM, SCM, SCM, SCM);
  81. SCM_API SCM scm_bytevector_u32_set_x (SCM, SCM, SCM, SCM);
  82. SCM_API SCM scm_bytevector_s32_set_x (SCM, SCM, SCM, SCM);
  83. SCM_API SCM scm_bytevector_u64_set_x (SCM, SCM, SCM, SCM);
  84. SCM_API SCM scm_bytevector_s64_set_x (SCM, SCM, SCM, SCM);
  85. SCM_API SCM scm_bytevector_ieee_single_ref (SCM, SCM, SCM);
  86. SCM_API SCM scm_bytevector_ieee_single_native_ref (SCM, SCM);
  87. SCM_API SCM scm_bytevector_ieee_single_set_x (SCM, SCM, SCM, SCM);
  88. SCM_API SCM scm_bytevector_ieee_single_native_set_x (SCM, SCM, SCM);
  89. SCM_API SCM scm_bytevector_ieee_double_ref (SCM, SCM, SCM);
  90. SCM_API SCM scm_bytevector_ieee_double_native_ref (SCM, SCM);
  91. SCM_API SCM scm_bytevector_ieee_double_set_x (SCM, SCM, SCM, SCM);
  92. SCM_API SCM scm_bytevector_ieee_double_native_set_x (SCM, SCM, SCM);
  93. SCM_API SCM scm_string_to_utf8 (SCM);
  94. SCM_API SCM scm_string_to_utf16 (SCM, SCM);
  95. SCM_API SCM scm_string_to_utf32 (SCM, SCM);
  96. SCM_API SCM scm_utf8_to_string (SCM);
  97. SCM_API SCM scm_utf16_to_string (SCM, SCM);
  98. SCM_API SCM scm_utf32_to_string (SCM, SCM);
  99. /* Internal API. */
  100. #define SCM_BYTEVECTOR_P(x) \
  101. (SCM_HAS_TYP7 (x, scm_tc7_bytevector))
  102. #define SCM_BYTEVECTOR_FLAGS(_bv) \
  103. (SCM_CELL_TYPE (_bv) >> 7UL)
  104. #define SCM_SET_BYTEVECTOR_FLAGS(_bv, _f) \
  105. SCM_SET_CELL_TYPE ((_bv), \
  106. scm_tc7_bytevector | ((scm_t_bits)(_f) << 7UL))
  107. #define SCM_F_BYTEVECTOR_CONTIGUOUS 0x100UL
  108. #define SCM_F_BYTEVECTOR_IMMUTABLE 0x200UL
  109. #define SCM_MUTABLE_BYTEVECTOR_P(x) \
  110. (SCM_NIMP (x) && \
  111. ((SCM_CELL_TYPE (x) & (0x7fUL | (SCM_F_BYTEVECTOR_IMMUTABLE << 7UL))) \
  112. == scm_tc7_bytevector))
  113. #define SCM_BYTEVECTOR_ELEMENT_TYPE(_bv) \
  114. (SCM_BYTEVECTOR_FLAGS (_bv) & 0xffUL)
  115. #define SCM_BYTEVECTOR_CONTIGUOUS_P(_bv) \
  116. (SCM_BYTEVECTOR_FLAGS (_bv) & SCM_F_BYTEVECTOR_CONTIGUOUS)
  117. #define SCM_BYTEVECTOR_TYPE_SIZE(var) \
  118. (scm_i_array_element_type_sizes[SCM_BYTEVECTOR_ELEMENT_TYPE (var)]/8)
  119. #define SCM_BYTEVECTOR_TYPED_LENGTH(var) \
  120. (SCM_BYTEVECTOR_LENGTH (var) / SCM_BYTEVECTOR_TYPE_SIZE (var))
  121. /* Hint that is passed to `scm_gc_malloc ()' and friends. */
  122. #define SCM_GC_BYTEVECTOR "bytevector"
  123. #define SCM_VALIDATE_BYTEVECTOR(_pos, _obj) \
  124. SCM_ASSERT_TYPE (SCM_BYTEVECTOR_P (_obj), (_obj), (_pos), \
  125. FUNC_NAME, "bytevector")
  126. SCM_INTERNAL SCM scm_i_make_typed_bytevector (size_t, scm_t_array_element_type);
  127. SCM_INTERNAL SCM scm_c_take_typed_bytevector (signed char *, size_t,
  128. scm_t_array_element_type, SCM);
  129. SCM_INTERNAL void scm_bootstrap_bytevectors (void);
  130. SCM_INTERNAL void scm_init_bytevectors (void);
  131. SCM_INTERNAL SCM scm_i_native_endianness;
  132. SCM_INTERNAL SCM scm_c_take_gc_bytevector (signed char *, size_t, SCM);
  133. SCM_INTERNAL int scm_i_print_bytevector (SCM, SCM, scm_print_state *);
  134. SCM_INTERNAL SCM scm_c_shrink_bytevector (SCM, size_t);
  135. SCM_INTERNAL void scm_i_bytevector_generalized_set_x (SCM, size_t, SCM);
  136. SCM_INTERNAL SCM scm_null_bytevector;
  137. #endif /* SCM_BYTEVECTORS_H */