Shared.xs

filter: Statementstmt 93.1% 493 / 529 Branchbran 57.9% 432 / 746 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 72.5% 925 / 1275 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« ndarray.h ↑ Summary
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5 #include "ppport.h"
6 #include "ndarray.h"
7  
8 #ifdef HAVE_PDL
9 #include <pdlcore.h>
10 static Core *PDL; /* PDL core C functions; bootstrapped lazily on first as_pdl_alias */
11  
12 /* Keep the mapping alive until PDL releases the external data. */
13 static void nda_pdl_nofree(pdl *p, Size_t param) {
14 dTHX;
15 p->data = 0;
16 SvREFCNT_dec(INT2PTR(SV *, param));
17 }
18  
19 /* Load PDL::Core and grab its Core struct on first use, so plain use of the module
20 without as_pdl_alias does not pull in PDL. */
21 static void nda_boot_pdl(pTHX) {
22 SV *coresv;
23 if (PDL) return;
24 perl_require_pv("PDL/Core.pm");
25 if (SvTRUE(ERRSV)) croak("%s", SvPV_nolen(ERRSV));
26 coresv = get_sv("PDL::SHARE", 0);
27 if (!coresv) croak("Data::NDArray::Shared: PDL::Core not loadable (needed for as_pdl_alias)");
28 Core *core = INT2PTR(Core *, SvIV(coresv));
29 if (!core) croak("Data::NDArray::Shared: got a NULL PDL Core pointer");
30 if (core->Version != PDL_CORE_VERSION)
31 croak("Data::NDArray::Shared: please rebuild against the installed PDL (Core version mismatch)");
32 PDL = core;
33 }
34 #endif
35  
36 #define EXTRACT(sv) \
37 if (!sv_isobject(sv) || !sv_derived_from(sv, "Data::NDArray::Shared")) \
38 croak("Expected a Data::NDArray::Shared object"); \
39 NdaHandle *h = INT2PTR(NdaHandle*, SvIV(SvRV(sv))); \
40 if (!h) croak("Attempted to use a destroyed Data::NDArray::Shared object"); \
41 NdaHandle *h0 = h; PERL_UNUSED_VAR(h0); \
42 sv_2mortal(SvREFCNT_inc(SvRV(sv)))
43  
44 /* Re-read the handle after a call that can run Perl code (tied/overloaded
45 * argument magic, tied-array fetches). That code may call $obj->DESTROY
46 * explicitly, which frees the handle and zeroes the IV; EXTRACT's mortal
47 * pins the referent only against refcount-driven destruction, not an
48 * explicit DESTROY, so the local `h` would dangle. Used only where magic
49 * can actually intervene between EXTRACT and the first use of h.
50 * The same Perl can also REPLACE the invocant ($obj = 42 mutates ST(0),
51 * because Perl passes aliases), hence the SvROK re-check before SvRV. */
52 #define REEXTRACT(sv) \
53 if (!SvROK(sv)) \
54 croak("Data::NDArray::Shared object was replaced during the call"); \
55 h = INT2PTR(NdaHandle*, SvIV(SvRV(sv))); \
56 if (h != h0) croak("Data::NDArray::Shared object replaced or destroyed during the call")
57  
58 #define MAKE_OBJ(class, handle) \
59 SV *obj = newSViv(PTR2IV(handle)); \
60 SV *ref = newRV_noinc(obj); \
61 sv_bless(ref, gv_stashpv(class, GV_ADD)); \
62 RETVAL = ref
63  
64 /* ----------------------------------------------------------------
65 * Typed element get: read element e of dtype dt, return the dtype-correct SV
66 * (float -> newSVnv, signed -> newSViv, unsigned -> newSVuv).
67 * ---------------------------------------------------------------- */
68 1231 static uint32_t nda_dtype_bytes(int dt) {
69 1231 switch (dt) {
70 1166 case NDA_F64: case NDA_I64: case NDA_U64: return 8;
71 43 case NDA_F32: case NDA_I32: case NDA_U32: return 4;
72 8 case NDA_I16: case NDA_U16: return 2;
73 14 default: return 1; /* I8 / U8 */
74 }
75 }
76  
77 /* Copy element e's raw bytes into buf (caller holds a lock). Building the SV is
78 deferred to nda_sv_from_raw so a croak-capable alloc never runs under the lock
79 (an OOM longjmp there would strand this process's read lock -> writers hang). */
80 1172 static void nda_read_raw(NdaHandle *h, uint64_t e, char buf[8]) {
81 1172 uint32_t nb = nda_dtype_bytes(h->dtype); /* cached: peer hdr->dtype must not resize the element */
82 1172 memcpy(buf, nda_data(h) + e * nb, nb);
83 1172 }
84  
85 /* Build an SV from raw element bytes (NO lock needed). */
86 4041 static SV *nda_sv_from_raw(pTHX_ int dtype, const char *buf) {
87 4041 switch (dtype) {
88 1610 case NDA_F64: { double v; memcpy(&v, buf, 8); return newSVnv(v); }
89 277 case NDA_F32: { float v; memcpy(&v, buf, 4); return newSVnv((double)v); }
90 278 case NDA_I64: { int64_t v; memcpy(&v, buf, 8); return newSViv((IV)v); }
91 305 case NDA_I32: { int32_t v; memcpy(&v, buf, 4); return newSViv((IV)v); }
92 259 case NDA_I16: { int16_t v; memcpy(&v, buf, 2); return newSViv((IV)v); }
93 259 case NDA_I8: { int8_t v = (int8_t)buf[0]; return newSViv((IV)v); }
94 262 case NDA_U64: { uint64_t v; memcpy(&v, buf, 8); return newSVuv((UV)v); }
95 263 case NDA_U32: { uint32_t v; memcpy(&v, buf, 4); return newSVuv((UV)v); }
96 261 case NDA_U16: { uint16_t v; memcpy(&v, buf, 2); return newSVuv((UV)v); }
97 267 case NDA_U8: { uint8_t v = (uint8_t)buf[0]; return newSVuv((UV)v); }
98 }
99 0 return newSViv(0);
100 }
101  
102 /* Read + build in one step (only for callers that already build after unlock). */
103 0 static SV *nda_get_sv(pTHX_ NdaHandle *h, uint64_t e) {
104 0 char buf[8]; nda_read_raw(h, e, buf); return nda_sv_from_raw(aTHX_ h->dtype, buf);
105 }
106  
107 /* ----------------------------------------------------------------
108 * Typed element set: write element e from an SV, casting/truncating to the
109 * element width (float -> SvNV, signed -> (intN)SvIV, unsigned -> (uintN)SvUV).
110 * Out-of-range integer values wrap to the element width per C cast rules.
111 * ---------------------------------------------------------------- */
112 /* Encode val to its dtype's raw bytes BEFORE locking: SvNV/SvIV/SvUV can croak
113 * (overload/FATAL-numeric/tied FETCH), which under the wrlock would leak it. */
114 480 static uint32_t nda_encode(pTHX_ uint8_t dtype, SV *val, char *out) {
115 480 switch (dtype) {
116 372 case NDA_F64: { double v = (double)SvNV(val); memcpy(out, &v, 8); return 8; }
117 11 case NDA_F32: { float v = (float)SvNV(val); memcpy(out, &v, 4); return 4; }
118 25 case NDA_I64: { int64_t v = (int64_t)SvIV(val); memcpy(out, &v, 8); return 8; }
119 34 case NDA_I32: { int32_t v = (int32_t)SvIV(val); memcpy(out, &v, 4); return 4; }
120 4 case NDA_I16: { int16_t v = (int16_t)SvIV(val); memcpy(out, &v, 2); return 2; }
121 3 case NDA_I8: { int8_t v = (int8_t)SvIV(val); out[0] = (char)v; return 1; }
122 5 case NDA_U64: { uint64_t v = (uint64_t)SvUV(val); memcpy(out, &v, 8); return 8; }
123 3 case NDA_U32: { uint32_t v = (uint32_t)SvUV(val); memcpy(out, &v, 4); return 4; }
124 7 case NDA_U16: { uint16_t v = (uint16_t)SvUV(val); memcpy(out, &v, 2); return 2; }
125 16 case NDA_U8: { uint8_t v = (uint8_t)SvUV(val); out[0] = (char)v; return 1; }
126 }
127 0 return 0;
128 }
129 /* Store pre-encoded bytes at element e (call under the write lock). */
130 434 static void nda_store_bytes(NdaHandle *h, uint64_t e, const char *enc, uint32_t nb) {
131 434 memcpy(nda_data(h) + e * nb, enc, nb);
132 434 }
133  
134 /* ----------------------------------------------------------------
135 * Fill every element with the typed value of val.
136 * ---------------------------------------------------------------- */
137 /* Splat pre-encoded bytes across every element (call under the write lock).
138 * The croak-prone numeric conversion is done by nda_encode BEFORE locking. */
139 43 static void nda_fill_bytes(NdaHandle *h, const char *enc, uint32_t nb) {
140 43 uint64_t size = h->size, e; /* cached immutable count, not peer hdr->size */
141 43 char *base = nda_data(h);
142 43 ▶ if (nb == 1) { memset(base, (unsigned char)enc[0], (size_t)size); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (nb == 1) { memset(base, (unsigned char)enc[0], (size_t)size); return; } 4 39 43
143 #define NDA_FILL_WIDTH(CT) do { \
144 CT v; memcpy(&v, enc, sizeof v); \
145 for (e = 0; e < size; e++) memcpy(base + e * sizeof v, &v, sizeof v); \
146 } while (0)
147 39 switch (nb) {
148 524 ▶ case 2: NDA_FILL_WIDTH(uint16_t); break;
Branch: 1 branch
BranchTrueFalseTotal
case 2: NDA_FILL_WIDTH(uint16_t); break; 520 4 524
149 819 ▶ case 4: NDA_FILL_WIDTH(uint32_t); break;
Branch: 1 branch
BranchTrueFalseTotal
case 4: NDA_FILL_WIDTH(uint32_t); break; 802 17 819
150 890 ▶ case 8: NDA_FILL_WIDTH(uint64_t); break;
Branch: 1 branch
BranchTrueFalseTotal
case 8: NDA_FILL_WIDTH(uint64_t); break; 872 18 890
151 }
152 #undef NDA_FILL_WIDTH
153 }
154  
155 /* ----------------------------------------------------------------
156 * In-place scalar op over every element: e = e OP s, in the dtype's arithmetic
157 * (float dtypes use double; int dtypes use integer arithmetic in the element
158 * width, wrapping per C rules). `op` is '+' or '*'.
159 * ---------------------------------------------------------------- */
160 /* Integer arithmetic is done in UT -- an unsigned type of at least `unsigned int`
161 * rank -- so overflow is a defined wrap (no signed-overflow UB, and no u16/u8
162 * promotion to signed int) and the narrowing cast back to CT gives the documented
163 * two's-complement wrap. UT must be uint32_t for the <=32-bit dtypes, uint64_t
164 * for the 64-bit ones. */
165 #define NDA_SCALAR_INT(CT, UT) do { \
166 CT *p = (CT *)base; \
167 if (op == '+') { CT s = (CT)siv; for (e=0;e<size;e++) p[e] = (CT)((UT)p[e] + (UT)s); } \
168 else { CT s = (CT)siv; for (e=0;e<size;e++) p[e] = (CT)((UT)p[e] * (UT)s); } \
169 } while (0)
170 #define NDA_SCALAR_UINT(CT, UT) do { \
171 CT *p = (CT *)base; \
172 if (op == '+') { CT s = (CT)suv; for (e=0;e<size;e++) p[e] = (CT)((UT)p[e] + (UT)s); } \
173 else { CT s = (CT)suv; for (e=0;e<size;e++) p[e] = (CT)((UT)p[e] * (UT)s); } \
174 } while (0)
175  
176 /* Read the scalar into its dtype form BEFORE the wrlock: SvNV/SvIV/SvUV can
177 * croak (overload / FATAL-numeric / tied FETCH), which under the lock leaks it. */
178 7 static void nda_read_scalar(pTHX_ uint8_t dt, SV *sv, double *s, int64_t *siv, uint64_t *suv) {
179 7 ▶ SvGETMAGIC(sv);
Branch: 2 branches
BranchTrueFalseTotal
SvGETMAGIC(sv); 0 7 7
SvGETMAGIC(sv); 0 0 0
180 7 ▶ if (nda_is_float(dt)) *s = SvNV(sv);
Branch: 1 branch
BranchTrueFalseTotal
if (nda_is_float(dt)) *s = SvNV(sv); 3 4 7
181 4 ▶ else if (nda_is_signed(dt)) *siv = SvIV(sv);
Branch: 1 branch
BranchTrueFalseTotal
else if (nda_is_signed(dt)) *siv = SvIV(sv); 4 0 4
182 0 else *suv = SvUV(sv);
183 7 }
184 7 static void nda_scalar_op_locked(NdaHandle *h, double s, int64_t siv, uint64_t suv, int op) {
185 7 uint64_t size = h->size, e; /* cached immutable count, not peer hdr->size */
186 7 char *base = nda_data(h);
187 7 uint32_t dt = h->dtype; /* cached: peer hdr->dtype must not resize the element */
188 7 ▶ if (nda_is_float(dt)) {
Branch: 1 branch
BranchTrueFalseTotal
if (nda_is_float(dt)) { 3 4 7
189 3 ▶ if (dt == NDA_F64) {
Branch: 1 branch
BranchTrueFalseTotal
if (dt == NDA_F64) { 1 2 3
190 1 double *p = (double *)base;
191 4 ▶ if (op == '+') for (e=0;e<size;e++) p[e] += s; else for (e=0;e<size;e++) p[e] *= s;
Branch: 3 branches
BranchTrueFalseTotal
if (op == '+') for (e=0;e<size;e++) p[e] += s; else for (e=0;e<size;e++) p[e] *= s; 0 1 1
if (op == '+') for (e=0;e<size;e++) p[e] += s; else for (e=0;e<size;e++) p[e] *= s; 0 0 0
if (op == '+') for (e=0;e<size;e++) p[e] += s; else for (e=0;e<size;e++) p[e] *= s; 3 1 4
192 } else { /* F32 */
193 2 float *p = (float *)base; float fs = (float)s;
194 10 ▶ if (op == '+') for (e=0;e<size;e++) p[e] += fs; else for (e=0;e<size;e++) p[e] *= fs;
Branch: 3 branches
BranchTrueFalseTotal
if (op == '+') for (e=0;e<size;e++) p[e] += fs; else for (e=0;e<size;e++) p[e] *= fs; 1 1 2
if (op == '+') for (e=0;e<size;e++) p[e] += fs; else for (e=0;e<size;e++) p[e] *= fs; 4 1 5
if (op == '+') for (e=0;e<size;e++) p[e] += fs; else for (e=0;e<size;e++) p[e] *= fs; 4 1 5
195 }
196 4 ▶ } else if (nda_is_signed(dt)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (nda_is_signed(dt)) { 4 0 4
197 4 switch (dt) {
198 2 ▶ case NDA_I64: NDA_SCALAR_INT(int64_t, uint64_t); break;
Branch: 3 branches
BranchTrueFalseTotal
case NDA_I64: NDA_SCALAR_INT(int64_t, uint64_t); break; 0 1 1
case NDA_I64: NDA_SCALAR_INT(int64_t, uint64_t); break; 0 0 0
case NDA_I64: NDA_SCALAR_INT(int64_t, uint64_t); break; 1 1 2
199 12 ▶ case NDA_I32: NDA_SCALAR_INT(int32_t, uint32_t); break;
Branch: 3 branches
BranchTrueFalseTotal
case NDA_I32: NDA_SCALAR_INT(int32_t, uint32_t); break; 1 2 3
case NDA_I32: NDA_SCALAR_INT(int32_t, uint32_t); break; 4 1 5
case NDA_I32: NDA_SCALAR_INT(int32_t, uint32_t); break; 5 2 7
200 0 ▶ case NDA_I16: NDA_SCALAR_INT(int16_t, uint32_t); break;
Branch: 3 branches
BranchTrueFalseTotal
case NDA_I16: NDA_SCALAR_INT(int16_t, uint32_t); break; 0 0 0
case NDA_I16: NDA_SCALAR_INT(int16_t, uint32_t); break; 0 0 0
case NDA_I16: NDA_SCALAR_INT(int16_t, uint32_t); break; 0 0 0
201 0 ▶ case NDA_I8: NDA_SCALAR_INT(int8_t, uint32_t); break;
Branch: 3 branches
BranchTrueFalseTotal
case NDA_I8: NDA_SCALAR_INT(int8_t, uint32_t); break; 0 0 0
case NDA_I8: NDA_SCALAR_INT(int8_t, uint32_t); break; 0 0 0
case NDA_I8: NDA_SCALAR_INT(int8_t, uint32_t); break; 0 0 0
202 }
203 } else {
204 0 switch (dt) {
205 0 ▶ case NDA_U64: NDA_SCALAR_UINT(uint64_t, uint64_t); break;
Branch: 3 branches
BranchTrueFalseTotal
case NDA_U64: NDA_SCALAR_UINT(uint64_t, uint64_t); break; 0 0 0
case NDA_U64: NDA_SCALAR_UINT(uint64_t, uint64_t); break; 0 0 0
case NDA_U64: NDA_SCALAR_UINT(uint64_t, uint64_t); break; 0 0 0
206 0 ▶ case NDA_U32: NDA_SCALAR_UINT(uint32_t, uint32_t); break;
Branch: 3 branches
BranchTrueFalseTotal
case NDA_U32: NDA_SCALAR_UINT(uint32_t, uint32_t); break; 0 0 0
case NDA_U32: NDA_SCALAR_UINT(uint32_t, uint32_t); break; 0 0 0
case NDA_U32: NDA_SCALAR_UINT(uint32_t, uint32_t); break; 0 0 0
207 0 ▶ case NDA_U16: NDA_SCALAR_UINT(uint16_t, uint32_t); break;
Branch: 3 branches
BranchTrueFalseTotal
case NDA_U16: NDA_SCALAR_UINT(uint16_t, uint32_t); break; 0 0 0
case NDA_U16: NDA_SCALAR_UINT(uint16_t, uint32_t); break; 0 0 0
case NDA_U16: NDA_SCALAR_UINT(uint16_t, uint32_t); break; 0 0 0
208 0 ▶ case NDA_U8: NDA_SCALAR_UINT(uint8_t, uint32_t); break;
Branch: 3 branches
BranchTrueFalseTotal
case NDA_U8: NDA_SCALAR_UINT(uint8_t, uint32_t); break; 0 0 0
case NDA_U8: NDA_SCALAR_UINT(uint8_t, uint32_t); break; 0 0 0
case NDA_U8: NDA_SCALAR_UINT(uint8_t, uint32_t); break; 0 0 0
209 }
210 }
211 7 }
212  
213 /* ----------------------------------------------------------------
214 * In-place element-wise array op: a[i] = a[i] OP b[i] (same dtype + size).
215 * `op` is '+', '-' or '*'. Both buffers are interpreted in the shared dtype.
216 * ---------------------------------------------------------------- */
217 /* float dtypes: native arithmetic (overflow -> +-inf, well-defined) */
218 #define NDA_EW_FLT(CT) do { \
219 CT *pa = (CT *)da; const CT *pb = (const CT *)db; \
220 if (op == '+') for (e=0;e<size;e++) pa[e] = (CT)(pa[e] + pb[e]); \
221 else if (op == '-') for (e=0;e<size;e++) pa[e] = (CT)(pa[e] - pb[e]); \
222 else for (e=0;e<size;e++) pa[e] = (CT)(pa[e] * pb[e]); \
223 } while (0)
224 /* integer dtypes: compute in UT (unsigned, >= int rank) for defined wrap -- see
225 * the NDA_SCALAR_INT note above. */
226 #define NDA_EW_INT(CT, UT) do { \
227 CT *pa = (CT *)da; const CT *pb = (const CT *)db; \
228 if (op == '+') for (e=0;e<size;e++) pa[e] = (CT)((UT)pa[e] + (UT)pb[e]); \
229 else if (op == '-') for (e=0;e<size;e++) pa[e] = (CT)((UT)pa[e] - (UT)pb[e]); \
230 else for (e=0;e<size;e++) pa[e] = (CT)((UT)pa[e] * (UT)pb[e]); \
231 } while (0)
232  
233 27 static void nda_elementwise_op_locked(NdaHandle *a, NdaHandle *b, int op) {
234 27 uint64_t size = a->size, e; /* cached immutable count (== b->size, checked), not peer hdr->size */
235 27 char *da = nda_data(a);
236 27 const char *db = nda_data(b);
237 27 switch (a->dtype) { /* cached: peer hdr->dtype must not resize the element */
238 163 ▶ case NDA_F64: NDA_EW_FLT(double); break;
Branch: 5 branches
BranchTrueFalseTotal
case NDA_F64: NDA_EW_FLT(double); break; 5 8 13
case NDA_F64: NDA_EW_FLT(double); break; 54 5 59
case NDA_F64: NDA_EW_FLT(double); break; 4 4 8
case NDA_F64: NDA_EW_FLT(double); break; 48 4 52
case NDA_F64: NDA_EW_FLT(double); break; 48 4 52
239 10 ▶ case NDA_F32: NDA_EW_FLT(float); break;
Branch: 5 branches
BranchTrueFalseTotal
case NDA_F32: NDA_EW_FLT(float); break; 0 2 2
case NDA_F32: NDA_EW_FLT(float); break; 0 0 0
case NDA_F32: NDA_EW_FLT(float); break; 1 1 2
case NDA_F32: NDA_EW_FLT(float); break; 4 1 5
case NDA_F32: NDA_EW_FLT(float); break; 4 1 5
240 0 ▶ case NDA_I64: NDA_EW_INT(int64_t, uint64_t); break;
Branch: 5 branches
BranchTrueFalseTotal
case NDA_I64: NDA_EW_INT(int64_t, uint64_t); break; 0 0 0
case NDA_I64: NDA_EW_INT(int64_t, uint64_t); break; 0 0 0
case NDA_I64: NDA_EW_INT(int64_t, uint64_t); break; 0 0 0
case NDA_I64: NDA_EW_INT(int64_t, uint64_t); break; 0 0 0
case NDA_I64: NDA_EW_INT(int64_t, uint64_t); break; 0 0 0
241 24 ▶ case NDA_I32: NDA_EW_INT(int32_t, uint32_t); break;
Branch: 5 branches
BranchTrueFalseTotal
case NDA_I32: NDA_EW_INT(int32_t, uint32_t); break; 4 4 8
case NDA_I32: NDA_EW_INT(int32_t, uint32_t); break; 8 4 12
case NDA_I32: NDA_EW_INT(int32_t, uint32_t); break; 2 2 4
case NDA_I32: NDA_EW_INT(int32_t, uint32_t); break; 4 2 6
case NDA_I32: NDA_EW_INT(int32_t, uint32_t); break; 4 2 6
242 0 ▶ case NDA_I16: NDA_EW_INT(int16_t, uint32_t); break;
Branch: 5 branches
BranchTrueFalseTotal
case NDA_I16: NDA_EW_INT(int16_t, uint32_t); break; 0 0 0
case NDA_I16: NDA_EW_INT(int16_t, uint32_t); break; 0 0 0
case NDA_I16: NDA_EW_INT(int16_t, uint32_t); break; 0 0 0
case NDA_I16: NDA_EW_INT(int16_t, uint32_t); break; 0 0 0
case NDA_I16: NDA_EW_INT(int16_t, uint32_t); break; 0 0 0
243 0 ▶ case NDA_I8: NDA_EW_INT(int8_t, uint32_t); break;
Branch: 5 branches
BranchTrueFalseTotal
case NDA_I8: NDA_EW_INT(int8_t, uint32_t); break; 0 0 0
case NDA_I8: NDA_EW_INT(int8_t, uint32_t); break; 0 0 0
case NDA_I8: NDA_EW_INT(int8_t, uint32_t); break; 0 0 0
case NDA_I8: NDA_EW_INT(int8_t, uint32_t); break; 0 0 0
case NDA_I8: NDA_EW_INT(int8_t, uint32_t); break; 0 0 0
244 0 ▶ case NDA_U64: NDA_EW_INT(uint64_t, uint64_t); break;
Branch: 5 branches
BranchTrueFalseTotal
case NDA_U64: NDA_EW_INT(uint64_t, uint64_t); break; 0 0 0
case NDA_U64: NDA_EW_INT(uint64_t, uint64_t); break; 0 0 0
case NDA_U64: NDA_EW_INT(uint64_t, uint64_t); break; 0 0 0
case NDA_U64: NDA_EW_INT(uint64_t, uint64_t); break; 0 0 0
case NDA_U64: NDA_EW_INT(uint64_t, uint64_t); break; 0 0 0
245 0 ▶ case NDA_U32: NDA_EW_INT(uint32_t, uint32_t); break;
Branch: 5 branches
BranchTrueFalseTotal
case NDA_U32: NDA_EW_INT(uint32_t, uint32_t); break; 0 0 0
case NDA_U32: NDA_EW_INT(uint32_t, uint32_t); break; 0 0 0
case NDA_U32: NDA_EW_INT(uint32_t, uint32_t); break; 0 0 0
case NDA_U32: NDA_EW_INT(uint32_t, uint32_t); break; 0 0 0
case NDA_U32: NDA_EW_INT(uint32_t, uint32_t); break; 0 0 0
246 3 ▶ case NDA_U16: NDA_EW_INT(uint16_t, uint32_t); break;
Branch: 5 branches
BranchTrueFalseTotal
case NDA_U16: NDA_EW_INT(uint16_t, uint32_t); break; 0 1 1
case NDA_U16: NDA_EW_INT(uint16_t, uint32_t); break; 0 0 0
case NDA_U16: NDA_EW_INT(uint16_t, uint32_t); break; 0 1 1
case NDA_U16: NDA_EW_INT(uint16_t, uint32_t); break; 0 0 0
case NDA_U16: NDA_EW_INT(uint16_t, uint32_t); break; 2 1 3
247 8 ▶ case NDA_U8: NDA_EW_INT(uint8_t, uint32_t); break;
Branch: 5 branches
BranchTrueFalseTotal
case NDA_U8: NDA_EW_INT(uint8_t, uint32_t); break; 1 2 3
case NDA_U8: NDA_EW_INT(uint8_t, uint32_t); break; 2 1 3
case NDA_U8: NDA_EW_INT(uint8_t, uint32_t); break; 1 1 2
case NDA_U8: NDA_EW_INT(uint8_t, uint32_t); break; 2 1 3
case NDA_U8: NDA_EW_INT(uint8_t, uint32_t); break; 1 1 2
248 }
249 27 }
250  
251 /* Acquire receiver `a`'s WRITE lock and other `b`'s READ lock in a
252 * globally-consistent order keyed on each array's shared identity (array_id),
253 * NOT the process-local handle pointer, so two unrelated processes mapping the
254 * same pair agree on order and cannot deadlock. a and b must be DISTINCT
255 * underlying arrays (the caller has already handled the same-array case).
256 *
257 * `b` may be a frozen/readonly (PROT_READ) handle: its reader-slot/wlock
258 * bookkeeping lives in the header region, which such a handle cannot WRITE to,
259 * so rdlock/rdunlock on it would SIGSEGV. Once sealed, b's data can never
260 * change (every mutator on it is refused), so skipping its lock entirely is
261 * safe -- only `a` (never readonly; the caller already refused that) takes a
262 * lock. `a` is always the caller's own (mutable) handle, so array_id ordering
263 * is only needed -- and only meaningful -- when both sides actually lock. */
264 34 static int nda_array_before(const NdaHandle *a, const NdaHandle *b) {
265 34 ▶ if (a->hdr->array_id != b->hdr->array_id)
Branch: 1 branch
BranchTrueFalseTotal
if (a->hdr->array_id != b->hdr->array_id) 24 10 34
266 24 return a->hdr->array_id < b->hdr->array_id;
267 10 ▶ if (a->device != b->device) return a->device < b->device;
Branch: 1 branch
BranchTrueFalseTotal
if (a->device != b->device) return a->device < b->device; 4 6 10
268 6 return a->inode < b->inode; /* copied files retain array_id */
269 }
270  
271 20 static void nda_lock_pair(NdaHandle *a, NdaHandle *b) {
272 20 ▶ if (b->readonly) {
Branch: 1 branch
BranchTrueFalseTotal
if (b->readonly) { 3 17 20
273 3 nda_rwlock_wrlock(a);
274 3 return;
275 }
276 17 ▶ if (nda_array_before(a, b)) {
Branch: 1 branch
BranchTrueFalseTotal
if (nda_array_before(a, b)) { 7 10 17
277 7 nda_rwlock_wrlock(a);
278 7 nda_rwlock_rdlock(b);
279 } else {
280 10 nda_rwlock_rdlock(b);
281 10 nda_rwlock_wrlock(a);
282 }
283 }
284 20 static void nda_unlock_pair(NdaHandle *a, NdaHandle *b) {
285 20 ▶ if (b->readonly) {
Branch: 1 branch
BranchTrueFalseTotal
if (b->readonly) { 3 17 20
286 3 nda_rwlock_wrunlock(a);
287 3 return;
288 }
289 17 ▶ if (nda_array_before(a, b)) {
Branch: 1 branch
BranchTrueFalseTotal
if (nda_array_before(a, b)) { 7 10 17
290 7 nda_rwlock_rdunlock(b);
291 7 nda_rwlock_wrunlock(a);
292 } else {
293 10 nda_rwlock_wrunlock(a);
294 10 nda_rwlock_rdunlock(b);
295 }
296 }
297  
298 23 static int nda_same_array(pTHX_ NdaHandle *a, NdaHandle *b) {
299 23 ▶ if (a->hdr->array_id != b->hdr->array_id) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (a->hdr->array_id != b->hdr->array_id) return 0; 15 8 23
300 8 NdaHandle *pair[2] = { a, b };
301 24 ▶ for (int i = 0; i < 2; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < 2; i++) { 16 8 24
302 16 NdaHandle *h = pair[i];
303 16 ▶ if (!h->inode && h->backing_fd >= 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (!h->inode && h->backing_fd >= 0) { 3 13 16
if (!h->inode && h->backing_fd >= 0) { 3 0 3
304 struct stat st;
305 3 ▶ if (fstat(h->backing_fd, &st) < 0) croak("Data::NDArray::Shared: fstat: %s", strerror(errno));
Branch: 1 branch
BranchTrueFalseTotal
if (fstat(h->backing_fd, &st) < 0) croak("Data::NDArray::Shared: fstat: %s", strerror(errno)); 0 3 3
306 3 h->device = st.st_dev;
307 3 h->inode = st.st_ino;
308 }
309 }
310 8 ▶ return a->inode && a->inode == b->inode && a->device == b->device;
Branch: 3 branches
BranchTrueFalseTotal
return a->inode && a->inode == b->inode && a->device == b->device; 8 0 8
return a->inode && a->inode == b->inode && a->device == b->device; 3 5 8
return a->inode && a->inode == b->inode && a->device == b->device; 3 0 3
311 }
312  
313 /* Shared body of add/subtract/multiply: validate `other`, take the locks in a
314 * deadlock-free id order (or a single wrlock for the same mapping), apply the
315 * element-wise op, bump stat_ops, and unlock. `op` is '+', '-' or '*'; `who`
316 * is the fully-qualified method name used in croak messages. */
317 40 static void nda_do_elementwise(pTHX_ SV *self_sv, NdaHandle *h, SV *other, int op, const char *who) {
318 40 NdaHandle *h0 = h; PERL_UNUSED_VAR(h0); /* identity anchor for the REEXTRACT below */
319 40 ▶ if (h->readonly) croak("%s: array is frozen (read-only)", who);
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) croak("%s: array is frozen (read-only)", who); 4 36 40
320 36 ▶ if (!sv_isobject(other) || !sv_derived_from(other, "Data::NDArray::Shared"))
Branch: 2 branches
BranchTrueFalseTotal
if (!sv_isobject(other) || !sv_derived_from(other, "Data::NDArray::Shared")) 33 3 36
if (!sv_isobject(other) || !sv_derived_from(other, "Data::NDArray::Shared")) 0 33 33
321 3 croak("%s: expected a Data::NDArray::Shared object", who);
322 33 NdaHandle *o = INT2PTR(NdaHandle*, SvIV(SvRV(other)));
323 33 ▶ if (!o) croak("Attempted to use a destroyed Data::NDArray::Shared object");
Branch: 1 branch
BranchTrueFalseTotal
if (!o) croak("Attempted to use a destroyed Data::NDArray::Shared object"); 0 33 33
324 /* sv_isobject/sv_derived_from above begin with SvGETMAGIC(other), so a tied
325 * `other` can have run Perl that destroyed self before h is used below.
326 * `h` is this function's own parameter, so re-reading it here is enough;
327 * `o` was read after the magic and needs no re-read. */
328 33 ▶ REEXTRACT(self_sv);
Branch: 2 branches
BranchTrueFalseTotal
REEXTRACT(self_sv); 0 33 33
REEXTRACT(self_sv); 0 33 33
329 33 ▶ if (o->dtype != h->dtype) /* cached: the kernel interprets both in the cached dtype */
Branch: 1 branch
BranchTrueFalseTotal
if (o->dtype != h->dtype) /* cached: the kernel interprets both in the cached dtype */ 3 30 33
330 3 croak("%s: dtype mismatch", who);
331 30 ▶ if (o->size != h->size) /* cached immutable counts, not peer hdr->size */
Branch: 1 branch
BranchTrueFalseTotal
if (o->size != h->size) /* cached immutable counts, not peer hdr->size */ 3 27 30
332 3 croak("%s: size mismatch (%" UVuf " vs %" UVuf ")",
333 who, (UV)h->size, (UV)o->size);
334 27 ▶ if (o == h || nda_same_array(aTHX_ h, o)) {
Branch: 2 branches
BranchTrueFalseTotal
if (o == h || nda_same_array(aTHX_ h, o)) { 23 4 27
if (o == h || nda_same_array(aTHX_ h, o)) { 3 20 23
335 7 nda_rwlock_wrlock(h);
336 7 ▶ if (h->hdr->sealed) { nda_rwlock_wrunlock(h); croak("%s: array is frozen (read-only)", who); }
Branch: 1 branch
BranchTrueFalseTotal
if (h->hdr->sealed) { nda_rwlock_wrunlock(h); croak("%s: array is frozen (read-only)", who); } 0 7 7
337 7 nda_elementwise_op_locked(h, h, op); /* self: +=->double, -=->zero, *=->square */
338 7 __atomic_fetch_add(&h->hdr->stat_ops, 1, __ATOMIC_RELAXED);
339 7 nda_rwlock_wrunlock(h);
340 } else {
341 /* o may be a frozen/readonly (PROT_READ) other -- nda_lock_pair/
342 * nda_unlock_pair skip its lock in that case (see their comment);
343 * its data is immutable so reading it lock-free is safe. */
344 20 nda_lock_pair(h, o);
345 20 ▶ if (h->hdr->sealed) { nda_unlock_pair(h, o); croak("%s: array is frozen (read-only)", who); }
Branch: 1 branch
BranchTrueFalseTotal
if (h->hdr->sealed) { nda_unlock_pair(h, o); croak("%s: array is frozen (read-only)", who); } 0 20 20
346 20 nda_elementwise_op_locked(h, o, op);
347 20 __atomic_fetch_add(&h->hdr->stat_ops, 1, __ATOMIC_RELAXED);
348 20 nda_unlock_pair(h, o);
349 }
350 27 }
351  
352 /* Resolve the uniform (path, dtype, @shape) argument convention shared by new
353 * and new_memfd, matching the rest of the Data::*::Shared family:
354 * ($path_or_undef, $dtype, @shape)
355 * ST(1) is ALWAYS the path/memfd label (an SV that may carry undef for an
356 * anonymous mapping), ST(2) is ALWAYS the dtype name string, and ST(3).. are
357 * the dimensions. Sets *out_label, *out_dt, fills dims[] and returns ndim.
358 * Croaks (no resource held) on a missing/unknown dtype or bad dims. */
359 142 static uint32_t nda_resolve_ctor_args(pTHX_ I32 ax, I32 items, const char *who,
360 SV **out_label, int *out_dt,
361 uint64_t dims[NDA_MAX_DIMS]) {
362 142 SV **sp_mark = PL_stack_base + ax; /* ST(0) == class */
363 142 ▶ if (items < 3) croak("%s: dtype required", who);
Branch: 1 branch
BranchTrueFalseTotal
if (items < 3) croak("%s: dtype required", who); 0 142 142
364 142 SV *label = sp_mark[1]; /* path/name SV (may be undef) */
365 142 SV *dsv = sp_mark[2]; /* dtype name */
366 142 STRLEN dlen; const char *dname = SvPVbyte(dsv, dlen);
367 142 int dt = nda_dtype_from_name(dname, dlen);
368 142 ▶ if (dt < 0) croak("%s: unknown dtype", who);
Branch: 1 branch
BranchTrueFalseTotal
if (dt < 0) croak("%s: unknown dtype", who); 2 140 142
369 /* parse @shape from ST(3).. */
370 140 I32 nd = items - 3;
371 140 ▶ if (nd < 1) croak("%s: at least one dimension required", who);
Branch: 1 branch
BranchTrueFalseTotal
if (nd < 1) croak("%s: at least one dimension required", who); 1 139 140
372 139 ▶ if (nd > NDA_MAX_DIMS) croak("%s: too many dimensions (max %d)", who, NDA_MAX_DIMS);
Branch: 1 branch
BranchTrueFalseTotal
if (nd > NDA_MAX_DIMS) croak("%s: too many dimensions (max %d)", who, NDA_MAX_DIMS); 1 138 139
373 320 ▶ for (I32 i = 0; i < nd; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (I32 i = 0; i < nd; i++) { 184 136 320
374 184 IV iv = SvIV(sp_mark[3 + i]);
375 184 ▶ if (iv < 1) croak("%s: dimension %d must be >= 1", who, (int)i);
Branch: 1 branch
BranchTrueFalseTotal
if (iv < 1) croak("%s: dimension %d must be >= 1", who, (int)i); 2 182 184
376 182 dims[i] = (uint64_t)iv;
377 }
378 136 *out_label = label;
379 136 *out_dt = dt;
380 136 return (uint32_t)nd;
381 }
382  
383 MODULE = Data::NDArray::Shared PACKAGE = Data::NDArray::Shared
384  
385 PROTOTYPES: DISABLE
386  
387 SV *
388 new(class, ...)
389 const char *class
390 PREINIT:
391 char errbuf[NDA_ERR_BUFLEN];
392 uint64_t dims[NDA_MAX_DIMS];
393 CODE:
394 {
395 SV *label; int dt;
396 /* Opt-in cross-user sharing: an optional trailing \%opts (e.g.
397 { mode => 0660 }) sets the backing-file mode; default 0600. @shape
398 is greedy, so mode cannot be a bare trailing positional (0600 is a
399 valid u8 dimension) -- it rides in a hashref, unambiguous and inert
400 for every existing ($path,$dtype,@shape) call. */
401 137 mode_t mode = 0600;
402 137 I32 eff_items = items;
403 137 ▶ if (items > 3) SvGETMAGIC(ST(items - 1)); /* a tied/overloaded scalar may FETCH to the opts hashref */
Branch: 3 branches
BranchTrueFalseTotal
if (items > 3) SvGETMAGIC(ST(items - 1)); /* a tied/overloaded scalar may FETCH to the opts hashref */ 135 2 137
if (items > 3) SvGETMAGIC(ST(items - 1)); /* a tied/overloaded scalar may FETCH to the opts hashref */ 0 135 135
if (items > 3) SvGETMAGIC(ST(items - 1)); /* a tied/overloaded scalar may FETCH to the opts hashref */ 0 0 0
404 137 ▶ if (items > 3 && SvROK(ST(items - 1))
Branch: 2 branches
BranchTrueFalseTotal
if (items > 3 && SvROK(ST(items - 1)) 135 2 137
if (items > 3 && SvROK(ST(items - 1)) 0 135 135
405 0 ▶ && SvTYPE(SvRV(ST(items - 1))) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(ST(items - 1))) == SVt_PVHV) { 0 0 0
406 0 HV *opts = (HV *)SvRV(ST(items - 1));
407 0 SV **msv = hv_fetchs(opts, "mode", 0);
408 0 ▶ if (msv && (SvGETMAGIC(*msv), SvOK(*msv))) mode = (mode_t)SvUV(*msv);
Branch: 4 branches
BranchTrueFalseTotal
if (msv && (SvGETMAGIC(*msv), SvOK(*msv))) mode = (mode_t)SvUV(*msv); 0 0 0
if (msv && (SvGETMAGIC(*msv), SvOK(*msv))) mode = (mode_t)SvUV(*msv); 0 0 0
if (msv && (SvGETMAGIC(*msv), SvOK(*msv))) mode = (mode_t)SvUV(*msv); 0 0 0
if (msv && (SvGETMAGIC(*msv), SvOK(*msv))) mode = (mode_t)SvUV(*msv); 0 0 0
409 0 eff_items = items - 1;
410 }
411 137 uint32_t ndim = nda_resolve_ctor_args(aTHX_ ax, eff_items,
412 "Data::NDArray::Shared->new", &label, &dt, dims); /* croaks before any alloc */
413 166 ▶ const char *p = (SvGETMAGIC(label), SvOK(label)) ? SvPV_nolen(label) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
const char *p = (SvGETMAGIC(label), SvOK(label)) ? SvPV_nolen(label) : NULL; 0 131 131
const char *p = (SvGETMAGIC(label), SvOK(label)) ? SvPV_nolen(label) : NULL; 0 0 0
const char *p = (SvGETMAGIC(label), SvOK(label)) ? SvPV_nolen(label) : NULL; 35 96 131
414 131 NdaHandle *h = nda_create(p, dt, dims, ndim, mode, errbuf);
415 131 ▶ if (!h) croak("Data::NDArray::Shared->new: %s", errbuf[0] ? errbuf : "out of memory");
Branch: 2 branches
BranchTrueFalseTotal
if (!h) croak("Data::NDArray::Shared->new: %s", errbuf[0] ? errbuf : "out of memory"); 6 125 131
if (!h) croak("Data::NDArray::Shared->new: %s", errbuf[0] ? errbuf : "out of memory"); 6 0 6
416 125 MAKE_OBJ(class, h);
417 }
418 OUTPUT:
419 RETVAL
420  
421 SV *
422 new_memfd(class, ...)
423 const char *class
424 PREINIT:
425 char errbuf[NDA_ERR_BUFLEN];
426 uint64_t dims[NDA_MAX_DIMS];
427 CODE:
428 {
429 SV *label; int dt;
430 5 uint32_t ndim = nda_resolve_ctor_args(aTHX_ ax, items,
431 "Data::NDArray::Shared->new_memfd", &label, &dt, dims);
432 6 ▶ const char *nm = (SvGETMAGIC(label), SvOK(label)) ? SvPV_nolen(label) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
const char *nm = (SvGETMAGIC(label), SvOK(label)) ? SvPV_nolen(label) : NULL; 0 5 5
const char *nm = (SvGETMAGIC(label), SvOK(label)) ? SvPV_nolen(label) : NULL; 0 0 0
const char *nm = (SvGETMAGIC(label), SvOK(label)) ? SvPV_nolen(label) : NULL; 1 4 5
433 5 NdaHandle *h = nda_create_memfd(nm, dt, dims, ndim, errbuf);
434 5 ▶ if (!h) croak("Data::NDArray::Shared->new_memfd: %s", errbuf[0] ? errbuf : "out of memory");
Branch: 2 branches
BranchTrueFalseTotal
if (!h) croak("Data::NDArray::Shared->new_memfd: %s", errbuf[0] ? errbuf : "out of memory"); 0 5 5
if (!h) croak("Data::NDArray::Shared->new_memfd: %s", errbuf[0] ? errbuf : "out of memory"); 0 0 0
435 5 MAKE_OBJ(class, h);
436 }
437 OUTPUT:
438 RETVAL
439  
440 SV *
441 new_from_fd(class, fd)
442 const char *class
443 int fd
444 PREINIT:
445 char errbuf[NDA_ERR_BUFLEN];
446 CODE:
447 5 NdaHandle *h = nda_open_fd(fd, errbuf);
448 5 ▶ if (!h) croak("Data::NDArray::Shared->new_from_fd: %s", errbuf[0] ? errbuf : "out of memory");
Branch: 2 branches
BranchTrueFalseTotal
if (!h) croak("Data::NDArray::Shared->new_from_fd: %s", errbuf[0] ? errbuf : "out of memory"); 2 3 5
if (!h) croak("Data::NDArray::Shared->new_from_fd: %s", errbuf[0] ? errbuf : "out of memory"); 2 0 2
449 3 MAKE_OBJ(class, h);
450 OUTPUT:
451 RETVAL
452  
453 SV *
454 new_readonly(class, path)
455 const char *class
456 SV *path
457 PREINIT:
458 char errbuf[NDA_ERR_BUFLEN];
459 CODE:
460 /* Open a FROZEN (sealed) file read-only: O_RDONLY + PROT_READ, no lock
461 ever. A sealed file is immutable and no read path writes the mapping,
462 so queries take no reader-slot / rwlock traffic and any number of
463 processes can share one PROT_READ mapping (same architecture; the
464 magic rejects a wrong-endian file). */
465 13 ▶ const char *p = (SvGETMAGIC(path), SvOK(path)) ? SvPV_nolen(path) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
const char *p = (SvGETMAGIC(path), SvOK(path)) ? SvPV_nolen(path) : NULL; 0 7 7
const char *p = (SvGETMAGIC(path), SvOK(path)) ? SvPV_nolen(path) : NULL; 0 0 0
const char *p = (SvGETMAGIC(path), SvOK(path)) ? SvPV_nolen(path) : NULL; 6 1 7
466 7 ▶ if (!p) croak("Data::NDArray::Shared->new_readonly: path is required");
Branch: 1 branch
BranchTrueFalseTotal
if (!p) croak("Data::NDArray::Shared->new_readonly: path is required"); 1 6 7
467 6 NdaHandle *h = nda_open_readonly(p, errbuf);
468 6 ▶ if (!h) croak("Data::NDArray::Shared->new_readonly: %s", errbuf);
Branch: 1 branch
BranchTrueFalseTotal
if (!h) croak("Data::NDArray::Shared->new_readonly: %s", errbuf); 2 4 6
469 4 MAKE_OBJ(class, h);
470 OUTPUT:
471 RETVAL
472  
473 void
474 DESTROY(self)
475 SV *self
476 CODE:
477 139 ▶ if (sv_isobject(self) && sv_derived_from(self, "Data::NDArray::Shared")) {
Branch: 2 branches
BranchTrueFalseTotal
if (sv_isobject(self) && sv_derived_from(self, "Data::NDArray::Shared")) { 139 0 139
if (sv_isobject(self) && sv_derived_from(self, "Data::NDArray::Shared")) { 139 0 139
478 139 NdaHandle *h = INT2PTR(NdaHandle*, SvIV(SvRV(self)));
479 139 ▶ if (h) { sv_setiv(SvRV(self), 0); nda_destroy(h); } /* null first: activates EXTRACT's use-after-destroy croak + makes a double DESTROY a no-op */
Branch: 1 branch
BranchTrueFalseTotal
if (h) { sv_setiv(SvRV(self), 0); nda_destroy(h); } /* null first: activates EXTRACT's use-after-destroy croak + makes a double DESTROY a no-op */ 137 2 139
480 }
481  
482 SV *
483 get(self, ...)
484 SV *self
485 PREINIT:
486 251 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 251 0 251
EXTRACT(self); 0 251 251
EXTRACT(self); 1 250 251
487 uint64_t idx[NDA_MAX_DIMS];
488 CODE:
489 {
490 250 uint32_t nix = (uint32_t)(items - 1);
491 250 ▶ if (nix > NDA_MAX_DIMS)
Branch: 1 branch
BranchTrueFalseTotal
if (nix > NDA_MAX_DIMS) 0 250 250
492 0 croak("Data::NDArray::Shared->get: too many indices (%u)", nix);
493 959 ▶ for (uint32_t d = 0; d < nix; d++) /* read the args before locking */
Branch: 1 branch
BranchTrueFalseTotal
for (uint32_t d = 0; d < nix; d++) /* read the args before locking */ 709 250 959
494 709 idx[d] = (uint64_t)SvUV(ST(1 + d));
495 250 ▶ REEXTRACT(self);
Branch: 2 branches
BranchTrueFalseTotal
REEXTRACT(self); 0 250 250
REEXTRACT(self); 0 250 250
496 char _raw[8]; uint32_t ndim;
497 250 ▶ if (h->readonly) {
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) { 17 233 250
498 /* Frozen: reshape (the only thing that can change ndim/shape/strides)
499 is refused, so they can never change -- safe to read lock-free.
500 Taking the rwlock here would WRITE into the PROT_READ-mapped header
501 (reader-slot claim) and SIGSEGV. */
502 17 ndim = h->hdr->ndim;
503 17 ▶ if (nix != ndim)
Branch: 1 branch
BranchTrueFalseTotal
if (nix != ndim) 1 16 17
504 1 croak("Data::NDArray::Shared->get: expected %u indices, got %u", ndim, nix);
505 46 ▶ for (uint32_t d = 0; d < ndim; d++)
Branch: 1 branch
BranchTrueFalseTotal
for (uint32_t d = 0; d < ndim; d++) 31 15 46
506 31 ▶ if (idx[d] >= h->hdr->shape[d])
Branch: 1 branch
BranchTrueFalseTotal
if (idx[d] >= h->hdr->shape[d]) 1 30 31
507 1 croak("Data::NDArray::Shared->get: index %u = %" UVuf " out of range (dim size %" UVuf ")",
508 d, (UV)idx[d], (UV)h->hdr->shape[d]);
509 15 uint64_t off = nda_flat_offset(h, idx, ndim);
510 15 ▶ if (off >= h->size) /* cached immutable count, not peer hdr->size */
Branch: 1 branch
BranchTrueFalseTotal
if (off >= h->size) /* cached immutable count, not peer hdr->size */ 0 15 15
511 0 croak("Data::NDArray::Shared->get: computed offset out of range");
512 15 nda_read_raw(h, off, _raw);
513 } else {
514 /* Validate shape + compute the flat offset UNDER the read lock: a concurrent
515 reshape() mutates ndim/shape/strides under the write lock, so doing this
516 lock-free could bound-check one shape yet offset with another -> OOB. */
517 233 nda_rwlock_rdlock(h);
518 233 ndim = h->hdr->ndim;
519 233 ▶ if (nix != ndim) { nda_rwlock_rdunlock(h);
Branch: 1 branch
BranchTrueFalseTotal
if (nix != ndim) { nda_rwlock_rdunlock(h); 2 231 233
520 2 croak("Data::NDArray::Shared->get: expected %u indices, got %u", ndim, nix); }
521 898 ▶ for (uint32_t d = 0; d < ndim; d++)
Branch: 1 branch
BranchTrueFalseTotal
for (uint32_t d = 0; d < ndim; d++) 670 228 898
522 670 ▶ if (idx[d] >= h->hdr->shape[d]) { uint64_t sz = h->hdr->shape[d]; nda_rwlock_rdunlock(h);
Branch: 1 branch
BranchTrueFalseTotal
if (idx[d] >= h->hdr->shape[d]) { uint64_t sz = h->hdr->shape[d]; nda_rwlock_rdunlock(h); 3 667 670
523 3 croak("Data::NDArray::Shared->get: index %u = %" UVuf " out of range (dim size %" UVuf ")",
524 d, (UV)idx[d], (UV)sz); }
525 228 uint64_t off = nda_flat_offset(h, idx, ndim);
526 /* Layer B: strides[] are read from the shared segment; a local peer with
527 write access to the backing file could corrupt them so an in-range
528 multi-index still yields a flat offset past the element count -> OOB
529 read. Canonical row-major strides always keep off < size, so this
530 never fires for good data. */
531 228 ▶ if (off >= h->size) { nda_rwlock_rdunlock(h); /* cached immutable count, not peer hdr->size */
Branch: 1 branch
BranchTrueFalseTotal
if (off >= h->size) { nda_rwlock_rdunlock(h); /* cached immutable count, not peer hdr->size */ 0 228 228
532 0 croak("Data::NDArray::Shared->get: computed offset out of range"); }
533 228 nda_read_raw(h, off, _raw); /* copy element under the lock */
534 228 nda_rwlock_rdunlock(h);
535 }
536 243 RETVAL = nda_sv_from_raw(aTHX_ h->dtype, _raw); /* build SV after unlock */
537 }
538 OUTPUT:
539 RETVAL
540  
541 void
542 set(self, ...)
543 SV *self
544 PREINIT:
545 223 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 223 0 223
EXTRACT(self); 0 223 223
EXTRACT(self); 0 223 223
546 uint64_t idx[NDA_MAX_DIMS];
547 CODE:
548 {
549 223 ▶ if (h->readonly) croak("Data::NDArray::Shared->set: array is frozen (read-only)");
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) croak("Data::NDArray::Shared->set: array is frozen (read-only)"); 2 221 223
550 221 ▶ if (items < 2) croak("Data::NDArray::Shared->set: expected indices + value");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 2) croak("Data::NDArray::Shared->set: expected indices + value"); 0 221 221
551 221 uint32_t nix = (uint32_t)(items - 2); /* self + ndim indices + value */
552 221 ▶ if (nix > NDA_MAX_DIMS)
Branch: 1 branch
BranchTrueFalseTotal
if (nix > NDA_MAX_DIMS) 0 221 221
553 0 croak("Data::NDArray::Shared->set: too many indices (%u)", nix);
554 221 SV *val = ST(items - 1);
555 872 ▶ for (uint32_t d = 0; d < nix; d++) /* read the args before locking */
Branch: 1 branch
BranchTrueFalseTotal
for (uint32_t d = 0; d < nix; d++) /* read the args before locking */ 651 221 872
556 651 idx[d] = (uint64_t)SvUV(ST(1 + d));
557 221 ▶ REEXTRACT(self);
Branch: 2 branches
BranchTrueFalseTotal
REEXTRACT(self); 0 221 221
REEXTRACT(self); 0 221 221
558 221 char enc[8]; uint32_t nb = nda_encode(aTHX_ h->dtype, val, enc); /* encode val lock-free */
559 /* Validate + compute the offset UNDER the write lock (see get: serializes the
560 shape read + offset against a concurrent reshape to prevent an OOB index). */
561 221 ▶ REEXTRACT(self);
Branch: 2 branches
BranchTrueFalseTotal
REEXTRACT(self); 0 221 221
REEXTRACT(self); 0 221 221
562 221 nda_rwlock_wrlock(h);
563 221 ▶ if (h->hdr->sealed) { nda_rwlock_wrunlock(h);
Branch: 1 branch
BranchTrueFalseTotal
if (h->hdr->sealed) { nda_rwlock_wrunlock(h); 1 220 221
564 1 croak("Data::NDArray::Shared->set: array is frozen (read-only)"); }
565 220 uint32_t ndim = h->hdr->ndim;
566 220 ▶ if (nix != ndim) { nda_rwlock_wrunlock(h);
Branch: 1 branch
BranchTrueFalseTotal
if (nix != ndim) { nda_rwlock_wrunlock(h); 1 219 220
567 1 croak("Data::NDArray::Shared->set: expected %u indices + value, got %u args", ndim, nix); }
568 865 ▶ for (uint32_t d = 0; d < ndim; d++)
Branch: 1 branch
BranchTrueFalseTotal
for (uint32_t d = 0; d < ndim; d++) 647 218 865
569 647 ▶ if (idx[d] >= h->hdr->shape[d]) { uint64_t sz = h->hdr->shape[d]; nda_rwlock_wrunlock(h);
Branch: 1 branch
BranchTrueFalseTotal
if (idx[d] >= h->hdr->shape[d]) { uint64_t sz = h->hdr->shape[d]; nda_rwlock_wrunlock(h); 1 646 647
570 1 croak("Data::NDArray::Shared->set: index %u = %" UVuf " out of range (dim size %" UVuf ")",
571 d, (UV)idx[d], (UV)sz); }
572 218 uint64_t off = nda_flat_offset(h, idx, ndim);
573 /* Layer B: bound the strides-derived flat offset against the element
574 count before writing -- corrupted strides in the shared segment must
575 not drive an OOB write (see get; never fires for canonical data). */
576 218 ▶ if (off >= h->size) { nda_rwlock_wrunlock(h); /* cached immutable count, not peer hdr->size */
Branch: 1 branch
BranchTrueFalseTotal
if (off >= h->size) { nda_rwlock_wrunlock(h); /* cached immutable count, not peer hdr->size */ 0 218 218
577 0 croak("Data::NDArray::Shared->set: computed offset out of range"); }
578 218 nda_store_bytes(h, off, enc, nb);
579 218 __atomic_fetch_add(&h->hdr->stat_ops, 1, __ATOMIC_RELAXED);
580 218 nda_rwlock_wrunlock(h);
581 }
582  
583 SV *
584 get_flat(self, e)
585 SV *self
586 UV e
587 PREINIT:
588 913 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 913 0 913
EXTRACT(self); 0 913 913
EXTRACT(self); 0 913 913
589 CODE:
590 913 ▶ if (e >= h->size) /* cached immutable count, not peer hdr->size */
Branch: 1 branch
BranchTrueFalseTotal
if (e >= h->size) /* cached immutable count, not peer hdr->size */ 2 911 913
591 2 croak("Data::NDArray::Shared->get_flat: index %" UVuf " out of range (size %" UVuf ")",
592 e, (UV)h->size);
593 {
594 char _raw[8];
595 911 ▶ if (h->readonly) {
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) { 14 897 911
596 14 nda_read_raw(h, (uint64_t)e, _raw);
597 } else {
598 897 nda_rwlock_rdlock(h);
599 897 nda_read_raw(h, (uint64_t)e, _raw);
600 897 nda_rwlock_rdunlock(h);
601 }
602 911 RETVAL = nda_sv_from_raw(aTHX_ h->dtype, _raw);
603 }
604 OUTPUT:
605 RETVAL
606  
607 void
608 set_flat(self, e, val)
609 SV *self
610 UV e
611 SV *val
612 PREINIT:
613 219 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 219 0 219
EXTRACT(self); 0 219 219
EXTRACT(self); 0 219 219
614 CODE:
615 219 ▶ if (h->readonly) croak("Data::NDArray::Shared->set_flat: array is frozen (read-only)");
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) croak("Data::NDArray::Shared->set_flat: array is frozen (read-only)"); 2 217 219
616 217 ▶ if (e >= h->size) /* cached immutable count, not peer hdr->size */
Branch: 1 branch
BranchTrueFalseTotal
if (e >= h->size) /* cached immutable count, not peer hdr->size */ 1 216 217
617 1 croak("Data::NDArray::Shared->set_flat: index %" UVuf " out of range (size %" UVuf ")",
618 e, (UV)h->size);
619 216 char enc[8]; uint32_t nb = nda_encode(aTHX_ h->dtype, val, enc); /* encode before locking */
620 216 ▶ REEXTRACT(self);
Branch: 2 branches
BranchTrueFalseTotal
REEXTRACT(self); 0 216 216
REEXTRACT(self); 0 216 216
621 216 nda_rwlock_wrlock(h);
622 216 ▶ if (h->hdr->sealed) { nda_rwlock_wrunlock(h);
Branch: 1 branch
BranchTrueFalseTotal
if (h->hdr->sealed) { nda_rwlock_wrunlock(h); 0 216 216
623 0 croak("Data::NDArray::Shared->set_flat: array is frozen (read-only)"); }
624 216 nda_store_bytes(h, (uint64_t)e, enc, nb);
625 216 __atomic_fetch_add(&h->hdr->stat_ops, 1, __ATOMIC_RELAXED);
626 216 nda_rwlock_wrunlock(h);
627  
628 SV *
629 fill(self, val)
630 SV *self
631 SV *val
632 PREINIT:
633 45 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 45 0 45
EXTRACT(self); 0 45 45
EXTRACT(self); 0 45 45
634 CODE:
635 45 ▶ if (h->readonly) croak("Data::NDArray::Shared->fill: array is frozen (read-only)");
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) croak("Data::NDArray::Shared->fill: array is frozen (read-only)"); 2 43 45
636 43 char enc[8]; uint32_t nb = nda_encode(aTHX_ h->dtype, val, enc); /* encode before locking */
637 43 ▶ REEXTRACT(self);
Branch: 2 branches
BranchTrueFalseTotal
REEXTRACT(self); 0 43 43
REEXTRACT(self); 0 43 43
638 43 nda_rwlock_wrlock(h);
639 43 ▶ if (h->hdr->sealed) { nda_rwlock_wrunlock(h);
Branch: 1 branch
BranchTrueFalseTotal
if (h->hdr->sealed) { nda_rwlock_wrunlock(h); 0 43 43
640 0 croak("Data::NDArray::Shared->fill: array is frozen (read-only)"); }
641 43 nda_fill_bytes(h, enc, nb);
642 43 __atomic_fetch_add(&h->hdr->stat_ops, 1, __ATOMIC_RELAXED);
643 43 nda_rwlock_wrunlock(h);
644 43 SvREFCNT_inc(self);
645 43 RETVAL = self;
646 OUTPUT:
647 RETVAL
648  
649 SV *
650 zero(self)
651 SV *self
652 PREINIT:
653 3 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 3 0 3
EXTRACT(self); 0 3 3
EXTRACT(self); 0 3 3
654 CODE:
655 3 ▶ if (h->readonly) croak("Data::NDArray::Shared->zero: array is frozen (read-only)");
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) croak("Data::NDArray::Shared->zero: array is frozen (read-only)"); 2 1 3
656 1 nda_rwlock_wrlock(h);
657 1 ▶ if (h->hdr->sealed) { nda_rwlock_wrunlock(h);
Branch: 1 branch
BranchTrueFalseTotal
if (h->hdr->sealed) { nda_rwlock_wrunlock(h); 0 1 1
658 0 croak("Data::NDArray::Shared->zero: array is frozen (read-only)"); }
659 1 memset(nda_data(h), 0, (size_t)(h->size * h->itemsize)); /* cached immutable geometry, not peer hdr fields */
660 1 __atomic_fetch_add(&h->hdr->stat_ops, 1, __ATOMIC_RELAXED);
661 1 nda_rwlock_wrunlock(h);
662 1 SvREFCNT_inc(self);
663 1 RETVAL = self;
664 OUTPUT:
665 RETVAL
666  
667 SV *
668 reshape(self, ...)
669 SV *self
670 PREINIT:
671 9 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 9 0 9
EXTRACT(self); 0 9 9
EXTRACT(self); 0 9 9
672 uint64_t dims[NDA_MAX_DIMS];
673 uint64_t strides[NDA_MAX_DIMS];
674 CODE:
675 {
676 9 ▶ if (h->readonly) croak("Data::NDArray::Shared->reshape: array is frozen (read-only)");
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) croak("Data::NDArray::Shared->reshape: array is frozen (read-only)"); 2 7 9
677 7 I32 nd = items - 1;
678 7 ▶ if (nd < 1) croak("Data::NDArray::Shared->reshape: at least one dimension required");
Branch: 1 branch
BranchTrueFalseTotal
if (nd < 1) croak("Data::NDArray::Shared->reshape: at least one dimension required"); 0 7 7
679 7 ▶ if (nd > NDA_MAX_DIMS) croak("Data::NDArray::Shared->reshape: too many dimensions (max %d)", NDA_MAX_DIMS);
Branch: 1 branch
BranchTrueFalseTotal
if (nd > NDA_MAX_DIMS) croak("Data::NDArray::Shared->reshape: too many dimensions (max %d)", NDA_MAX_DIMS); 1 6 7
680 6 uint64_t newsize = 1;
681 14 ▶ for (I32 i = 0; i < nd; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (I32 i = 0; i < nd; i++) { 10 4 14
682 10 IV iv = SvIV(ST(1 + i));
683 10 ▶ if (iv < 1) croak("Data::NDArray::Shared->reshape: dimension %d must be >= 1", (int)i);
Branch: 1 branch
BranchTrueFalseTotal
if (iv < 1) croak("Data::NDArray::Shared->reshape: dimension %d must be >= 1", (int)i); 2 8 10
684 8 dims[i] = (uint64_t)iv;
685 8 ▶ if (dims[i] > UINT64_MAX / newsize) croak("Data::NDArray::Shared->reshape: shape too large");
Branch: 1 branch
BranchTrueFalseTotal
if (dims[i] > UINT64_MAX / newsize) croak("Data::NDArray::Shared->reshape: shape too large"); 0 8 8
686 8 newsize *= dims[i];
687 }
688 4 ▶ REEXTRACT(self);
Branch: 2 branches
BranchTrueFalseTotal
REEXTRACT(self); 0 4 4
REEXTRACT(self); 0 4 4
689 4 ▶ if (newsize != h->size) /* cached immutable count: reshape must preserve element count */
Branch: 1 branch
BranchTrueFalseTotal
if (newsize != h->size) /* cached immutable count: reshape must preserve element count */ 1 3 4
690 1 croak("Data::NDArray::Shared->reshape: total size %" UVuf " does not match current size %" UVuf,
691 (UV)newsize, (UV)h->size);
692 /* row-major strides for the new shape */
693 3 strides[nd - 1] = 1;
694 5 ▶ for (int d = nd - 2; d >= 0; d--) strides[d] = strides[d + 1] * dims[d + 1];
Branch: 1 branch
BranchTrueFalseTotal
for (int d = nd - 2; d >= 0; d--) strides[d] = strides[d + 1] * dims[d + 1]; 2 3 5
695 3 nda_rwlock_wrlock(h);
696 3 ▶ if (h->hdr->sealed) { nda_rwlock_wrunlock(h);
Branch: 1 branch
BranchTrueFalseTotal
if (h->hdr->sealed) { nda_rwlock_wrunlock(h); 0 3 3
697 0 croak("Data::NDArray::Shared->reshape: array is frozen (read-only)"); }
698 3 h->hdr->ndim = (uint32_t)nd;
699 8 ▶ for (I32 i = 0; i < nd; i++) { h->hdr->shape[i] = dims[i]; h->hdr->strides[i] = strides[i]; }
Branch: 1 branch
BranchTrueFalseTotal
for (I32 i = 0; i < nd; i++) { h->hdr->shape[i] = dims[i]; h->hdr->strides[i] = strides[i]; } 5 3 8
700 3 __atomic_fetch_add(&h->hdr->stat_ops, 1, __ATOMIC_RELAXED);
701 3 nda_rwlock_wrunlock(h);
702 3 SvREFCNT_inc(self);
703 3 RETVAL = self;
704 }
705 OUTPUT:
706 RETVAL
707  
708 NV
709 sum(self)
710 SV *self
711 PREINIT:
712 12 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 12 0 12
EXTRACT(self); 0 12 12
EXTRACT(self); 0 12 12
713 CODE:
714 12 ▶ if (h->readonly) {
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) { 4 8 12
715 4 RETVAL = nda_sum_locked(h); /* frozen: immutable, no lock needed */
716 } else {
717 8 nda_rwlock_rdlock(h);
718 8 RETVAL = nda_sum_locked(h);
719 8 nda_rwlock_rdunlock(h);
720 }
721 OUTPUT:
722 RETVAL
723  
724 NV
725 mean(self)
726 SV *self
727 PREINIT:
728 6 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 6 0 6
EXTRACT(self); 0 6 6
EXTRACT(self); 0 6 6
729 CODE:
730 {
731 double acc;
732 6 ▶ if (h->readonly) {
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) { 1 5 6
733 1 acc = nda_sum_locked(h); /* frozen: immutable, no lock needed */
734 } else {
735 5 nda_rwlock_rdlock(h);
736 5 acc = nda_sum_locked(h);
737 5 nda_rwlock_rdunlock(h);
738 }
739 6 ▶ RETVAL = acc / (double)h->size; /* cached immutable count (>= 1); matches the summed span */
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = acc / (double)h->size; /* cached immutable count (>= 1); matches the summed span */ 0 6 6
740 }
741 OUTPUT:
742 RETVAL
743  
744 SV *
745 min(self)
746 SV *self
747 PREINIT:
748 9 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 9 0 9
EXTRACT(self); 0 9 9
EXTRACT(self); 0 9 9
749 CODE:
750 {
751 uint64_t best; char _raw[8];
752 9 ▶ if (h->readonly) {
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) { 1 8 9
753 1 best = nda_argextreme_locked(h, 0); /* frozen: immutable, no lock needed */
754 1 nda_read_raw(h, best, _raw);
755 } else {
756 8 nda_rwlock_rdlock(h);
757 8 best = nda_argextreme_locked(h, 0); /* compare in native dtype */
758 8 nda_read_raw(h, best, _raw); /* copy the min element under the lock */
759 8 nda_rwlock_rdunlock(h);
760 }
761 9 RETVAL = nda_sv_from_raw(aTHX_ h->dtype, _raw); /* build SV after unlock */
762 }
763 OUTPUT:
764 RETVAL
765  
766 SV *
767 max(self)
768 SV *self
769 PREINIT:
770 9 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 9 0 9
EXTRACT(self); 0 9 9
EXTRACT(self); 0 9 9
771 CODE:
772 {
773 uint64_t best; char _raw[8];
774 9 ▶ if (h->readonly) {
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) { 1 8 9
775 1 best = nda_argextreme_locked(h, 1); /* frozen: immutable, no lock needed */
776 1 nda_read_raw(h, best, _raw);
777 } else {
778 8 nda_rwlock_rdlock(h);
779 8 best = nda_argextreme_locked(h, 1); /* compare in native dtype */
780 8 nda_read_raw(h, best, _raw); /* copy the max element under the lock */
781 8 nda_rwlock_rdunlock(h);
782 }
783 9 RETVAL = nda_sv_from_raw(aTHX_ h->dtype, _raw); /* build SV after unlock */
784 }
785 OUTPUT:
786 RETVAL
787  
788 SV *
789 add_scalar(self, s)
790 SV *self
791 SV *s
792 PREINIT:
793 4 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 4 0 4
EXTRACT(self); 0 4 4
EXTRACT(self); 0 4 4
794 CODE:
795 4 ▶ if (h->readonly) croak("Data::NDArray::Shared->add_scalar: array is frozen (read-only)");
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) croak("Data::NDArray::Shared->add_scalar: array is frozen (read-only)"); 2 2 4
796 2 double sd=0; int64_t si=0; uint64_t su=0;
797 2 nda_read_scalar(aTHX_ h->dtype, s, &sd, &si, &su); /* pre-read lock-free */
798 2 ▶ REEXTRACT(self);
Branch: 2 branches
BranchTrueFalseTotal
REEXTRACT(self); 0 2 2
REEXTRACT(self); 0 2 2
799 2 nda_rwlock_wrlock(h);
800 2 ▶ if (h->hdr->sealed) { nda_rwlock_wrunlock(h);
Branch: 1 branch
BranchTrueFalseTotal
if (h->hdr->sealed) { nda_rwlock_wrunlock(h); 0 2 2
801 0 croak("Data::NDArray::Shared->add_scalar: array is frozen (read-only)"); }
802 2 nda_scalar_op_locked(h, sd, si, su, '+');
803 2 __atomic_fetch_add(&h->hdr->stat_ops, 1, __ATOMIC_RELAXED);
804 2 nda_rwlock_wrunlock(h);
805 2 SvREFCNT_inc(self);
806 2 RETVAL = self;
807 OUTPUT:
808 RETVAL
809  
810 SV *
811 mul_scalar(self, s)
812 SV *self
813 SV *s
814 PREINIT:
815 7 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 7 0 7
EXTRACT(self); 0 7 7
EXTRACT(self); 0 7 7
816 CODE:
817 7 ▶ if (h->readonly) croak("Data::NDArray::Shared->mul_scalar: array is frozen (read-only)");
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) croak("Data::NDArray::Shared->mul_scalar: array is frozen (read-only)"); 2 5 7
818 5 double sd=0; int64_t si=0; uint64_t su=0;
819 5 nda_read_scalar(aTHX_ h->dtype, s, &sd, &si, &su); /* pre-read lock-free */
820 5 ▶ REEXTRACT(self);
Branch: 2 branches
BranchTrueFalseTotal
REEXTRACT(self); 0 5 5
REEXTRACT(self); 0 5 5
821 5 nda_rwlock_wrlock(h);
822 5 ▶ if (h->hdr->sealed) { nda_rwlock_wrunlock(h);
Branch: 1 branch
BranchTrueFalseTotal
if (h->hdr->sealed) { nda_rwlock_wrunlock(h); 0 5 5
823 0 croak("Data::NDArray::Shared->mul_scalar: array is frozen (read-only)"); }
824 5 nda_scalar_op_locked(h, sd, si, su, '*');
825 5 __atomic_fetch_add(&h->hdr->stat_ops, 1, __ATOMIC_RELAXED);
826 5 nda_rwlock_wrunlock(h);
827 5 SvREFCNT_inc(self);
828 5 RETVAL = self;
829 OUTPUT:
830 RETVAL
831  
832 SV *
833 add(self, other)
834 SV *self
835 SV *other
836 PREINIT:
837 15 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 15 0 15
EXTRACT(self); 0 15 15
EXTRACT(self); 0 15 15
838 CODE:
839 15 nda_do_elementwise(aTHX_ self, h, other, '+', "Data::NDArray::Shared->add");
840 10 SvREFCNT_inc(self);
841 10 RETVAL = self;
842 OUTPUT:
843 RETVAL
844  
845 SV *
846 subtract(self, other)
847 SV *self
848 SV *other
849 PREINIT:
850 12 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 12 0 12
EXTRACT(self); 0 12 12
EXTRACT(self); 0 12 12
851 CODE:
852 12 nda_do_elementwise(aTHX_ self, h, other, '-', "Data::NDArray::Shared->subtract");
853 8 SvREFCNT_inc(self);
854 8 RETVAL = self;
855 OUTPUT:
856 RETVAL
857  
858 SV *
859 multiply(self, other)
860 SV *self
861 SV *other
862 PREINIT:
863 13 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 13 0 13
EXTRACT(self); 0 13 13
EXTRACT(self); 0 13 13
864 CODE:
865 13 nda_do_elementwise(aTHX_ self, h, other, '*', "Data::NDArray::Shared->multiply");
866 9 SvREFCNT_inc(self);
867 9 RETVAL = self;
868 OUTPUT:
869 RETVAL
870  
871 SV *
872 to_list(self)
873 SV *self
874 PREINIT:
875 59 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 59 0 59
EXTRACT(self); 0 59 59
EXTRACT(self); 0 59 59
876 CODE:
877 {
878 59 uint64_t size = h->size, e; /* cached immutable count, not peer hdr->size */
879 59 int dt = h->dtype;
880 59 uint32_t nb = nda_dtype_bytes(dt);
881 59 char *snap = NULL;
882 59 AV *av = newAV();
883 59 ▶ if (size) {
Branch: 1 branch
BranchTrueFalseTotal
if (size) { 59 0 59
884 59 av_extend(av, (SSize_t)(size - 1)); /* pre-extend BEFORE the lock */
885 59 Newx(snap, size * (STRLEN)nb, char);
886 59 SAVEFREEPV(snap); /* freed on scope exit / croak */
887 }
888 59 ▶ if (h->readonly) {
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) { 2 57 59
889 2 ▶ if (size) memcpy(snap, nda_data(h), (size_t)(size * nb)); /* frozen: immutable, no lock needed */
Branch: 1 branch
BranchTrueFalseTotal
if (size) memcpy(snap, nda_data(h), (size_t)(size * nb)); /* frozen: immutable, no lock needed */ 2 0 2
890 } else {
891 57 nda_rwlock_rdlock(h);
892 57 ▶ if (size) memcpy(snap, nda_data(h), (size_t)(size * nb)); /* snapshot raw bytes under the lock */
Branch: 1 branch
BranchTrueFalseTotal
if (size) memcpy(snap, nda_data(h), (size_t)(size * nb)); /* snapshot raw bytes under the lock */ 57 0 57
893 57 nda_rwlock_rdunlock(h);
894 }
895 2928 ▶ for (e = 0; e < size; e++) /* build the SVs AFTER unlocking */
Branch: 1 branch
BranchTrueFalseTotal
for (e = 0; e < size; e++) /* build the SVs AFTER unlocking */ 2869 59 2928
896 2869 av_store(av, (SSize_t)e, nda_sv_from_raw(aTHX_ dt, snap + e * nb));
897 59 RETVAL = newRV_noinc((SV *)av);
898 }
899 OUTPUT:
900 RETVAL
901  
902 void
903 shape(self)
904 SV *self
905 PREINIT:
906 30 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 30 0 30
EXTRACT(self); 0 30 30
EXTRACT(self); 0 30 30
907 PPCODE:
908 {
909 uint64_t snap[NDA_MAX_DIMS]; uint32_t ndim, d;
910 /* Snapshot ndim+shape under the read lock: reshape mutates them under
911 * the write lock, so an unlocked read could tear (a new ndim with a
912 * stale shape[]). Copy out, unlock, then build the SVs. Frozen: reshape
913 * is refused, so ndim/shape can never change -- read lock-free (the
914 * rwlock would WRITE into the PROT_READ header and SIGSEGV). */
915 30 ▶ if (h->readonly) {
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) { 3 27 30
916 3 ndim = h->hdr->ndim;
917 3 ▶ if (ndim > NDA_MAX_DIMS) ndim = NDA_MAX_DIMS; /* clamp a peer-corrupted ndim */
Branch: 1 branch
BranchTrueFalseTotal
if (ndim > NDA_MAX_DIMS) ndim = NDA_MAX_DIMS; /* clamp a peer-corrupted ndim */ 0 3 3
918 9 ▶ for (d = 0; d < ndim; d++) snap[d] = (uint64_t)h->hdr->shape[d];
Branch: 1 branch
BranchTrueFalseTotal
for (d = 0; d < ndim; d++) snap[d] = (uint64_t)h->hdr->shape[d]; 6 3 9
919 } else {
920 27 nda_rwlock_rdlock(h);
921 27 ndim = h->hdr->ndim;
922 27 ▶ if (ndim > NDA_MAX_DIMS) ndim = NDA_MAX_DIMS; /* clamp a peer-corrupted ndim */
Branch: 1 branch
BranchTrueFalseTotal
if (ndim > NDA_MAX_DIMS) ndim = NDA_MAX_DIMS; /* clamp a peer-corrupted ndim */ 0 27 27
923 64 ▶ for (d = 0; d < ndim; d++) snap[d] = (uint64_t)h->hdr->shape[d];
Branch: 1 branch
BranchTrueFalseTotal
for (d = 0; d < ndim; d++) snap[d] = (uint64_t)h->hdr->shape[d]; 37 27 64
924 27 nda_rwlock_rdunlock(h);
925 }
926 30 ▶ EXTEND(SP, (SSize_t)ndim);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, (SSize_t)ndim); 0 30 30
927 73 ▶ for (d = 0; d < ndim; d++)
Branch: 1 branch
BranchTrueFalseTotal
for (d = 0; d < ndim; d++) 43 30 73
928 43 PUSHs(sv_2mortal(newSVuv((UV)snap[d])));
929 }
930  
931 void
932 strides(self)
933 SV *self
934 PREINIT:
935 6 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 6 0 6
EXTRACT(self); 0 6 6
EXTRACT(self); 0 6 6
936 PPCODE:
937 {
938 uint64_t snap[NDA_MAX_DIMS]; uint32_t ndim, d;
939 /* Snapshot ndim+strides under the read lock (see shape): reshape mutates
940 * them under the write lock; an unlocked read could tear. Frozen: lock-free
941 * (see shape). */
942 6 ▶ if (h->readonly) {
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) { 1 5 6
943 1 ndim = h->hdr->ndim;
944 1 ▶ if (ndim > NDA_MAX_DIMS) ndim = NDA_MAX_DIMS; /* clamp a peer-corrupted ndim */
Branch: 1 branch
BranchTrueFalseTotal
if (ndim > NDA_MAX_DIMS) ndim = NDA_MAX_DIMS; /* clamp a peer-corrupted ndim */ 0 1 1
945 3 ▶ for (d = 0; d < ndim; d++) snap[d] = (uint64_t)h->hdr->strides[d];
Branch: 1 branch
BranchTrueFalseTotal
for (d = 0; d < ndim; d++) snap[d] = (uint64_t)h->hdr->strides[d]; 2 1 3
946 } else {
947 5 nda_rwlock_rdlock(h);
948 5 ndim = h->hdr->ndim;
949 5 ▶ if (ndim > NDA_MAX_DIMS) ndim = NDA_MAX_DIMS; /* clamp a peer-corrupted ndim */
Branch: 1 branch
BranchTrueFalseTotal
if (ndim > NDA_MAX_DIMS) ndim = NDA_MAX_DIMS; /* clamp a peer-corrupted ndim */ 0 5 5
950 15 ▶ for (d = 0; d < ndim; d++) snap[d] = (uint64_t)h->hdr->strides[d];
Branch: 1 branch
BranchTrueFalseTotal
for (d = 0; d < ndim; d++) snap[d] = (uint64_t)h->hdr->strides[d]; 10 5 15
951 5 nda_rwlock_rdunlock(h);
952 }
953 6 ▶ EXTEND(SP, (SSize_t)ndim);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, (SSize_t)ndim); 0 6 6
954 18 ▶ for (d = 0; d < ndim; d++)
Branch: 1 branch
BranchTrueFalseTotal
for (d = 0; d < ndim; d++) 12 6 18
955 12 PUSHs(sv_2mortal(newSVuv((UV)snap[d])));
956 }
957  
958 UV
959 ndim(self)
960 SV *self
961 PREINIT:
962 4 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 4 0 4
EXTRACT(self); 0 4 4
EXTRACT(self); 0 4 4
963 CODE:
964 4 ▶ RETVAL = (UV)h->hdr->ndim; /* current rank (1..8); reshape() can change it */
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)h->hdr->ndim; /* current rank (1..8); reshape() can change it */ 0 4 4
965 OUTPUT:
966 RETVAL
967  
968 UV
969 size(self)
970 SV *self
971 PREINIT:
972 10 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 10 0 10
EXTRACT(self); 0 10 10
EXTRACT(self); 0 10 10
973 CODE:
974 10 ▶ RETVAL = (UV)h->hdr->size; /* immutable after creation -- lock-free */
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)h->hdr->size; /* immutable after creation -- lock-free */ 0 10 10
975 OUTPUT:
976 RETVAL
977  
978 UV
979 itemsize(self)
980 SV *self
981 PREINIT:
982 5 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 5 0 5
EXTRACT(self); 0 5 5
EXTRACT(self); 0 5 5
983 CODE:
984 5 ▶ RETVAL = (UV)h->hdr->itemsize; /* immutable after creation -- lock-free */
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)h->hdr->itemsize; /* immutable after creation -- lock-free */ 0 5 5
985 OUTPUT:
986 RETVAL
987  
988 # Raw contiguous data region as a byte string (read-locked snapshot copy). The
989 # bytes are row-major C-order; pair with shape()/dtype() to interpret them.
990 SV *
991 buffer(self)
992 SV *self
993 PREINIT:
994 22 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 22 0 22
EXTRACT(self); 0 22 22
EXTRACT(self); 0 22 22
995 CODE:
996 {
997 22 uint64_t bytes = h->size * h->itemsize; /* cached immutable geometry, not peer hdr fields */
998 22 char *base = nda_data(h);
999 22 RETVAL = newSVpvn("", 0);
1000 22 ▶ (void)SvGROW(RETVAL, (STRLEN)(bytes + 1)); /* size the buffer BEFORE the lock */
Branch: 2 branches
BranchTrueFalseTotal
(void)SvGROW(RETVAL, (STRLEN)(bytes + 1)); /* size the buffer BEFORE the lock */ 22 0 22
(void)SvGROW(RETVAL, (STRLEN)(bytes + 1)); /* size the buffer BEFORE the lock */ 11 11 22
1001 22 ▶ if (h->readonly) {
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) { 3 19 22
1002 3 Copy(base, SvPVX(RETVAL), bytes, char); /* frozen: immutable, no lock needed */
1003 } else {
1004 19 nda_rwlock_rdlock(h);
1005 19 Copy(base, SvPVX(RETVAL), bytes, char);
1006 19 nda_rwlock_rdunlock(h);
1007 }
1008 22 SvCUR_set(RETVAL, (STRLEN)bytes);
1009 22 *SvEND(RETVAL) = '\0';
1010 }
1011 OUTPUT:
1012 RETVAL
1013  
1014 # Overwrite the whole data region from a byte string of exactly size*itemsize
1015 # bytes (write-locked). Used by from_pdl/update_from_pdl.
1016 void
1017 update_from_bytes(self, src)
1018 SV *self
1019 SV *src
1020 PREINIT:
1021 20 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 20 0 20
EXTRACT(self); 0 20 20
EXTRACT(self); 0 20 20
1022 CODE:
1023 {
1024 20 ▶ if (h->readonly) croak("Data::NDArray::Shared->update_from_bytes: array is frozen (read-only)");
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) croak("Data::NDArray::Shared->update_from_bytes: array is frozen (read-only)"); 2 18 20
1025 STRLEN slen;
1026 18 const char *sbytes = SvPVbyte(src, slen); /* resolve + any croak BEFORE the lock */
1027 18 ▶ REEXTRACT(self);
Branch: 2 branches
BranchTrueFalseTotal
REEXTRACT(self); 0 18 18
REEXTRACT(self); 0 18 18
1028 18 uint64_t bytes = h->size * h->itemsize; /* cached immutable geometry, not peer hdr fields */
1029 char *base;
1030 18 ▶ if ((uint64_t)slen != bytes)
Branch: 1 branch
BranchTrueFalseTotal
if ((uint64_t)slen != bytes) 1 17 18
1031 1 croak("Data::NDArray::Shared->update_from_bytes: got %" UVuf
1032 " bytes, expected %" UVuf, (UV)slen, (UV)bytes);
1033 17 base = nda_data(h);
1034 17 nda_rwlock_wrlock(h);
1035 17 ▶ if (h->hdr->sealed) { nda_rwlock_wrunlock(h);
Branch: 1 branch
BranchTrueFalseTotal
if (h->hdr->sealed) { nda_rwlock_wrunlock(h); 0 17 17
1036 0 croak("Data::NDArray::Shared->update_from_bytes: array is frozen (read-only)"); }
1037 17 Copy(sbytes, base, bytes, char);
1038 17 nda_rwlock_wrunlock(h);
1039 }
1040  
1041 # Zero-copy: build a PDL ndarray whose data IS our shared mmap, via PDL's C API
1042 # (PDL_DONTTOUCHDATA -- PDL never frees or reallocates the mapping, the only way to
1043 # alias external memory PDL won't silently detach from). The PDL C path is compiled
1044 # only when the module was built against PDL; otherwise this croaks. Returns SV*
1045 # (not pdl*) so the XSUB needs no PDL typemap and the no-PDL build still compiles.
1046 # `datatype` is the PDL type number; `dims_av` is in PDL order (fastest axis first).
1047 SV *
1048 _alias_pdl_create(self, datatype, dims_av)
1049 SV *self
1050 int datatype
1051 AV *dims_av
1052 PREINIT:
1053 3 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 3 0 3
EXTRACT(self); 0 3 3
EXTRACT(self); 0 3 3
1054 CODE:
1055 #ifdef HAVE_PDL
1056 {
1057 /* PDL's readonly flag does not cover every write path. */
1058 if (h->readonly || h->hdr->sealed)
1059 croak("Data::NDArray::Shared->as_pdl_alias: array is frozen (read-only); "
1060 "use to_pdl for a safe copy instead");
1061 IV nd = av_len(dims_av) + 1, i;
1062 PDL_Indx dims[NDA_MAX_DIMS];
1063 pdl_error err;
1064 pdl *p;
1065 nda_boot_pdl(aTHX);
1066 if (nd < 1 || nd > NDA_MAX_DIMS)
1067 croak("Data::NDArray::Shared: bad dim count %" IVdf, nd);
1068 for (i = 0; i < nd; i++) {
1069 SV **e = av_fetch(dims_av, (SSize_t)i, 0);
1070 dims[i] = (PDL_Indx)(e ? SvIV(*e) : 0);
1071 }
1072 p = PDL->pdlnew();
1073 if (!p) croak("Data::NDArray::Shared: PDL->pdlnew failed");
1074 p->datatype = (pdl_datatypes)datatype; /* set type before setdims so nbytes is right */
1075 err = PDL->setdims(p, dims, (PDL_Indx)nd);
1076 if (err.error) { PDL->destroy(p); croak("Data::NDArray::Shared: PDL->setdims failed"); }
1077 REEXTRACT(self);
1078 if (h->readonly || h->hdr->sealed) {
1079 PDL->destroy(p);
1080 croak("Data::NDArray::Shared->as_pdl_alias: array is frozen (read-only); "
1081 "use to_pdl for a safe copy instead");
1082 }
1083 p->data = nda_data(h); /* alias the shared mmap */
1084 p->state |= PDL_DONTTOUCHDATA | PDL_ALLOCATED;
1085 SV *owner = newSVsv(self);
1086 err = PDL->add_deletedata_magic(p, nda_pdl_nofree, (Size_t)PTR2UV(owner));
1087 if (err.error) {
1088 SvREFCNT_dec(owner);
1089 PDL->destroy(p);
1090 croak("Data::NDArray::Shared: PDL->add_deletedata_magic failed");
1091 }
1092 RETVAL = newSV(0);
1093 PDL->SetSV_PDL(RETVAL, p); /* wrap the pdl as its Perl object */
1094 }
1095 #else
1096 PERL_UNUSED_VAR(datatype);
1097 PERL_UNUSED_VAR(dims_av);
1098 3 croak("Data::NDArray::Shared: as_pdl_alias needs the module built with PDL "
1099 "installed; reinstall with PDL present (to_pdl/update_from_pdl need no rebuild)");
1100 RETVAL = &PL_sv_undef; /* not reached (croak does not return) */
1101 #endif
1102 OUTPUT:
1103 RETVAL
1104  
1105 SV *
1106 dtype(self)
1107 SV *self
1108 PREINIT:
1109 53 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 53 0 53
EXTRACT(self); 0 53 53
EXTRACT(self); 0 53 53
1110 CODE:
1111 53 RETVAL = newSVpv(nda_name_tab[h->dtype], 0); /* immutable */
1112 OUTPUT:
1113 RETVAL
1114  
1115 void
1116 freeze(self)
1117 SV *self
1118 PREINIT:
1119 5 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 5 0 5
EXTRACT(self); 0 5 5
EXTRACT(self); 0 5 5
1120 CODE:
1121 5 ▶ if (h->readonly) croak("Data::NDArray::Shared->freeze: cannot freeze a read-only handle");
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) croak("Data::NDArray::Shared->freeze: cannot freeze a read-only handle"); 2 3 5
1122 3 ▶ if (nda_freeze(h) != 0) croak("Data::NDArray::Shared->freeze: msync: %s", strerror(errno));
Branch: 1 branch
BranchTrueFalseTotal
if (nda_freeze(h) != 0) croak("Data::NDArray::Shared->freeze: msync: %s", strerror(errno)); 0 3 3
1123 3 h->readonly = 1; /* this handle now rejects mutation too (the file is sealed) */
1124  
1125 UV
1126 frozen(self)
1127 SV *self
1128 PREINIT:
1129 3 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 3 0 3
EXTRACT(self); 0 3 3
EXTRACT(self); 0 3 3
1130 CODE:
1131 3 ▶ RETVAL = h->hdr->sealed ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = h->hdr->sealed ? 1 : 0; 0 3 3
1132 OUTPUT:
1133 RETVAL
1134  
1135 UV
1136 readonly(self)
1137 SV *self
1138 PREINIT:
1139 4 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 4 0 4
EXTRACT(self); 0 4 4
EXTRACT(self); 0 4 4
1140 CODE:
1141 4 ▶ RETVAL = h->readonly ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = h->readonly ? 1 : 0; 0 4 4
1142 OUTPUT:
1143 RETVAL
1144  
1145 SV *
1146 stats(self)
1147 SV *self
1148 PREINIT:
1149 2 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 2 0 2
EXTRACT(self); 0 2 2
EXTRACT(self); 0 2 2
1150 CODE:
1151 {
1152 uint32_t dtype, ndim, itemsize, d;
1153 uint64_t size, ops, shp[NDA_MAX_DIMS];
1154 uint8_t sealed;
1155 /* Snapshot under the lock; build the HV after releasing it so an OOM
1156 in newHV/newSV* can never strand the lock. Frozen: immutable, no
1157 lock needed (the rwlock would WRITE into the PROT_READ header). */
1158 2 ▶ if (h->readonly) {
Branch: 1 branch
BranchTrueFalseTotal
if (h->readonly) { 1 1 2
1159 1 dtype = h->dtype; /* cached + in-range: safe to index nda_name_tab */
1160 1 ndim = h->hdr->ndim;
1161 1 ▶ if (ndim > NDA_MAX_DIMS) ndim = NDA_MAX_DIMS; /* clamp a peer-corrupted ndim */
Branch: 1 branch
BranchTrueFalseTotal
if (ndim > NDA_MAX_DIMS) ndim = NDA_MAX_DIMS; /* clamp a peer-corrupted ndim */ 0 1 1
1162 1 itemsize = h->hdr->itemsize;
1163 1 size = h->hdr->size;
1164 1 ops = h->hdr->stat_ops;
1165 1 sealed = h->hdr->sealed;
1166 3 ▶ for (d = 0; d < ndim; d++) shp[d] = h->hdr->shape[d];
Branch: 1 branch
BranchTrueFalseTotal
for (d = 0; d < ndim; d++) shp[d] = h->hdr->shape[d]; 2 1 3
1167 } else {
1168 1 nda_rwlock_rdlock(h);
1169 1 dtype = h->dtype; /* cached + in-range: safe to index nda_name_tab */
1170 1 ndim = h->hdr->ndim;
1171 1 ▶ if (ndim > NDA_MAX_DIMS) ndim = NDA_MAX_DIMS; /* clamp a peer-corrupted ndim */
Branch: 1 branch
BranchTrueFalseTotal
if (ndim > NDA_MAX_DIMS) ndim = NDA_MAX_DIMS; /* clamp a peer-corrupted ndim */ 0 1 1
1172 1 itemsize = h->hdr->itemsize;
1173 1 size = h->hdr->size;
1174 1 ops = h->hdr->stat_ops;
1175 1 sealed = h->hdr->sealed;
1176 3 ▶ for (d = 0; d < ndim; d++) shp[d] = h->hdr->shape[d];
Branch: 1 branch
BranchTrueFalseTotal
for (d = 0; d < ndim; d++) shp[d] = h->hdr->shape[d]; 2 1 3
1177 1 nda_rwlock_rdunlock(h);
1178 }
1179  
1180 2 AV *shape_av = newAV();
1181 /* Guard the low side: a peer-corrupted ndim==0 would wrap ndim-1 to
1182 UINT32_MAX -> a ~34GB av_extend -> uncatchable OOM abort. ndim==0 is
1183 handled gracefully (empty shape), matching shape()/strides(). */
1184 2 ▶ if (ndim > 0) av_extend(shape_av, (SSize_t)(ndim - 1));
Branch: 1 branch
BranchTrueFalseTotal
if (ndim > 0) av_extend(shape_av, (SSize_t)(ndim - 1)); 2 0 2
1185 6 ▶ for (d = 0; d < ndim; d++) av_store(shape_av, (SSize_t)d, newSVuv((UV)shp[d]));
Branch: 1 branch
BranchTrueFalseTotal
for (d = 0; d < ndim; d++) av_store(shape_av, (SSize_t)d, newSVuv((UV)shp[d])); 4 2 6
1186  
1187 2 HV *hv = newHV();
1188 2 hv_stores(hv, "dtype", newSVpv(nda_name_tab[dtype], 0));
1189 2 hv_stores(hv, "ndim", newSVuv((UV)ndim));
1190 2 hv_stores(hv, "size", newSVuv((UV)size));
1191 2 hv_stores(hv, "itemsize", newSVuv((UV)itemsize));
1192 2 hv_stores(hv, "shape", newRV_noinc((SV *)shape_av));
1193 2 hv_stores(hv, "ops", newSVuv((UV)ops));
1194 2 hv_stores(hv, "mmap_size", newSVuv((UV)h->mmap_size));
1195 2 hv_stores(hv, "frozen", newSVuv(sealed ? 1 : 0));
1196 2 hv_stores(hv, "readonly", newSVuv(h->readonly ? 1 : 0));
1197 2 RETVAL = newRV_noinc((SV *)hv);
1198 }
1199 OUTPUT:
1200 RETVAL
1201  
1202 SV *
1203 path(self)
1204 SV *self
1205 PREINIT:
1206 5 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 5 0 5
EXTRACT(self); 0 5 5
EXTRACT(self); 0 5 5
1207 CODE:
1208 5 ▶ RETVAL = h->path ? newSVpv(h->path, 0) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = h->path ? newSVpv(h->path, 0) : &PL_sv_undef; 3 2 5
1209 OUTPUT:
1210 RETVAL
1211  
1212 int
1213 memfd(self)
1214 SV *self
1215 PREINIT:
1216 7 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 7 0 7
EXTRACT(self); 0 7 7
EXTRACT(self); 0 7 7
1217 CODE:
1218 7 ▶ RETVAL = h->backing_fd;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = h->backing_fd; 0 7 7
1219 OUTPUT:
1220 RETVAL
1221  
1222 void
1223 sync(self)
1224 SV *self
1225 PREINIT:
1226 3 ▶ EXTRACT(self);
Branch: 3 branches
BranchTrueFalseTotal
EXTRACT(self); 3 0 3
EXTRACT(self); 0 3 3
EXTRACT(self); 0 3 3
1227 CODE:
1228 3 ▶ if (!h->readonly && nda_msync(h) != 0) croak("sync: %s", strerror(errno));
Branch: 2 branches
BranchTrueFalseTotal
if (!h->readonly && nda_msync(h) != 0) croak("sync: %s", strerror(errno)); 2 1 3
if (!h->readonly && nda_msync(h) != 0) croak("sync: %s", strerror(errno)); 0 2 2
1229  
1230 void
1231 unlink(self, ...)
1232 SV *self
1233 CODE:
1234 5 ▶ if (sv_isobject(self) && sv_derived_from(self, "Data::NDArray::Shared")) {
Branch: 2 branches
BranchTrueFalseTotal
if (sv_isobject(self) && sv_derived_from(self, "Data::NDArray::Shared")) { 2 1 3
if (sv_isobject(self) && sv_derived_from(self, "Data::NDArray::Shared")) { 2 0 2
1235 2 NdaHandle *h = INT2PTR(NdaHandle*, SvIV(SvRV(self)));
1236 2 ▶ if (h && h->path && unlink(h->path) != 0 && errno != ENOENT)
Branch: 4 branches
BranchTrueFalseTotal
if (h && h->path && unlink(h->path) != 0 && errno != ENOENT) 2 0 2
if (h && h->path && unlink(h->path) != 0 && errno != ENOENT) 2 0 2
if (h && h->path && unlink(h->path) != 0 && errno != ENOENT) 0 2 2
if (h && h->path && unlink(h->path) != 0 && errno != ENOENT) 0 0 0
1237 0 croak("Data::NDArray::Shared->unlink(%s): %s", h->path, strerror(errno));
1238 1 ▶ } else if (items >= 2 && (SvGETMAGIC(ST(1)), SvOK(ST(1)))) {
Branch: 4 branches
BranchTrueFalseTotal
} else if (items >= 2 && (SvGETMAGIC(ST(1)), SvOK(ST(1)))) { 1 0 1
} else if (items >= 2 && (SvGETMAGIC(ST(1)), SvOK(ST(1)))) { 0 1 1
} else if (items >= 2 && (SvGETMAGIC(ST(1)), SvOK(ST(1)))) { 0 0 0
} else if (items >= 2 && (SvGETMAGIC(ST(1)), SvOK(ST(1)))) { 1 0 1
1239 {
1240 1 const char *up = SvPV_nolen(ST(1));
1241 1 ▶ if (unlink(up) != 0 && errno != ENOENT)
Branch: 2 branches
BranchTrueFalseTotal
if (unlink(up) != 0 && errno != ENOENT) 0 1 1
if (unlink(up) != 0 && errno != ENOENT) 0 0 0
1242 0 croak("Data::NDArray::Shared->unlink(%s): %s", up, strerror(errno));
1243 }
1244 }
« ndarray.h ↑ Summary