
Hi, The docs for newArray# states: "Create a new mutable array of specified size (in bytes), in the specified state thread, with each element containing the specified initial value." Why is the size in bytes? Is Array# meant to be used both for boxed and unboxed values? For arrays of boxed values I'd expect the size to be the number of elements and the allocated size the number of elements multiplied by the size of a pointer. Are the docs correct? If so, how do I allocate an Array# of pointers? I'm trying to implement the following array type: data MurableArray s a = Array { unArray :: !(MutableArray# s a) } i.e. an array of boxed values. I can't figure out how to write new :: Int -> a -> ST s (MutableArray s a) where the first argument is the number of elements (of type a). Johan

On Tue, Jan 18, 2011 at 4:18 PM, Johan Tibell
Hi,
The docs for newArray# states:
"Create a new mutable array of specified size (in bytes), in the specified state thread, with each element containing the specified initial value."
Why is the size in bytes? Is Array# meant to be used both for boxed and unboxed values? For arrays of boxed values I'd expect the size to be the number of elements and the allocated size the number of elements multiplied by the size of a pointer. Are the docs correct? If so, how do I allocate an Array# of pointers? I'm trying to implement the following array type:
data MurableArray s a = Array { unArray :: !(MutableArray# s a) }
i.e. an array of boxed values. I can't figure out how to write
new :: Int -> a -> ST s (MutableArray s a)
where the first argument is the number of elements (of type a).
Johan
The docs are wrong. taking a look in GHC.Arr we find: unsafeArray' :: Ix i => (i,i) -> Int -> [(Int, e)] -> Array i e unsafeArray' (l,u) n@(I# n#) ies = runST (ST $ \s1# -> case newArray# n# arrEleBottom s1# of (# s2#, marr# #) -> foldr (fill marr#) (done l u n marr#) ies s2#) The 'n' argument gets passed in from 'safeRange', so it is really the number of elements you'd like to have in the created array. Antoine
_______________________________________________ Glasgow-haskell-users mailing list Glasgow-haskell-users@haskell.org http://www.haskell.org/mailman/listinfo/glasgow-haskell-users

On 18 January 2011 22:18, Johan Tibell
Why is the size in bytes?
I think the docs are wrong. The code for newArray# (in PrimOps.cmm) interprets n as a size in words: {{{ stg_newArrayzh { W_ words, n, init, arr, p, size; /* Args: R1 = words, R2 = initialisation value */ n = R1; MAYBE_GC(R2_PTR,stg_newArrayzh); // the mark area contains one byte for each 2^MUT_ARR_PTRS_CARD_BITS words // in the array, making sure we round up, and then rounding up to a whole // number of words. size = n + mutArrPtrsCardWords(n); words = BYTES_TO_WDS(SIZEOF_StgMutArrPtrs) + size; ("ptr" arr) = foreign "C" allocate(MyCapability() "ptr",words) [R2]; TICK_ALLOC_PRIM(SIZEOF_StgMutArrPtrs, WDS(n), 0); ... } }}} Cheers, Max

On 18/01/2011, at 22:18, Johan Tibell wrote:
The docs for newArray# states:
"Create a new mutable array of specified size (in bytes), in the specified state thread, with each element containing the specified initial value."
The docs are wrong.
I'm trying to implement the following array type:
data MurableArray s a = Array { unArray :: !(MutableArray# s a) }
Have a look at the implementation of http://hackage.haskell.org/packages/archive/primitive/0.3.1/doc/html/Data-Pr... Roman
participants (4)
-
Antoine Latter
-
Johan Tibell
-
Max Bolingbroke
-
Roman Leshchinskiy