bufr2synop 0.24.0
Functions
bufrdeco_memory.c File Reference

This file has the memory stufs for library bufrdeco. More...

#include "bufrdeco.h"
Include dependency graph for bufrdeco_memory.c:

Go to the source code of this file.

Functions

int bufrdeco_init_tables (struct bufr_tables **t)
 Init a struct bufr_tables allocating space. More...
 
int bufrdeco_free_tables (struct bufr_tables **t)
 Frees the allocated space for a struct bufr_tables. More...
 
int bufrdeco_init_expanded_tree (struct bufrdeco_expanded_tree **t)
 Init a struct bufrdeco_expanded_tree allocating space. More...
 
int bufrdeco_free_expanded_tree (struct bufrdeco_expanded_tree **t)
 Frees the allocated space for a struct bufrdeco_expanded_tree. More...
 
int bufrdeco_substitute_tables (struct bufr_tables **replaced, struct bufr_tables *source, struct bufrdeco *b)
 substitute an struct bufr_tables into a struct bufrdeco More...
 
int bufrdeco_init_subset_sequence_data (struct bufrdeco_subset_sequence_data *ba)
 Init a struct bufrdeco_subset_sequence_data. More...
 
int bufrdeco_clean_subset_sequence_data (struct bufrdeco_subset_sequence_data *ba)
 Cleans a struct bufrdeco_subset_sequence_data. More...
 
int bufrdeco_free_subset_sequence_data (struct bufrdeco_subset_sequence_data *ba)
 Free the memory for sequence array in a struct bufrdeco_subset_sequence_data. More...
 
int bufrdeco_init_compressed_data_references (struct bufrdeco_compressed_data_references *rf)
 Init a struct bufrdeco_compressed_data_references. More...
 
int bufrdeco_clean_compressed_data_references (struct bufrdeco_compressed_data_references *rf)
 Clean a struct bufrdeco_compressed_data_references. More...
 
int bufrdeco_free_compressed_data_references (struct bufrdeco_compressed_data_references *rf)
 Free the memory allocated for array of references in a struct bufrdeco_compressed_data_references. More...
 
int bufrdeco_allocate_bitmap (struct bufrdeco *b)
 allocate bitmap More...
 
int bufrdeco_clean_bitmaps (struct bufrdeco *b)
 Clean all allocated bitmaps, but still is in memory. More...
 
int bufrdeco_free_bitmap_array (struct bufrdeco_bitmap_array *a)
 Free an allocated bitmap array. More...
 

Detailed Description

This file has the memory stufs for library bufrdeco.

Definition in file bufrdeco_memory.c.

Function Documentation

◆ bufrdeco_allocate_bitmap()

int bufrdeco_allocate_bitmap ( struct bufrdeco b)

allocate bitmap

Parameters
bthe active struct bufrdeco
Returns
If succeeded return 0, otherwise 1

Definition at line 269 of file bufrdeco_memory.c.

270{
271 buf_t nba;
272
273 bufrdeco_assert ( b != NULL );
274
275 nba = b->bitmap.nba;
276
277 if ( nba < BUFR_MAX_BITMAPS )
278 {
279 if ( b->bitmap.bmap[nba] != NULL )
280 {
281 // the bitmap already is allocated
282 return 0;
283 }
284 // let's try to allocate it!
285 if ( ( b->bitmap.bmap[nba] = ( struct bufrdeco_bitmap * ) calloc ( 1, sizeof ( struct bufrdeco_bitmap ) ) ) == NULL )
286 {
287 snprintf ( b->error, sizeof ( b->error ),"%s(): Cannot allocate space for struct bufrdeco_bitmap\n", __func__ );
288 return 1;
289 }
290 // Update de counter
291 ( b->bitmap.nba )++;
292 return 0;
293 }
294 else
295 {
296 snprintf ( b->error, sizeof ( b->error ),"%s(): Too much bitmaps to allocate. The limit is %d\n", __func__, BUFR_MAX_BITMAPS );
297 return 1;
298 }
299}
uint32_t buf_t
Type to set offsets and dimension of arrays or counters used in bufrdeco.
Definition: bufrdeco.h:346
#define bufrdeco_assert(__my_expr__)
Check a expression and exit if it fails.
Definition: bufrdeco.h:374
#define BUFR_MAX_BITMAPS
Max number of structs bufrdeco_bitmap that can be allocated in a struct bufrdeco_bitmap_array.
Definition: bufrdeco.h:238
struct bufrdeco_bitmap * bmap[BUFR_MAX_BITMAPS]
Definition: bufrdeco.h:493
Stores all needed data for a bufr bitmap.
Definition: bufrdeco.h:470
struct bufrdeco_bitmap_array bitmap
Definition: bufrdeco.h:980
char error[1024]
Definition: bufrdeco.h:983

References bufrdeco::bitmap, bufrdeco_bitmap_array::bmap, BUFR_MAX_BITMAPS, bufrdeco_assert, bufrdeco::error, and bufrdeco_bitmap_array::nba.

Referenced by bufrdeco_parse_f2_compressed(), and bufrdeco_parse_f2_descriptor().

Here is the caller graph for this function:

◆ bufrdeco_clean_bitmaps()

int bufrdeco_clean_bitmaps ( struct bufrdeco b)

Clean all allocated bitmaps, but still is in memory.

Parameters
bthe active struct bufrdeco
Returns
If succeeded return 0, otherwise 1

Definition at line 308 of file bufrdeco_memory.c.

309{
310 buf_t i;
311
312 bufrdeco_assert ( b != NULL );
313
314 for ( i = 0; i < b->bitmap.nba ; i++ )
315 {
316 if ( b->bitmap.bmap[i] == NULL )
317 continue;
318 memset ( b->bitmap.bmap[i], 0, sizeof ( struct bufrdeco_bitmap ) );
319 }
320 b->bitmap.nba = 0;
321 return 0;
322}

References bufrdeco::bitmap, bufrdeco_bitmap_array::bmap, bufrdeco_assert, and bufrdeco_bitmap_array::nba.

Referenced by bufrdeco_decode_data_subset().

Here is the caller graph for this function:

◆ bufrdeco_clean_compressed_data_references()

int bufrdeco_clean_compressed_data_references ( struct bufrdeco_compressed_data_references rf)

Clean a struct bufrdeco_compressed_data_references.

Parameters
rfpointer to the target struct bufrdeco_compressed_data_references to clean
Returns
If succeeded return 0, otherwise 1

Definition at line 233 of file bufrdeco_memory.c.

234{
235 bufrdeco_assert ( rf != NULL );
236
237 if ( rf->refs != NULL && rf->nd != 0 )
238 rf->nd = 0;
239 else
241 return 0;
242}
int bufrdeco_init_compressed_data_references(struct bufrdeco_compressed_data_references *rf)
Init a struct bufrdeco_compressed_data_references.
struct bufrdeco_compressed_ref * refs
Definition: bufrdeco.h:671

References bufrdeco_assert, bufrdeco_init_compressed_data_references(), bufrdeco_compressed_data_references::nd, and bufrdeco_compressed_data_references::refs.

Here is the call graph for this function:

◆ bufrdeco_clean_subset_sequence_data()

int bufrdeco_clean_subset_sequence_data ( struct bufrdeco_subset_sequence_data ba)

Cleans a struct bufrdeco_subset_sequence_data.

Parameters
baPointer to struct bufrdeco_subset_sequence_data to clean
Returns
0 if succeeded, 1 otherwise

For eficience, if sequence in the struct bufrdeco_subset_sequence_data is allocated, just set the used elements to zero. If is still no allocated memory for sequence inits the struct

Definition at line 164 of file bufrdeco_memory.c.

165{
166 bufrdeco_assert ( ba != NULL );
167
168 if ( ba->sequence != NULL )
169 {
170 ba->nd = 0;
171 return 0;
172 }
173 else
175}
int bufrdeco_init_subset_sequence_data(struct bufrdeco_subset_sequence_data *ba)
Init a struct bufrdeco_subset_sequence_data.
struct bufr_atom_data * sequence
Definition: bufrdeco.h:462

References bufrdeco_assert, bufrdeco_init_subset_sequence_data(), bufrdeco_subset_sequence_data::nd, and bufrdeco_subset_sequence_data::sequence.

Referenced by bufrdeco_decode_data_subset().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ bufrdeco_free_bitmap_array()

int bufrdeco_free_bitmap_array ( struct bufrdeco_bitmap_array a)

Free an allocated bitmap array.

Parameters
apointer to target struct bufrdeco_bitmap_array to free
Returns
If succeeded return 0, otherwise 1

Definition at line 331 of file bufrdeco_memory.c.

332{
333 buf_t i;
334
335 bufrdeco_assert ( a != NULL );
336
337 for ( i = 0; i < a->nba ; i++ )
338 {
339 if ( a->bmap[i] == NULL )
340 continue;
341
342 free ( ( void* ) a->bmap[i] );
343 a->bmap[i] = NULL;
344 }
345 a->nba = 0;
346 return 0;
347}

References bufrdeco_bitmap_array::bmap, bufrdeco_assert, and bufrdeco_bitmap_array::nba.

Referenced by bufrdeco_close(), and bufrdeco_reset().

Here is the caller graph for this function:

◆ bufrdeco_free_compressed_data_references()

int bufrdeco_free_compressed_data_references ( struct bufrdeco_compressed_data_references rf)

Free the memory allocated for array of references in a struct bufrdeco_compressed_data_references.

Parameters
rfpointer to the target struct bufrdeco_compressed_data_references to free
Returns
If succeeded return 0, otherwise 1

Definition at line 250 of file bufrdeco_memory.c.

251{
252 bufrdeco_assert ( rf != NULL );
253
254 if ( rf->refs != NULL )
255 {
256 free ( ( void * ) rf->refs );
257 rf->refs = NULL;
258 }
259 return 0;
260}

References bufrdeco_assert, and bufrdeco_compressed_data_references::refs.

Referenced by bufrdeco_close(), and bufrdeco_reset().

Here is the caller graph for this function:

◆ bufrdeco_free_expanded_tree()

int bufrdeco_free_expanded_tree ( struct bufrdeco_expanded_tree **  t)

Frees the allocated space for a struct bufrdeco_expanded_tree.

Parameters
tpointer to the target pointer to struct bufrdeco_expanded_tree
Returns
0 and *t is set to NULL

Definition at line 89 of file bufrdeco_memory.c.

90{
91 bufrdeco_assert ( t != NULL );
92
93 if ( *t != NULL )
94 {
95 free ( ( void * ) *t );
96 *t = NULL;
97 }
98 return 0;
99}

References bufrdeco_assert.

Referenced by bufrdeco_close(), and bufrdeco_reset().

Here is the caller graph for this function:

◆ bufrdeco_free_subset_sequence_data()

int bufrdeco_free_subset_sequence_data ( struct bufrdeco_subset_sequence_data ba)

Free the memory for sequence array in a struct bufrdeco_subset_sequence_data.

Parameters
bapointer to the target struct to free
Returns
0 if succeeded

Definition at line 183 of file bufrdeco_memory.c.

184{
185 bufrdeco_assert ( ba != NULL );
186
187 if ( ba->sequence != NULL )
188 {
189 free ( ( void * ) ba->sequence );
190 ba->sequence = NULL;
191 }
192 return 0;
193}

References bufrdeco_assert, and bufrdeco_subset_sequence_data::sequence.

Referenced by bufrdeco_close(), and bufrdeco_reset().

Here is the caller graph for this function:

◆ bufrdeco_free_tables()

int bufrdeco_free_tables ( struct bufr_tables **  t)

Frees the allocated space for a struct bufr_tables.

Parameters
tpointer to the target pointer to struct bufr_tables
Returns
0 and *t is set to NULL

Definition at line 51 of file bufrdeco_memory.c.

52{
53 bufrdeco_assert ( t != NULL );
54
55 if ( *t != NULL )
56 {
57 free ( ( void * ) *t );
58 *t = NULL;
59 }
60 return 0;
61}

References bufrdeco_assert.

Referenced by bufrdeco_close().

Here is the caller graph for this function:

◆ bufrdeco_init_compressed_data_references()

int bufrdeco_init_compressed_data_references ( struct bufrdeco_compressed_data_references rf)

Init a struct bufrdeco_compressed_data_references.

Parameters
rfpointer ti the target struct
Returns
0 if succeded, otherwise 1

If already memory is allocated for array of references then just adjust the used index to zero. Otherwise it allocate the needed memory and init the struct

Definition at line 205 of file bufrdeco_memory.c.

206{
207 bufrdeco_assert ( rf != NULL );
208
209 if ( rf->refs != NULL && rf->dim != 0 )
210 {
211 rf->nd = 0; // Here we set the used elements to 0 of dim
212 }
213 else if ( rf->refs == NULL )
214 {
215 // Here memory is still not allocated. Proceed to allocate with BUFR_NMAXSEQ
216 if ( ( rf->refs = ( struct bufrdeco_compressed_ref * ) calloc ( 1, BUFR_NMAXSEQ * sizeof ( struct bufrdeco_compressed_ref ) ) ) == NULL )
217 {
218 fprintf ( stderr,"%s(): Cannot allocate memory for bufrdeco_compressed_ref array\n", __func__ );
219 return 1;
220 }
221 rf->nd = 0; // Set de used elements (bufrdeco_compressed_ref)
222 rf->dim = BUFR_NMAXSEQ; // Set de allocated bufr_compressed_rer elements
223 }
224 return 0;
225}
#define BUFR_NMAXSEQ
Maximum expected descriptors in a expanded sequence for a single subset.
Definition: bufrdeco.h:116
Struct to hold the needed reference bit offsets, descriptor tree and replications in a compressed BUF...
Definition: bufrdeco.h:639

References BUFR_NMAXSEQ, bufrdeco_assert, bufrdeco_compressed_data_references::dim, bufrdeco_compressed_data_references::nd, and bufrdeco_compressed_data_references::refs.

Referenced by bufrdeco_clean_compressed_data_references(), and bufrdeco_parse_compressed().

Here is the caller graph for this function:

◆ bufrdeco_init_expanded_tree()

int bufrdeco_init_expanded_tree ( struct bufrdeco_expanded_tree **  t)

Init a struct bufrdeco_expanded_tree allocating space.

Parameters
tpointer to the target pointer to struct bufrdeco_expanded_tree
Returns
0 if succeeded, 1 otherwise

Definition at line 70 of file bufrdeco_memory.c.

71{
72 bufrdeco_assert ( t != NULL );
73
74 if ( *t != NULL )
75 free ( ( void * ) *t );
76
77 if ( ( *t = ( struct bufrdeco_expanded_tree * ) calloc ( 1, sizeof ( struct bufrdeco_expanded_tree ) ) ) == NULL )
78 return 1;
79 return 0;
80}
Array of structs bufr_sequence from which bufr expanded tree is made.
Definition: bufrdeco.h:596

References bufrdeco_assert.

Referenced by bufrdeco_init(), and bufrdeco_reset().

Here is the caller graph for this function:

◆ bufrdeco_init_subset_sequence_data()

int bufrdeco_init_subset_sequence_data ( struct bufrdeco_subset_sequence_data ba)

Init a struct bufrdeco_subset_sequence_data.

Parameters
bapointer to the target struct
Returns
0 if succeeded, 1 otherwise

It is supossed that no memory is allocated for sequence. If we are not sure better use function bufrdeco_clean_subset_sequence_data

Definition at line 140 of file bufrdeco_memory.c.

141{
142 bufrdeco_assert ( ba != NULL );
143
144 memset ( ba, 0, sizeof ( struct bufrdeco_subset_sequence_data ) );
145 if ( ( ba->sequence = ( struct bufr_atom_data * ) calloc ( 1, BUFR_NMAXSEQ * sizeof ( struct bufr_atom_data ) ) ) == NULL )
146 {
147 fprintf ( stderr,"%s():Cannot allocate memory for atom data array\n", __func__ );
148 return 1;
149 }
150 ba->dim = BUFR_NMAXSEQ;
151 return 0;
152}
Contains all the information for a single data related with a descriptor in a expanded squence.
Definition: bufrdeco.h:435
Contains all the information for a subset in a expanded squence This is a version to use with bufrdec...
Definition: bufrdeco.h:458

References BUFR_NMAXSEQ, bufrdeco_assert, bufrdeco_subset_sequence_data::dim, and bufrdeco_subset_sequence_data::sequence.

Referenced by bufrdeco_clean_subset_sequence_data().

Here is the caller graph for this function:

◆ bufrdeco_init_tables()

int bufrdeco_init_tables ( struct bufr_tables **  t)

Init a struct bufr_tables allocating space.

Parameters
tpointer to the target pointer to struct bufr_tables
Returns
0 if succeeded, 1 otherwise

Definition at line 32 of file bufrdeco_memory.c.

33{
34 bufrdeco_assert ( t != NULL );
35
36 if ( *t != NULL )
37 free ( ( void * ) *t );
38
39 if ( ( *t = ( struct bufr_tables * ) calloc ( 1, sizeof ( struct bufr_tables ) ) ) == NULL )
40 return 1;
41 return 0;
42}
Contains all tables needed to parse a bufr file.
Definition: bufrdeco.h:938

References bufrdeco_assert.

Referenced by bufr_read_tables(), bufrdeco_init(), bufrdeco_store_tables(), and bufrdeco_substitute_tables().

Here is the caller graph for this function:

◆ bufrdeco_substitute_tables()

int bufrdeco_substitute_tables ( struct bufr_tables **  replaced,
struct bufr_tables source,
struct bufrdeco b 
)

substitute an struct bufr_tables into a struct bufrdeco

Parameters
replacedPointer where to set the replaced pointer
sourcePointer to a struct bufr_tables
bpointer to the container basic struct bufrdeco
Returns
0 if succeeded, 1 otherwise

Remember that the struct bufr_tables used in bufrdeco library is the one which pointer is in struct bufrdeco . To avoid problems the struct must be initialized before substituted in this fucntion. Both source and replaced structs are not modified.

This is useful if we do not want to read and parse tables again if the caller has a pool of already readed tables.

Definition at line 116 of file bufrdeco_memory.c.

117{
118 bufrdeco_assert ( b != NULL );
119
120 *replaced = b->tables;
121 if ( source == NULL )
122 {
123 // allocate memory for table
124 return bufrdeco_init_tables ( & ( b->tables ) );
125 }
126 else
127 b->tables = source;
128 return 0;
129}
int bufrdeco_init_tables(struct bufr_tables **t)
Init a struct bufr_tables allocating space.
struct bufr_tables * tables
Definition: bufrdeco.h:973

References bufrdeco_assert, bufrdeco_init_tables(), and bufrdeco::tables.

Here is the call graph for this function: