bufr2synop 0.24.0
bufrdeco_memory.c
Go to the documentation of this file.
1/***************************************************************************
2 * Copyright (C) 2013-2022 by Guillermo Ballester Valor *
3 * gbv@ogimet.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20/*!
21 \file bufrdeco_memory.c
22 \brief This file has the memory stufs for library bufrdeco
23*/
24#include "bufrdeco.h"
25
26/*!
27 \fn int bufrdeco_init_tables ( struct bufr_tables **t )
28 \brief Init a struct \ref bufr_tables allocating space
29 \param t pointer to the target pointer to struct \ref bufr_tables
30 \return 0 if succeeded, 1 otherwise
31*/
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}
43
44
45/*!
46 \fn int bufrdeco_free_tables ( struct bufr_tables **t )
47 \brief Frees the allocated space for a struct \ref bufr_tables
48 \param t pointer to the target pointer to struct \ref bufr_tables
49 \return 0 and \a *t is set to NULL
50*/
52{
53 bufrdeco_assert ( t != NULL );
54
55 if ( *t != NULL )
56 {
57 free ( ( void * ) *t );
58 *t = NULL;
59 }
60 return 0;
61}
62
63/*!
64 \fn int bufrdeco_init_expanded_tree ( struct bufrdeco_expanded_tree **t )
65 \brief Init a struct \ref bufrdeco_expanded_tree allocating space
66 \param t pointer to the target pointer to struct \ref bufrdeco_expanded_tree
67
68 \return 0 if succeeded, 1 otherwise
69*/
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}
81
82/*!
83 \fn int bufrdeco_free_expanded_tree ( struct bufrdeco_expanded_tree **t )
84 \brief Frees the allocated space for a struct \ref bufrdeco_expanded_tree
85 \param t pointer to the target pointer to struct \ref bufrdeco_expanded_tree
86
87 \return 0 and \a *t is set to NULL
88*/
90{
91 bufrdeco_assert ( t != NULL );
92
93 if ( *t != NULL )
94 {
95 free ( ( void * ) *t );
96 *t = NULL;
97 }
98 return 0;
99}
100
101/*!
102 \fn int bufrdeco_substitute_tables ( struct bufr_tables **replaced, struct bufr_tables *source, struct bufrdeco *b )
103 \brief substitute an struct \ref bufr_tables into a struct \ref bufrdeco
104 \param replaced Pointer where to set the replaced pointer
105 \param source Pointer to a struct \ref bufr_tables
106 \param b pointer to the container basic struct \ref bufrdeco
107 \return 0 if succeeded, 1 otherwise
108
109 Remember that the struct \ref bufr_tables used in bufrdeco library is the one which pointer is in struct
110 \ref bufrdeco . To avoid problems the struct must be initialized before substituted in this fucntion.
111 Both source and replaced structs are not modified.
112
113 This is useful if we do not want to read and parse tables again if the caller has a pool of
114 already readed tables.
115*/
116int bufrdeco_substitute_tables ( struct bufr_tables **replaced, struct bufr_tables *source, struct bufrdeco *b )
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}
130
131/*!
132 \fn int bufrdeco_init_subset_sequence_data ( struct bufrdeco_subset_sequence_data *ba )
133 \brief Init a struct \ref bufrdeco_subset_sequence_data
134 \param ba pointer to the target struct
135 \return 0 if succeeded, 1 otherwise
136
137 It is supossed that no memory is allocated for sequence. If we are not sure better use
138 function \ref bufrdeco_clean_subset_sequence_data
139*/
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}
153
154/*!
155 \fn int bufrdeco_clean_subset_sequence_data ( struct bufrdeco_subset_sequence_data *ba )
156 \brief Cleans a struct \ref bufrdeco_subset_sequence_data
157 \param ba Pointer to struct \ref bufrdeco_subset_sequence_data to clean
158 \return 0 if succeeded, 1 otherwise
159
160 For eficience, if sequence in the struct \ref bufrdeco_subset_sequence_data is allocated, just set the used
161 elements to zero. If is still no allocated memory for sequence inits the struct
162
163*/
165{
166 bufrdeco_assert ( ba != NULL );
167
168 if ( ba->sequence != NULL )
169 {
170 ba->nd = 0;
171 return 0;
172 }
173 else
175}
176
177/*!
178 \fn int bufrdeco_free_subset_sequence_data ( struct bufrdeco_subset_sequence_data *ba )
179 \brief Free the memory for sequence array in a struct \ref bufrdeco_subset_sequence_data
180 \param ba pointer to the target struct to free
181 \return 0 if succeeded
182*/
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}
194
195/*!
196 \fn int bufrdeco_init_compressed_data_references ( struct bufrdeco_compressed_data_references *rf )
197 \brief Init a struct bufrdeco_compressed_data_references
198 \param rf pointer ti the target struct
199 \return 0 if succeded, otherwise 1
200
201 If already memory is allocated for array of references then just adjust the used index to zero. Otherwise
202 it allocate the needed memory and init the struct
203
204*/
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}
226
227/*!
228 \fn int bufrdeco_clean_compressed_data_references ( struct bufrdeco_compressed_data_references *rf )
229 \brief Clean a struct \ref bufrdeco_compressed_data_references
230 \param rf pointer to the target struct \ref bufrdeco_compressed_data_references to clean
231 \return If succeeded return 0, otherwise 1
232*/
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}
243
244/*!
245 \fn int bufrdeco_free_compressed_data_references ( struct bufrdeco_compressed_data_references *rf )
246 \brief Free the memory allocated for array of references in a struct \ref bufrdeco_compressed_data_references
247 \param rf pointer to the target struct \ref bufrdeco_compressed_data_references to free
248 \return If succeeded return 0, otherwise 1
249*/
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}
261
262
263/*!
264 * \fn int bufrdeco_allocate_bitmap ( struct bufrdeco *b )
265 * \brief allocate bitmap
266 * \param b the active struct \ref bufrdeco
267 * \return If succeeded return 0, otherwise 1
268 */
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}
300
301
302/*!
303 * \fn int bufrdeco_clean_bitmaps ( struct bufrdeco *b )
304 * \brief Clean all allocated bitmaps, but still is in memory
305 * \param b the active struct \ref bufrdeco
306 * \return If succeeded return 0, otherwise 1
307 */
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}
323
324
325/*!
326 * \fn int bufrdeco_free_bitmap_array ( struct bufrdeco_bitmap_array *a )
327 * \brief Free an allocated bitmap array
328 * \param a pointer to target struct \ref bufrdeco_bitmap_array to free
329 * \return If succeeded return 0, otherwise 1
330 */
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}
348
Include header file for bufrdeco library.
#define BUFR_NMAXSEQ
Maximum expected descriptors in a expanded sequence for a single subset.
Definition: bufrdeco.h:116
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
int bufrdeco_init_tables(struct bufr_tables **t)
Init a struct bufr_tables allocating space.
int bufrdeco_clean_bitmaps(struct bufrdeco *b)
Clean all allocated bitmaps, but still is in memory.
int bufrdeco_init_subset_sequence_data(struct bufrdeco_subset_sequence_data *ba)
Init a struct bufrdeco_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.
int bufrdeco_init_expanded_tree(struct bufrdeco_expanded_tree **t)
Init a struct bufrdeco_expanded_tree allocating space.
int bufrdeco_free_tables(struct bufr_tables **t)
Frees the allocated space for a struct bufr_tables.
int bufrdeco_allocate_bitmap(struct bufrdeco *b)
allocate bitmap
int bufrdeco_free_bitmap_array(struct bufrdeco_bitmap_array *a)
Free an allocated bitmap array.
int bufrdeco_init_compressed_data_references(struct bufrdeco_compressed_data_references *rf)
Init a struct bufrdeco_compressed_data_references.
int bufrdeco_clean_compressed_data_references(struct bufrdeco_compressed_data_references *rf)
Clean a struct bufrdeco_compressed_data_references.
int bufrdeco_substitute_tables(struct bufr_tables **replaced, struct bufr_tables *source, struct bufrdeco *b)
substitute an struct bufr_tables into a struct bufrdeco
int bufrdeco_free_expanded_tree(struct bufrdeco_expanded_tree **t)
Frees the allocated space for a struct bufrdeco_expanded_tree.
int bufrdeco_clean_subset_sequence_data(struct bufrdeco_subset_sequence_data *ba)
Cleans a struct bufrdeco_subset_sequence_data.
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.
Contains all the information for a single data related with a descriptor in a expanded squence.
Definition: bufrdeco.h:435
Contains all tables needed to parse a bufr file.
Definition: bufrdeco.h:938
Stores all structs bufrdeco_bitmap for a bufr bitmap.
Definition: bufrdeco.h:491
struct bufrdeco_bitmap * bmap[BUFR_MAX_BITMAPS]
Definition: bufrdeco.h:493
Stores all needed data for a bufr bitmap.
Definition: bufrdeco.h:470
Manage an array of structs bufrdeco_compressed_ref.
Definition: bufrdeco.h:668
struct bufrdeco_compressed_ref * refs
Definition: bufrdeco.h:671
Struct to hold the needed reference bit offsets, descriptor tree and replications in a compressed BUF...
Definition: bufrdeco.h:639
Array of structs bufr_sequence from which bufr expanded tree is made.
Definition: bufrdeco.h:596
Contains all the information for a subset in a expanded squence This is a version to use with bufrdec...
Definition: bufrdeco.h:458
struct bufr_atom_data * sequence
Definition: bufrdeco.h:462
This struct contains all needed data to parse and decode a BUFR file.
Definition: bufrdeco.h:965
struct bufrdeco_bitmap_array bitmap
Definition: bufrdeco.h:980
struct bufr_tables * tables
Definition: bufrdeco.h:973
char error[1024]
Definition: bufrdeco.h:983