ndslice and RC containers

ZombineDev via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Sep 22 06:30:28 PDT 2016


On Thursday, 22 September 2016 at 12:38:57 UTC, Nordlöw wrote:
> Is ndslice' Slice() prepared for integration with containers 
> with reference counting semantics?
>
> I wonder because according to the docs they internally store a 
> pointer and an offset. What is that pointer supposed to point 
> to except from obviously currently a GC-allocated array?
>
> See:
>
> https://dlang.org/phobos/std_experimental_ndslice_slice.html#.Slice

ndslice (i.e. Slice(size_t N, Range) ) is a generalization of D's 
built-in slices (i.e. T[]) to N dimensions. Just like them, it 
doesn't handle memory ownership - it just provides a view over 
existing data. Usually, there are two use cases:
1) Wrapping an existing array:
    1.1) You allocate an array however you want (GC, RC, plain 
malloc)
         or just use a slice to an array returned from a 
third-party library
    1.2) Wrap it via sliced [1]
    1.3) Use it
    1.4) Free it:
    1.4.a) Let the GC reclaim it
    1.4.b) Let the RC reclaim it
    1.4.c) Free it manually.

2) Making a new array
    2.1.a) Using the GC via slice [2]
    2.1.b) Using std.experimental.allcator via makeSlice [3]
    2.2) Use it
    2.3) Free it:
    2.3.a) Let the GC reclaim it automatically
    2.3.b) Manually dispose [4] it from the allocator it was 
allocated from
           (look for the allocator.dispose(tuple.array) pattern in
           the examples of makeSlice).

Please note that the support for creating ndslices via custom 
memory allocators (i.e. makeSlice) was added after dmd-2.071 was 
branched, so you either have to wait for dmd-2.072, or use a 
nightly build (which I recommend).


[1]: 
http://dlang.org/phobos-prerelease/std_experimental_ndslice_slice.html#.sliced

[2]: 
http://dlang.org/phobos-prerelease/std_experimental_ndslice_slice.html#.slice

[3]: 
http://dlang.org/phobos-prerelease/std_experimental_ndslice_slice.html#.makeSlice

[4]: 
http://dlang.org/phobos-prerelease/std_experimental_allocator.html#.dispose


More information about the Digitalmars-d-learn mailing list