Coverage Report

Created: 2026-09-03 06:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/c-blosc2/blosc/context.h
Line
Count
Source
1
/*********************************************************************
2
  Blosc - Blocked Shuffling and Compression Library
3
4
  Copyright (c) 2021  Blosc Development Team <blosc@blosc.org>
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
#ifndef BLOSC_CONTEXT_H
12
#define BLOSC_CONTEXT_H
13
14
#include "b2nd.h"
15
#include "blosc2.h"
16
17
#if defined(HAVE_ZSTD)
18
#include "zstd.h"
19
#endif
20
21
#include <threading.h>
22
23
#include <stddef.h>
24
#include <stdint.h>
25
26
/* Have problems using posix barriers when symbol value is 200112L */
27
/* Requires more investigation, but this will work for the moment */
28
#if defined(_POSIX_BARRIERS) && ( (_POSIX_BARRIERS - 20012L) >= 0 && _POSIX_BARRIERS != 200112L)
29
#define BLOSC_POSIX_BARRIERS
30
#endif
31
32
275k
#define BLOSC_BACKEND_SERIAL 0
33
551k
#define BLOSC_BACKEND_SHARED_POOL 1
34
209k
#define BLOSC_BACKEND_CALLBACK 2
35
120k
#define BLOSC_BACKEND_PER_CONTEXT 3   /* per-context threads; used on Windows */
36
37
struct blosc2_context_s {
38
  const uint8_t* src;  /* The source buffer */
39
  uint8_t* dest;  /* The destination buffer */
40
  uint8_t header_flags;  /* Flags for header */
41
  uint8_t blosc2_flags;  /* Flags specific for blosc2 */
42
  uint8_t blosc2_flags2;  /* Secondary flags specific for blosc2 */
43
  int32_t sourcesize;  /* Number of bytes in source buffer */
44
  int32_t header_overhead;  /* The number of bytes in chunk header */
45
  int32_t nblocks;  /* Number of total blocks in buffer */
46
  int32_t leftover;  /* Extra bytes at end of buffer */
47
  int32_t blocksize;  /* Length of the block in bytes */
48
  int32_t header_blocksize;  /* Raw blocksize field as stored in the chunk header */
49
  int32_t splitmode;  /* Whether the blocks should be split or not */
50
  int32_t output_bytes;  /* Counter for the number of input bytes */
51
  int32_t srcsize;  /* Counter for the number of output bytes */
52
  int32_t destsize;  /* Maximum size for destination buffer */
53
  int32_t typesize;  /* Type size */
54
  int32_t* bstarts;  /* Starts for every block inside the compressed buffer */
55
  int32_t* blocknbytes;  /* Uncompressed sizes for blocks in VL-block chunks */
56
  int32_t* blockoffsets;  /* Uncompressed offsets for blocks in VL-block chunks */
57
  int32_t* blockcbytes;  /* Compressed byte spans for blocks in VL-block chunks */
58
  const uint8_t** vlblock_sources;  /* Per-block sources when compressing VL-block chunks */
59
  uint8_t** vlblock_dests;  /* Per-block destinations when decompressing VL-block chunks */
60
  int32_t special_type;  /* Special type for chunk.  0 if not special. */
61
  int compcode;  /* Compressor code to use */
62
  uint8_t compcode_meta;  /* The metainfo for the compressor code */
63
  int clevel;  /* Compression level (1-9) */
64
  int use_dict;  /* Whether to use dicts or not */
65
  void* dict_buffer;  /* The buffer to keep the trained dictionary */
66
  int32_t dict_size;  /* The size of the trained dictionary */
67
  void* dict_cdict;  /* The dictionary in digested form for compression */
68
  void* dict_ddict;  /* The dictionary in digested form for decompression */
69
  uint8_t filter_flags;  /* The filter flags in the filter pipeline */
70
  uint8_t filters[BLOSC2_MAX_FILTERS];  /* The (sequence of) filters */
71
  uint8_t filters_meta[BLOSC2_MAX_FILTERS];  /* The metainfo for filters */
72
  blosc2_filter urfilters[BLOSC2_MAX_UDFILTERS];  /* The user-defined filters */
73
  blosc2_prefilter_fn prefilter;  /* prefilter function */
74
  blosc2_postfilter_fn postfilter;  /* postfilter function */
75
  blosc2_prefilter_params *preparams;  /* prefilter params */
76
  blosc2_postfilter_params *postparams;  /* postfilter params */
77
  bool* block_maskout;  /* The blocks that are not meant to be decompressed.
78
                         * If NULL (default), all blocks in a chunk should be read. */
79
  int block_maskout_nitems;  /* The number of items in block_maskout array (must match
80
                              * the number of blocks in chunk) */
81
  blosc2_schunk* schunk;  /* Associated super-chunk (if available) */
82
  struct thread_context* serial_context;  /* Cache for temporaries for serial operation */
83
  int do_compress;  /* 1 if we are compressing, 0 if decompressing */
84
  void *tuner_params;  /* Entry point for tuner persistence between runs */
85
  int tuner_id;  /* User-defined tuner id */
86
  void *codec_params; /* User defined parameters for the codec */
87
  void *filter_params[BLOSC2_MAX_FILTERS]; /* User defined parameters for the filters */
88
  /* Threading */
89
  int16_t nthreads;
90
  int16_t new_nthreads;
91
  int16_t thread_backend;
92
  int16_t threads_started;
93
  struct thread_context *thread_contexts;  /* Only for callback-managed threads */
94
  struct blosc_shared_pool *thread_pool;
95
  int32_t pool_epoch;  /* value of g_destroy_count when pool was attached */
96
  struct blosc_job_group *job;
97
  blosc2_pthread_mutex_t count_mutex;
98
  blosc2_pthread_mutex_t nchunk_mutex;
99
  int thread_giveup_code;  /* error code when give up */
100
  int dref_not_init;  /* data ref in delta not initialized */
101
  blosc2_pthread_mutex_t delta_mutex;
102
  blosc2_pthread_cond_t delta_cv;
103
  bool dict_buffer_owned;  /* Whether dict_buffer must be freed by the context */
104
  /* Per-context worker threads (Windows only; BLOSC_BACKEND_PER_CONTEXT) */
105
  int16_t end_threads;                   /* set to 1 to signal workers to exit */
106
  uint32_t job_seq;                      /* incremented each new job dispatch */
107
  int16_t active_workers;               /* workers still processing current job */
108
  int32_t thread_nblock;               /* next block index for dynamic scheduling */
109
  blosc2_pthread_t *threads;            /* per-context thread handles */
110
  blosc2_pthread_mutex_t jobs_mutex;    /* guards job_seq, end_threads, active_workers */
111
  blosc2_pthread_cond_t jobs_ready;     /* workers sleep here between jobs */
112
  blosc2_pthread_cond_t jobs_done;      /* main sleeps here until job completes */
113
  // Add new fields here to avoid breaking the ABI.
114
};
115
116
struct b2nd_context_s {
117
  int8_t ndim;
118
  //!< The array dimensions.
119
  int64_t shape[B2ND_MAX_DIM];
120
  //!< The array shape.
121
  int32_t chunkshape[B2ND_MAX_DIM];
122
  //!< The shape of each chunk of Blosc.
123
  int32_t blockshape[B2ND_MAX_DIM];
124
  //!< The shape of each block of Blosc.
125
  char *dtype;
126
  //!< Data type. Different formats can be supported (see dtype_format).
127
  int8_t dtype_format;
128
  //!< The format of the data type.  Default is 0 (NumPy).
129
  blosc2_storage *b2_storage;
130
  //!< The Blosc storage properties
131
  blosc2_metalayer metalayers[B2ND_MAX_METALAYERS];
132
  //!< List with the metalayers desired.
133
  int32_t nmetalayers;
134
  //!< The number of metalayers.
135
};
136
137
struct thread_context {
138
  blosc2_context* parent_context;
139
  struct blosc_shared_pool* owner_pool;
140
  int tid;
141
  uint8_t* tmp;
142
  uint8_t* tmp2;
143
  uint8_t* tmp3;
144
  uint8_t* tmp4;
145
  int32_t tmp_blocksize;  /* the blocksize for different temporaries */
146
  size_t tmp_nbytes;   /* keep track of how big the temporary buffers are */
147
  int32_t zfp_cell_start;  /* cell starter index for ZFP fixed-rate mode */
148
  int32_t zfp_cell_nitems;  /* number of items to get for ZFP fixed-rate mode */
149
#if defined(HAVE_ZSTD)
150
  /* The contexts for ZSTD */
151
  ZSTD_CCtx* zstd_cctx;
152
  ZSTD_DCtx* zstd_dctx;
153
#endif /* HAVE_ZSTD */
154
  /* Working streams for LZ4/LZ4HC dictionary compression */
155
  void* lz4_cstream;   /* LZ4_stream_t* pre-loaded with dict; NULL when no dict active */
156
  void* lz4hc_cstream; /* LZ4_streamHC_t* pre-loaded with dict; NULL when no dict active */
157
  uint32_t my_job_seq;  /* last job_seq processed; used by BLOSC_BACKEND_PER_CONTEXT on Windows */
158
};
159
160
0
static inline bool ctx_uses_parallel_backend(const blosc2_context *context) {
161
0
  return context != NULL && context->thread_backend != BLOSC_BACKEND_SERIAL && context->nthreads > 1;
162
0
}
Unexecuted instantiation: blosc2.c:ctx_uses_parallel_backend
Unexecuted instantiation: blosclz.c:ctx_uses_parallel_backend
Unexecuted instantiation: schunk.c:ctx_uses_parallel_backend
Unexecuted instantiation: frame.c:ctx_uses_parallel_backend
Unexecuted instantiation: stune.c:ctx_uses_parallel_backend
Unexecuted instantiation: b2nd.c:ctx_uses_parallel_backend
Unexecuted instantiation: blosc2-zfp.c:ctx_uses_parallel_backend
163
164
#endif  /* BLOSC_CONTEXT_H */