Skip to content

Commit 009dee5

Browse files
committed
add stdcSlice, stdcUninitSlice, stdcFreeSlice allocation utilities
1 parent 62456df commit 009dee5

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

source/mir/ndslice/allocation.d

+69-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ $(T2 ndarray, Allocates a common n-dimensional array from a slice. )
1212
$(T2 shape, Returns a shape of a common n-dimensional array. )
1313
$(T2 slice, Allocates a slice using GC.)
1414
$(T2 uninitSlice, Allocates an uninitialized slice using GC. )
15+
$(T2 stdcSlice, Allocates a slice copy using `core.stdc.stdlib.malloc`)
16+
$(T2 stdcFreeSlice, Frees memory using `core.stdc.stdlib.free`)
17+
$(T2 stdcUninitSlice, Allocates an uninitialized slice using `core.stdc.stdlib.malloc`.)
1518
)
1619
1720
@@ -133,7 +136,7 @@ Allocates an uninitialized array and an n-dimensional slice over it.
133136
Params:
134137
lengths = list of lengths for each dimension
135138
Returns:
136-
uninitialized n-dimensional slice
139+
contiguous uninitialized n-dimensional slice
137140
+/
138141
auto uninitSlice(T, size_t N)(size_t[N] lengths...)
139142
{
@@ -446,3 +449,68 @@ nothrow unittest
446449

447450
Mallocator.instance.dispose(sl2.field);
448451
}
452+
453+
/++
454+
Allocates an uninitialized array using `core.stdc.stdlib.malloc` and an n-dimensional slice over it.
455+
Params:
456+
lengths = list of lengths for each dimension
457+
Returns:
458+
contiguous uninitialized n-dimensional slice
459+
See_also:
460+
$(MREF stdcSlice), $(MREF stdcFreeSlice)
461+
+/
462+
Slice!(Contiguous, [N], T*) stdcUninitSlice(T, size_t N)(size_t[N] lengths...)
463+
{
464+
import core.stdc.stdlib: malloc;
465+
immutable len = lengthsProduct(lengths);
466+
auto ptr = len ? cast(T*) malloc(len * T.sizeof) : null;
467+
return ptr.sliced(lengths);
468+
}
469+
470+
/++
471+
Allocates a copy of a slice using `core.stdc.stdlib.malloc`.
472+
Params:
473+
slice = n-dimensional slice
474+
Returns:
475+
contiguous n-dimensional slice
476+
See_also:
477+
$(MREF stdcUninitSlice), $(MREF stdcFreeSlice)
478+
+/
479+
auto stdcSlice(SliceKind kind, size_t[] packs, Iterator)(Slice!(kind, packs, Iterator) slice)
480+
{
481+
alias T = Unqual!(slice.DeepElemType);
482+
static assert (!hasElaborateAssign!T, "stdcSlice is not miplemented for slices that have elaborate assign");
483+
auto ret = stdcUninitSlice!T(slice.shape);
484+
ret[] = slice;
485+
return ret;
486+
}
487+
488+
/++
489+
Frees memory using `core.stdc.stdlib.free`.
490+
Params:
491+
slice = n-dimensional slice
492+
See_also:
493+
$(MREF stdcSlice), $(MREF stdcUninitSlice)
494+
+/
495+
void stdcFreeSlice(size_t[] packs, T)(Slice!(Contiguous, packs, T*) slice)
496+
{
497+
import core.stdc.stdlib: free;
498+
slice._iterator.free;
499+
}
500+
501+
///
502+
unittest
503+
{
504+
import mir.ndslice.topology: iota;
505+
506+
auto i = iota(3, 4);
507+
auto s = i.stdcSlice;
508+
auto t = s.shape.stdcUninitSlice!size_t;
509+
510+
t[] = s;
511+
512+
assert(t == i);
513+
514+
s.stdcFreeSlice;
515+
t.stdcFreeSlice;
516+
}

source/mir/ndslice/package.d

+3
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ $(TR $(TDNW $(SUBMODULE allocation) $(BR)
108108
$(SUBREF allocation, shape)
109109
$(SUBREF allocation, slice)
110110
$(SUBREF allocation, uninitializedSlice)
111+
$(SUBREF allocation, stdcSlice)
112+
$(SUBREF allocation, stdcFreeSlice)
113+
$(SUBREF allocation, stdcUninitSlice)
111114
)
112115
)
113116

0 commit comments

Comments
 (0)