From: Chris Hanson Date: Fri, 28 Apr 2017 06:34:48 +0000 (-0700) Subject: Move bytevector access macros into "object.h". X-Git-Tag: mit-scheme-pucked-9.2.12~14^2~117 X-Git-Url: https://birchwood-abbey.net/git?a=commitdiff_plain;h=e0bd9e9d3c5c7c0e75b31e51fd026fa5ea8a6d28;p=mit-scheme.git Move bytevector access macros into "object.h". --- diff --git a/src/microcode/bytevector.c b/src/microcode/bytevector.c index d25b33de9..1cc19b9d3 100644 --- a/src/microcode/bytevector.c +++ b/src/microcode/bytevector.c @@ -29,14 +29,6 @@ USA. #include "scheme.h" #include "prims.h" -#define BYTEVECTOR_LENGTH(v) \ - (OBJECT_DATUM (MEMORY_REF ((v), BYTEVECTOR_LENGTH_INDEX))) - -#define SET_BYTEVECTOR_LENGTH(v, n_bytes) \ - MEMORY_SET ((v), BYTEVECTOR_LENGTH_INDEX, (MAKE_OBJECT (0, (n_bytes)))) - -#define BYTEVECTOR_POINTER(v) ((uint8_t *) (MEMORY_LOC ((v), BYTEVECTOR_DATA))) - uint8_t * arg_bytevector (int n, unsigned long * len_r) { @@ -100,51 +92,42 @@ DEFINE_PRIMITIVE ("bytevector-length", Prim_bytevector_length, 1, 1, 0) DEFINE_PRIMITIVE ("bytevector-u8-ref", Prim_bytevector_u8_ref, 2, 2, 0) { PRIMITIVE_HEADER (2); - { - unsigned long length; - uint8_t * v = (arg_bytevector (1, (&length))); - unsigned long index = (arg_ulong_index_integer (2, length)); - PRIMITIVE_RETURN (ulong_to_integer (v[index])); - } + unsigned long length; + uint8_t * v = (arg_bytevector (1, (&length))); + unsigned long index = (arg_ulong_index_integer (2, length)); + PRIMITIVE_RETURN (ulong_to_integer (v[index])); } DEFINE_PRIMITIVE ("bytevector-u8-set!", Prim_bytevector_u8_set, 3, 3, 0) { PRIMITIVE_HEADER (3); - { - unsigned long length; - uint8_t * v = (arg_bytevector (1, (&length))); - unsigned long index = (arg_ulong_index_integer (2, length)); - uint8_t value = (arg_byte (3)); - v[index] = value; - } + unsigned long length; + uint8_t * v = (arg_bytevector (1, (&length))); + unsigned long index = (arg_ulong_index_integer (2, length)); + (v[index]) = (arg_byte (3)); PRIMITIVE_RETURN (UNSPECIFIC); } DEFINE_PRIMITIVE ("bytevector-fill!", Prim_bytevector_fill, 4, 4, 0) { PRIMITIVE_HEADER (4); - { - unsigned long length; - uint8_t * v = (arg_bytevector (1, (&length))); - uint8_t value = (arg_byte (2)); - unsigned long end = (arg_ulong_index_integer (4, (length + 1))); - unsigned long start = (arg_ulong_index_integer (3, (end + 1))); - memset ((v + start), value, (end - start)); - } + unsigned long length; + uint8_t * v = (arg_bytevector (1, (&length))); + uint8_t value = (arg_byte (2)); + unsigned long end = (arg_ulong_index_integer (4, (length + 1))); + unsigned long start = (arg_ulong_index_integer (3, (end + 1))); + memset ((v + start), value, (end - start)); PRIMITIVE_RETURN (UNSPECIFIC); } DEFINE_PRIMITIVE ("bytevector-copy", Prim_bytevector_copy, 3, 3, 0) { PRIMITIVE_HEADER (3); - { - unsigned long length; - uint8_t * v = (arg_bytevector (1, (&length))); - unsigned long end = (arg_ulong_index_integer (3, (length + 1))); - unsigned long start = (arg_ulong_index_integer (2, (end + 1))); - PRIMITIVE_RETURN (memory_to_bytevector ((end - start), (v + start))); - } + unsigned long length; + uint8_t * v = (arg_bytevector (1, (&length))); + unsigned long end = (arg_ulong_index_integer (3, (length + 1))); + unsigned long start = (arg_ulong_index_integer (2, (end + 1))); + PRIMITIVE_RETURN (memory_to_bytevector ((end - start), (v + start))); } DEFINE_PRIMITIVE ("bytevector-copy!", Prim_bytevector_copyx, 5, 5, 0) diff --git a/src/microcode/object.h b/src/microcode/object.h index 9b5e02a28..fcc36ee0a 100644 --- a/src/microcode/object.h +++ b/src/microcode/object.h @@ -255,39 +255,51 @@ extern bool string_p (SCHEME_OBJECT); #define VECTOR_REF(v, i) (MEMORY_REF ((v), ((i) + 1))) #define VECTOR_SET(v, i, object) MEMORY_SET ((v), ((i) + 1), (object)) -/* String Operations */ +/* Bytevector operations */ -/* Add 1 byte to length to account for '\0' at end of string. - Add 1 word to length to account for string header word. */ -#define STRING_LENGTH_TO_GC_LENGTH(n_chars) \ - ((BYTES_TO_WORDS ((n_chars) + 1)) + BYTEVECTOR_LENGTH_SIZE) +/* Add 1 word to length to account for string header word. */ +#define BYTEVECTOR_LENGTH_TO_GC_LENGTH(n_chars) \ + ((BYTES_TO_WORDS (n_chars)) + BYTEVECTOR_LENGTH_SIZE) -#define STRING_LENGTH(s) \ - (OBJECT_DATUM (MEMORY_REF ((s), BYTEVECTOR_LENGTH_INDEX))) +#define BYTEVECTOR_DATA_LENGTH(v) ((VECTOR_LENGTH (v)) - BYTEVECTOR_LENGTH_SIZE) -#define SET_STRING_LENGTH(s, n_chars) do \ -{ \ - MEMORY_SET ((s), \ - BYTEVECTOR_LENGTH_INDEX, \ - (MAKE_OBJECT (0, (n_chars)))); \ - STRING_SET ((s), (n_chars), '\0'); \ +#define BYTEVECTOR_LENGTH(v) \ + (OBJECT_DATUM (MEMORY_REF ((v), BYTEVECTOR_LENGTH_INDEX))) + +#define SET_BYTEVECTOR_LENGTH(v, n_bytes) \ + MEMORY_SET ((v), BYTEVECTOR_LENGTH_INDEX, (MAKE_OBJECT (0, (n_bytes)))) + +#define BYTEVECTOR_POINTER(v) ((uint8_t *) (MEMORY_LOC ((v), BYTEVECTOR_DATA))) +#define BYTEVECTOR_LOC(v, i) ((BYTEVECTOR_POINTER (v)) + (i)) +#define BYTEVECTOR_REF(s, i) (* (BYTEVECTOR_LOC ((s), (i)))) +#define BYTEVECTOR_SET(s, i, c) ((* (BYTEVECTOR_LOC ((s), (i)))) = (c)) + +/* Legacy string operations */ + +/* Legacy strings are laid out exactly the same way as bytevectors, + except that they have a zero byte at the end that isn't included in + the string's length. */ + +#define STRING_LENGTH_TO_GC_LENGTH(n_chars) (BYTEVECTOR_LENGTH_TO_GC_LENGTH ((n_chars) + 1)) +#define STRING_LENGTH BYTEVECTOR_LENGTH + +#define SET_STRING_LENGTH(s, n_chars) do \ +{ \ + SET_BYTEVECTOR_LENGTH((s), (n_chars)); \ + STRING_SET ((s), (n_chars), '\0'); \ } while (0) /* Subtract 1 to account for the fact that we maintain a '\0' at the end of the string. */ #define MAXIMUM_STRING_LENGTH(s) \ - ((((VECTOR_LENGTH (s)) - BYTEVECTOR_LENGTH_SIZE) * (sizeof (SCHEME_OBJECT))) \ - - 1) + (((BYTEVECTOR_DATA_LENGTH (s)) * (sizeof (SCHEME_OBJECT))) - 1) #define SET_MAXIMUM_STRING_LENGTH(s, n_chars) \ (SET_VECTOR_LENGTH ((s), (STRING_LENGTH_TO_GC_LENGTH (n_chars)))) -#define STRING_LOC(s, i) \ - (((unsigned char *) (MEMORY_LOC (s, BYTEVECTOR_DATA))) + (i)) - -#define STRING_POINTER(s) ((char *) (MEMORY_LOC (s, BYTEVECTOR_DATA))) -#define STRING_BYTE_PTR(s) ((uint8_t *) (MEMORY_LOC (s, BYTEVECTOR_DATA))) - +#define STRING_BYTE_PTR BYTEVECTOR_POINTER +#define STRING_POINTER(s) ((char *) (STRING_BYTE_PTR (s))) +#define STRING_LOC(s, i) (((unsigned char *) (STRING_BYTE_PTR (s))) + (i)) #define STRING_REF(s, i) (* (STRING_LOC ((s), (i)))) #define STRING_SET(s, i, c) ((* (STRING_LOC ((s), (i)))) = (c))