Skip to content

Commit 1c3ebee

Browse files
committed
added free and null macro
1 parent 4b58b1c commit 1c3ebee

22 files changed

Lines changed: 92 additions & 62 deletions

include/memory_wrappers.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef MEMORY_WRAPPERS_H
2+
#define MEMORY_WRAPPERS_H
3+
4+
#include <stdlib.h>
5+
6+
#define FREE_AND_NULL(p) \
7+
do \
8+
{ \
9+
free(p); \
10+
(p) = NULL; \
11+
} while (0)
12+
13+
#endif /* MEMORY_WRAPPERS_H */

include/utils/Vec_macros.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#ifndef VEC_MACROS_H
2020
#define VEC_MACROS_H
2121

22+
#include "memory_wrappers.h"
2223
#include <assert.h>
2324
#include <stdio.h>
2425
#include <stdlib.h>
@@ -48,7 +49,7 @@
4849
vec->data = (TYPE *) malloc(capacity * sizeof(TYPE)); \
4950
if (vec->data == NULL) \
5051
{ \
51-
free(vec); \
52+
FREE_AND_NULL(vec); \
5253
return NULL; \
5354
} \
5455
\
@@ -59,8 +60,9 @@
5960
\
6061
static inline void TYPE_NAME##Vec_free(TYPE_NAME##Vec *vec) \
6162
{ \
62-
free(vec->data); \
63-
free(vec); \
63+
if (!vec) return; \
64+
FREE_AND_NULL(vec->data); \
65+
FREE_AND_NULL(vec); \
6466
} \
6567
\
6668
static inline void TYPE_NAME##Vec_clear_no_resize(TYPE_NAME##Vec *vec) \

src/affine/hstack.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* limitations under the License.
1717
*/
1818
#include "affine.h"
19+
#include "memory_wrappers.h"
1920
#include "utils/CSR_sum.h"
2021
#include <assert.h>
2122
#include <stdio.h>
@@ -168,7 +169,7 @@ static void free_type_data(expr *node)
168169
}
169170

170171
free_csr_matrix(hnode->CSR_work);
171-
free(hnode->args);
172+
FREE_AND_NULL(hnode->args);
172173
}
173174

174175
expr *new_hstack(expr **args, int n_args, int n_vars)

src/affine/index.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* limitations under the License.
1717
*/
1818
#include "affine.h"
19+
#include "memory_wrappers.h"
1920
#include "subexpr.h"
2021
#include <assert.h>
2122
#include <stdio.h>
@@ -38,7 +39,7 @@ static bool check_for_duplicates(const int *indices, int n_idxs, int max_idx)
3839
}
3940
seen[indices[i]] = true;
4041
}
41-
free(seen);
42+
FREE_AND_NULL(seen);
4243
return has_dup;
4344
}
4445

@@ -154,8 +155,7 @@ static bool is_affine(const expr *node)
154155
static void free_type_data(expr *node)
155156
{
156157
index_expr *idx = (index_expr *) node;
157-
free(idx->indices);
158-
idx->indices = NULL;
158+
FREE_AND_NULL(idx->indices);
159159
}
160160

161161
expr *new_index(expr *child, int d1, int d2, const int *indices, int n_idxs)

src/affine/linear_op.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* limitations under the License.
1717
*/
1818
#include "affine.h"
19+
#include "memory_wrappers.h"
1920
#include <assert.h>
2021
#include <stdlib.h>
2122
#include <string.h>
@@ -60,8 +61,7 @@ static void free_type_data(expr *node)
6061

6162
free_csc_matrix(lin_node->A_csc);
6263
lin_node->A_csc = NULL;
63-
free(lin_node->b);
64-
lin_node->b = NULL;
64+
FREE_AND_NULL(lin_node->b);
6565
}
6666

6767
static void jacobian_init(expr *node)

src/affine/sum.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* limitations under the License.
1717
*/
1818
#include "affine.h"
19+
#include "memory_wrappers.h"
1920
#include "utils/CSR_sum.h"
2021
#include "utils/int_double_pair.h"
2122
#include "utils/mini_numpy.h"
@@ -175,7 +176,7 @@ static bool is_affine(const expr *node)
175176
static void free_type_data(expr *node)
176177
{
177178
sum_expr *snode = (sum_expr *) node;
178-
free(snode->idx_map);
179+
FREE_AND_NULL(snode->idx_map);
179180
}
180181

181182
expr *new_sum(expr *child, int axis)

src/affine/trace.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* limitations under the License.
1717
*/
1818
#include "affine.h"
19+
#include "memory_wrappers.h"
1920
#include "utils/CSR_sum.h"
2021
#include "utils/int_double_pair.h"
2122
#include "utils/utils.h"
@@ -139,7 +140,7 @@ static void free_type_data(expr *node)
139140
if (node)
140141
{
141142
trace_expr *tnode = (trace_expr *) node;
142-
free(tnode->idx_map);
143+
FREE_AND_NULL(tnode->idx_map);
143144
}
144145
}
145146

src/bivariate/left_matmul.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* limitations under the License.
1717
*/
1818
#include "bivariate.h"
19+
#include "memory_wrappers.h"
1920
#include "subexpr.h"
2021
#include "utils/Timer.h"
2122
#include "utils/linalg_sparse_matmuls.h"
@@ -98,8 +99,8 @@ static void free_type_data(expr *node)
9899
free_csr_matrix(lin_node->AT);
99100
free_csc_matrix(lin_node->Jchild_CSC);
100101
free_csc_matrix(lin_node->J_CSC);
101-
free(lin_node->csc_to_csr_workspace);
102-
free(lin_node->AT_iwork);
102+
FREE_AND_NULL(lin_node->csc_to_csr_workspace);
103+
FREE_AND_NULL(lin_node->AT_iwork);
103104
free_expr(lin_node->param_source);
104105
}
105106

src/bivariate/quad_over_lin.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* limitations under the License.
1717
*/
1818
#include "bivariate.h"
19+
#include "memory_wrappers.h"
1920
#include "subexpr.h"
2021
#include "utils/CSC_Matrix.h"
2122
#include <assert.h>
@@ -102,7 +103,7 @@ static void jacobian_init(expr *node)
102103
}
103104
assert(nonzero_cols == node->jacobian->nnz);
104105

105-
free(col_nz);
106+
FREE_AND_NULL(col_nz);
106107

107108
/* insert y variable index at correct position */
108109
insert_idx(y->var_id, node->jacobian->i, node->jacobian->nnz);

src/bivariate/right_matmul.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
#include "affine.h"
1919
#include "bivariate.h"
20+
#include "memory_wrappers.h"
2021
#include "subexpr.h"
2122
#include "utils/CSR_Matrix.h"
2223
#include "utils/linalg_sparse_matmuls.h"
@@ -59,7 +60,7 @@ expr *new_right_matmul(expr *param_node, expr *u, const CSR_Matrix *A)
5960

6061
/* functionality for parameter */
6162
left_matmul_expr *left_matmul_data = (left_matmul_expr *) left_matmul_node;
62-
free(left_matmul_data->AT_iwork);
63+
FREE_AND_NULL(left_matmul_data->AT_iwork);
6364
left_matmul_data->AT_iwork = work_transpose;
6465
left_matmul_data->refresh_param_values = refresh_param_values;
6566

0 commit comments

Comments
 (0)