Debian Codegraph @

src/c-blosc2/blosc/b2nd.c

raw
1 /*********************************************************************
2 Blosc - Blocked Shuffling and Compression Library
3
4 Copyright (c) 2021 Blosc Development Team <[email protected]>
5 https://blosc.org
6 License: BSD 3-Clause (see LICENSE.txt)
7
8 See LICENSE.txt for details about copyright and rights to use.
9 **********************************************************************/
10
11 #include "b2nd.h"
12 #include "context.h"
13 #include "blosc2/blosc2-common.h"
14 #include "blosc2.h"
15
16 #include <inttypes.h>
17 #include <stdlib.h>
18 #include <stdint.h>
19 #include <string.h>
20
21
22 int b2nd_serialize_meta(int8_t ndim, const int64_t *shape, const int32_t *chunkshape,
23 const int32_t *blockshape, const char *dtype, int8_t dtype_format,
24 uint8_t **smeta) {
25 if (dtype == NULL) {
26 dtype = B2ND_DEFAULT_DTYPE;
27 }
28 // dtype checks
29 if (dtype_format < 0) {
30 BLOSC_TRACE_ERROR("dtype_format cannot be negative");
31 BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
32 }
33 size_t dtype_len0 = strlen(dtype);
34 if (dtype_len0 > INT32_MAX) {
35 BLOSC_TRACE_ERROR("dtype is too large (len > %d)", INT32_MAX);
36 BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
37 }
38 const int32_t dtype_len = (int32_t) dtype_len0;
39 // Allocate space for b2nd metalayer
40 int32_t max_smeta_len = (int32_t) (1 + 1 + 1 + (1 + ndim * (1 + sizeof(int64_t))) +
41 (1 + ndim * (1 + sizeof(int32_t))) + (1 + ndim * (1 + sizeof(int32_t))) +
42 1 + 1 + sizeof(int32_t) + dtype_len);
43 *smeta = malloc((size_t) max_smeta_len);
44 BLOSC_ERROR_NULL(*smeta, BLOSC2_ERROR_MEMORY_ALLOC);
45 uint8_t *pmeta = *smeta;
46
47 // Build an array with 7 entries (version, ndim, shape, chunkshape, blockshape, dtype_format, dtype)
48 *pmeta++ = 0x90 + 7;
49
50 // version entry
51 *pmeta++ = B2ND_METALAYER_VERSION; // positive fixnum (7-bit positive integer)
52
53 // ndim entry
54 *pmeta++ = (uint8_t) ndim; // positive fixnum (7-bit positive integer)
55
56 // shape entry
57 *pmeta++ = (uint8_t) (0x90) + ndim; // fix array with ndim elements
58 for (uint8_t i = 0; i < ndim; i++) {
59 *pmeta++ = 0xd3; // int64
60 swap_store(pmeta, shape + i, sizeof(int64_t));
61 pmeta += sizeof(int64_t);
62 }
63
64 // chunkshape entry
65 *pmeta++ = (uint8_t) (0x90) + ndim; // fix array with ndim elements
66 for (uint8_t i = 0; i < ndim; i++) {
67 *pmeta++ = 0xd2; // int32
68 swap_store(pmeta, chunkshape + i, sizeof(int32_t));
69 pmeta += sizeof(int32_t);
70 }
71
72 // blockshape entry
73 *pmeta++ = (uint8_t) (0x90) + ndim; // fix array with ndim elements
74 for (uint8_t i = 0; i < ndim; i++) {
75 *pmeta++ = 0xd2; // int32
76 swap_store(pmeta, blockshape + i, sizeof(int32_t));
77 pmeta += sizeof(int32_t);
78 }
79
80 // dtype entry
81 *pmeta++ = dtype_format; // positive fixint (7-bit positive integer)
82 *pmeta++ = (uint8_t) (0xdb); // str with up to 2^31 elements
83 swap_store(pmeta, &dtype_len, sizeof(int32_t));
84 pmeta += sizeof(int32_t);
85 memcpy(pmeta, dtype, dtype_len);
86 pmeta += dtype_len;
87
88 int32_t slen = (int32_t) (pmeta - *smeta);
89 if (max_smeta_len != slen) {
90 BLOSC_TRACE_ERROR("meta length is inconsistent!");
91 return BLOSC2_ERROR_FAILURE;
92 }
93
94 return (int)slen;
95 }
96
97
98 int b2nd_deserialize_meta(const uint8_t *smeta, int32_t smeta_len, int8_t *ndim, int64_t *shape,
99 int32_t *chunkshape, int32_t *blockshape, char **dtype, int8_t *dtype_format) {
100 const uint8_t *pmeta = smeta;
101
102 // Check that we have an array with 7 entries (version, ndim, shape, chunkshape, blockshape, dtype_format, dtype)
103 pmeta += 1;
104
105 // version entry
106 // int8_t version = (int8_t)pmeta[0]; // positive fixnum (7-bit positive integer) commented to avoid warning
107 pmeta += 1;
108
109 // ndim entry
110 *ndim = (int8_t) pmeta[0];
111 int8_t ndim_aux = *ndim; // positive fixnum (7-bit positive integer)
112 pmeta += 1;
113
114 // shape entry
115 // Initialize to ones, as required by b2nd
116 for (int i = 0; i < ndim_aux; i++) shape[i] = 1;
117 pmeta += 1;
118 for (int8_t i = 0; i < ndim_aux; i++) {
119 pmeta += 1;
120 swap_store(shape + i, pmeta, sizeof(int64_t));
121 pmeta += sizeof(int64_t);
122 }
123
124 // chunkshape entry
125 // Initialize to ones, as required by b2nd
126 for (int i = 0; i < ndim_aux; i++) chunkshape[i] = 1;
127 pmeta += 1;
128 for (int8_t i = 0; i < ndim_aux; i++) {
129 pmeta += 1;
130 swap_store(chunkshape + i, pmeta, sizeof(int32_t));
131 pmeta += sizeof(int32_t);
132 }
133
134 // blockshape entry
135 // Initialize to ones, as required by b2nd
136 for (int i = 0; i < ndim_aux; i++) blockshape[i] = 1;
137 pmeta += 1;
138 for (int8_t i = 0; i < ndim_aux; i++) {
139 pmeta += 1;
140 swap_store(blockshape + i, pmeta, sizeof(int32_t));
141 pmeta += sizeof(int32_t);
142 }
143
144 // dtype entry
145 if (dtype_format == NULL || dtype == NULL) {
146 return (int32_t)(pmeta - smeta);
147 }
148 if (pmeta - smeta < smeta_len) {
149 // dtype info is here
150 *dtype_format = (int8_t) *(pmeta++);
151 pmeta += 1;
152 int dtype_len;
153 swap_store(&dtype_len, pmeta, sizeof(int32_t));
154 pmeta += sizeof(int32_t);
155 *dtype = (char*)malloc(dtype_len + 1);
156 char* dtype_ = *dtype;
157 memcpy(dtype_, (char*)pmeta, dtype_len);
158 dtype_[dtype_len] = '\0';
159 pmeta += dtype_len;
160 }
161 else {
162 // dtype is mandatory in b2nd metalayer, but this is mainly meant as
163 // a fall-back for deprecated caterva headers
164 *dtype = NULL;
165 *dtype_format = 0;
166 }
167
168 int32_t slen = (int32_t) (pmeta - smeta);
169 return (int)slen;
170 }
171
172
173
174 int update_shape(b2nd_array_t *array, int8_t ndim, const int64_t *shape,
175 const int32_t *chunkshape, const int32_t *blockshape) {
176 array->ndim = ndim;
177 array->nitems = 1;
178 array->extnitems = 1;
179 array->extchunknitems = 1;
180 array->chunknitems = 1;
181 array->blocknitems = 1;
182 for (int i = 0; i < B2ND_MAX_DIM; ++i) {
183 if (i < ndim) {
184 array->shape[i] = shape[i];
185 array->chunkshape[i] = chunkshape[i];
186 array->blockshape[i] = blockshape[i];
187 if (shape[i] != 0) {
188 if (shape[i] % array->chunkshape[i] == 0) {
189 array->extshape[i] = shape[i];
190 } else {
191 array->extshape[i] = shape[i] + chunkshape[i] - shape[i] % chunkshape[i];
192 }
193 if (chunkshape[i] % blockshape[i] == 0) {
194 array->extchunkshape[i] = chunkshape[i];
195 } else {
196 array->extchunkshape[i] =
197 chunkshape[i] + blockshape[i] - chunkshape[i] % blockshape[i];
198 }
199 } else {
200 array->extchunkshape[i] = chunkshape[i];
201 array->extshape[i] = 0;
202 }
203 } else {
204 array->blockshape[i] = 1;
205 array->chunkshape[i] = 1;
206 array->extshape[i] = 1;
207 array->extchunkshape[i] = 1;
208 array->shape[i] = 1;
209 }
210 array->nitems *= array->shape[i];
211 array->extnitems *= array->extshape[i];
212 array->extchunknitems *= array->extchunkshape[i];
213 array->chunknitems *= array->chunkshape[i];
214 array->blocknitems *= array->blockshape[i];
215 }
216
217 // Compute strides
218 array->item_array_strides[ndim - 1] = 1;
219 array->item_extchunk_strides[ndim - 1] = 1;
220 array->item_chunk_strides[ndim - 1] = 1;
221 array->item_block_strides[ndim - 1] = 1;
222 array->block_chunk_strides[ndim - 1] = 1;
223 array->chunk_array_strides[ndim - 1] = 1;
224 for (int i = ndim - 2; i >= 0; --i) {
225 if (shape[i + 1] != 0) {
226 array->item_array_strides[i] = array->item_array_strides[i + 1] * array->shape[i + 1];
227 array->item_extchunk_strides[i] =
228 array->item_extchunk_strides[i + 1] * array->extchunkshape[i + 1];
229 array->item_chunk_strides[i] =
230 array->item_chunk_strides[i + 1] * array->chunkshape[i + 1];
231 array->item_block_strides[i] =
232 array->item_block_strides[i + 1] * array->blockshape[i + 1];
233 array->block_chunk_strides[i] = array->block_chunk_strides[i + 1] *
234 (array->extchunkshape[i + 1] /
235 array->blockshape[i + 1]);
236 array->chunk_array_strides[i] = array->chunk_array_strides[i + 1] *
237 (array->extshape[i + 1] * array->chunkshape[i + 1]);
238 } else {
239 array->item_array_strides[i] = 0;
240 array->item_extchunk_strides[i] = 0;
241 array->item_chunk_strides[i] = 0;
242 array->item_block_strides[i] = 0;
243 array->block_chunk_strides[i] = 0;
244 array->chunk_array_strides[i] = 0;
245 }
246 }
247 if (array->sc) {
248 uint8_t *smeta = NULL;
249 // Serialize the dimension info ...
250 int32_t smeta_len =
251 b2nd_serialize_meta(array->ndim, array->shape, array->chunkshape, array->blockshape,
252 array->dtype, array->dtype_format, &smeta);
253 if (smeta_len < 0) {
254 BLOSC_TRACE_ERROR("Error during serializing dims info for Blosc2 NDim");
255 BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
256 }
257 // ... and update it in its metalayer
258 if (blosc2_meta_exists(array->sc, "b2nd") < 0) {
259 if (blosc2_meta_add(array->sc, "b2nd", smeta, smeta_len) < 0) {
260 BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
261 }
262 } else {
263 if (blosc2_meta_update(array->sc, "b2nd", smeta, smeta_len) < 0) {
264 BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
265 }
266 }
267 free(smeta);
268 }
269
270 return BLOSC2_ERROR_SUCCESS;
271 }
272
273
274 int array_without_schunk(b2nd_context_t *ctx, b2nd_array_t **array) {
275 /* Create a b2nd_array_t buffer */
276 (*array) = (b2nd_array_t *) malloc(sizeof(b2nd_array_t));
277 BLOSC_ERROR_NULL(*array, BLOSC2_ERROR_MEMORY_ALLOC);
278
279 (*array)->sc = NULL;
280
281 (*array)->ndim = ctx->ndim;
282 int64_t *shape = ctx->shape;
283 int32_t *chunkshape = ctx->chunkshape;
284 int32_t *blockshape = ctx->blockshape;
285 BLOSC_ERROR(update_shape(*array, ctx->ndim, shape, chunkshape, blockshape));
286
287 if (ctx->dtype != NULL) {
288 (*array)->dtype = malloc(strlen(ctx->dtype) + 1);
289 strcpy((*array)->dtype, ctx->dtype);
290 } else {
291 (*array)->dtype = NULL;
292 }
293
294 (*array)->dtype_format = ctx->dtype_format;
295
296 // The partition cache (empty initially)
297 (*array)->chunk_cache.data = NULL;
298 (*array)->chunk_cache.nchunk = -1; // means no valid cache yet
299
300 return BLOSC2_ERROR_SUCCESS;
301 }
302
303
304 int array_new(b2nd_context_t *ctx, int special_value, b2nd_array_t **array) {
305 BLOSC_ERROR(array_without_schunk(ctx, array));
306
307 blosc2_schunk *sc = blosc2_schunk_new(ctx->b2_storage);
308 if (sc == NULL) {
309 BLOSC_TRACE_ERROR("Pointer is NULL");
310 return BLOSC2_ERROR_FAILURE;
311 }
312 // Set the chunksize for the schunk, as it cannot be derived from storage
313 int32_t chunksize = (int32_t) (*array)->extchunknitems * sc->typesize;
314 sc->chunksize = chunksize;
315
316 // Serialize the dimension info
317 if (sc->nmetalayers >= BLOSC2_MAX_METALAYERS) {
318 BLOSC_TRACE_ERROR("the number of metalayers for this schunk has been exceeded");
319 return BLOSC2_ERROR_FAILURE;
320 }
321 uint8_t *smeta = NULL;
322 int32_t smeta_len = b2nd_serialize_meta(ctx->ndim,
323 (*array)->shape,
324 (*array)->chunkshape,
325 (*array)->blockshape,
326 (*array)->dtype,
327 (*array)->dtype_format,
328 &smeta);
329 if (smeta_len < 0) {
330 BLOSC_TRACE_ERROR("error during serializing dims info for Blosc2 NDim");
331 return BLOSC2_ERROR_FAILURE;
332 }
333
334 // And store it in b2nd metalayer
335 if (blosc2_meta_add(sc, "b2nd", smeta, smeta_len) < 0) {
336 return BLOSC2_ERROR_FAILURE;
337 }
338
339 free(smeta);
340
341 for (int i = 0; i < ctx->nmetalayers; ++i) {
342 char *name = ctx->metalayers[i].name;
343 uint8_t *data = ctx->metalayers[i].content;
344 int32_t size = ctx->metalayers[i].content_len;
345 if (blosc2_meta_add(sc, name, data, size) < 0) {
346 BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
347 }
348 }
349
350 if ((*array)->extchunknitems * sc->typesize > BLOSC2_MAX_BUFFERSIZE){
351 BLOSC_TRACE_ERROR("Chunksize exceeds maximum of %d", BLOSC2_MAX_BUFFERSIZE);
352 return BLOSC2_ERROR_MAX_BUFSIZE_EXCEEDED;
353 }
354 // Fill schunk with uninit values
355 if ((*array)->nitems != 0) {
356 int64_t nchunks = (*array)->extnitems / (*array)->chunknitems;
357 int64_t nitems = nchunks * (*array)->extchunknitems;
358 BLOSC_ERROR(blosc2_schunk_fill_special(sc, nitems, special_value, chunksize));
359 }
360 (*array)->sc = sc;
361
362 return BLOSC2_ERROR_SUCCESS;
363 }
364
365
366 int b2nd_uninit(b2nd_context_t *ctx, b2nd_array_t **array) {
367 BLOSC_ERROR_NULL(ctx, BLOSC2_ERROR_NULL_POINTER);
368 BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
369
370 BLOSC_ERROR(array_new(ctx, BLOSC2_SPECIAL_UNINIT, array));
371
372 return BLOSC2_ERROR_SUCCESS;
373 }
374
375
376 int b2nd_empty(b2nd_context_t *ctx, b2nd_array_t **array) {
377 BLOSC_ERROR_NULL(ctx, BLOSC2_ERROR_NULL_POINTER);
378 BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
379
380 // Fill with zeros to avoid variable cratios
381 BLOSC_ERROR(array_new(ctx, BLOSC2_SPECIAL_ZERO, array));
382
383 return BLOSC2_ERROR_SUCCESS;
384 }
385
386
387 int b2nd_zeros(b2nd_context_t *ctx, b2nd_array_t **array) {
388 BLOSC_ERROR_NULL(ctx, BLOSC2_ERROR_NULL_POINTER);
389 BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
390
391 BLOSC_ERROR(array_new(ctx, BLOSC2_SPECIAL_ZERO, array));
392
393 return BLOSC2_ERROR_SUCCESS;
394 }
395
396
397 int b2nd_nans(b2nd_context_t *ctx, b2nd_array_t **array) {
398 BLOSC_ERROR_NULL(ctx, BLOSC2_ERROR_NULL_POINTER);
399 BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
400
401 BLOSC_ERROR(array_new(ctx, BLOSC2_SPECIAL_NAN, array));
402
403 const int32_t typesize = (*array)->sc->typesize;
404 if (typesize != 4 && typesize != 8)
405 {
406 BLOSC_TRACE_ERROR("Unsupported typesize for NaN");
407 return BLOSC2_ERROR_DATA;
408 }
409
410 return BLOSC2_ERROR_SUCCESS;
411 }
412
413
414 int b2nd_full(b2nd_context_t *ctx, b2nd_array_t **array, const void *fill_value) {
415 BLOSC_ERROR_NULL(ctx, BLOSC2_ERROR_NULL_POINTER);
416 BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
417
418 BLOSC_ERROR(b2nd_empty(ctx, array));
419
420 int32_t chunkbytes = (int32_t) (*array)->extchunknitems * (*array)->sc->typesize;
421
422 blosc2_cparams *cparams;
423 if (blosc2_schunk_get_cparams((*array)->sc, &cparams) != 0) {
424 BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
425 }
426
427 int32_t chunksize = BLOSC_EXTENDED_HEADER_LENGTH + (*array)->sc->typesize;
428 uint8_t *chunk = malloc(chunksize);
429 BLOSC_ERROR_NULL(chunk, BLOSC2_ERROR_MEMORY_ALLOC);
430 if (blosc2_chunk_repeatval(*cparams, chunkbytes, chunk, chunksize, fill_value) < 0) {
431 BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
432 }
433 free(cparams);
434
435 for (int i = 0; i < (*array)->sc->nchunks; ++i) {
436 if (blosc2_schunk_update_chunk((*array)->sc, i, chunk, true) < 0) {
437 BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
438 }
439 }
440 free(chunk);
441
442 return BLOSC2_ERROR_SUCCESS;
443 }
444
445
446 int b2nd_from_schunk(blosc2_schunk *schunk, b2nd_array_t **array) {
447 BLOSC_ERROR_NULL(schunk, BLOSC2_ERROR_NULL_POINTER);
448 BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
449
450 if (schunk == NULL) {
451 BLOSC_TRACE_ERROR("Schunk is null");
452 return BLOSC2_ERROR_NULL_POINTER;
453 }
454
455 blosc2_cparams *cparams;
456 if (blosc2_schunk_get_cparams(schunk, &cparams) < 0) {
457 BLOSC_TRACE_ERROR("Blosc error");
458 return BLOSC2_ERROR_NULL_POINTER;
459 }
460 free(cparams);
461
462 b2nd_context_t params = {0};
463 params.b2_storage = schunk->storage;
464
465 // Deserialize the b2nd metalayer
466 uint8_t *smeta;
467 int32_t smeta_len;
468 if (blosc2_meta_get(schunk, "b2nd", &smeta, &smeta_len) < 0) {
469 // Try with a caterva metalayer; we are meant to be backward compatible with it
470 if (blosc2_meta_get(schunk, "caterva", &smeta, &smeta_len) < 0) {
471 BLOSC_ERROR(BLOSC2_ERROR_METALAYER_NOT_FOUND);
472 }
473 }
474 BLOSC_ERROR(b2nd_deserialize_meta(smeta, smeta_len, &params.ndim, params.shape,
475 params.chunkshape, params.blockshape, &params.dtype,
476 &params.dtype_format));
477 free(smeta);
478
479 BLOSC_ERROR(array_without_schunk(&params, array));
480 free(params.dtype);
481
482 (*array)->sc = schunk;
483
484 if ((*array) == NULL) {
485 BLOSC_TRACE_ERROR("Error creating a b2nd container from a frame");
486 return BLOSC2_ERROR_NULL_POINTER;
487 }
488
489 return BLOSC2_ERROR_SUCCESS;
490 }
491
492
493 int b2nd_to_cframe(const b2nd_array_t *array, uint8_t **cframe, int64_t *cframe_len,
494 bool *needs_free) {
495 BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
496 BLOSC_ERROR_NULL(cframe, BLOSC2_ERROR_NULL_POINTER);
497 BLOSC_ERROR_NULL(cframe_len, BLOSC2_ERROR_NULL_POINTER);
498 BLOSC_ERROR_NULL(needs_free, BLOSC2_ERROR_NULL_POINTER);
499
500 *cframe_len = blosc2_schunk_to_buffer(array->sc, cframe, needs_free);
501 if (*cframe_len <= 0) {
502 BLOSC_TRACE_ERROR("Error serializing the b2nd array");
503 return BLOSC2_ERROR_FAILURE;
504 }
505 return BLOSC2_ERROR_SUCCESS;
506 }
507
508
509 int b2nd_from_cframe(uint8_t *cframe, int64_t cframe_len, bool copy, b2nd_array_t **array) {
510 BLOSC_ERROR_NULL(cframe, BLOSC2_ERROR_NULL_POINTER);
511 BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
512
513 blosc2_schunk *sc = blosc2_schunk_from_buffer(cframe, cframe_len, copy);
514 if (sc == NULL) {
515 BLOSC_TRACE_ERROR("Blosc error");
516 return BLOSC2_ERROR_FAILURE;
517 }
518 // ...and create a b2nd array out of it
519 BLOSC_ERROR(b2nd_from_schunk(sc, array));
520
521 return BLOSC2_ERROR_SUCCESS;
522 }
523
524
525 int b2nd_open(const char *urlpath, b2nd_array_t **array) {
526 BLOSC_ERROR_NULL(urlpath, BLOSC2_ERROR_NULL_POINTER);
527 BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
528
529 blosc2_schunk *sc = blosc2_schunk_open(urlpath);
530
531 // ...and create a b2nd array out of it
532 BLOSC_ERROR(b2nd_from_schunk(sc, array));
533
534 return BLOSC2_ERROR_SUCCESS;
535 }
536
537
538 int b2nd_open_offset(const char *urlpath, b2nd_array_t **array, int64_t offset) {
539 BLOSC_ERROR_NULL(urlpath, BLOSC2_ERROR_NULL_POINTER);
540 BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
541
542 blosc2_schunk *sc = blosc2_schunk_open_offset(urlpath, offset);
543
544 // ...and create a b2nd array out of it
545 BLOSC_ERROR(b2nd_from_schunk(sc, array));
546
547 return BLOSC2_ERROR_SUCCESS;
548 }
549
550
551 int b2nd_free(b2nd_array_t *array) {
552 BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
553
554 if (array) {
555 if (array->sc != NULL) {
556 blosc2_schunk_free(array->sc);
557 }
558 free(array->dtype);
559 free(array);
560 }
561 return BLOSC2_ERROR_SUCCESS;
562 }
563
564
565 int b2nd_from_cbuffer(b2nd_context_t *ctx, b2nd_array_t **array, const void *buffer, int64_t buffersize) {
566 BLOSC_ERROR_NULL(ctx, BLOSC2_ERROR_NULL_POINTER);
567 BLOSC_ERROR_NULL(buffer, BLOSC2_ERROR_NULL_POINTER);
568 BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
569
570 BLOSC_ERROR(b2nd_empty(ctx, array));
571
572 if (buffersize < (int64_t) (*array)->nitems * (*array)->sc->typesize) {
573 BLOSC_TRACE_ERROR("The buffersize (%lld) is smaller than the array size (%lld)",
574 (long long) buffersize, (long long) (*array)->nitems * (*array)->sc->typesize);
575 BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
576 }
577
578 if ((*array)->nitems == 0) {
579 return BLOSC2_ERROR_SUCCESS;
580 }
581
582 int64_t start[B2ND_MAX_DIM] = {0};
583 int64_t *stop = (*array)->shape;
584 int64_t *shape = (*array)->shape;
585 BLOSC_ERROR(b2nd_set_slice_cbuffer(buffer, shape, buffersize, start, stop, *array));
586
587 return BLOSC2_ERROR_SUCCESS;
588 }
589
590
591 int b2nd_to_cbuffer(const b2nd_array_t *array, void *buffer,
592 int64_t buffersize) {
593 BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
594 BLOSC_ERROR_NULL(buffer, BLOSC2_ERROR_NULL_POINTER);
595
596 if (buffersize < (int64_t) array->nitems * array->sc->typesize) {
597 BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
598 }
599
600 if (array->nitems == 0) {
601 return BLOSC2_ERROR_SUCCESS;
602 }
603
604 int64_t start[B2ND_MAX_DIM] = {0};
605 const int64_t *stop = array->shape;
606 BLOSC_ERROR(b2nd_get_slice_cbuffer(array, start, stop, buffer, array->shape, buffersize));
607 return BLOSC2_ERROR_SUCCESS;
608 }
609
610 int b2nd_get_slice_nchunks(const b2nd_array_t *array, const int64_t *start, const int64_t *stop, int64_t **chunks_idx) {
611 BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
612 BLOSC_ERROR_NULL(start, BLOSC2_ERROR_NULL_POINTER);
613 BLOSC_ERROR_NULL(stop, BLOSC2_ERROR_NULL_POINTER);
614
615 int8_t ndim = array->ndim;
616
617 // 0-dim case
618 if (ndim == 0) {
619 *chunks_idx = malloc(1 * sizeof(int64_t));
620 *chunks_idx[0] = 0;
621 return 1;
622 }
623
624 int64_t chunks_in_array[B2ND_MAX_DIM] = {0};
625 for (int i = 0; i < ndim; ++i) {
626 chunks_in_array[i] = array->extshape[i] / array->chunkshape[i];
627 }
628
629 int64_t chunks_in_array_strides[B2ND_MAX_DIM];
630 chunks_in_array_strides[ndim - 1] = 1;
631 for (int i = ndim - 2; i >= 0; --i) {
632 chunks_in_array_strides[i] = chunks_in_array_strides[i + 1] * chunks_in_array[i + 1];
633 }
634
635 // Compute the number of chunks to update
636 int64_t update_start[B2ND_MAX_DIM];
637 int64_t update_shape[B2ND_MAX_DIM];
638
639 int64_t update_nchunks = 1;
640 for (int i = 0; i < ndim; ++i) {
641 int64_t pos = 0;
642 while (pos <= start[i]) {
643 pos += array->chunkshape[i];
644 }
645 update_start[i] = pos / array->chunkshape[i] - 1;
646 while (pos < stop[i]) {
647 pos += array->chunkshape[i];
648 }
649 update_shape[i] = pos / array->chunkshape[i] - update_start[i];
650 update_nchunks *= update_shape[i];
651 }
652
653 int nchunks = 0;
654 // Initially we do not know the number of chunks that will be affected
655 *chunks_idx = malloc(array->sc->nchunks * sizeof(int64_t));
656 int64_t *ptr = *chunks_idx;
657 for (int update_nchunk = 0; update_nchunk < update_nchunks; ++update_nchunk) {
658 int64_t nchunk_ndim[B2ND_MAX_DIM] = {0};
659 blosc2_unidim_to_multidim(ndim, update_shape, update_nchunk, nchunk_ndim);
660 for (int i = 0; i < ndim; ++i) {
661 nchunk_ndim[i] += update_start[i];
662 }
663 int64_t nchunk;
664 blosc2_multidim_to_unidim(nchunk_ndim, ndim, chunks_in_array_strides, &nchunk);
665
666 // Check if the chunk is inside the slice domain
667 int64_t chunk_start[B2ND_MAX_DIM] = {0};
668 int64_t chunk_stop[B2ND_MAX_DIM] = {0};
669 for (int i = 0; i < ndim; ++i) {
670 chunk_start[i] = nchunk_ndim[i] * array->chunkshape[i];
671 chunk_stop[i] = chunk_start[i] + array->chunkshape[i];
672 if (chunk_stop[i] > array->shape[i]) {
673 chunk_stop[i] = array->shape[i];
674 }
675 }
676 bool chunk_empty = false;
677 for (int i = 0; i < ndim; ++i) {
678 chunk_empty |= (chunk_stop[i] <= start[i] || chunk_start[i] >= stop[i]);
679 }
680 if (chunk_empty) {
681 continue;
682 }
683
684 ptr[nchunks] = nchunk;
685 nchunks++;
686 }
687
688 if (nchunks < array->sc->nchunks) {
689 *chunks_idx = realloc(ptr, nchunks * sizeof(int64_t));
690 }
691
692 return nchunks;
693 }
694
695
696 // Check whether the slice defined by start and stop is a single chunk and contiguous
697 // in the C order. We also need blocks inside a chunk, and chunks inside an array,
698 // are C contiguous. This is a fast path for the get_slice and set_slice functions.
699 int64_t nchunk_fastpath(const b2nd_array_t *array, const int64_t *start,
700 const int64_t *stop, const int64_t slice_size) {
701 if (slice_size != array->chunknitems) {
702 return -1;
703 }
704
705 int ndim = (int) array->ndim;
706 int inner_dim = ndim - 1;
707 int64_t partial_slice_size = 1;
708 int outer_dims = 0;
709 for (int j = 0; j < inner_dim; ++j) {
710 outer_dims += (array->blockshape[j] != 1);
711 }
712 int64_t partial_chunk_size = 1;
713 for (int i = ndim - 1; i >= 0; --i) {
714 // We need to check if the slice is contiguous in the C order
715 if (array->extshape[i] != array->shape[i]) {
716 return -1;
717 }
718 if (array->extchunkshape[i] != array->chunkshape[i]) {
719 return -1;
720 }
721
722 // blocks need to be C contiguous inside the chunk as well
723 if (array->chunkshape[i] > array->blockshape[i]) {
724 if (i < inner_dim) {
725 if (array->chunkshape[i] % array->blockshape[i] != 0) {
726 return -1;
727 }
728 }
729 else {
730 // A block is still contiguous in the C order if the outer dimensions are 1 and the inner is
731 // a divisor of the chunkshape
732 if ( ! (array->chunkshape[i] == array->blockshape[i] ||
733 (outer_dims == 0 && array->chunkshape[i] % array->blockshape[i] == 0))) {
734 return -1;
735 }
736 }
737 }
738
739 // We need start and stop to be aligned with the chunkshape
740 // Do that by computing the slice size in reverse order and compare with the chunkshape
741 partial_slice_size *= stop[i] - start[i];
742 partial_chunk_size *= array->chunkshape[i];
743 if (partial_slice_size != partial_chunk_size) {
744 return -1;
745 }
746 // Ensure that the slice starts at the beginning of the chunk
747 if (start[i] % array->chunkshape[i] != 0) {
748 return -1;
749 }
750 }
751 // Compute the chunk number
752 int64_t *chunks_idx;
753 int nchunks = b2nd_get_slice_nchunks(array, start, stop, &chunks_idx);
754 if (nchunks != 1) {
755 free(chunks_idx);
756 BLOSC_TRACE_ERROR("The number of chunks to read is not 1; go fix the code");
757 BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
758 }
759 int64_t nchunk = chunks_idx[0];
760 free(chunks_idx);
761
762 return nchunk;
763 }
764
765
766 // Setting and getting slices
767 int get_set_slice(void *buffer, int64_t buffersize, const int64_t *start, const int64_t *stop,
768 const int64_t *shape, b2nd_array_t *array, bool set_slice) {
769 BLOSC_ERROR_NULL(buffer, BLOSC2_ERROR_NULL_POINTER);
770 BLOSC_ERROR_NULL(start, BLOSC2_ERROR_NULL_POINTER);
771 BLOSC_ERROR_NULL(stop, BLOSC2_ERROR_NULL_POINTER);
772 BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
773 if (buffersize < 0) {
774 BLOSC_TRACE_ERROR("buffersize is < 0");
775 BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
776 }
777
778 uint8_t *buffer_b = buffer;
779 const int64_t *buffer_start = start;
780 const int64_t *buffer_stop = stop;
781 const int64_t *buffer_shape = shape;
782
783 int8_t ndim = array->ndim;
784
785 // 0-dim case
786 if (ndim == 0) {
787 if (set_slice) {
788 int32_t chunk_size = array->sc->typesize + BLOSC2_MAX_OVERHEAD;
789 uint8_t *chunk = malloc(chunk_size);
790 BLOSC_ERROR_NULL(chunk, BLOSC2_ERROR_MEMORY_ALLOC);
791 if (blosc2_compress_ctx(array->sc->cctx, buffer_b, array->sc->typesize, chunk, chunk_size) < 0) {
792 BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
793 }
794 if (blosc2_schunk_update_chunk(array->sc, 0, chunk, false) < 0) {
795 BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
796 }
797
798 } else {
799 if (blosc2_schunk_decompress_chunk(array->sc, 0, buffer_b, array->sc->typesize) < 0) {
800 BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
801 }
802 }
803 return BLOSC2_ERROR_SUCCESS;
804 }
805
806 if (array->nitems == 0) {
807 return BLOSC2_ERROR_SUCCESS;
808 }
809
810 int64_t nelems_slice = 1;
811 for (int i = 0; i < array->ndim; ++i) {
812 if (stop[i] - start[i] > shape[i]) {
813 BLOSC_TRACE_ERROR("The buffer shape can not be smaller than the slice shape");
814 return BLOSC2_ERROR_INVALID_PARAM;
815 }
816 nelems_slice *= stop[i] - start[i];
817 }
818 int64_t slice_nbytes = nelems_slice * array->sc->typesize;
819 int32_t data_nbytes = (int32_t) array->extchunknitems * array->sc->typesize;
820
821 if (buffersize < slice_nbytes) {
822 BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
823 }
824
825 // Check for fast path for aligned slices with chunks and blocks (only 1 chunk is supported)
826 int64_t nchunk = nchunk_fastpath(array, start, stop, nelems_slice);
827 if (nchunk >= 0) {
828 if (set_slice) {
829 // Fast path for set. Let's set the chunk buffer straight into the array.
830 // Compress the chunk
831 int32_t chunk_nbytes = data_nbytes + BLOSC2_MAX_OVERHEAD;
832 uint8_t *chunk = malloc(chunk_nbytes);
833 BLOSC_ERROR_NULL(chunk, BLOSC2_ERROR_MEMORY_ALLOC);
834 int brc;
835 // Update current_chunk in case a prefilter is applied
836 array->sc->current_nchunk = nchunk;
837 brc = blosc2_compress_ctx(array->sc->cctx, buffer, data_nbytes, chunk, chunk_nbytes);
838 if (brc < 0) {
839 BLOSC_TRACE_ERROR("Blosc can not compress the data");
840 BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
841 }
842 int64_t brc_ = blosc2_schunk_update_chunk(array->sc, nchunk, chunk, false);
843 if (brc_ < 0) {
844 BLOSC_TRACE_ERROR("Blosc can not update the chunk");
845 BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
846 }
847 // We are done
848 return BLOSC2_ERROR_SUCCESS;
849 }
850 else {
851 // Fast path for get. Let's read the chunk straight into the buffer.
852 if (blosc2_schunk_decompress_chunk(array->sc, nchunk, buffer, (int32_t) slice_nbytes) < 0) {
853 BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
854 }
855 return BLOSC2_ERROR_SUCCESS;
856 }
857 }
858
859 // Slow path for set and get
860
861 uint8_t *data = malloc(data_nbytes);
862 BLOSC_ERROR_NULL(data, BLOSC2_ERROR_MEMORY_ALLOC);
863
864 int64_t chunks_in_array[B2ND_MAX_DIM] = {0};
865 for (int i = 0; i < ndim; ++i) {
866 chunks_in_array[i] = array->extshape[i] / array->chunkshape[i];
867 }
868
869 int64_t chunks_in_array_strides[B2ND_MAX_DIM];
870 chunks_in_array_strides[ndim - 1] = 1;
871 for (int i = ndim - 2; i >= 0; --i) {
872 chunks_in_array_strides[i] = chunks_in_array_strides[i + 1] * chunks_in_array[i + 1];
873 }
874
875 int64_t blocks_in_chunk[B2ND_MAX_DIM] = {0};
876 for (int i = 0; i < ndim; ++i) {
877 blocks_in_chunk[i] = array->extchunkshape[i] / array->blockshape[i];
878 }
879
880 // Compute the number of chunks to update
881 int64_t update_start[B2ND_MAX_DIM];
882 int64_t update_shape[B2ND_MAX_DIM];
883
884 int64_t update_nchunks = 1;
885 for (int i = 0; i < ndim; ++i) {
886 int64_t pos = 0;
887 while (pos <= buffer_start[i]) {
888 pos += array->chunkshape[i];
889 }
890 update_start[i] = pos / array->chunkshape[i] - 1;
891 while (pos < buffer_stop[i]) {
892 pos += array->chunkshape[i];
893 }
894 update_shape[i] = pos / array->chunkshape[i] - update_start[i];
895 update_nchunks *= update_shape[i];
896 }
897
898 for (int update_nchunk = 0; update_nchunk < update_nchunks; ++update_nchunk) {
899 int64_t nchunk_ndim[B2ND_MAX_DIM] = {0};
900 blosc2_unidim_to_multidim(ndim, update_shape, update_nchunk, nchunk_ndim);
901 for (int i = 0; i < ndim; ++i) {
902 nchunk_ndim[i] += update_start[i];
903 }
904 int64_t nchunk;
905 blosc2_multidim_to_unidim(nchunk_ndim, ndim, chunks_in_array_strides, &nchunk);
906
907 // Check if the chunk needs to be updated
908 int64_t chunk_start[B2ND_MAX_DIM] = {0};
909 int64_t chunk_stop[B2ND_MAX_DIM] = {0};
910 for (int i = 0; i < ndim; ++i) {
911 chunk_start[i] = nchunk_ndim[i] * array->chunkshape[i];
912 chunk_stop[i] = chunk_start[i] + array->chunkshape[i];
913 if (chunk_stop[i] > array->shape[i]) {
914 chunk_stop[i] = array->shape[i];
915 }
916 }
917 bool chunk_empty = false;
918 for (int i = 0; i < ndim; ++i) {
919 chunk_empty |= (chunk_stop[i] <= buffer_start[i] || chunk_start[i] >= buffer_stop[i]);
920 }
921 if (chunk_empty) {
922 continue;
923 }
924
925 int32_t nblocks = (int32_t) array->extchunknitems / array->blocknitems;
926 if (set_slice) {
927 // Check if all the chunk is going to be updated and avoid the decompression
928 bool decompress_chunk = false;
929 for (int i = 0; i < ndim; ++i) {
930 decompress_chunk |= (chunk_start[i] < buffer_start[i] || chunk_stop[i] > buffer_stop[i]);
931 }
932
933 if (decompress_chunk) {
934 int err = blosc2_schunk_decompress_chunk(array->sc, nchunk, data, data_nbytes);
935 if (err < 0) {
936 BLOSC_TRACE_ERROR("Error decompressing chunk");
937 BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
938 }
939 } else {
940 // Avoid writing non zero padding from previous chunk
941 memset(data, 0, data_nbytes);
942 }
943 } else {
944 bool *block_maskout = malloc(nblocks);
945 BLOSC_ERROR_NULL(block_maskout, BLOSC2_ERROR_MEMORY_ALLOC);
946 for (int nblock = 0; nblock < nblocks; ++nblock) {
947 int64_t nblock_ndim[B2ND_MAX_DIM] = {0};
948 blosc2_unidim_to_multidim(ndim, blocks_in_chunk, nblock, nblock_ndim);
949
950 // Check if the block needs to be updated
951 int64_t block_start[B2ND_MAX_DIM] = {0};
952 int64_t block_stop[B2ND_MAX_DIM] = {0};
953 for (int i = 0; i < ndim; ++i) {
954 block_start[i] = nblock_ndim[i] * array->blockshape[i];
955 block_stop[i] = block_start[i] + array->blockshape[i];
956 block_start[i] += chunk_start[i];
957 block_stop[i] += chunk_start[i];
958
959 if (block_start[i] > chunk_stop[i]) {
960 block_start[i] = chunk_stop[i];
961 }
962 if (block_stop[i] > chunk_stop[i]) {
963 block_stop[i] = chunk_stop[i];
964 }
965 }
966
967 bool block_empty = false;
968 for (int i = 0; i < ndim; ++i) {
969 block_empty |= (block_stop[i] <= start[i] || block_start[i] >= stop[i]);
970 }
971 block_maskout[nblock] = block_empty ? true : false;
972 }
973
974 if (blosc2_set_maskout(array->sc->dctx, block_maskout, nblocks) != BLOSC2_ERROR_SUCCESS) {
975 BLOSC_TRACE_ERROR("Error setting the maskout");
976 BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
977 }
978
979 int err = blosc2_schunk_decompress_chunk(array->sc, nchunk, data, data_nbytes);
980 if (err < 0) {
981 BLOSC_TRACE_ERROR("Error decompressing chunk");
982 BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
983 }
984
985 free(block_maskout);
986 }
987
988 // Iterate over blocks
989
990 for (int nblock = 0; nblock < nblocks; ++nblock) {
991 int64_t nblock_ndim[B2ND_MAX_DIM] = {0};
992 blosc2_unidim_to_multidim(ndim, blocks_in_chunk, nblock, nblock_ndim);
993
994 // Check if the block needs to be updated
995 int64_t block_start[B2ND_MAX_DIM] = {0};
996 int64_t block_stop[B2ND_MAX_DIM] = {0};
997 for (int i = 0; i < ndim; ++i) {
998 block_start[i] = nblock_ndim[i] * array->blockshape[i];
999 block_stop[i] = block_start[i] + array->blockshape[i];
1000 block_start[i] += chunk_start[i];
1001 block_stop[i] += chunk_start[i];
1002
1003 if (block_start[i] > chunk_stop[i]) {
1004 block_start[i] = chunk_stop[i];
1005 }
1006 if (block_stop[i] > chunk_stop[i]) {
1007 block_stop[i] = chunk_stop[i];
1008 }
1009 }
1010 int64_t block_shape[B2ND_MAX_DIM] = {0};
1011 for (int i = 0; i < ndim; ++i) {
1012 block_shape[i] = block_stop[i] - block_start[i];
1013 }
1014 bool block_empty = false;
1015 for (int i = 0; i < ndim; ++i) {
1016 block_empty |= (block_stop[i] <= start[i] || block_start[i] >= stop[i]);
1017 }
1018 if (block_empty) {
1019 continue;
1020 }
1021
1022 // compute the start of the slice inside the block
1023 int64_t slice_start[B2ND_MAX_DIM] = {0};
1024 for (int i = 0; i < ndim; ++i) {
1025 if (block_start[i] < buffer_start[i]) {
1026 slice_start[i] = buffer_start[i] - block_start[i];
1027 } else {
1028 slice_start[i] = 0;
1029 }
1030 slice_start[i] += block_start[i];
1031 }
1032
1033 int64_t slice_stop[B2ND_MAX_DIM] = {0};
1034 for (int i = 0; i < ndim; ++i) {
1035 if (block_stop[i] > buffer_stop[i]) {
1036 slice_stop[i] = block_shape[i] - (block_stop[i] - buffer_stop[i]);
1037 } else {
1038 slice_stop[i] = block_stop[i] - block_start[i];
1039 }
1040 slice_stop[i] += block_start[i];
1041 }
1042
1043 int64_t slice_shape[B2ND_MAX_DIM] = {0};
1044 for (int i = 0; i < ndim; ++i) {
1045 slice_shape[i] = slice_stop[i] - slice_start[i];
1046 }
1047
1048 uint8_t *src = &buffer_b[0];
1049 const int64_t *src_pad_shape = buffer_shape;
1050
1051 int64_t src_start[B2ND_MAX_DIM] = {0};
1052 int64_t src_stop[B2ND_MAX_DIM] = {0};
1053 for (int i = 0; i < ndim; ++i) {
1054 src_start[i] = slice_start[i] - buffer_start[i];
1055 src_stop[i] = slice_stop[i] - buffer_start[i];
1056 }
1057
1058 uint8_t *dst = &data[nblock * array->blocknitems * array->sc->typesize];
1059 int64_t dst_pad_shape[B2ND_MAX_DIM];
1060 for (int i = 0; i < ndim; ++i) {
1061 dst_pad_shape[i] = array->blockshape[i];
1062 }
1063
1064 int64_t dst_start[B2ND_MAX_DIM] = {0};
1065 int64_t dst_stop[B2ND_MAX_DIM] = {0};
1066 for (int i = 0; i < ndim; ++i) {
1067 dst_start[i] = slice_start[i] - block_start[i];
1068 dst_stop[i] = dst_start[i] + slice_shape[i];
1069 }
1070
1071 if (set_slice) {
1072 b2nd_copy_buffer2(ndim, array->sc->typesize,
1073 src, src_pad_shape, src_start, src_stop,
1074 dst, dst_pad_shape, dst_start);
1075 } else {
1076 b2nd_copy_buffer2(ndim, array->sc->typesize,
1077 dst, dst_pad_shape, dst_start, dst_stop,
1078 src, src_pad_shape, src_start);
1079 }
1080 }
1081
1082 if (set_slice) {
1083 // Recompress the data
1084 int32_t chunk_nbytes = data_nbytes + BLOSC2_MAX_OVERHEAD;
1085 uint8_t *chunk = malloc(chunk_nbytes);
1086 BLOSC_ERROR_NULL(chunk, BLOSC2_ERROR_MEMORY_ALLOC);
1087 int brc;
1088 // Update current_chunk in case a prefilter is applied
1089 array->sc->current_nchunk = nchunk;
1090 brc = blosc2_compress_ctx(array->sc->cctx, data, data_nbytes, chunk, chunk_nbytes);
1091 if (brc < 0) {
1092 BLOSC_TRACE_ERROR("Blosc can not compress the data");
1093 BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
1094 }
1095 int64_t brc_ = blosc2_schunk_update_chunk(array->sc, nchunk, chunk, false);
1096 if (brc_ < 0) {
1097 BLOSC_TRACE_ERROR("Blosc can not update the chunk");
1098 BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
1099 }
1100 }
1101 }
1102
1103 free(data);
1104
1105 return BLOSC2_ERROR_SUCCESS;
1106 }
1107
1108
1109 int b2nd_get_slice_cbuffer(const b2nd_array_t *array, const int64_t *start, const int64_t *stop,
1110 void *buffer, const int64_t *buffershape, int64_t buffersize) {
1111 BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1112 BLOSC_ERROR_NULL(start, BLOSC2_ERROR_NULL_POINTER);
1113 BLOSC_ERROR_NULL(stop, BLOSC2_ERROR_NULL_POINTER);
1114 BLOSC_ERROR_NULL(buffershape, BLOSC2_ERROR_NULL_POINTER);
1115 BLOSC_ERROR_NULL(buffer, BLOSC2_ERROR_NULL_POINTER);
1116
1117 BLOSC_ERROR(get_set_slice(buffer, buffersize, start, stop, buffershape, (b2nd_array_t *)array, false));
1118
1119 return BLOSC2_ERROR_SUCCESS;
1120 }
1121
1122
1123 int b2nd_set_slice_cbuffer(const void *buffer, const int64_t *buffershape, int64_t buffersize,
1124 const int64_t *start, const int64_t *stop,
1125 b2nd_array_t *array) {
1126 BLOSC_ERROR_NULL(buffer, BLOSC2_ERROR_NULL_POINTER);
1127 BLOSC_ERROR_NULL(start, BLOSC2_ERROR_NULL_POINTER);
1128 BLOSC_ERROR_NULL(stop, BLOSC2_ERROR_NULL_POINTER);
1129 BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1130
1131 BLOSC_ERROR(get_set_slice((void*)buffer, buffersize, start, stop, (int64_t *)buffershape, array, true));
1132
1133 return BLOSC2_ERROR_SUCCESS;
1134 }
1135
1136
1137 int b2nd_get_slice(b2nd_context_t *ctx, b2nd_array_t **array, const b2nd_array_t *src, const int64_t *start,
1138 const int64_t *stop) {
1139 BLOSC_ERROR_NULL(src, BLOSC2_ERROR_NULL_POINTER);
1140 BLOSC_ERROR_NULL(start, BLOSC2_ERROR_NULL_POINTER);
1141 BLOSC_ERROR_NULL(stop, BLOSC2_ERROR_NULL_POINTER);
1142 BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1143
1144 ctx->ndim = src->ndim;
1145 for (int i = 0; i < src->ndim; ++i) {
1146 ctx->shape[i] = stop[i] - start[i];
1147 }
1148
1149 // Add data
1150 BLOSC_ERROR(b2nd_empty(ctx, array));
1151
1152 if ((*array)->nitems == 0) {
1153 return BLOSC2_ERROR_SUCCESS;
1154 }
1155
1156 int8_t ndim = (*array)->ndim;
1157 int64_t chunks_in_array[B2ND_MAX_DIM] = {0};
1158 for (int i = 0; i < ndim; ++i) {
1159 chunks_in_array[i] = (*array)->extshape[i] / (*array)->chunkshape[i];
1160 }
1161 int64_t nchunks = (*array)->sc->nchunks;
1162 for (int nchunk = 0; nchunk < nchunks; ++nchunk) {
1163 int64_t nchunk_ndim[B2ND_MAX_DIM] = {0};
1164 blosc2_unidim_to_multidim(ndim, chunks_in_array, nchunk, nchunk_ndim);
1165
1166 // Check if the chunk needs to be updated
1167 int64_t chunk_start[B2ND_MAX_DIM] = {0};
1168 int64_t chunk_stop[B2ND_MAX_DIM] = {0};
1169 int64_t chunk_shape[B2ND_MAX_DIM] = {0};
1170 for (int i = 0; i < ndim; ++i) {
1171 chunk_start[i] = nchunk_ndim[i] * (*array)->chunkshape[i];
1172 chunk_stop[i] = chunk_start[i] + (*array)->chunkshape[i];
1173 if (chunk_stop[i] > (*array)->shape[i]) {
1174 chunk_stop[i] = (*array)->shape[i];
1175 }
1176 chunk_shape[i] = chunk_stop[i] - chunk_start[i];
1177 }
1178
1179 int64_t src_start[B2ND_MAX_DIM] = {0};
1180 int64_t src_stop[B2ND_MAX_DIM] = {0};
1181 for (int i = 0; i < ndim; ++i) {
1182 src_start[i] = chunk_start[i] + start[i];
1183 src_stop[i] = chunk_stop[i] + start[i];
1184 }
1185 int64_t buffersize = ctx->b2_storage->cparams->typesize;
1186 for (int i = 0; i < ndim; ++i) {
1187 buffersize *= chunk_shape[i];
1188 }
1189 uint8_t *buffer = malloc(buffersize);
1190 BLOSC_ERROR_NULL(buffer, BLOSC2_ERROR_MEMORY_ALLOC);
1191 BLOSC_ERROR(b2nd_get_slice_cbuffer(src, src_start, src_stop, buffer, chunk_shape,
1192 buffersize));
1193 BLOSC_ERROR(b2nd_set_slice_cbuffer(buffer, chunk_shape, buffersize, chunk_start,
1194 chunk_stop, *array));
1195 free(buffer);
1196 }
1197
1198 return BLOSC2_ERROR_SUCCESS;
1199 }
1200
1201
1202 int b2nd_squeeze(b2nd_array_t *array) {
1203 BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1204
1205 bool index[B2ND_MAX_DIM];
1206
1207 for (int i = 0; i < array->ndim; ++i) {
1208 if (array->shape[i] != 1) {
1209 index[i] = false;
1210 } else {
1211 index[i] = true;
1212 }
1213 }
1214 BLOSC_ERROR(b2nd_squeeze_index(array, index));
1215
1216 return BLOSC2_ERROR_SUCCESS;
1217 }
1218
1219
1220 int b2nd_squeeze_index(b2nd_array_t *array, const bool *index) {
1221 BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1222
1223 uint8_t nones = 0;
1224 int64_t newshape[B2ND_MAX_DIM];
1225 int32_t newchunkshape[B2ND_MAX_DIM];
1226 int32_t newblockshape[B2ND_MAX_DIM];
1227
1228 for (int i = 0; i < array->ndim; ++i) {
1229 if (index[i] == true) {
1230 if (array->shape[i] != 1) {
1231 BLOSC_ERROR(BLOSC2_ERROR_INVALID_INDEX);
1232 }
1233 } else {
1234 newshape[nones] = array->shape[i];
1235 newchunkshape[nones] = array->chunkshape[i];
1236 newblockshape[nones] = array->blockshape[i];
1237 nones += 1;
1238 }
1239 }
1240
1241 for (int i = 0; i < B2ND_MAX_DIM; ++i) {
1242 if (i < nones) {
1243 array->chunkshape[i] = newchunkshape[i];
1244 array->blockshape[i] = newblockshape[i];
1245 } else {
1246 array->chunkshape[i] = 1;
1247 array->blockshape[i] = 1;
1248 }
1249 }
1250
1251 BLOSC_ERROR(update_shape(array, nones, newshape, newchunkshape, newblockshape));
1252
1253 return BLOSC2_ERROR_SUCCESS;
1254 }
1255
1256
1257 int b2nd_copy(b2nd_context_t *ctx, const b2nd_array_t *src, b2nd_array_t **array) {
1258 BLOSC_ERROR_NULL(src, BLOSC2_ERROR_NULL_POINTER);
1259 BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1260
1261 ctx->ndim = src->ndim;
1262
1263 for (int i = 0; i < src->ndim; ++i) {
1264 ctx->shape[i] = src->shape[i];
1265 }
1266
1267 bool equals = true;
1268 for (int i = 0; i < src->ndim; ++i) {
1269 if (src->chunkshape[i] != ctx->chunkshape[i]) {
1270 equals = false;
1271 break;
1272 }
1273 if (src->blockshape[i] != ctx->blockshape[i]) {
1274 equals = false;
1275 break;
1276 }
1277 }
1278
1279 if (equals) {
1280 BLOSC_ERROR(array_without_schunk(ctx, array));
1281
1282 blosc2_schunk *new_sc = blosc2_schunk_copy(src->sc, ctx->b2_storage);
1283
1284 if (new_sc == NULL) {
1285 return BLOSC2_ERROR_FAILURE;
1286 }
1287 (*array)->sc = new_sc;
1288
1289 } else {
1290 int64_t start[B2ND_MAX_DIM] = {0};
1291
1292 int64_t stop[B2ND_MAX_DIM];
1293 for (int i = 0; i < src->ndim; ++i) {
1294 stop[i] = src->shape[i];
1295 }
1296 // Copy metalayers
1297 b2nd_context_t params_meta;
1298 memcpy(&params_meta, ctx, sizeof(params_meta));
1299 int j = 0;
1300
1301 for (int i = 0; i < src->sc->nmetalayers; ++i) {
1302 if (strcmp(src->sc->metalayers[i]->name, "b2nd") == 0) {
1303 continue;
1304 }
1305 blosc2_metalayer *meta = &params_meta.metalayers[j];
1306 meta->name = src->sc->metalayers[i]->name;
1307 meta->content = src->sc->metalayers[i]->content;
1308 meta->content_len = src->sc->metalayers[i]->content_len;
1309 j++;
1310 }
1311 params_meta.nmetalayers = j;
1312
1313 // Copy data
1314 BLOSC_ERROR(b2nd_get_slice(&params_meta, array, src, start, stop));
1315
1316 // Copy vlmetayers
1317 for (int i = 0; i < src->sc->nvlmetalayers; ++i) {
1318 uint8_t *content;
1319 int32_t content_len;
1320 if (blosc2_vlmeta_get(src->sc, src->sc->vlmetalayers[i]->name, &content,
1321 &content_len) < 0) {
1322 BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
1323 }
1324 BLOSC_ERROR(blosc2_vlmeta_add((*array)->sc, src->sc->vlmetalayers[i]->name, content, content_len,
1325 (*array)->sc->storage->cparams));
1326 free(content);
1327 }
1328 }
1329 return BLOSC2_ERROR_SUCCESS;
1330 }
1331
1332
1333 int b2nd_save(const b2nd_array_t *array, char *urlpath) {
1334 BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1335 BLOSC_ERROR_NULL(urlpath, BLOSC2_ERROR_NULL_POINTER);
1336
1337 b2nd_array_t *tmp;
1338 blosc2_storage b2_storage = BLOSC2_STORAGE_DEFAULTS;
1339 b2nd_context_t params = {.b2_storage=&b2_storage};
1340 b2_storage.urlpath = urlpath;
1341 b2_storage.contiguous = array->sc->storage->contiguous;
1342
1343 for (int i = 0; i < array->ndim; ++i) {
1344 params.chunkshape[i] = array->chunkshape[i];
1345 params.blockshape[i] = array->blockshape[i];
1346 }
1347
1348 BLOSC_ERROR(b2nd_copy(&params, array, &tmp));
1349 BLOSC_ERROR(b2nd_free(tmp));
1350
1351 return BLOSC2_ERROR_SUCCESS;
1352 }
1353
1354
1355 int b2nd_print_meta(const b2nd_array_t *array) {
1356 BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1357 int8_t ndim;
1358 int64_t shape[B2ND_MAX_DIM];
1359 int32_t chunkshape[B2ND_MAX_DIM];
1360 int32_t blockshape[B2ND_MAX_DIM];
1361 char *dtype;
1362 int8_t dtype_format;
1363 uint8_t *smeta;
1364 int32_t smeta_len;
1365 if (blosc2_meta_get(array->sc, "b2nd", &smeta, &smeta_len) < 0) {
1366 // Try with a caterva metalayer; we are meant to be backward compatible with it
1367 if (blosc2_meta_get(array->sc, "caterva", &smeta, &smeta_len) < 0) {
1368 BLOSC_ERROR(BLOSC2_ERROR_METALAYER_NOT_FOUND);
1369 }
1370 }
1371 BLOSC_ERROR(b2nd_deserialize_meta(smeta, smeta_len, &ndim, shape, chunkshape, blockshape,
1372 &dtype, &dtype_format));
1373 free(smeta);
1374
1375 printf("b2nd metalayer parameters:\n Ndim: %d", ndim);
1376 printf("\n shape: %" PRId64 "", shape[0]);
1377 for (int i = 1; i < ndim; ++i) {
1378 printf(", %" PRId64 "", shape[i]);
1379 }
1380 printf("\n chunkshape: %d", chunkshape[0]);
1381 for (int i = 1; i < ndim; ++i) {
1382 printf(", %d", chunkshape[i]);
1383 }
1384 if (dtype != NULL) {
1385 printf("\n dtype: %s", dtype);
1386 free(dtype);
1387 }
1388
1389 printf("\n blockshape: %d", blockshape[0]);
1390 for (int i = 1; i < ndim; ++i) {
1391 printf(", %d", blockshape[i]);
1392 }
1393 printf("\n");
1394
1395 return BLOSC2_ERROR_SUCCESS;
1396 }
1397
1398
1399 int extend_shape(b2nd_array_t *array, const int64_t *new_shape, const int64_t *start) {
1400 BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1401 BLOSC_ERROR_NULL(new_shape, BLOSC2_ERROR_NULL_POINTER);
1402
1403 int8_t ndim = array->ndim;
1404 int64_t diffs_shape[B2ND_MAX_DIM];
1405 int64_t diffs_sum = 0;
1406 for (int i = 0; i < ndim; i++) {
1407 diffs_shape[i] = new_shape[i] - array->shape[i];
1408 diffs_sum += diffs_shape[i];
1409 if (diffs_shape[i] < 0) {
1410 BLOSC_TRACE_ERROR("The new shape must be greater than the old one");
1411 BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
1412 }
1413 if (array->shape[i] == INT64_MAX) {
1414 BLOSC_TRACE_ERROR("Cannot extend array with shape[%d] = %" PRId64 "d", i, INT64_MAX);
1415 BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
1416 }
1417 }
1418 if (diffs_sum == 0) {
1419 // Shapes are equal. Do nothing.
1420 return BLOSC2_ERROR_SUCCESS;
1421 }
1422
1423 int64_t old_nchunks = array->sc->nchunks;
1424 // aux array to keep old shapes
1425 b2nd_array_t *aux = malloc(sizeof(b2nd_array_t));
1426 BLOSC_ERROR_NULL(aux, BLOSC2_ERROR_MEMORY_ALLOC);
1427 aux->sc = NULL;
1428 BLOSC_ERROR(update_shape(aux, ndim, array->shape, array->chunkshape, array->blockshape));
1429
1430 BLOSC_ERROR(update_shape(array, ndim, new_shape, array->chunkshape, array->blockshape));
1431
1432 int64_t nchunks = array->extnitems / array->chunknitems;
1433 int64_t nchunks_;
1434 int64_t nchunk_ndim[B2ND_MAX_DIM];
1435 blosc2_cparams *cparams;
1436 BLOSC_ERROR(blosc2_schunk_get_cparams(array->sc, &cparams));
1437 void *chunk;
1438 int64_t csize;
1439 if (nchunks != old_nchunks) {
1440 if (start == NULL) {
1441 start = aux->shape;
1442 }
1443 int64_t chunks_in_array[B2ND_MAX_DIM] = {0};
1444 for (int i = 0; i < ndim; ++i) {
1445 chunks_in_array[i] = array->extshape[i] / array->chunkshape[i];
1446 }
1447 for (int i = 0; i < nchunks; ++i) {
1448 blosc2_unidim_to_multidim(ndim, chunks_in_array, i, nchunk_ndim);
1449 for (int j = 0; j < ndim; ++j) {
1450 if (start[j] <= (array->chunkshape[j] * nchunk_ndim[j])
1451 && (array->chunkshape[j] * nchunk_ndim[j]) < (start[j] + new_shape[j] - aux->shape[j])) {
1452 chunk = malloc(BLOSC_EXTENDED_HEADER_LENGTH);
1453 BLOSC_ERROR_NULL(chunk, BLOSC2_ERROR_MEMORY_ALLOC);
1454 csize = blosc2_chunk_zeros(*cparams, array->sc->chunksize, chunk, BLOSC_EXTENDED_HEADER_LENGTH);
1455 if (csize < 0) {
1456 free(aux);
1457 free(cparams);
1458 BLOSC_TRACE_ERROR("Blosc error when creating a chunk");
1459 return BLOSC2_ERROR_FAILURE;
1460 }
1461 nchunks_ = blosc2_schunk_insert_chunk(array->sc, i, chunk, false);
1462 if (nchunks_ < 0) {
1463 free(aux);
1464 free(cparams);
1465 BLOSC_TRACE_ERROR("Blosc error when inserting a chunk");
1466 return BLOSC2_ERROR_FAILURE;
1467 }
1468 break;
1469 }
1470 }
1471 }
1472 }
1473 free(aux);
1474 free(cparams);
1475
1476 return BLOSC2_ERROR_SUCCESS;
1477 }
1478
1479
1480 int shrink_shape(b2nd_array_t *array, const int64_t *new_shape, const int64_t *start) {
1481 BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1482 BLOSC_ERROR_NULL(new_shape, BLOSC2_ERROR_NULL_POINTER);
1483
1484 int8_t ndim = array->ndim;
1485 int64_t diffs_shape[B2ND_MAX_DIM];
1486 int64_t diffs_sum = 0;
1487 for (int i = 0; i < ndim; i++) {
1488 diffs_shape[i] = new_shape[i] - array->shape[i];
1489 diffs_sum += diffs_shape[i];
1490 if (diffs_shape[i] > 0) {
1491 BLOSC_TRACE_ERROR("The new shape must be smaller than the old one");
1492 BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
1493 }
1494 if (array->shape[i] == 0) {
1495 continue;
1496 }
1497 }
1498 if (diffs_sum == 0) {
1499 // Shapes are equal. Do nothing.
1500 return BLOSC2_ERROR_SUCCESS;
1501 }
1502
1503 int64_t old_nchunks = array->sc->nchunks;
1504 // aux array to keep old shapes
1505 b2nd_array_t *aux = malloc(sizeof(b2nd_array_t));
1506 BLOSC_ERROR_NULL(aux, BLOSC2_ERROR_MEMORY_ALLOC);
1507 aux->sc = NULL;
1508 BLOSC_ERROR(update_shape(aux, ndim, array->shape, array->chunkshape, array->blockshape));
1509
1510 BLOSC_ERROR(update_shape(array, ndim, new_shape, array->chunkshape, array->blockshape));
1511
1512 // Delete chunks if needed
1513 int64_t chunks_in_array_old[B2ND_MAX_DIM] = {0};
1514 for (int i = 0; i < ndim; ++i) {
1515 chunks_in_array_old[i] = aux->extshape[i] / aux->chunkshape[i];
1516 }
1517 if (start == NULL) {
1518 start = new_shape;
1519 }
1520
1521 int64_t nchunk_ndim[B2ND_MAX_DIM] = {0};
1522 int64_t nchunks_;
1523 for (int i = (int) old_nchunks - 1; i >= 0; --i) {
1524 blosc2_unidim_to_multidim(ndim, chunks_in_array_old, i, nchunk_ndim);
1525 for (int j = 0; j < ndim; ++j) {
1526 if (start[j] <= (array->chunkshape[j] * nchunk_ndim[j])
1527 && (array->chunkshape[j] * nchunk_ndim[j]) < (start[j] + aux->shape[j] - new_shape[j])) {
1528 nchunks_ = blosc2_schunk_delete_chunk(array->sc, i);
1529 if (nchunks_ < 0) {
1530 free(aux);
1531 BLOSC_TRACE_ERROR("Blosc error when deleting a chunk");
1532 return BLOSC2_ERROR_FAILURE;
1533 }
1534 break;
1535 }
1536 }
1537 }
1538 free(aux);
1539
1540 return BLOSC2_ERROR_SUCCESS;
1541 }
1542
1543
1544 int b2nd_resize(b2nd_array_t *array, const int64_t *new_shape,
1545 const int64_t *start) {
1546 BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1547 BLOSC_ERROR_NULL(new_shape, BLOSC2_ERROR_NULL_POINTER);
1548
1549 if (start != NULL) {
1550 for (int i = 0; i < array->ndim; ++i) {
1551 if (start[i] > array->shape[i]) {
1552 BLOSC_TRACE_ERROR("`start` must be lower or equal than old array shape in all dims");
1553 BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
1554 }
1555 if ((new_shape[i] > array->shape[i] && start[i] != array->shape[i])
1556 || (new_shape[i] < array->shape[i]
1557 && (start[i] + array->shape[i] - new_shape[i]) != array->shape[i])) {
1558 // Chunks cannot be cut unless they are in the last position
1559 if (start[i] % array->chunkshape[i] != 0) {
1560 BLOSC_TRACE_ERROR("If array end is not being modified "
1561 "`start` must be a multiple of chunkshape in all dims");
1562 BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
1563 }
1564 if ((new_shape[i] - array->shape[i]) % array->chunkshape[i] != 0) {
1565 BLOSC_TRACE_ERROR("If array end is not being modified "
1566 "`(new_shape - shape)` must be multiple of chunkshape in all dims");
1567 BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
1568 }
1569 }
1570 }
1571 }
1572
1573 // Get shrunk shape
1574 int64_t shrunk_shape[B2ND_MAX_DIM] = {0};
1575 for (int i = 0; i < array->ndim; ++i) {
1576 if (new_shape[i] <= array->shape[i]) {
1577 shrunk_shape[i] = new_shape[i];
1578 } else {
1579 shrunk_shape[i] = array->shape[i];
1580 }
1581 }
1582
1583 BLOSC_ERROR(shrink_shape(array, shrunk_shape, start));
1584 BLOSC_ERROR(extend_shape(array, new_shape, start));
1585
1586 return BLOSC2_ERROR_SUCCESS;
1587 }
1588
1589
1590 int b2nd_insert(b2nd_array_t *array, const void *buffer, int64_t buffersize,
1591 int8_t axis, int64_t insert_start) {
1592
1593 BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1594 BLOSC_ERROR_NULL(buffer, BLOSC2_ERROR_NULL_POINTER);
1595
1596 if (axis >= array->ndim) {
1597 BLOSC_TRACE_ERROR("`axis` cannot be greater than the number of dimensions");
1598 BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
1599 }
1600
1601 int64_t axis_size = array->sc->typesize;
1602 int64_t buffershape[B2ND_MAX_DIM];
1603 for (int i = 0; i < array->ndim; ++i) {
1604 if (i != axis) {
1605 axis_size *= array->shape[i];
1606 buffershape[i] = array->shape[i];
1607 }
1608 }
1609 if (buffersize % axis_size != 0) {
1610 BLOSC_TRACE_ERROR("`buffersize` must be multiple of the array");
1611 BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
1612 }
1613 int64_t newshape[B2ND_MAX_DIM];
1614 memcpy(newshape, array->shape, array->ndim * sizeof(int64_t));
1615 newshape[axis] += buffersize / axis_size;
1616 buffershape[axis] = newshape[axis] - array->shape[axis];
1617 int64_t start[B2ND_MAX_DIM] = {0};
1618 start[axis] = insert_start;
1619
1620 if (insert_start == array->shape[axis]) {
1621 BLOSC_ERROR(b2nd_resize(array, newshape, NULL));
1622 } else {
1623 BLOSC_ERROR(b2nd_resize(array, newshape, start));
1624 }
1625
1626 int64_t stop[B2ND_MAX_DIM];
1627 memcpy(stop, array->shape, sizeof(int64_t) * array->ndim);
1628 stop[axis] = start[axis] + buffershape[axis];
1629 BLOSC_ERROR(b2nd_set_slice_cbuffer(buffer, buffershape, buffersize, start, stop, array));
1630
1631 return BLOSC2_ERROR_SUCCESS;
1632 }
1633
1634
1635 int b2nd_append(b2nd_array_t *array, const void *buffer, int64_t buffersize,
1636 int8_t axis) {
1637 BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1638 BLOSC_ERROR_NULL(buffer, BLOSC2_ERROR_NULL_POINTER);
1639
1640 int32_t chunksize = array->sc->chunksize;
1641 int64_t nchunks_append = buffersize / chunksize;
1642 // Check whether chunkshape and blockshape are compatible with accelerated path.
1643 // Essentially, we are checking whether the buffer is a multiple of the chunksize
1644 // and that the chunkshape and blockshape are the same, except for the first axis.
1645 // Also, axis needs to be the first one.
1646 bool compat_chunks_blocks = true;
1647 for (int i = 1; i < array->ndim; ++i) {
1648 if (array->chunkshape[i] != array->blockshape[i]) {
1649 compat_chunks_blocks = false;
1650 break;
1651 }
1652 }
1653 if (axis > 0) {
1654 compat_chunks_blocks = false;
1655 }
1656 // General case where a buffer has a different size than the chunksize
1657 if (!compat_chunks_blocks || buffersize % chunksize != 0 || nchunks_append != 1) {
1658 BLOSC_ERROR(b2nd_insert(array, buffer, buffersize, axis, array->shape[axis]));
1659 return BLOSC2_ERROR_SUCCESS;
1660 }
1661
1662 // Accelerated path for buffers that are of the same size as the chunksize
1663 // printf("accelerated path\n");
1664
1665 // Append the buffer to the underlying schunk. This is very fast, as
1666 // it doesn't need to do internal partitioning.
1667 BLOSC_ERROR(blosc2_schunk_append_buffer(array->sc, (void*)buffer, buffersize));
1668
1669 // Finally, resize the array
1670 int64_t newshape[B2ND_MAX_DIM];
1671 memcpy(newshape, array->shape, array->ndim * sizeof(int64_t));
1672 newshape[axis] += nchunks_append * array->chunkshape[axis];
1673 BLOSC_ERROR(b2nd_resize(array, newshape, NULL));
1674
1675 return BLOSC2_ERROR_SUCCESS;
1676 }
1677
1678
1679 int b2nd_delete(b2nd_array_t *array, const int8_t axis,
1680 int64_t delete_start, int64_t delete_len) {
1681 BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
1682
1683 if (axis >= array->ndim) {
1684 BLOSC_TRACE_ERROR("axis cannot be greater than the number of dimensions");
1685 BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
1686 }
1687
1688
1689 int64_t newshape[B2ND_MAX_DIM];
1690 memcpy(newshape, array->shape, array->ndim * sizeof(int64_t));
1691 newshape[axis] -= delete_len;
1692 int64_t start[B2ND_MAX_DIM] = {0};
1693 start[axis] = delete_start;
1694
1695 if (delete_start == (array->shape[axis] - delete_len)) {
1696 BLOSC_ERROR(b2nd_resize(array, newshape, NULL));
1697 } else {
1698 BLOSC_ERROR(b2nd_resize(array, newshape, start));
1699 }
1700
1701 return BLOSC2_ERROR_SUCCESS;
1702 }
1703
1704 // Indexing
1705
1706 typedef struct {
1707 int64_t value;
1708 int64_t index;
1709 } b2nd_selection_t;
1710
1711
1712 int compare_selection(const void *a, const void *b) {
1713 int res = (int) (((b2nd_selection_t *) a)->value - ((b2nd_selection_t *) b)->value);
1714 // In case values are equal, sort by index
1715 if (res == 0) {
1716 res = (int) (((b2nd_selection_t *) a)->index - ((b2nd_selection_t *) b)->index);
1717 }
1718 return res;
1719 }
1720
1721
1722 int copy_block_buffer_data(b2nd_array_t *array,
1723 int8_t ndim,
1724 int64_t *block_selection_size,
1725 b2nd_selection_t **chunk_selection,
1726 b2nd_selection_t **p_block_selection_0,
1727 b2nd_selection_t **p_block_selection_1,
1728 uint8_t *block,
1729 uint8_t *buffer,
1730 int64_t *buffershape,
1731 int64_t *bufferstrides,
1732 bool get) {
1733 p_block_selection_0[ndim] = chunk_selection[ndim];
1734 p_block_selection_1[ndim] = chunk_selection[ndim];
1735 while (p_block_selection_1[ndim] - p_block_selection_0[ndim] < block_selection_size[ndim]) {
1736 if (ndim == array->ndim - 1) {
1737
1738 int64_t index_in_block_n[B2ND_MAX_DIM];
1739 for (int i = 0; i < array->ndim; ++i) {
1740 index_in_block_n[i] = p_block_selection_1[i]->value % array->chunkshape[i] % array->blockshape[i];
1741 }
1742 int64_t index_in_block = 0;
1743 for (int i = 0; i < array->ndim; ++i) {
1744 index_in_block += index_in_block_n[i] * array->item_block_strides[i];
1745 }
1746
1747 int64_t index_in_buffer_n[B2ND_MAX_DIM];
1748 for (int i = 0; i < array->ndim; ++i) {
1749 index_in_buffer_n[i] = p_block_selection_1[i]->index;
1750 }
1751 int64_t index_in_buffer = 0;
1752 for (int i = 0; i < array->ndim; ++i) {
1753 index_in_buffer += index_in_buffer_n[i] * bufferstrides[i];
1754 }
1755 if (get) {
1756 memcpy(&buffer[index_in_buffer * array->sc->typesize],
1757 &block[index_in_block * array->sc->typesize],
1758 array->sc->typesize);
1759 } else {
1760 memcpy(&block[index_in_block * array->sc->typesize],
1761 &buffer[index_in_buffer * array->sc->typesize],
1762 array->sc->typesize);
1763 }
1764 } else {
1765 BLOSC_ERROR(copy_block_buffer_data(array, (int8_t) (ndim + 1), block_selection_size,
1766 chunk_selection,
1767 p_block_selection_0, p_block_selection_1, block,
1768 buffer, buffershape, bufferstrides, get)
1769 );
1770 }
1771 p_block_selection_1[ndim]++;
1772 }
1773 return BLOSC2_ERROR_SUCCESS;
1774 }
1775
1776
1777 int iter_block_copy(b2nd_array_t *array, int8_t ndim,
1778 int64_t *chunk_selection_size,
1779 b2nd_selection_t **ordered_selection,
1780 b2nd_selection_t **chunk_selection_0,
1781 b2nd_selection_t **chunk_selection_1,
1782 uint8_t *data,
1783 uint8_t *buffer,
1784 int64_t *buffershape,
1785 int64_t *bufferstrides,
1786 bool get) {
1787 chunk_selection_0[ndim] = ordered_selection[ndim];
1788 chunk_selection_1[ndim] = ordered_selection[ndim];
1789 while (chunk_selection_1[ndim] - ordered_selection[ndim] < chunk_selection_size[ndim]) {
1790 int64_t block_index_ndim = ((*chunk_selection_1[ndim]).value % array->chunkshape[ndim]) / array->blockshape[ndim];
1791 while (chunk_selection_1[ndim] - ordered_selection[ndim] < chunk_selection_size[ndim] &&
1792 block_index_ndim == ((*chunk_selection_1[ndim]).value % array->chunkshape[ndim]) / array->blockshape[ndim]) {
1793 chunk_selection_1[ndim]++;
1794 }
1795 if (ndim == array->ndim - 1) {
1796 int64_t block_chunk_strides[B2ND_MAX_DIM];
1797 block_chunk_strides[array->ndim - 1] = 1;
1798 for (int i = array->ndim - 2; i >= 0; --i) {
1799 block_chunk_strides[i] = block_chunk_strides[i + 1] * (array->extchunkshape[i + 1] / array->blockshape[i + 1]);
1800 }
1801 int64_t block_index[B2ND_MAX_DIM];
1802 for (int i = 0; i < array->ndim; ++i) {
1803 block_index[i] = ((*chunk_selection_0[i]).value % array->chunkshape[i]) / array->blockshape[i];
1804 }
1805 int64_t nblock = 0;
1806 for (int i = 0; i < array->ndim; ++i) {
1807 nblock += block_index[i] * block_chunk_strides[i];
1808 }
1809 b2nd_selection_t **p_block_selection_0 = malloc(array->ndim * sizeof(b2nd_selection_t *));
1810 BLOSC_ERROR_NULL(p_block_selection_0, BLOSC2_ERROR_MEMORY_ALLOC);
1811 b2nd_selection_t **p_block_selection_1 = malloc(array->ndim * sizeof(b2nd_selection_t *));
1812 BLOSC_ERROR_NULL(p_block_selection_1, BLOSC2_ERROR_MEMORY_ALLOC);
1813 int64_t *block_selection_size = malloc(array->ndim * sizeof(int64_t));
1814 BLOSC_ERROR_NULL(block_selection_size, BLOSC2_ERROR_MEMORY_ALLOC);
1815 for (int i = 0; i < array->ndim; ++i) {
1816 block_selection_size[i] = chunk_selection_1[i] - chunk_selection_0[i];
1817 }
1818
1819 BLOSC_ERROR(copy_block_buffer_data(array,
1820 (int8_t) 0,
1821 block_selection_size,
1822 chunk_selection_0,
1823 p_block_selection_0,
1824 p_block_selection_1,
1825 &data[nblock * array->blocknitems * array->sc->typesize],
1826 buffer,
1827 buffershape,
1828 bufferstrides,
1829 get)
1830 );
1831 free(p_block_selection_0);
1832 free(p_block_selection_1);
1833 free(block_selection_size);
1834 } else {
1835 BLOSC_ERROR(iter_block_copy(array, (int8_t) (ndim + 1), chunk_selection_size,
1836 ordered_selection, chunk_selection_0, chunk_selection_1,
1837 data, buffer, buffershape, bufferstrides, get)
1838 );
1839 }
1840 chunk_selection_0[ndim] = chunk_selection_1[ndim];
1841
1842 }
1843
1844 return BLOSC2_ERROR_SUCCESS;
1845 }
1846
1847
1848 int iter_block_maskout(b2nd_array_t *array, int8_t ndim,
1849 int64_t *sel_block_size,
1850 b2nd_selection_t **o_selection,
1851 b2nd_selection_t **p_o_sel_block_0,
1852 b2nd_selection_t **p_o_sel_block_1,
1853 bool *maskout) {
1854 p_o_sel_block_0[ndim] = o_selection[ndim];
1855 p_o_sel_block_1[ndim] = o_selection[ndim];
1856 while (p_o_sel_block_1[ndim] - o_selection[ndim] < sel_block_size[ndim]) {
1857 int64_t block_index_ndim = ((*p_o_sel_block_1[ndim]).value % array->chunkshape[ndim]) / array->blockshape[ndim];
1858 while (p_o_sel_block_1[ndim] - o_selection[ndim] < sel_block_size[ndim] &&
1859 block_index_ndim == ((*p_o_sel_block_1[ndim]).value % array->chunkshape[ndim]) / array->blockshape[ndim]) {
1860 p_o_sel_block_1[ndim]++;
1861 }
1862 if (ndim == array->ndim - 1) {
1863 int64_t block_chunk_strides[B2ND_MAX_DIM];
1864 block_chunk_strides[array->ndim - 1] = 1;
1865 for (int i = array->ndim - 2; i >= 0; --i) {
1866 block_chunk_strides[i] = block_chunk_strides[i + 1] * (array->extchunkshape[i + 1] / array->blockshape[i + 1]);
1867 }
1868 int64_t block_index[B2ND_MAX_DIM];
1869 for (int i = 0; i < array->ndim; ++i) {
1870 block_index[i] = ((*p_o_sel_block_0[i]).value % array->chunkshape[i]) / array->blockshape[i];
1871 }
1872 int64_t nblock = 0;
1873 for (int i = 0; i < array->ndim; ++i) {
1874 nblock += block_index[i] * block_chunk_strides[i];
1875 }
1876 maskout[nblock] = false;
1877 } else {
1878 BLOSC_ERROR(iter_block_maskout(array, (int8_t) (ndim + 1), sel_block_size,
1879 o_selection, p_o_sel_block_0, p_o_sel_block_1,
1880 maskout)
1881 );
1882 }
1883 p_o_sel_block_0[ndim] = p_o_sel_block_1[ndim];
1884
1885 }
1886
1887 return BLOSC2_ERROR_SUCCESS;
1888 }
1889
1890
1891 int iter_chunk(b2nd_array_t *array, int8_t ndim,
1892 int64_t *selection_size,
1893 b2nd_selection_t **ordered_selection,
1894 b2nd_selection_t **p_ordered_selection_0,
1895 b2nd_selection_t **p_ordered_selection_1,
1896 uint8_t *buffer,
1897 int64_t *buffershape,
1898 int64_t *bufferstrides,
1899 bool get) {
1900 p_ordered_selection_0[ndim] = ordered_selection[ndim];
1901 p_ordered_selection_1[ndim] = ordered_selection[ndim];
1902 while (p_ordered_selection_1[ndim] - ordered_selection[ndim] < selection_size[ndim]) {
1903 int64_t chunk_index_ndim = (*p_ordered_selection_1[ndim]).value / array->chunkshape[ndim];
1904 while (p_ordered_selection_1[ndim] - ordered_selection[ndim] < selection_size[ndim] &&
1905 chunk_index_ndim == (*p_ordered_selection_1[ndim]).value / array->chunkshape[ndim]) {
1906 p_ordered_selection_1[ndim]++;
1907 }
1908 if (ndim == array->ndim - 1) {
1909 int64_t chunk_array_strides[B2ND_MAX_DIM];
1910 chunk_array_strides[array->ndim - 1] = 1;
1911 for (int i = array->ndim - 2; i >= 0; --i) {
1912 chunk_array_strides[i] = chunk_array_strides[i + 1] *
1913 (array->extshape[i + 1] / array->chunkshape[i + 1]);
1914 }
1915 int64_t chunk_index[B2ND_MAX_DIM];
1916 for (int i = 0; i < array->ndim; ++i) {
1917 chunk_index[i] = (*p_ordered_selection_0[i]).value / array->chunkshape[i];
1918 }
1919 int64_t nchunk = 0;
1920 for (int i = 0; i < array->ndim; ++i) {
1921 nchunk += chunk_index[i] * chunk_array_strides[i];
1922 }
1923
1924 int64_t nblocks = array->extchunknitems / array->blocknitems;
1925 b2nd_selection_t **p_chunk_selection_0 = malloc(array->ndim * sizeof(b2nd_selection_t *));
1926 BLOSC_ERROR_NULL(p_chunk_selection_0, BLOSC2_ERROR_MEMORY_ALLOC);
1927 b2nd_selection_t **p_chunk_selection_1 = malloc(array->ndim * sizeof(b2nd_selection_t *));
1928 BLOSC_ERROR_NULL(p_chunk_selection_1, BLOSC2_ERROR_MEMORY_ALLOC);
1929 int64_t *chunk_selection_size = malloc(array->ndim * sizeof(int64_t));
1930 BLOSC_ERROR_NULL(chunk_selection_size, BLOSC2_ERROR_MEMORY_ALLOC);
1931 for (int i = 0; i < array->ndim; ++i) {
1932 chunk_selection_size[i] = p_ordered_selection_1[i] - p_ordered_selection_0[i];
1933 }
1934
1935 if (get) {
1936 bool *maskout = calloc(nblocks, sizeof(bool));
1937 for (int i = 0; i < nblocks; ++i) {
1938 maskout[i] = true;
1939 }
1940
1941 BLOSC_ERROR(iter_block_maskout(array, (int8_t) 0,
1942 chunk_selection_size,
1943 p_ordered_selection_0,
1944 p_chunk_selection_0,
1945 p_chunk_selection_1,
1946 maskout));
1947
1948 if (blosc2_set_maskout(array->sc->dctx, maskout, (int) nblocks) !=
1949 BLOSC2_ERROR_SUCCESS) {
1950 BLOSC_TRACE_ERROR("Error setting the maskout");
1951 BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
1952 }
1953 free(maskout);
1954 }
1955 int data_nitems = (int) array->extchunknitems;
1956 int data_nbytes = data_nitems * array->sc->typesize;
1957 uint8_t *data = malloc(data_nitems * array->sc->typesize);
1958 BLOSC_ERROR_NULL(data, BLOSC2_ERROR_MEMORY_ALLOC);
1959 int err = blosc2_schunk_decompress_chunk(array->sc, nchunk, data, data_nbytes);
1960 if (err < 0) {
1961 BLOSC_TRACE_ERROR("Error decompressing chunk");
1962 BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
1963 }
1964 BLOSC_ERROR(iter_block_copy(array, 0, chunk_selection_size,
1965 p_ordered_selection_0, p_chunk_selection_0, p_chunk_selection_1,
1966 data, buffer, buffershape, bufferstrides, get));
1967
1968 if (!get) {
1969 int32_t chunk_size = data_nbytes + BLOSC_EXTENDED_HEADER_LENGTH;
1970 uint8_t *chunk = malloc(chunk_size);
1971 BLOSC_ERROR_NULL(chunk, BLOSC2_ERROR_MEMORY_ALLOC);
1972 err = blosc2_compress_ctx(array->sc->cctx, data, data_nbytes, chunk, chunk_size);
1973 if (err < 0) {
1974 BLOSC_TRACE_ERROR("Error compressing data");
1975 BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
1976 }
1977 err = (int) blosc2_schunk_update_chunk(array->sc, nchunk, chunk, false);
1978 if (err < 0) {
1979 BLOSC_TRACE_ERROR("Error updating chunk");
1980 BLOSC_ERROR(BLOSC2_ERROR_FAILURE);
1981 }
1982 }
1983 free(data);
1984 free(chunk_selection_size);
1985 free(p_chunk_selection_0);
1986 free(p_chunk_selection_1);
1987 } else {
1988 BLOSC_ERROR(iter_chunk(array, (int8_t) (ndim + 1), selection_size,
1989 ordered_selection, p_ordered_selection_0, p_ordered_selection_1,
1990 buffer, buffershape, bufferstrides, get));
1991 }
1992
1993 p_ordered_selection_0[ndim] = p_ordered_selection_1[ndim];
1994 }
1995 return BLOSC2_ERROR_SUCCESS;
1996 }
1997
1998
1999 int orthogonal_selection(b2nd_array_t *array, int64_t **selection, int64_t *selection_size, void *buffer,
2000 int64_t *buffershape, int64_t buffersize, bool get) {
2001 BLOSC_ERROR_NULL(array, BLOSC2_ERROR_NULL_POINTER);
2002 BLOSC_ERROR_NULL(selection, BLOSC2_ERROR_NULL_POINTER);
2003 BLOSC_ERROR_NULL(selection_size, BLOSC2_ERROR_NULL_POINTER);
2004
2005 int8_t ndim = array->ndim;
2006
2007 for (int i = 0; i < ndim; ++i) {
2008 BLOSC_ERROR_NULL(selection[i], BLOSC2_ERROR_NULL_POINTER);
2009 // Check that indexes are not larger than array shape
2010 for (int j = 0; j < selection_size[i]; ++j) {
2011 if (selection[i][j] > array->shape[i]) {
2012 BLOSC_ERROR(BLOSC2_ERROR_INVALID_INDEX);
2013 }
2014 }
2015 }
2016
2017 // Check buffer size
2018 int64_t sel_size = array->sc->typesize;
2019 for (int i = 0; i < ndim; ++i) {
2020 sel_size *= selection_size[i];
2021 }
2022
2023 if (sel_size < buffersize) {
2024 BLOSC_ERROR(BLOSC2_ERROR_INVALID_PARAM);
2025 }
2026
2027 // Sort selections
2028 b2nd_selection_t **ordered_selection = malloc(ndim * sizeof(b2nd_selection_t *));
2029 BLOSC_ERROR_NULL(ordered_selection, BLOSC2_ERROR_MEMORY_ALLOC);
2030 for (int i = 0; i < ndim; ++i) {
2031 ordered_selection[i] = malloc(selection_size[i] * sizeof(b2nd_selection_t));
2032 for (int j = 0; j < selection_size[i]; ++j) {
2033 ordered_selection[i][j].index = j;
2034 ordered_selection[i][j].value = selection[i][j];
2035 }
2036 qsort(ordered_selection[i], selection_size[i], sizeof(b2nd_selection_t), compare_selection);
2037 }
2038
2039 // Define pointers to iterate over ordered_selection data
2040 b2nd_selection_t **p_ordered_selection_0 = malloc(ndim * sizeof(b2nd_selection_t *));
2041 BLOSC_ERROR_NULL(p_ordered_selection_0, BLOSC2_ERROR_MEMORY_ALLOC);
2042 b2nd_selection_t **p_ordered_selection_1 = malloc(ndim * sizeof(b2nd_selection_t *));
2043 BLOSC_ERROR_NULL(p_ordered_selection_1, BLOSC2_ERROR_MEMORY_ALLOC);
2044
2045 int64_t bufferstrides[B2ND_MAX_DIM];
2046 bufferstrides[array->ndim - 1] = 1;
2047 for (int i = array->ndim - 2; i >= 0; --i) {
2048 bufferstrides[i] = bufferstrides[i + 1] * buffershape[i + 1];
2049 }
2050
2051 BLOSC_ERROR(iter_chunk(array, 0,
2052 selection_size, ordered_selection,
2053 p_ordered_selection_0,
2054 p_ordered_selection_1,
2055 buffer, buffershape, bufferstrides, get));
2056
2057 // Free allocated memory
2058 free(p_ordered_selection_0);
2059 free(p_ordered_selection_1);
2060 for (int i = 0; i < ndim; ++i) {
2061 free(ordered_selection[i]);
2062 }
2063 free(ordered_selection);
2064
2065 return BLOSC2_ERROR_SUCCESS;
2066 }
2067
2068
2069 int b2nd_get_orthogonal_selection(const b2nd_array_t *array, int64_t **selection, int64_t *selection_size, void *buffer,
2070 int64_t *buffershape, int64_t buffersize) {
2071 return orthogonal_selection((b2nd_array_t *)array, selection, selection_size, buffer, buffershape, buffersize, true);
2072 }
2073
2074
2075 int b2nd_set_orthogonal_selection(b2nd_array_t *array, int64_t **selection, int64_t *selection_size, const void *buffer,
2076 int64_t *buffershape, int64_t buffersize) {
2077 return orthogonal_selection(array, selection, selection_size, (void*)buffer, buffershape, buffersize, false);
2078 }
2079
2080
2081 b2nd_context_t *
2082 b2nd_create_ctx(const blosc2_storage *b2_storage, int8_t ndim, const int64_t *shape, const int32_t *chunkshape,
2083 const int32_t *blockshape, const char *dtype, int8_t dtype_format, const blosc2_metalayer *metalayers,
2084 int32_t nmetalayers) {
2085 b2nd_context_t *ctx = malloc(sizeof(b2nd_context_t));
2086 BLOSC_ERROR_NULL(ctx, NULL);
2087 blosc2_storage *params_b2_storage = malloc(sizeof(blosc2_storage));
2088 BLOSC_ERROR_NULL(params_b2_storage, NULL);
2089 if (b2_storage == NULL) {
2090 memcpy(params_b2_storage, &BLOSC2_STORAGE_DEFAULTS, sizeof(blosc2_storage));
2091 }
2092 else {
2093 memcpy(params_b2_storage, b2_storage, sizeof(blosc2_storage));
2094 }
2095 blosc2_cparams *cparams = malloc(sizeof(blosc2_cparams));
2096 BLOSC_ERROR_NULL(cparams, NULL);
2097 // We need a copy of cparams mainly to be able to modify blocksize
2098 if (b2_storage->cparams == NULL) {
2099 memcpy(cparams, &BLOSC2_CPARAMS_DEFAULTS, sizeof(blosc2_cparams));
2100 }
2101 else {
2102 memcpy(cparams, b2_storage->cparams, sizeof(blosc2_cparams));
2103 }
2104
2105 if (dtype == NULL) {
2106 // ctx->dtype = strdup(B2ND_DEFAULT_DTYPE);
2107 char buf[16] = {0};
2108 snprintf(buf, sizeof(buf), "|S%d", cparams->typesize);
2109 ctx->dtype = strdup(buf);
2110 }
2111 else {
2112 ctx->dtype = strdup(dtype);
2113 }
2114 ctx->dtype_format = dtype_format;
2115
2116 params_b2_storage->cparams = cparams;
2117 ctx->b2_storage = params_b2_storage;
2118 ctx->ndim = ndim;
2119 int32_t blocknitems = 1;
2120 for (int i = 0; i < ndim; i++) {
2121 ctx->shape[i] = shape[i];
2122 ctx->chunkshape[i] = chunkshape[i];
2123 ctx->blockshape[i] = blockshape[i];
2124 blocknitems *= ctx->blockshape[i];
2125 }
2126 cparams->blocksize = blocknitems * cparams->typesize;
2127
2128 ctx->nmetalayers = nmetalayers;
2129 for (int i = 0; i < nmetalayers; ++i) {
2130 ctx->metalayers[i] = metalayers[i];
2131 }
2132
2133 #if defined(HAVE_PLUGINS)
2134 #include "blosc2/codecs-registry.h"
2135 if ((ctx->b2_storage->cparams->compcode >= BLOSC_CODEC_ZFP_FIXED_ACCURACY) &&
2136 (ctx->b2_storage->cparams->compcode <= BLOSC_CODEC_ZFP_FIXED_RATE)) {
2137 for (int i = 0; i < BLOSC2_MAX_FILTERS; ++i) {
2138 if ((ctx->b2_storage->cparams->filters[i] == BLOSC_SHUFFLE) ||
2139 (ctx->b2_storage->cparams->filters[i] == BLOSC_BITSHUFFLE)) {
2140 BLOSC_TRACE_ERROR("ZFP cannot be run in presence of SHUFFLE / BITSHUFFLE");
2141 return NULL;
2142 }
2143 }
2144 }
2145 #endif /* HAVE_PLUGINS */
2146
2147 return ctx;
2148 }
2149
2150
2151 int b2nd_free_ctx(b2nd_context_t *ctx) {
2152 ctx->b2_storage->cparams->schunk = NULL;
2153 free(ctx->b2_storage->cparams);
2154 free(ctx->b2_storage);
2155 free(ctx->dtype);
2156 free(ctx);
2157
2158 return BLOSC2_ERROR_SUCCESS;
2159 }