on e856425 help sign in
39 lines 1.9 kB history raw
1-------------------------------------------------------------------------------
2-- Macros to access array pointers
3-------------------------------------------------------------------------------
5-- The Storable instance of () has size 0. We ensure that the size is non-zero
6-- to avoid a zero sized element and issues due to that.
7-- See https://mail.haskell.org/pipermail/libraries/2022-January/thread.html
8--
9-- XXX Check the core to see if max can be statically eliminated. llvm can
10-- eliminate the comparison, but not sure if GHC NCG can.
11#define SIZE_OF(a) max 1 (sizeOf (undefined :: a))
13-- Move the pointer to ith element of specified type. Type is specified as the
14-- type variable in the signature of the function where this macro is used.
15#define PTR_NEXT(ptr,a) ptr `plusPtr` SIZE_OF(a)
16#define PTR_PREV(ptr,a) ptr `plusPtr` negate (SIZE_OF(a))
18#define PTR_INDEX(ptr,i,a) ptr `plusPtr` (SIZE_OF(a) * i)
19#define PTR_RINDEX(ptr,i,a) ptr `plusPtr` negate (SIZE_OF(a) * (i + 1))
21-- XXX If we know that the array is guaranteed to have size multiples of the
22-- element size then we can use a simpler check saying "ptr < end". Since we
23-- always allocate in multiples of elem we can use the simpler check and assert
24-- the rigorous check.
25#define PTR_VALID(ptr,end,a) ptr `plusPtr` SIZE_OF(a) <= end
26#define PTR_INVALID(ptr,end,a) ptr `plusPtr` SIZE_OF(a) > end
28-------------------------------------------------------------------------------
29-- Macros to access array indices
30-------------------------------------------------------------------------------
32#define INDEX_NEXT(i,a) i + SIZE_OF(a)
33#define INDEX_PREV(i,a) i - SIZE_OF(a)
35#define INDEX_OF(base,i,a) base + (SIZE_OF(a) * i)
36#define RINDEX_OF(base,i,a) base - (SIZE_OF(a) * (i + 1))
38#define INDEX_VALID(i,end,a) i + SIZE_OF(a) <= end
39#define INDEX_INVALID(i,end,a) i + SIZE_OF(a) > end