Debian Codegraph @

src/c-blosc2/blosc/schunk.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 "frame.h"
12 #include "stune.h"
13 #include "blosc-private.h"
14 #include "blosc2/tuners-registry.h"
15 #include "blosc2.h"
16
17 #if defined(_WIN32)
18 #include <windows.h>
19 #include <direct.h>
20 #include <malloc.h>
21 #define mkdir(D, M) _mkdir(D)
22 #endif /* _WIN32 */
23
24 #include <sys/stat.h>
25
26 #include <inttypes.h>
27 #include <stdio.h>
28 #include <stdint.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 /* If C11 is supported, use it's built-in aligned allocation. */
33 #if __STDC_VERSION__ >= 201112L
34 #include <stdalign.h>
35 #endif
36
37
38 /* Get the cparams associated with a super-chunk */
39 int blosc2_schunk_get_cparams(blosc2_schunk *schunk, blosc2_cparams **cparams) {
40 *cparams = calloc(1, sizeof(blosc2_cparams));
41 (*cparams)->schunk = schunk;
42 for (int i = 0; i < BLOSC2_MAX_FILTERS; i++) {
43 (*cparams)->filters[i] = schunk->filters[i];
44 (*cparams)->filters_meta[i] = schunk->filters_meta[i];
45 }
46 (*cparams)->compcode = schunk->compcode;
47 (*cparams)->compcode_meta = schunk->compcode_meta;
48 (*cparams)->clevel = schunk->clevel;
49 (*cparams)->typesize = schunk->typesize;
50 (*cparams)->blocksize = schunk->blocksize;
51 (*cparams)->splitmode = schunk->splitmode;
52 if (schunk->cctx == NULL) {
53 (*cparams)->nthreads = blosc2_get_nthreads();
54 }
55 else {
56 (*cparams)->nthreads = (int16_t)schunk->cctx->nthreads;
57 }
58 return 0;
59 }
60
61
62 /* Get the dparams associated with a super-chunk */
63 int blosc2_schunk_get_dparams(blosc2_schunk *schunk, blosc2_dparams **dparams) {
64 *dparams = calloc(1, sizeof(blosc2_dparams));
65 (*dparams)->schunk = schunk;
66 if (schunk->dctx == NULL) {
67 (*dparams)->nthreads = blosc2_get_nthreads();
68 }
69 else {
70 (*dparams)->nthreads = schunk->dctx->nthreads;
71 }
72 return 0;
73 }
74
75
76 int update_schunk_properties(struct blosc2_schunk* schunk) {
77 blosc2_cparams* cparams = schunk->storage->cparams;
78 blosc2_dparams* dparams = schunk->storage->dparams;
79
80 for (int i = 0; i < BLOSC2_MAX_FILTERS; i++) {
81 schunk->filters[i] = cparams->filters[i];
82 schunk->filters_meta[i] = cparams->filters_meta[i];
83 }
84 schunk->compcode = cparams->compcode;
85 schunk->compcode_meta = cparams->compcode_meta;
86 schunk->clevel = cparams->clevel;
87 schunk->splitmode = cparams->splitmode;
88 schunk->typesize = cparams->typesize;
89 schunk->blocksize = cparams->blocksize;
90 schunk->chunksize = -1;
91 schunk->tuner_params = cparams->tuner_params;
92 schunk->tuner_id = cparams->tuner_id;
93 if (cparams->tuner_id == BLOSC_BTUNE) {
94 cparams->use_dict = 0;
95 }
96 /* The compression context */
97 if (schunk->cctx != NULL) {
98 blosc2_free_ctx(schunk->cctx);
99 }
100 cparams->schunk = schunk;
101 schunk->cctx = blosc2_create_cctx(*cparams);
102 if (schunk->cctx == NULL) {
103 BLOSC_TRACE_ERROR("Could not create compression ctx");
104 return BLOSC2_ERROR_NULL_POINTER;
105 }
106
107 /* The decompression context */
108 if (schunk->dctx != NULL) {
109 blosc2_free_ctx(schunk->dctx);
110 }
111 dparams->schunk = schunk;
112 schunk->dctx = blosc2_create_dctx(*dparams);
113 if (schunk->dctx == NULL) {
114 BLOSC_TRACE_ERROR("Could not create decompression ctx");
115 return BLOSC2_ERROR_NULL_POINTER;
116 }
117
118 return BLOSC2_ERROR_SUCCESS;
119 }
120
121
122 static bool file_exists (char *filename) {
123 struct stat buffer;
124 return (stat (filename, &buffer) == 0);
125 }
126
127
128 /* Create a new super-chunk */
129 blosc2_schunk* blosc2_schunk_new(blosc2_storage *storage) {
130 blosc2_schunk* schunk = calloc(1, sizeof(blosc2_schunk));
131 schunk->version = 0; /* pre-first version */
132
133 // Get the storage with proper defaults
134 schunk->storage = get_new_storage(storage, &BLOSC2_CPARAMS_DEFAULTS, &BLOSC2_DPARAMS_DEFAULTS, &BLOSC2_IO_DEFAULTS);
135 // Update the (local variable) storage
136 storage = schunk->storage;
137
138 char* tradeoff = getenv("BTUNE_TRADEOFF");
139 if (tradeoff != NULL) {
140 // If BTUNE_TRADEOFF passed, automatically use btune
141 storage->cparams->tuner_id = BLOSC_BTUNE;
142 }
143
144 // ...and update internal properties
145 if (update_schunk_properties(schunk) < 0) {
146 BLOSC_TRACE_ERROR("Error when updating schunk properties");
147 return NULL;
148 }
149
150 if (!storage->contiguous && storage->urlpath != NULL){
151 char* urlpath;
152 char last_char = storage->urlpath[strlen(storage->urlpath) - 1];
153 urlpath = malloc(strlen(storage->urlpath) + 1);
154 strcpy(urlpath, storage->urlpath);
155 if (last_char == '\\' || last_char == '/') {
156 urlpath[strlen(storage->urlpath) - 1] = '\0';
157 }
158 // Create directory
159 if (mkdir(urlpath, 0777) == -1) {
160 BLOSC_TRACE_ERROR("Error during the creation of the directory, maybe it already exists.");
161 return NULL;
162 }
163 // We want a sparse (directory) frame as storage
164 blosc2_frame_s* frame = frame_new(urlpath);
165 free(urlpath);
166 frame->sframe = true;
167 // Initialize frame (basically, encode the header)
168 frame->schunk = schunk;
169 int64_t frame_len = frame_from_schunk(schunk, frame);
170 if (frame_len < 0) {
171 BLOSC_TRACE_ERROR("Error during the conversion of schunk to frame.");
172 return NULL;
173 }
174 schunk->frame = (blosc2_frame*)frame;
175 }
176 if (storage->contiguous){
177 // We want a contiguous frame as storage
178 if (storage->urlpath != NULL) {
179 if (file_exists(storage->urlpath)) {
180 BLOSC_TRACE_ERROR("You are trying to overwrite an existing frame. Remove it first!");
181 return NULL;
182 }
183 }
184 blosc2_frame_s* frame = frame_new(storage->urlpath);
185 frame->sframe = false;
186 // Initialize frame (basically, encode the header)
187 frame->schunk = schunk;
188 int64_t frame_len = frame_from_schunk(schunk, frame);
189 if (frame_len < 0) {
190 BLOSC_TRACE_ERROR("Error during the conversion of schunk to frame.");
191 return NULL;
192 }
193 schunk->frame = (blosc2_frame*)frame;
194 }
195
196 return schunk;
197 }
198
199
200 /* Create a copy of a super-chunk */
201 blosc2_schunk* blosc2_schunk_copy(blosc2_schunk *schunk, blosc2_storage *storage) {
202 if (schunk == NULL) {
203 BLOSC_TRACE_ERROR("Can not copy a NULL `schunk`.");
204 return NULL;
205 }
206
207 // Check if cparams are equals
208 bool cparams_equal = true;
209 blosc2_cparams cparams = {0};
210 if (storage->cparams == NULL) {
211 // When cparams are not specified, just use the same of schunk
212 cparams.typesize = schunk->cctx->typesize;
213 cparams.clevel = schunk->cctx->clevel;
214 cparams.compcode = schunk->cctx->compcode;
215 cparams.compcode_meta = schunk->cctx->compcode_meta;
216 cparams.splitmode = schunk->cctx->splitmode;
217 cparams.use_dict = schunk->cctx->use_dict;
218 cparams.blocksize = schunk->cctx->blocksize;
219 memcpy(cparams.filters, schunk->cctx->filters, BLOSC2_MAX_FILTERS);
220 memcpy(cparams.filters_meta, schunk->cctx->filters_meta, BLOSC2_MAX_FILTERS);
221 storage->cparams = &cparams;
222 }
223 else {
224 cparams = *storage->cparams;
225 }
226 if (cparams.blocksize == 0) {
227 // TODO: blocksize should be read from schunk->blocksize
228 // For this, it should be updated during the first append
229 // (or change API to make this a property during schunk creation).
230 cparams.blocksize = schunk->cctx->blocksize;
231 }
232
233 if (cparams.typesize != schunk->cctx->typesize ||
234 cparams.clevel != schunk->cctx->clevel ||
235 cparams.compcode != schunk->cctx->compcode ||
236 cparams.use_dict != schunk->cctx->use_dict ||
237 cparams.blocksize != schunk->cctx->blocksize ||
238 // In case of prefilters or postfilters, force their execution.
239 schunk->cctx->prefilter != NULL ||
240 schunk->dctx->postfilter != NULL) {
241 cparams_equal = false;
242 }
243 for (int i = 0; i < BLOSC2_MAX_FILTERS; ++i) {
244 if (cparams.filters[i] != schunk->cctx->filters[i] ||
245 cparams.filters_meta[i] != schunk->cctx->filters_meta[i]) {
246 cparams_equal = false;
247 }
248 }
249
250 // Create new schunk
251 blosc2_schunk *new_schunk = blosc2_schunk_new(storage);
252 if (new_schunk == NULL) {
253 BLOSC_TRACE_ERROR("Can not create a new schunk");
254 return NULL;
255 }
256
257 // Copy metalayers
258 for (int nmeta = 0; nmeta < schunk->nmetalayers; ++nmeta) {
259 blosc2_metalayer *meta = schunk->metalayers[nmeta];
260 if (blosc2_meta_add(new_schunk, meta->name, meta->content, meta->content_len) < 0) {
261 BLOSC_TRACE_ERROR("Can not add %s `metalayer`.", meta->name);
262 return NULL;
263 }
264 }
265
266 // Copy chunks
267 if (cparams_equal) {
268 for (int nchunk = 0; nchunk < schunk->nchunks; ++nchunk) {
269 uint8_t *chunk;
270 bool needs_free;
271 if (blosc2_schunk_get_chunk(schunk, nchunk, &chunk, &needs_free) < 0) {
272 BLOSC_TRACE_ERROR("Can not get the `chunk` %d.", nchunk);
273 return NULL;
274 }
275 if (blosc2_schunk_append_chunk(new_schunk, chunk, !needs_free) < 0) {
276 BLOSC_TRACE_ERROR("Can not append the `chunk` into super-chunk.");
277 return NULL;
278 }
279 }
280 } else {
281 int32_t chunksize = schunk->chunksize == -1 ? 0 : schunk->chunksize;
282 uint8_t *buffer = malloc(chunksize);
283 for (int nchunk = 0; nchunk < schunk->nchunks; ++nchunk) {
284 if (blosc2_schunk_decompress_chunk(schunk, nchunk, buffer, schunk->chunksize) < 0) {
285 BLOSC_TRACE_ERROR("Can not decompress the `chunk` %d.", nchunk);
286 return NULL;
287 }
288 if (blosc2_schunk_append_buffer(new_schunk, buffer, schunk->chunksize) < 0) {
289 BLOSC_TRACE_ERROR("Can not append the `buffer` into super-chunk.");
290 return NULL;
291 }
292 }
293 free(buffer);
294 }
295
296 // Copy vlmetalayers
297 for (int nmeta = 0; nmeta < schunk->nvlmetalayers; ++nmeta) {
298 uint8_t *content;
299 int32_t content_len;
300 char* name = schunk->vlmetalayers[nmeta]->name;
301 if (blosc2_vlmeta_get(schunk, name, &content, &content_len) < 0) {
302 BLOSC_TRACE_ERROR("Can not get %s `vlmetalayer`.", name);
303 }
304 if (blosc2_vlmeta_add(new_schunk, name, content, content_len, NULL) < 0) {
305 BLOSC_TRACE_ERROR("Can not add %s `vlmetalayer`.", name);
306 return NULL;
307 }
308 free(content);
309 }
310 return new_schunk;
311 }
312
313
314 /* Open an existing super-chunk that is on-disk (no copy is made). */
315 blosc2_schunk* blosc2_schunk_open_udio(const char* urlpath, const blosc2_io *udio) {
316 return blosc2_schunk_open_offset_udio(urlpath, 0, udio);
317 }
318
319 blosc2_schunk* blosc2_schunk_open_offset_udio(const char* urlpath, int64_t offset, const blosc2_io *udio) {
320 if (urlpath == NULL) {
321 BLOSC_TRACE_ERROR("You need to supply a urlpath.");
322 return NULL;
323 }
324
325 blosc2_frame_s* frame = frame_from_file_offset(urlpath, udio, offset);
326 if (frame == NULL) {
327 blosc2_io_cb *io_cb = blosc2_get_io_cb(udio->id);
328 if (io_cb == NULL) {
329 BLOSC_TRACE_ERROR("Error getting the input/output API");
330 return NULL;
331 }
332 int rc = io_cb->destroy(udio->params);
333 if (rc < 0) {
334 BLOSC_TRACE_ERROR("Cannot destroy the input/output object.");
335 }
336 return NULL;
337 }
338 blosc2_schunk* schunk = frame_to_schunk(frame, false, udio);
339
340 // Set the storage with proper defaults
341 size_t pathlen = strlen(urlpath);
342 schunk->storage->urlpath = malloc(pathlen + 1);
343 strcpy(schunk->storage->urlpath, urlpath);
344 schunk->storage->contiguous = !frame->sframe;
345
346 return schunk;
347 }
348
349 blosc2_schunk* blosc2_schunk_open(const char* urlpath) {
350 return blosc2_schunk_open_udio(urlpath, &BLOSC2_IO_DEFAULTS);
351 }
352
353 blosc2_schunk* blosc2_schunk_open_offset(const char* urlpath, int64_t offset) {
354 return blosc2_schunk_open_offset_udio(urlpath, offset, &BLOSC2_IO_DEFAULTS);
355 }
356
357 int64_t blosc2_schunk_to_buffer(blosc2_schunk* schunk, uint8_t** dest, bool* needs_free) {
358 blosc2_frame_s* frame;
359 int64_t cframe_len;
360
361 // Initialize defaults in case of errors
362 *dest = NULL;
363 *needs_free = false;
364
365 if ((schunk->storage->contiguous == true) && (schunk->storage->urlpath == NULL)) {
366 frame = (blosc2_frame_s*)(schunk->frame);
367 *dest = frame->cframe;
368 cframe_len = frame->len;
369 *needs_free = false;
370 }
371 else {
372 // Copy to a contiguous storage
373 blosc2_storage frame_storage = {.contiguous=true};
374 blosc2_schunk* schunk_copy = blosc2_schunk_copy(schunk, &frame_storage);
375 if (schunk_copy == NULL) {
376 BLOSC_TRACE_ERROR("Error during the conversion of schunk to buffer.");
377 return BLOSC2_ERROR_SCHUNK_COPY;
378 }
379 frame = (blosc2_frame_s*)(schunk_copy->frame);
380 *dest = frame->cframe;
381 cframe_len = frame->len;
382 *needs_free = true;
383 frame->avoid_cframe_free = true;
384 blosc2_schunk_free(schunk_copy);
385 }
386
387 return cframe_len;
388
389 }
390
391
392 /* Write an in-memory frame out to a file. */
393 int64_t frame_to_file(blosc2_frame_s* frame, const char* urlpath) {
394 blosc2_io_cb *io_cb = blosc2_get_io_cb(frame->schunk->storage->io->id);
395 if (io_cb == NULL) {
396 BLOSC_TRACE_ERROR("Error getting the input/output API");
397 return BLOSC2_ERROR_PLUGIN_IO;
398 }
399 void* fp = io_cb->open(urlpath, "wb", frame->schunk->storage->io);
400 int64_t io_pos = 0;
401 int64_t nitems = io_cb->write(frame->cframe, frame->len, 1, io_pos, fp);
402 io_cb->close(fp);
403 return nitems * frame->len;
404 }
405
406
407 /* Append an in-memory frame to a file. */
408 int64_t append_frame_to_file(blosc2_frame_s* frame, const char* urlpath) {
409 blosc2_io_cb *io_cb = blosc2_get_io_cb(frame->schunk->storage->io->id);
410 if (io_cb == NULL) {
411 BLOSC_TRACE_ERROR("Error getting the input/output API");
412 return BLOSC2_ERROR_PLUGIN_IO;
413 }
414 void* fp = io_cb->open(urlpath, "ab", frame->schunk->storage->io);
415
416 int64_t io_pos = io_cb->size(fp);
417 io_cb->write(frame->cframe, frame->len, 1, io_pos, fp);
418 io_cb->close(fp);
419 return io_pos;
420 }
421
422
423 /* Write super-chunk out to a file. */
424 int64_t blosc2_schunk_to_file(blosc2_schunk* schunk, const char* urlpath) {
425 if (urlpath == NULL) {
426 BLOSC_TRACE_ERROR("urlpath cannot be NULL");
427 return BLOSC2_ERROR_INVALID_PARAM;
428 }
429
430 // Accelerated path for in-memory frames
431 if (schunk->storage->contiguous && schunk->storage->urlpath == NULL) {
432 int64_t len = frame_to_file((blosc2_frame_s*)(schunk->frame), urlpath);
433 if (len <= 0) {
434 BLOSC_TRACE_ERROR("Error writing to file");
435 return len;
436 }
437 return len;
438 }
439
440 // Copy to a contiguous file
441 blosc2_storage frame_storage = {.contiguous=true, .urlpath=(char*)urlpath};
442 blosc2_schunk* schunk_copy = blosc2_schunk_copy(schunk, &frame_storage);
443 if (schunk_copy == NULL) {
444 BLOSC_TRACE_ERROR("Error during the conversion of schunk to buffer.");
445 return BLOSC2_ERROR_SCHUNK_COPY;
446 }
447 blosc2_frame_s* frame = (blosc2_frame_s*)(schunk_copy->frame);
448 int64_t frame_len = frame->len;
449 blosc2_schunk_free(schunk_copy);
450 return frame_len;
451 }
452
453
454 /* Append a super-chunk to a file. */
455 int64_t blosc2_schunk_append_file(blosc2_schunk* schunk, const char* urlpath) {
456 if (urlpath == NULL) {
457 BLOSC_TRACE_ERROR("urlpath cannot be NULL");
458 return BLOSC2_ERROR_INVALID_PARAM;
459 }
460
461 // Accelerated path for in-memory frames
462 if (schunk->storage->contiguous && schunk->storage->urlpath == NULL) {
463 int64_t offset = append_frame_to_file((blosc2_frame_s*)(schunk->frame), urlpath);
464 if (offset <= 0) {
465 BLOSC_TRACE_ERROR("Error writing to file");
466 return offset;
467 }
468 return offset;
469 }
470
471 // Copy to a contiguous file
472 blosc2_storage frame_storage = {.contiguous=true, .urlpath=NULL};
473 blosc2_schunk* schunk_copy = blosc2_schunk_copy(schunk, &frame_storage);
474 if (schunk_copy == NULL) {
475 BLOSC_TRACE_ERROR("Error during the conversion of schunk to buffer.");
476 return BLOSC2_ERROR_SCHUNK_COPY;
477 }
478 blosc2_frame_s* frame = (blosc2_frame_s*)(schunk_copy->frame);
479 int64_t offset = append_frame_to_file(frame, urlpath);
480 blosc2_schunk_free(schunk_copy);
481 return offset;
482 }
483
484
485 /* Free all memory from a super-chunk. */
486 int blosc2_schunk_free(blosc2_schunk *schunk) {
487 if (schunk->data != NULL) {
488 for (int i = 0; i < schunk->nchunks; i++) {
489 free(schunk->data[i]);
490 }
491 free(schunk->data);
492 }
493 if (schunk->cctx != NULL)
494 blosc2_free_ctx(schunk->cctx);
495 if (schunk->dctx != NULL)
496 blosc2_free_ctx(schunk->dctx);
497 if (schunk->blockshape != NULL)
498 free(schunk->blockshape);
499
500 if (schunk->nmetalayers > 0) {
501 for (int i = 0; i < schunk->nmetalayers; i++) {
502 if (schunk->metalayers[i] != NULL) {
503 if (schunk->metalayers[i]->name != NULL)
504 free(schunk->metalayers[i]->name);
505 if (schunk->metalayers[i]->content != NULL)
506 free(schunk->metalayers[i]->content);
507 free(schunk->metalayers[i]);
508 }
509 }
510 schunk->nmetalayers = 0;
511 }
512
513 if (schunk->storage != NULL) {
514 blosc2_io_cb *io_cb = blosc2_get_io_cb(schunk->storage->io->id);
515 if (io_cb != NULL) {
516 int rc = io_cb->destroy(schunk->storage->io->params);
517 if (rc < 0) {
518 BLOSC_TRACE_ERROR("Could not free the I/O resources.");
519 }
520 }
521
522 if (schunk->storage->urlpath != NULL) {
523 free(schunk->storage->urlpath);
524 }
525 free(schunk->storage->cparams);
526 free(schunk->storage->dparams);
527 free(schunk->storage->io);
528 free(schunk->storage);
529 }
530
531 if (schunk->frame != NULL) {
532 frame_free((blosc2_frame_s *) schunk->frame);
533 }
534
535 if (schunk->nvlmetalayers > 0) {
536 for (int i = 0; i < schunk->nvlmetalayers; ++i) {
537 if (schunk->vlmetalayers[i] != NULL) {
538 if (schunk->vlmetalayers[i]->name != NULL)
539 free(schunk->vlmetalayers[i]->name);
540 if (schunk->vlmetalayers[i]->content != NULL)
541 free(schunk->vlmetalayers[i]->content);
542 free(schunk->vlmetalayers[i]);
543 }
544 }
545 }
546
547 free(schunk);
548
549 return 0;
550 }
551
552
553 /* Create a super-chunk out of a contiguous frame buffer */
554 blosc2_schunk* blosc2_schunk_from_buffer(uint8_t *cframe, int64_t len, bool copy) {
555 blosc2_frame_s* frame = frame_from_cframe(cframe, len, false);
556 if (frame == NULL) {
557 return NULL;
558 }
559 // Check that the buffer actually comes from a cframe
560 char *magic_number = (char *)cframe;
561 magic_number += FRAME_HEADER_MAGIC;
562 if (strcmp(magic_number, "b2frame\0") != 0) {
563 frame_free(frame);
564 return NULL;
565 }
566 blosc2_schunk* schunk = frame_to_schunk(frame, copy, &BLOSC2_IO_DEFAULTS);
567 if (schunk && copy) {
568 // Super-chunk has its own copy of frame
569 frame_free(frame);
570 }
571 return schunk;
572 }
573
574
575 /* Create a super-chunk out of a contiguous frame buffer */
576 void blosc2_schunk_avoid_cframe_free(blosc2_schunk *schunk, bool avoid_cframe_free) {
577 blosc2_frame_s* frame = (blosc2_frame_s*)schunk->frame;
578 frame_avoid_cframe_free(frame, avoid_cframe_free);
579 }
580
581
582 /* Fill an empty frame with special values (fast path). */
583 int64_t blosc2_schunk_fill_special(blosc2_schunk* schunk, int64_t nitems, int special_value,
584 int32_t chunksize) {
585 if (nitems == 0) {
586 return 0;
587 }
588
589 int32_t typesize = schunk->typesize;
590
591 if ((nitems * typesize / chunksize) > INT_MAX) {
592 BLOSC_TRACE_ERROR("nitems is too large. Try increasing the chunksize.");
593 return BLOSC2_ERROR_SCHUNK_SPECIAL;
594 }
595
596 if ((schunk->nbytes > 0) || (schunk->cbytes > 0)) {
597 BLOSC_TRACE_ERROR("Filling with special values only works on empty super-chunks");
598 return BLOSC2_ERROR_FRAME_SPECIAL;
599 }
600
601 // Compute the number of chunks and the length of the offsets chunk
602 int32_t chunkitems = chunksize / typesize;
603 int64_t nchunks = nitems / chunkitems;
604 int32_t leftover_items = (int32_t)(nitems % chunkitems);
605
606 if (schunk->frame == NULL) {
607 // Build the special chunks
608 int32_t leftover_size = leftover_items * typesize;
609 void* chunk = malloc(BLOSC_EXTENDED_HEADER_LENGTH);
610 void* chunk2 = malloc(BLOSC_EXTENDED_HEADER_LENGTH);
611 blosc2_cparams* cparams;
612 blosc2_schunk_get_cparams(schunk, &cparams);
613 int csize, csize2;
614 switch (special_value) {
615 case BLOSC2_SPECIAL_ZERO:
616 csize = blosc2_chunk_zeros(*cparams, chunksize, chunk, BLOSC_EXTENDED_HEADER_LENGTH);
617 csize2 = blosc2_chunk_zeros(*cparams, leftover_size, chunk2, BLOSC_EXTENDED_HEADER_LENGTH);
618 break;
619 case BLOSC2_SPECIAL_UNINIT:
620 csize = blosc2_chunk_uninit(*cparams, chunksize, chunk, BLOSC_EXTENDED_HEADER_LENGTH);
621 csize2 = blosc2_chunk_uninit(*cparams, leftover_size, chunk2, BLOSC_EXTENDED_HEADER_LENGTH);
622 break;
623 case BLOSC2_SPECIAL_NAN:
624 csize = blosc2_chunk_nans(*cparams, chunksize, chunk, BLOSC_EXTENDED_HEADER_LENGTH);
625 csize2 = blosc2_chunk_nans(*cparams, leftover_size, chunk2, BLOSC_EXTENDED_HEADER_LENGTH);
626 break;
627 default:
628 BLOSC_TRACE_ERROR("Only zeros, NaNs or non-initialized values are supported.");
629 return BLOSC2_ERROR_SCHUNK_SPECIAL;
630 }
631 free(cparams);
632 if (csize < 0 || csize2 < 0) {
633 BLOSC_TRACE_ERROR("Error creating special chunks.");
634 return BLOSC2_ERROR_SCHUNK_SPECIAL;
635 }
636
637 for (int nchunk = 0; nchunk < nchunks; nchunk++) {
638 int64_t nchunk_ = blosc2_schunk_append_chunk(schunk, chunk, true);
639 if (nchunk_ != nchunk + 1) {
640 BLOSC_TRACE_ERROR("Error appending special chunks.");
641 return BLOSC2_ERROR_SCHUNK_SPECIAL;
642 }
643 }
644
645 if (leftover_items) {
646 int64_t nchunk_ = blosc2_schunk_append_chunk(schunk, chunk2, true);
647 if (nchunk_ != nchunks + 1) {
648 BLOSC_TRACE_ERROR("Error appending last special chunk.");
649 return BLOSC2_ERROR_SCHUNK_SPECIAL;
650 }
651 }
652 free(chunk);
653 free(chunk2);
654 }
655 else {
656 /* Fill an empty frame with special values (fast path). */
657 blosc2_frame_s *frame = (blosc2_frame_s *) schunk->frame;
658 /* Update counters (necessary for the frame_fill_special() logic) */
659 if (leftover_items) {
660 nchunks += 1;
661 }
662 schunk->chunksize = chunksize;
663 schunk->nchunks = nchunks;
664 schunk->nbytes = nitems * typesize;
665 int64_t frame_len = frame_fill_special(frame, nitems, special_value, chunksize, schunk);
666 if (frame_len < 0) {
667 BLOSC_TRACE_ERROR("Error creating special frame.");
668 return frame_len;
669 }
670 }
671
672 return schunk->nchunks;
673 }
674
675 /* Append an existing chunk into a super-chunk. */
676 int64_t blosc2_schunk_append_chunk(blosc2_schunk *schunk, uint8_t *chunk, bool copy) {
677 int32_t chunk_nbytes;
678 int32_t chunk_cbytes;
679 int64_t nchunks = schunk->nchunks;
680
681 int rc = blosc2_cbuffer_sizes(chunk, &chunk_nbytes, &chunk_cbytes, NULL);
682 if (rc < 0) {
683 return rc;
684 }
685
686 if (schunk->chunksize == -1) {
687 schunk->chunksize = chunk_nbytes; // The super-chunk is initialized now
688 }
689 if (chunk_nbytes > schunk->chunksize) {
690 BLOSC_TRACE_ERROR("Appending chunks that have different lengths in the same schunk "
691 "is not supported yet: %d > %d.", chunk_nbytes, schunk->chunksize);
692 return BLOSC2_ERROR_CHUNK_APPEND;
693 }
694
695 /* Update counters */
696 schunk->current_nchunk = nchunks;
697 schunk->nchunks = nchunks + 1;
698 schunk->nbytes += chunk_nbytes;
699 if (schunk->frame == NULL) {
700 schunk->cbytes += chunk_cbytes;
701 } else {
702 // A frame
703 int special_value = (chunk[BLOSC2_CHUNK_BLOSC2_FLAGS] >> 4) & BLOSC2_SPECIAL_MASK;
704 switch (special_value) {
705 case BLOSC2_SPECIAL_ZERO:
706 case BLOSC2_SPECIAL_NAN:
707 case BLOSC2_SPECIAL_UNINIT:
708 schunk->cbytes += 0;
709 break;
710 default:
711 schunk->cbytes += chunk_cbytes;
712 }
713 }
714
715 if (copy) {
716 // Make a copy of the chunk
717 uint8_t *chunk_copy = malloc(chunk_cbytes);
718 memcpy(chunk_copy, chunk, chunk_cbytes);
719 chunk = chunk_copy;
720 }
721
722 // Update super-chunk or frame
723 blosc2_frame_s* frame = (blosc2_frame_s*)schunk->frame;
724 if (frame == NULL) {
725 // Check that we are not appending a small chunk after another small chunk
726 if ((schunk->nchunks > 1) && (chunk_nbytes < schunk->chunksize)) {
727 uint8_t* last_chunk = schunk->data[nchunks - 1];
728 int32_t last_nbytes;
729 rc = blosc2_cbuffer_sizes(last_chunk, &last_nbytes, NULL, NULL);
730 if (rc < 0) {
731 return rc;
732 }
733 if ((last_nbytes < schunk->chunksize) && (chunk_nbytes < schunk->chunksize)) {
734 BLOSC_TRACE_ERROR(
735 "Appending two consecutive chunks with a chunksize smaller than the schunk chunksize "
736 "is not allowed yet: %d != %d.", chunk_nbytes, schunk->chunksize);
737 return BLOSC2_ERROR_CHUNK_APPEND;
738 }
739 }
740
741 if (!copy && (chunk_cbytes < chunk_nbytes)) {
742 // We still want to do a shrink of the chunk
743 chunk = realloc(chunk, chunk_cbytes);
744 }
745
746 /* Make space for appending the copy of the chunk and do it */
747 if ((nchunks + 1) * sizeof(void *) > schunk->data_len) {
748 // Extend the data pointer by one memory page (4k)
749 schunk->data_len += 4096; // must be a multiple of sizeof(void*)
750 schunk->data = realloc(schunk->data, schunk->data_len);
751 }
752 schunk->data[nchunks] = chunk;
753 }
754 else {
755 if (frame_append_chunk(frame, chunk, schunk) == NULL) {
756 BLOSC_TRACE_ERROR("Problems appending a chunk.");
757 return BLOSC2_ERROR_CHUNK_APPEND;
758 }
759 }
760 return schunk->nchunks;
761 }
762
763
764 /* Insert an existing @p chunk in a specified position on a super-chunk */
765 int64_t blosc2_schunk_insert_chunk(blosc2_schunk *schunk, int64_t nchunk, uint8_t *chunk, bool copy) {
766 int32_t chunk_nbytes;
767 int32_t chunk_cbytes;
768 int64_t nchunks = schunk->nchunks;
769
770 int rc = blosc2_cbuffer_sizes(chunk, &chunk_nbytes, &chunk_cbytes, NULL);
771 if (rc < 0) {
772 return rc;
773 }
774
775 if (schunk->chunksize == -1) {
776 schunk->chunksize = chunk_nbytes; // The super-chunk is initialized now
777 }
778
779 if (chunk_nbytes > schunk->chunksize) {
780 BLOSC_TRACE_ERROR("Inserting chunks that have different lengths in the same schunk "
781 "is not supported yet: %d > %d.", chunk_nbytes, schunk->chunksize);
782 return BLOSC2_ERROR_CHUNK_INSERT;
783 }
784
785 /* Update counters */
786 schunk->current_nchunk = nchunk;
787 schunk->nchunks = nchunks + 1;
788 schunk->nbytes += chunk_nbytes;
789 if (schunk->frame == NULL) {
790 schunk->cbytes += chunk_cbytes;
791 } else {
792 // A frame
793 int special_value = (chunk[BLOSC2_CHUNK_BLOSC2_FLAGS] >> 4) & BLOSC2_SPECIAL_MASK;
794 switch (special_value) {
795 case BLOSC2_SPECIAL_ZERO:
796 case BLOSC2_SPECIAL_NAN:
797 case BLOSC2_SPECIAL_UNINIT:
798 schunk->cbytes += 0;
799 break;
800 default:
801 schunk->cbytes += chunk_cbytes;
802 }
803 }
804
805 if (copy) {
806 // Make a copy of the chunk
807 uint8_t *chunk_copy = malloc(chunk_cbytes);
808 memcpy(chunk_copy, chunk, chunk_cbytes);
809 chunk = chunk_copy;
810 }
811
812 // Update super-chunk or frame
813 blosc2_frame_s* frame = (blosc2_frame_s*)schunk->frame;
814 if (frame == NULL) {
815 // Check that we are not appending a small chunk after another small chunk
816 if ((schunk->nchunks > 0) && (chunk_nbytes < schunk->chunksize)) {
817 uint8_t* last_chunk = schunk->data[nchunks - 1];
818 int32_t last_nbytes;
819 rc = blosc2_cbuffer_sizes(last_chunk, &last_nbytes, NULL, NULL);
820 if (rc < 0) {
821 return rc;
822 }
823 if ((last_nbytes < schunk->chunksize) && (chunk_nbytes < schunk->chunksize)) {
824 BLOSC_TRACE_ERROR("Appending two consecutive chunks with a chunksize smaller "
825 "than the schunk chunksize is not allowed yet: %d != %d",
826 chunk_nbytes, schunk->chunksize);
827 return BLOSC2_ERROR_CHUNK_APPEND;
828 }
829 }
830
831 if (!copy && (chunk_cbytes < chunk_nbytes)) {
832 // We still want to do a shrink of the chunk
833 chunk = realloc(chunk, chunk_cbytes);
834 }
835
836 // Make space for appending the copy of the chunk and do it
837 if ((nchunks + 1) * sizeof(void *) > schunk->data_len) {
838 // Extend the data pointer by one memory page (4k)
839 schunk->data_len += 4096; // must be a multiple of sizeof(void*)
840 schunk->data = realloc(schunk->data, schunk->data_len);
841 }
842
843 // Reorder the offsets and insert the new chunk
844 for (int64_t i = nchunks; i > nchunk; --i) {
845 schunk->data[i] = schunk->data[i-1];
846 }
847 schunk->data[nchunk] = chunk;
848 }
849
850 else {
851 if (frame_insert_chunk(frame, nchunk, chunk, schunk) == NULL) {
852 BLOSC_TRACE_ERROR("Problems inserting a chunk in a frame.");
853 return BLOSC2_ERROR_CHUNK_INSERT;
854 }
855 }
856 return schunk->nchunks;
857 }
858
859
860 int64_t blosc2_schunk_update_chunk(blosc2_schunk *schunk, int64_t nchunk, uint8_t *chunk, bool copy) {
861 int32_t chunk_nbytes;
862 int32_t chunk_cbytes;
863
864 int rc = blosc2_cbuffer_sizes(chunk, &chunk_nbytes, &chunk_cbytes, NULL);
865 if (rc < 0) {
866 return rc;
867 }
868
869 if (schunk->chunksize == -1) {
870 schunk->chunksize = chunk_nbytes; // The super-chunk is initialized now
871 }
872
873 if (schunk->chunksize != 0 && (chunk_nbytes > schunk->chunksize ||
874 (chunk_nbytes < schunk->chunksize && nchunk != schunk->nchunks - 1))) {
875 BLOSC_TRACE_ERROR("Updating chunks having different lengths in the same schunk "
876 "is not supported yet (unless it's the last one and smaller):"
877 " %d > %d.", chunk_nbytes, schunk->chunksize);
878 return BLOSC2_ERROR_CHUNK_UPDATE;
879 }
880
881 bool needs_free;
882 uint8_t *chunk_old;
883 int err = blosc2_schunk_get_chunk(schunk, nchunk, &chunk_old, &needs_free);
884 if (err < 0) {
885 BLOSC_TRACE_ERROR("%" PRId64 " chunk can not be obtained from schunk.", nchunk);
886 return -1;
887 }
888 int32_t chunk_nbytes_old = 0;
889 int32_t chunk_cbytes_old = 0;
890 schunk->current_nchunk = nchunk;
891
892 if (chunk_old != 0) {
893 rc = blosc2_cbuffer_sizes(chunk_old, &chunk_nbytes_old, &chunk_cbytes_old, NULL);
894 if (rc < 0) {
895 return rc;
896 }
897 if (chunk_cbytes_old == BLOSC2_MAX_OVERHEAD) {
898 chunk_cbytes_old = 0;
899 }
900 }
901 if (needs_free) {
902 free(chunk_old);
903 }
904
905 if (copy) {
906 // Make a copy of the chunk
907 uint8_t *chunk_copy = malloc(chunk_cbytes);
908 memcpy(chunk_copy, chunk, chunk_cbytes);
909 chunk = chunk_copy;
910 }
911
912 blosc2_frame_s* frame = (blosc2_frame_s*)(schunk->frame);
913 if (schunk->frame == NULL) {
914 /* Update counters */
915 schunk->nbytes += chunk_nbytes;
916 schunk->nbytes -= chunk_nbytes_old;
917 schunk->cbytes += chunk_cbytes;
918 schunk->cbytes -= chunk_cbytes_old;
919 } else {
920 // A frame
921 int special_value = (chunk[BLOSC2_CHUNK_BLOSC2_FLAGS] >> 4) & BLOSC2_SPECIAL_MASK;
922 switch (special_value) {
923 case BLOSC2_SPECIAL_ZERO:
924 case BLOSC2_SPECIAL_NAN:
925 case BLOSC2_SPECIAL_UNINIT:
926 schunk->nbytes += chunk_nbytes;
927 schunk->nbytes -= chunk_nbytes_old;
928 if (frame->sframe) {
929 schunk->cbytes -= chunk_cbytes_old;
930 }
931 break;
932 default:
933 /* Update counters */
934 schunk->nbytes += chunk_nbytes;
935 schunk->nbytes -= chunk_nbytes_old;
936 schunk->cbytes += chunk_cbytes;
937 if (frame->sframe) {
938 schunk->cbytes -= chunk_cbytes_old;
939 }
940 else {
941 if (chunk_cbytes_old >= chunk_cbytes) {
942 schunk->cbytes -= chunk_cbytes;
943 }
944 }
945 }
946 }
947
948 // Update super-chunk or frame
949 if (schunk->frame == NULL) {
950 if (!copy && (chunk_cbytes < chunk_nbytes)) {
951 // We still want to do a shrink of the chunk
952 chunk = realloc(chunk, chunk_cbytes);
953 }
954
955 // Free old chunk and add reference to new chunk
956 if (schunk->data[nchunk] != 0) {
957 free(schunk->data[nchunk]);
958 }
959 schunk->data[nchunk] = chunk;
960 }
961 else {
962 if (frame_update_chunk(frame, nchunk, chunk, schunk) == NULL) {
963 BLOSC_TRACE_ERROR("Problems updating a chunk in a frame.");
964 return BLOSC2_ERROR_CHUNK_UPDATE;
965 }
966 }
967
968 return schunk->nchunks;
969 }
970
971 int64_t blosc2_schunk_delete_chunk(blosc2_schunk *schunk, int64_t nchunk) {
972 int rc;
973 if (schunk->nchunks < nchunk) {
974 BLOSC_TRACE_ERROR("The schunk has not enough chunks (%" PRId64 ")!", schunk->nchunks);
975 }
976
977 bool needs_free;
978 uint8_t *chunk_old;
979 int err = blosc2_schunk_get_chunk(schunk, nchunk, &chunk_old, &needs_free);
980 if (err < 0) {
981 BLOSC_TRACE_ERROR("%" PRId64 "chunk can not be obtained from schunk.", nchunk);
982 return -1;
983 }
984 int32_t chunk_nbytes_old = 0;
985 int32_t chunk_cbytes_old = 0;
986 schunk->current_nchunk = nchunk;
987
988 if (chunk_old != 0) {
989 rc = blosc2_cbuffer_sizes(chunk_old, &chunk_nbytes_old, &chunk_cbytes_old, NULL);
990 if (rc < 0) {
991 return rc;
992 }
993 if (chunk_cbytes_old == BLOSC2_MAX_OVERHEAD) {
994 chunk_cbytes_old = 0;
995 }
996 }
997 if (needs_free) {
998 free(chunk_old);
999 }
1000
1001 blosc2_frame_s* frame = (blosc2_frame_s*)(schunk->frame);
1002 schunk->nchunks -= 1;
1003 if (schunk->frame == NULL) {
1004 /* Update counters */
1005 schunk->nbytes -= chunk_nbytes_old;
1006 schunk->cbytes -= chunk_cbytes_old;
1007 } else {
1008 // A frame
1009 schunk->nbytes -= chunk_nbytes_old;
1010 if (frame->sframe) {
1011 schunk->cbytes -= chunk_cbytes_old;
1012 }
1013 }
1014
1015 // Update super-chunk or frame
1016 if (schunk->frame == NULL) {
1017 // Free old chunk
1018 if (schunk->data[nchunk] != 0) {
1019 free(schunk->data[nchunk]);
1020 }
1021 // Reorder the offsets and insert the new chunk
1022 for (int64_t i = nchunk; i < schunk->nchunks; i++) {
1023 schunk->data[i] = schunk->data[i + 1];
1024 }
1025 schunk->data[schunk->nchunks] = NULL;
1026
1027 }
1028 else {
1029 if (frame_delete_chunk(frame, nchunk, schunk) == NULL) {
1030 BLOSC_TRACE_ERROR("Problems deleting a chunk in a frame.");
1031 return BLOSC2_ERROR_CHUNK_UPDATE;
1032 }
1033 }
1034 return schunk->nchunks;
1035 }
1036
1037
1038 /* Append a data buffer to a super-chunk. */
1039 int64_t blosc2_schunk_append_buffer(blosc2_schunk *schunk, const void *src, int32_t nbytes) {
1040 uint8_t* chunk = malloc(nbytes + BLOSC2_MAX_OVERHEAD);
1041 schunk->current_nchunk = schunk->nchunks;
1042 /* Compress the src buffer using super-chunk context */
1043 int cbytes = blosc2_compress_ctx(schunk->cctx, src, nbytes, chunk,
1044 nbytes + BLOSC2_MAX_OVERHEAD);
1045 if (cbytes < 0) {
1046 free(chunk);
1047 return cbytes;
1048 }
1049 // We don't need a copy of the chunk, as it will be shrunk if necessary
1050 int64_t nchunks = blosc2_schunk_append_chunk(schunk, chunk, false);
1051 if (nchunks < 0) {
1052 BLOSC_TRACE_ERROR("Error appending a buffer in super-chunk");
1053 return nchunks;
1054 }
1055
1056 return nchunks;
1057 }
1058
1059
1060 /* Decompress and return a chunk that is part of a super-chunk. */
1061 int blosc2_schunk_decompress_chunk(blosc2_schunk *schunk, int64_t nchunk,
1062 void *dest, int32_t nbytes) {
1063 int32_t chunk_nbytes;
1064 int32_t chunk_cbytes;
1065 int chunksize;
1066 int rc;
1067 blosc2_frame_s* frame = (blosc2_frame_s*)schunk->frame;
1068
1069 schunk->current_nchunk = nchunk;
1070 if (frame == NULL) {
1071 if (nchunk >= schunk->nchunks) {
1072 BLOSC_TRACE_ERROR("nchunk ('%" PRId64 "') exceeds the number of chunks "
1073 "('%" PRId64 "') in super-chunk.", nchunk, schunk->nchunks);
1074 return BLOSC2_ERROR_INVALID_PARAM;
1075 }
1076 uint8_t* src = schunk->data[nchunk];
1077 if (src == 0) {
1078 return 0;
1079 }
1080
1081 rc = blosc2_cbuffer_sizes(src, &chunk_nbytes, &chunk_cbytes, NULL);
1082 if (rc < 0) {
1083 return rc;
1084 }
1085
1086 if (nbytes < chunk_nbytes) {
1087 BLOSC_TRACE_ERROR("Buffer size is too small for the decompressed buffer "
1088 "('%d' bytes, but '%d' are needed).", nbytes, chunk_nbytes);
1089 return BLOSC2_ERROR_INVALID_PARAM;
1090 }
1091
1092 chunksize = blosc2_decompress_ctx(schunk->dctx, src, chunk_cbytes, dest, nbytes);
1093 if (chunksize < 0 || chunksize != chunk_nbytes) {
1094 BLOSC_TRACE_ERROR("Error in decompressing chunk.");
1095 if (chunksize < 0)
1096 return chunksize;
1097 return BLOSC2_ERROR_FAILURE;
1098 }
1099 } else {
1100 chunksize = frame_decompress_chunk(schunk->dctx, frame, nchunk, dest, nbytes);
1101 if (chunksize < 0) {
1102 return chunksize;
1103 }
1104 }
1105 return chunksize;
1106 }
1107
1108
1109 /* Return a compressed chunk that is part of a super-chunk in the `chunk` parameter.
1110 * If the super-chunk is backed by a frame that is disk-based, a buffer is allocated for the
1111 * (compressed) chunk, and hence a free is needed. You can check if the chunk requires a free
1112 * with the `needs_free` parameter.
1113 * If the chunk does not need a free, it means that a pointer to the location in the super-chunk
1114 * (or the backing in-memory frame) is returned in the `chunk` parameter.
1115 *
1116 * The size of the (compressed) chunk is returned. If some problem is detected, a negative code
1117 * is returned instead.
1118 */
1119 int blosc2_schunk_get_chunk(blosc2_schunk *schunk, int64_t nchunk, uint8_t **chunk, bool *needs_free) {
1120 if (schunk->dctx->threads_started > 1) {
1121 pthread_mutex_lock(&schunk->dctx->nchunk_mutex);
1122 schunk->current_nchunk = nchunk;
1123 pthread_mutex_unlock(&schunk->dctx->nchunk_mutex);
1124 }
1125 else {
1126 schunk->current_nchunk = nchunk;
1127 }
1128 blosc2_frame_s* frame = (blosc2_frame_s*)schunk->frame;
1129 if (frame != NULL) {
1130 return frame_get_chunk(frame, nchunk, chunk, needs_free);
1131 }
1132
1133 if (nchunk >= schunk->nchunks) {
1134 BLOSC_TRACE_ERROR("nchunk ('%" PRId64 "') exceeds the number of chunks "
1135 "('%" PRId64 "') in schunk.", nchunk, schunk->nchunks);
1136 return BLOSC2_ERROR_INVALID_PARAM;
1137 }
1138
1139 *chunk = schunk->data[nchunk];
1140 if (*chunk == 0) {
1141 *needs_free = 0;
1142 return 0;
1143 }
1144
1145 *needs_free = false;
1146 int32_t chunk_cbytes;
1147 int rc = blosc2_cbuffer_sizes(*chunk, NULL, &chunk_cbytes, NULL);
1148 if (rc < 0) {
1149 return rc;
1150 }
1151 return (int)chunk_cbytes;
1152 }
1153
1154
1155 /* Return a compressed chunk that is part of a super-chunk in the `chunk` parameter.
1156 * If the super-chunk is backed by a frame that is disk-based, a buffer is allocated for the
1157 * (compressed) chunk, and hence a free is needed. You can check if the chunk requires a free
1158 * with the `needs_free` parameter.
1159 * If the chunk does not need a free, it means that a pointer to the location in the super-chunk
1160 * (or the backing in-memory frame) is returned in the `chunk` parameter.
1161 *
1162 * The size of the (compressed) chunk is returned. If some problem is detected, a negative code
1163 * is returned instead.
1164 */
1165 int blosc2_schunk_get_lazychunk(blosc2_schunk *schunk, int64_t nchunk, uint8_t **chunk, bool *needs_free) {
1166 if (schunk->dctx->threads_started > 1) {
1167 pthread_mutex_lock(&schunk->dctx->nchunk_mutex);
1168 schunk->current_nchunk = nchunk;
1169 pthread_mutex_unlock(&schunk->dctx->nchunk_mutex);
1170 }
1171 else {
1172 schunk->current_nchunk = nchunk;
1173 }
1174 blosc2_frame_s* frame = (blosc2_frame_s*)schunk->frame;
1175 if (schunk->frame != NULL) {
1176 return frame_get_lazychunk(frame, nchunk, chunk, needs_free);
1177 }
1178
1179 if (nchunk >= schunk->nchunks) {
1180 BLOSC_TRACE_ERROR("nchunk ('%" PRId64 "') exceeds the number of chunks "
1181 "('%" PRId64 "') in schunk.", nchunk, schunk->nchunks);
1182 return BLOSC2_ERROR_INVALID_PARAM;
1183 }
1184
1185 *chunk = schunk->data[nchunk];
1186 if (*chunk == 0) {
1187 *needs_free = 0;
1188 return 0;
1189 }
1190
1191 *needs_free = false;
1192 int32_t chunk_cbytes;
1193 int rc = blosc2_cbuffer_sizes(*chunk, NULL, &chunk_cbytes, NULL);
1194 if (rc < 0) {
1195 return rc;
1196 }
1197 return (int)chunk_cbytes;
1198 }
1199
1200
1201 int blosc2_schunk_get_slice_buffer(blosc2_schunk *schunk, int64_t start, int64_t stop, void *buffer) {
1202 int64_t byte_start = start * schunk->typesize;
1203 int64_t byte_stop = stop * schunk->typesize;
1204 int64_t nchunk_start = byte_start / schunk->chunksize;
1205 int32_t chunk_start = (int32_t) (byte_start % schunk->chunksize); // 0 indexed
1206 int32_t chunk_stop; // 0 indexed
1207 if (byte_stop >= (nchunk_start + 1) * schunk->chunksize) {
1208 chunk_stop = schunk->chunksize;
1209 }
1210 else {
1211 chunk_stop = (int32_t) (byte_stop % schunk->chunksize);
1212 }
1213
1214 uint8_t *dst_ptr = (uint8_t *) buffer;
1215 bool needs_free;
1216 uint8_t *chunk;
1217 int32_t cbytes;
1218 int64_t nchunk = nchunk_start;
1219 int64_t nbytes_read = 0;
1220 int32_t nbytes;
1221 int32_t chunksize = schunk->chunksize;
1222
1223 while (nbytes_read < ((stop - start) * schunk->typesize)) {
1224 cbytes = blosc2_schunk_get_lazychunk(schunk, nchunk, &chunk, &needs_free);
1225 if (cbytes < 0) {
1226 BLOSC_TRACE_ERROR("Cannot get lazychunk ('%" PRId64 "').", nchunk);
1227 return BLOSC2_ERROR_FAILURE;
1228 }
1229 int32_t blocksize = sw32_(chunk + BLOSC2_CHUNK_BLOCKSIZE);
1230
1231 int32_t nblock_start = (int32_t) (chunk_start / blocksize);
1232 int32_t nblock_stop = (int32_t) ((chunk_stop - 1) / blocksize);
1233 if (nchunk == (schunk->nchunks - 1) && schunk->nbytes % schunk->chunksize != 0) {
1234 chunksize = schunk->nbytes % schunk->chunksize;
1235 }
1236 int32_t nblocks = chunksize / blocksize;
1237 if (chunksize % blocksize != 0) {
1238 nblocks++;
1239 }
1240
1241 if (chunk_start == 0 && chunk_stop == chunksize) {
1242 // Avoid memcpy
1243 nbytes = blosc2_decompress_ctx(schunk->dctx, chunk, cbytes, dst_ptr, chunksize);
1244 if (nbytes < 0) {
1245 BLOSC_TRACE_ERROR("Cannot decompress chunk ('%" PRId64 "').", nchunk);
1246 return BLOSC2_ERROR_FAILURE;
1247 }
1248 }
1249 else {
1250 // After extensive timing I have not been able to see lots of situations where
1251 // a maskout read is better than a getitem one. Disabling for now.
1252 // if (nblock_start != nblock_stop) {
1253 if (false) {
1254 uint8_t *data = malloc(chunksize);
1255 /* We have more than 1 block to read, so use a masked read */
1256 bool *block_maskout = calloc(nblocks, 1);
1257 for (int32_t nblock = 0; nblock < nblocks; nblock++) {
1258 if ((nblock < nblock_start) || (nblock > nblock_stop)) {
1259 block_maskout[nblock] = true;
1260 }
1261 }
1262 if (blosc2_set_maskout(schunk->dctx, block_maskout, nblocks) < 0) {
1263 BLOSC_TRACE_ERROR("Cannot set maskout");
1264 return BLOSC2_ERROR_FAILURE;
1265 }
1266
1267 nbytes = blosc2_decompress_ctx(schunk->dctx, chunk, cbytes, data, chunksize);
1268 if (nbytes < 0) {
1269 BLOSC_TRACE_ERROR("Cannot decompress chunk ('%" PRId64 "').", nchunk);
1270 return BLOSC2_ERROR_FAILURE;
1271 }
1272 nbytes = chunk_stop - chunk_start;
1273 memcpy(dst_ptr, &data[chunk_start], nbytes);
1274 free(block_maskout);
1275 free(data);
1276 }
1277 else {
1278 /* Less than 1 block to read; use a getitem call */
1279 nbytes = blosc2_getitem_ctx(schunk->dctx, chunk, cbytes, (int32_t) (chunk_start / schunk->typesize),
1280 (chunk_stop - chunk_start) / schunk->typesize, dst_ptr, chunksize);
1281 if (nbytes < 0) {
1282 BLOSC_TRACE_ERROR("Cannot get item from ('%" PRId64 "') chunk.", nchunk);
1283 return BLOSC2_ERROR_FAILURE;
1284 }
1285 }
1286 }
1287
1288 dst_ptr += nbytes;
1289 nbytes_read += nbytes;
1290 nchunk++;
1291
1292 if (needs_free) {
1293 free(chunk);
1294 }
1295 chunk_start = 0;
1296 if (byte_stop >= (nchunk + 1) * chunksize) {
1297 chunk_stop = chunksize;
1298 }
1299 else {
1300 chunk_stop = (int32_t)(byte_stop % chunksize);
1301 }
1302 }
1303
1304 return BLOSC2_ERROR_SUCCESS;
1305 }
1306
1307
1308 int blosc2_schunk_set_slice_buffer(blosc2_schunk *schunk, int64_t start, int64_t stop, void *buffer) {
1309 int64_t byte_start = start * schunk->typesize;
1310 int64_t byte_stop = stop * schunk->typesize;
1311 int64_t nchunk_start = byte_start / schunk->chunksize;
1312 int32_t chunk_start = (int32_t) (byte_start % schunk->chunksize);
1313 int32_t chunk_stop;
1314 if (byte_stop >= (nchunk_start + 1) * schunk->chunksize) {
1315 chunk_stop = schunk->chunksize;
1316 }
1317 else {
1318 chunk_stop = (int32_t) (byte_stop % schunk->chunksize);
1319 }
1320
1321 uint8_t *src_ptr = (uint8_t *) buffer;
1322 int64_t nchunk = nchunk_start;
1323 int64_t nbytes_written = 0;
1324 int32_t nbytes;
1325 uint8_t *data = malloc(schunk->chunksize);
1326 int64_t nchunks;
1327 int32_t chunksize = schunk->chunksize;
1328
1329 while (nbytes_written < ((stop - start) * schunk->typesize)) {
1330 if (chunk_start == 0 &&
1331 (chunk_stop == schunk->chunksize || chunk_stop == schunk->nbytes % schunk->chunksize)) {
1332 if (chunk_stop == schunk->nbytes % schunk->chunksize) {
1333 chunksize = chunk_stop;
1334 }
1335 uint8_t *chunk = malloc(chunksize + BLOSC2_MAX_OVERHEAD);
1336 if (blosc2_compress_ctx(schunk->cctx, src_ptr, chunksize, chunk, chunksize + BLOSC2_MAX_OVERHEAD) < 0) {
1337 BLOSC_TRACE_ERROR("Cannot compress data of chunk ('%" PRId64 "').", nchunk);
1338 return BLOSC2_ERROR_FAILURE;
1339 }
1340 nchunks = blosc2_schunk_update_chunk(schunk, nchunk, chunk, false);
1341 if (nchunks != schunk->nchunks) {
1342 BLOSC_TRACE_ERROR("Cannot update chunk ('%" PRId64 "').", nchunk);
1343 return BLOSC2_ERROR_CHUNK_UPDATE;
1344 }
1345 }
1346 else {
1347 nbytes = blosc2_schunk_decompress_chunk(schunk, nchunk, data, schunk->chunksize);
1348 if (nbytes < 0) {
1349 BLOSC_TRACE_ERROR("Cannot decompress chunk ('%" PRId64 "').", nchunk);
1350 return BLOSC2_ERROR_FAILURE;
1351 }
1352 memcpy(&data[chunk_start], src_ptr, chunk_stop - chunk_start);
1353 uint8_t *chunk = malloc(nbytes + BLOSC2_MAX_OVERHEAD);
1354 if (blosc2_compress_ctx(schunk->cctx, data, nbytes, chunk, nbytes + BLOSC2_MAX_OVERHEAD) < 0) {
1355 BLOSC_TRACE_ERROR("Cannot compress data of chunk ('%" PRId64 "').", nchunk);
1356 return BLOSC2_ERROR_FAILURE;
1357 }
1358 nchunks = blosc2_schunk_update_chunk(schunk, nchunk, chunk, false);
1359 if (nchunks != schunk->nchunks) {
1360 BLOSC_TRACE_ERROR("Cannot update chunk ('%" PRId64 "').", nchunk);
1361 return BLOSC2_ERROR_CHUNK_UPDATE;
1362 }
1363 }
1364 nchunk++;
1365 nbytes_written += chunk_stop - chunk_start;
1366 src_ptr += chunk_stop - chunk_start;
1367 chunk_start = 0;
1368 if (byte_stop >= (nchunk + 1) * schunk->chunksize) {
1369 chunk_stop = schunk->chunksize;
1370 }
1371 else {
1372 chunk_stop = (int32_t) (byte_stop % schunk->chunksize);
1373 }
1374 }
1375 free(data);
1376
1377 return BLOSC2_ERROR_SUCCESS;
1378 }
1379
1380
1381 int schunk_get_slice_nchunks(blosc2_schunk *schunk, int64_t start, int64_t stop, int64_t **chunks_idx) {
1382 BLOSC_ERROR_NULL(schunk, BLOSC2_ERROR_NULL_POINTER);
1383
1384 int64_t byte_start = start * schunk->typesize;
1385 int64_t byte_stop = stop * schunk->typesize;
1386 int64_t nchunk_start = byte_start / schunk->chunksize;
1387 int64_t nchunk_stop = byte_stop / schunk->chunksize;
1388 if (byte_stop % schunk->chunksize != 0) {
1389 nchunk_stop++;
1390 }
1391 int64_t nchunk = nchunk_start;
1392 int nchunks = (int)(nchunk_stop - nchunk_start);
1393 *chunks_idx = malloc(nchunks * sizeof(int64_t));
1394 int64_t *ptr = *chunks_idx;
1395 for (int64_t i = 0; i < nchunks; ++i) {
1396 ptr[i] = nchunk;
1397 nchunk++;
1398 }
1399 return nchunks;
1400 }
1401
1402 /* Reorder the chunk offsets of an existing super-chunk. */
1403 int blosc2_schunk_reorder_offsets(blosc2_schunk *schunk, int64_t *offsets_order) {
1404 // Check that the offsets order are correct
1405 bool *index_check = (bool *) calloc(schunk->nchunks, sizeof(bool));
1406 for (int i = 0; i < schunk->nchunks; ++i) {
1407 int64_t index = offsets_order[i];
1408 if (index >= schunk->nchunks) {
1409 BLOSC_TRACE_ERROR("Index is bigger than the number of chunks.");
1410 free(index_check);
1411 return BLOSC2_ERROR_DATA;
1412 }
1413 if (index_check[index] == false) {
1414 index_check[index] = true;
1415 } else {
1416 BLOSC_TRACE_ERROR("Index is yet used.");
1417 free(index_check);
1418 return BLOSC2_ERROR_DATA;
1419 }
1420 }
1421 free(index_check);
1422
1423 blosc2_frame_s* frame = (blosc2_frame_s*)schunk->frame;
1424 if (frame != NULL) {
1425 return frame_reorder_offsets(frame, offsets_order, schunk);
1426 }
1427 uint8_t **offsets = schunk->data;
1428
1429 // Make a copy of the chunk offsets and reorder it
1430 uint8_t **offsets_copy = malloc(schunk->data_len);
1431 memcpy(offsets_copy, offsets, schunk->data_len);
1432
1433 for (int i = 0; i < schunk->nchunks; ++i) {
1434 offsets[i] = offsets_copy[offsets_order[i]];
1435 }
1436 free(offsets_copy);
1437
1438 return 0;
1439 }
1440
1441
1442 // Get the length (in bytes) of the internal frame of the super-chunk
1443 int64_t blosc2_schunk_frame_len(blosc2_schunk* schunk) {
1444 int64_t len;
1445 blosc2_frame_s* frame_s = (blosc2_frame_s*)(schunk->frame);
1446 if (frame_s != NULL) {
1447 len = frame_s->len;
1448 }
1449 else {
1450 // No attached frame, but we can still come with an estimate
1451 len = (int64_t) (schunk->cbytes + schunk->nchunks * sizeof(int64_t));
1452 }
1453
1454 return len;
1455 }
1456
1457
1458 /**
1459 * @brief Flush metalayers content into a possible attached frame.
1460 *
1461 * @param schunk The super-chunk to which the flush should be applied.
1462 *
1463 * @return If successful, a 0 is returned. Else, return a negative value.
1464 */
1465 // Initially, this was a public function, but as it is really meant to be used only
1466 // in the schunk_add_metalayer(), I decided to convert it into private and call it
1467 // implicitly instead of requiring the user to do so. The only drawback is that
1468 // each add operation requires a complete frame re-build, but as users should need
1469 // very few metalayers, this overhead should be negligible in practice.
1470 int metalayer_flush(blosc2_schunk* schunk) {
1471 int rc = BLOSC2_ERROR_SUCCESS;
1472 blosc2_frame_s* frame = (blosc2_frame_s*)schunk->frame;
1473 if (frame == NULL) {
1474 return rc;
1475 }
1476 rc = frame_update_header(frame, schunk, true);
1477 if (rc < 0) {
1478 BLOSC_TRACE_ERROR("Unable to update metalayers into frame.");
1479 return rc;
1480 }
1481 rc = frame_update_trailer(frame, schunk);
1482 if (rc < 0) {
1483 BLOSC_TRACE_ERROR("Unable to update trailer into frame.");
1484 return rc;
1485 }
1486 return rc;
1487 }
1488
1489
1490 /* Add content into a new metalayer.
1491 *
1492 * If successful, return the index of the new metalayer. Else, return a negative value.
1493 */
1494 int blosc2_meta_add(blosc2_schunk *schunk, const char *name, uint8_t *content, int32_t content_len) {
1495 int nmetalayer = blosc2_meta_exists(schunk, name);
1496 if (nmetalayer >= 0) {
1497 BLOSC_TRACE_ERROR("Metalayer \"%s\" already exists.", name);
1498 return BLOSC2_ERROR_INVALID_PARAM;
1499 }
1500
1501 // Add the metalayer
1502 blosc2_metalayer *metalayer = malloc(sizeof(blosc2_metalayer));
1503 char* name_ = malloc(strlen(name) + 1);
1504 strcpy(name_, name);
1505 metalayer->name = name_;
1506 uint8_t* content_buf = malloc((size_t)content_len);
1507 memcpy(content_buf, content, content_len);
1508 metalayer->content = content_buf;
1509 metalayer->content_len = content_len;
1510 schunk->metalayers[schunk->nmetalayers] = metalayer;
1511 schunk->nmetalayers += 1;
1512
1513 int rc = metalayer_flush(schunk);
1514 if (rc < 0) {
1515 return rc;
1516 }
1517
1518 return schunk->nmetalayers - 1;
1519 }
1520
1521
1522 /* Update the content of an existing metalayer.
1523 *
1524 * If successful, return the index of the new metalayer. Else, return a negative value.
1525 */
1526 int blosc2_meta_update(blosc2_schunk *schunk, const char *name, uint8_t *content, int32_t content_len) {
1527 int nmetalayer = blosc2_meta_exists(schunk, name);
1528 if (nmetalayer < 0) {
1529 BLOSC_TRACE_ERROR("Metalayer \"%s\" not found.", name);
1530 return nmetalayer;
1531 }
1532
1533 blosc2_metalayer *metalayer = schunk->metalayers[nmetalayer];
1534 if (content_len > metalayer->content_len) {
1535 BLOSC_TRACE_ERROR("`content_len` cannot exceed the existing size of %d bytes.", metalayer->content_len);
1536 return nmetalayer;
1537 }
1538
1539 // Update the contents of the metalayer
1540 memcpy(metalayer->content, content, content_len);
1541
1542 // Update the metalayers in frame (as size has not changed, we don't need to update the trailer)
1543 blosc2_frame_s* frame = (blosc2_frame_s*)schunk->frame;
1544 if (frame != NULL) {
1545 int rc = frame_update_header(frame, schunk, false);
1546 if (rc < 0) {
1547 BLOSC_TRACE_ERROR("Unable to update meta info from frame.");
1548 return rc;
1549 }
1550 }
1551
1552 return nmetalayer;
1553 }
1554
1555
1556 /* Find whether the schunk has a variable-length metalayer or not.
1557 *
1558 * If successful, return the index of the variable-length metalayer. Else, return a negative value.
1559 */
1560 int blosc2_vlmeta_exists(blosc2_schunk *schunk, const char *name) {
1561 if (strlen(name) > BLOSC2_METALAYER_NAME_MAXLEN) {
1562 BLOSC_TRACE_ERROR("Variable-length metalayer names cannot be larger than %d chars.", BLOSC2_METALAYER_NAME_MAXLEN);
1563 return BLOSC2_ERROR_INVALID_PARAM;
1564 }
1565
1566 for (int nvlmetalayer = 0; nvlmetalayer < schunk->nvlmetalayers; nvlmetalayer++) {
1567 if (strcmp(name, schunk->vlmetalayers[nvlmetalayer]->name) == 0) {
1568 return nvlmetalayer;
1569 }
1570 }
1571 return BLOSC2_ERROR_NOT_FOUND;
1572 }
1573
1574 int vlmetalayer_flush(blosc2_schunk* schunk) {
1575 int rc = BLOSC2_ERROR_SUCCESS;
1576 blosc2_frame_s* frame = (blosc2_frame_s*)schunk->frame;
1577 if (frame == NULL) {
1578 return rc;
1579 }
1580 rc = frame_update_header(frame, schunk, false);
1581 if (rc < 0) {
1582 BLOSC_TRACE_ERROR("Unable to update metalayers into frame.");
1583 return rc;
1584 }
1585 rc = frame_update_trailer(frame, schunk);
1586 if (rc < 0) {
1587 BLOSC_TRACE_ERROR("Unable to update trailer into frame.");
1588 return rc;
1589 }
1590 return rc;
1591 }
1592
1593 /* Add content into a new variable-length metalayer.
1594 *
1595 * If successful, return the index of the new variable-length metalayer. Else, return a negative value.
1596 */
1597 int blosc2_vlmeta_add(blosc2_schunk *schunk, const char *name, uint8_t *content, int32_t content_len,
1598 blosc2_cparams *cparams) {
1599 int nvlmetalayer = blosc2_vlmeta_exists(schunk, name);
1600 if (nvlmetalayer >= 0) {
1601 BLOSC_TRACE_ERROR("Variable-length metalayer \"%s\" already exists.", name);
1602 return BLOSC2_ERROR_INVALID_PARAM;
1603 }
1604
1605 // Add the vlmetalayer
1606 blosc2_metalayer *vlmetalayer = malloc(sizeof(blosc2_metalayer));
1607 vlmetalayer->name = strdup(name);
1608 uint8_t* content_buf = malloc((size_t) content_len + BLOSC2_MAX_OVERHEAD);
1609
1610 blosc2_context *cctx;
1611 if (cparams != NULL) {
1612 cctx = blosc2_create_cctx(*cparams);
1613 } else {
1614 cctx = blosc2_create_cctx(BLOSC2_CPARAMS_DEFAULTS);
1615 }
1616 if (cctx == NULL) {
1617 BLOSC_TRACE_ERROR("Error while creating the compression context");
1618 return BLOSC2_ERROR_NULL_POINTER;
1619 }
1620
1621 int csize = blosc2_compress_ctx(cctx, content, content_len, content_buf, content_len + BLOSC2_MAX_OVERHEAD);
1622 if (csize < 0) {
1623 BLOSC_TRACE_ERROR("Can not compress the `%s` variable-length metalayer.", name);
1624 return csize;
1625 }
1626 blosc2_free_ctx(cctx);
1627
1628 vlmetalayer->content = realloc(content_buf, csize);
1629 vlmetalayer->content_len = csize;
1630 schunk->vlmetalayers[schunk->nvlmetalayers] = vlmetalayer;
1631 schunk->nvlmetalayers += 1;
1632
1633 // Propagate to frames
1634 int rc = vlmetalayer_flush(schunk);
1635 if (rc < 0) {
1636 BLOSC_TRACE_ERROR("Can not propagate de `%s` variable-length metalayer to a frame.", name);
1637 return rc;
1638 }
1639
1640 return schunk->nvlmetalayers - 1;
1641 }
1642
1643
1644 int blosc2_vlmeta_get(blosc2_schunk *schunk, const char *name, uint8_t **content,
1645 int32_t *content_len) {
1646 int nvlmetalayer = blosc2_vlmeta_exists(schunk, name);
1647 if (nvlmetalayer < 0) {
1648 BLOSC_TRACE_ERROR("User metalayer \"%s\" not found.", name);
1649 return nvlmetalayer;
1650 }
1651 blosc2_metalayer *meta = schunk->vlmetalayers[nvlmetalayer];
1652 int32_t nbytes, cbytes;
1653 blosc2_cbuffer_sizes(meta->content, &nbytes, &cbytes, NULL);
1654 if (cbytes != meta->content_len) {
1655 BLOSC_TRACE_ERROR("User metalayer \"%s\" is corrupted.", meta->name);
1656 return BLOSC2_ERROR_DATA;
1657 }
1658 *content_len = nbytes;
1659 *content = malloc((size_t) nbytes);
1660 blosc2_context *dctx = blosc2_create_dctx(*schunk->storage->dparams);
1661 if (dctx == NULL) {
1662 BLOSC_TRACE_ERROR("Error while creating the decompression context");
1663 return BLOSC2_ERROR_NULL_POINTER;
1664 }
1665 int nbytes_ = blosc2_decompress_ctx(dctx, meta->content, meta->content_len, *content, nbytes);
1666 blosc2_free_ctx(dctx);
1667 if (nbytes_ != nbytes) {
1668 BLOSC_TRACE_ERROR("User metalayer \"%s\" is corrupted.", meta->name);
1669 return BLOSC2_ERROR_READ_BUFFER;
1670 }
1671 return nvlmetalayer;
1672 }
1673
1674 int blosc2_vlmeta_update(blosc2_schunk *schunk, const char *name, uint8_t *content, int32_t content_len,
1675 blosc2_cparams *cparams) {
1676 int nvlmetalayer = blosc2_vlmeta_exists(schunk, name);
1677 if (nvlmetalayer < 0) {
1678 BLOSC_TRACE_ERROR("User vlmetalayer \"%s\" not found.", name);
1679 return nvlmetalayer;
1680 }
1681
1682 blosc2_metalayer *vlmetalayer = schunk->vlmetalayers[nvlmetalayer];
1683 free(vlmetalayer->content);
1684 uint8_t* content_buf = malloc((size_t) content_len + BLOSC2_MAX_OVERHEAD);
1685
1686 blosc2_context *cctx;
1687 if (cparams != NULL) {
1688 cctx = blosc2_create_cctx(*cparams);
1689 } else {
1690 cctx = blosc2_create_cctx(BLOSC2_CPARAMS_DEFAULTS);
1691 }
1692 if (cctx == NULL) {
1693 BLOSC_TRACE_ERROR("Error while creating the compression context");
1694 return BLOSC2_ERROR_NULL_POINTER;
1695 }
1696
1697 int csize = blosc2_compress_ctx(cctx, content, content_len, content_buf, content_len + BLOSC2_MAX_OVERHEAD);
1698 if (csize < 0) {
1699 BLOSC_TRACE_ERROR("Can not compress the `%s` variable-length metalayer.", name);
1700 return csize;
1701 }
1702 blosc2_free_ctx(cctx);
1703
1704 vlmetalayer->content = realloc(content_buf, csize);
1705 vlmetalayer->content_len = csize;
1706
1707 // Propagate to frames
1708 int rc = vlmetalayer_flush(schunk);
1709 if (rc < 0) {
1710 BLOSC_TRACE_ERROR("Can not propagate de `%s` variable-length metalayer to a frame.", name);
1711 return rc;
1712 }
1713
1714 return nvlmetalayer;
1715 }
1716
1717 int blosc2_vlmeta_delete(blosc2_schunk *schunk, const char *name) {
1718 int nvlmetalayer = blosc2_vlmeta_exists(schunk, name);
1719 if (nvlmetalayer < 0) {
1720 BLOSC_TRACE_ERROR("User vlmetalayer \"%s\" not found.", name);
1721 return nvlmetalayer;
1722 }
1723
1724 blosc2_metalayer *vlmetalayer = schunk->vlmetalayers[nvlmetalayer];
1725 for (int i = nvlmetalayer; i < (schunk->nvlmetalayers - 1); i++) {
1726 schunk->vlmetalayers[i] = schunk->vlmetalayers[i + 1];
1727 }
1728 free(vlmetalayer->content);
1729 schunk->nvlmetalayers--;
1730
1731 // Propagate to frames
1732 int rc = vlmetalayer_flush(schunk);
1733 if (rc < 0) {
1734 BLOSC_TRACE_ERROR("Can not propagate de `%s` variable-length metalayer to a frame.", name);
1735 return rc;
1736 }
1737
1738 return schunk->nvlmetalayers;
1739 }
1740
1741
1742 int blosc2_vlmeta_get_names(blosc2_schunk *schunk, char **names) {
1743 int16_t nvlmetalayers = schunk->nvlmetalayers;
1744
1745 for (int i = 0; i < nvlmetalayers; ++i) {
1746 names[i] = schunk->vlmetalayers[i]->name;
1747 }
1748
1749 return nvlmetalayers;
1750 }