Skip to content

Commit deb7cab

Browse files
authored
wrap malloc/alloc (#70)
* wrap malloc/alloc * message * alloc file * alloc file again
1 parent b9d282f commit deb7cab

46 files changed

Lines changed: 215 additions & 121 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

include/problem.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ typedef struct
3838
int nnz_hessian;
3939
int n_vars;
4040
int total_constraint_size;
41+
size_t memory_bytes;
4142
} Diff_engine_stats;
4243

4344
typedef struct problem

include/utils/tracked_alloc.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef TRACKED_ALLOC_H
2+
#define TRACKED_ALLOC_H
3+
4+
#include <stddef.h>
5+
#include <stdlib.h>
6+
7+
extern size_t g_allocated_bytes;
8+
9+
static inline void *SP_MALLOC(size_t size)
10+
{
11+
void *ptr = malloc(size);
12+
if (ptr) g_allocated_bytes += size;
13+
return ptr;
14+
}
15+
16+
static inline void *SP_CALLOC(size_t count, size_t size)
17+
{
18+
void *ptr = calloc(count, size);
19+
if (ptr) g_allocated_bytes += count * size;
20+
return ptr;
21+
}
22+
23+
#endif /* TRACKED_ALLOC_H */

src/atoms/affine/add.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
#include "atoms/affine.h"
1919
#include "utils/CSR_sum.h"
20+
#include "utils/tracked_alloc.h"
2021
#include <assert.h>
2122
#include <stdio.h>
2223
#include <stdlib.h>
@@ -91,7 +92,7 @@ static bool is_affine(const expr *node)
9192
expr *new_add(expr *left, expr *right)
9293
{
9394
assert(left->d1 == right->d1 && left->d2 == right->d2);
94-
expr *node = (expr *) calloc(1, sizeof(expr));
95+
expr *node = (expr *) SP_CALLOC(1, sizeof(expr));
9596
init_expr(node, left->d1, left->d2, left->n_vars, forward, jacobian_init_impl,
9697
eval_jacobian, is_affine, wsum_hess_init_impl, eval_wsum_hess, NULL);
9798
node->left = left;

src/atoms/affine/broadcast.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "atoms/affine.h"
1919
#include "subexpr.h"
2020
#include "utils/mini_numpy.h"
21+
#include "utils/tracked_alloc.h"
2122
#include <assert.h>
2223
#include <stdio.h>
2324
#include <stdlib.h>
@@ -194,7 +195,7 @@ static void wsum_hess_init_impl(expr *node)
194195
node->wsum_hess = new_csr_copy_sparsity(x->wsum_hess);
195196

196197
/* allocate space for weight vector */
197-
node->work->dwork = malloc(node->size * sizeof(double));
198+
node->work->dwork = SP_MALLOC(node->size * sizeof(double));
198199
}
199200

200201
static void eval_wsum_hess(expr *node, const double *w)
@@ -273,7 +274,7 @@ expr *new_broadcast(expr *child, int d1, int d2)
273274
exit(1);
274275
}
275276

276-
broadcast_expr *bcast = (broadcast_expr *) calloc(1, sizeof(broadcast_expr));
277+
broadcast_expr *bcast = (broadcast_expr *) SP_CALLOC(1, sizeof(broadcast_expr));
277278
expr *node = (expr *) bcast;
278279

279280
// --------------------------------------------------------------------------

src/atoms/affine/const_scalar_mult.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
#include "atoms/affine.h"
1919
#include "subexpr.h"
20+
#include "utils/tracked_alloc.h"
2021
#include <assert.h>
2122
#include <stdio.h>
2223
#include <stdlib.h>
@@ -97,7 +98,7 @@ static bool is_affine(const expr *node)
9798
expr *new_const_scalar_mult(double a, expr *child)
9899
{
99100
const_scalar_mult_expr *mult_node =
100-
(const_scalar_mult_expr *) calloc(1, sizeof(const_scalar_mult_expr));
101+
(const_scalar_mult_expr *) SP_CALLOC(1, sizeof(const_scalar_mult_expr));
101102
expr *node = &mult_node->base;
102103

103104
init_expr(node, child->d1, child->d2, child->n_vars, forward, jacobian_init_impl,

src/atoms/affine/const_vector_mult.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
#include "atoms/affine.h"
1919
#include "subexpr.h"
20+
#include "utils/tracked_alloc.h"
2021
#include <stdio.h>
2122
#include <stdlib.h>
2223
#include <string.h>
@@ -77,7 +78,7 @@ static void wsum_hess_init_impl(expr *node)
7778
/* same sparsity as child */
7879
node->wsum_hess = new_csr_copy_sparsity(x->wsum_hess);
7980

80-
node->work->dwork = (double *) malloc(node->size * sizeof(double));
81+
node->work->dwork = (double *) SP_MALLOC(node->size * sizeof(double));
8182
}
8283

8384
static void eval_wsum_hess(expr *node, const double *w)
@@ -112,7 +113,7 @@ static bool is_affine(const expr *node)
112113
expr *new_const_vector_mult(const double *a, expr *child)
113114
{
114115
const_vector_mult_expr *vnode =
115-
(const_vector_mult_expr *) calloc(1, sizeof(const_vector_mult_expr));
116+
(const_vector_mult_expr *) SP_CALLOC(1, sizeof(const_vector_mult_expr));
116117
expr *node = &vnode->base;
117118

118119
init_expr(node, child->d1, child->d2, child->n_vars, forward, jacobian_init_impl,
@@ -122,7 +123,7 @@ expr *new_const_vector_mult(const double *a, expr *child)
122123
expr_retain(child);
123124

124125
/* copy a vector */
125-
vnode->a = (double *) malloc(child->size * sizeof(double));
126+
vnode->a = (double *) SP_MALLOC(child->size * sizeof(double));
126127
memcpy(vnode->a, a, child->size * sizeof(double));
127128

128129
return node;

src/atoms/affine/constant.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 "atoms/affine.h"
19+
#include "utils/tracked_alloc.h"
1920
#include <stdlib.h>
2021
#include <string.h>
2122

@@ -60,7 +61,7 @@ static bool is_affine(const expr *node)
6061

6162
expr *new_constant(int d1, int d2, int n_vars, const double *values)
6263
{
63-
expr *node = (expr *) calloc(1, sizeof(expr));
64+
expr *node = (expr *) SP_CALLOC(1, sizeof(expr));
6465
init_expr(node, d1, d2, n_vars, forward, jacobian_init_impl, eval_jacobian,
6566
is_affine, wsum_hess_init_impl, eval_wsum_hess, NULL);
6667
memcpy(node->value, values, node->size * sizeof(double));

src/atoms/affine/diag_vec.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// SPDX-License-Identifier: Apache-2.0
1919

2020
#include "atoms/affine.h"
21+
#include "utils/tracked_alloc.h"
2122
#include <assert.h>
2223
#include <stdlib.h>
2324
#include <string.h>
@@ -100,7 +101,7 @@ static void wsum_hess_init_impl(expr *node)
100101
wsum_hess_init(x);
101102

102103
/* workspace for extracting diagonal weights */
103-
node->work->dwork = (double *) calloc(x->size, sizeof(double));
104+
node->work->dwork = (double *) SP_CALLOC(x->size, sizeof(double));
104105

105106
/* Copy child's Hessian structure (diag_vec is linear, so its own Hessian is
106107
* zero) */
@@ -136,7 +137,7 @@ expr *new_diag_vec(expr *child)
136137

137138
/* n is the number of elements (works for both row and column vectors) */
138139
int n = child->size;
139-
expr *node = (expr *) calloc(1, sizeof(expr));
140+
expr *node = (expr *) SP_CALLOC(1, sizeof(expr));
140141
init_expr(node, n, n, child->n_vars, forward, jacobian_init_impl, eval_jacobian,
141142
is_affine, wsum_hess_init_impl, eval_wsum_hess, NULL);
142143
node->left = child;

src/atoms/affine/hstack.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
#include "atoms/affine.h"
1919
#include "utils/CSR_sum.h"
20+
#include "utils/tracked_alloc.h"
2021
#include <assert.h>
2122
#include <stdio.h>
2223
#include <stdlib.h>
@@ -185,14 +186,14 @@ expr *new_hstack(expr **args, int n_args, int n_vars)
185186
}
186187

187188
/* Allocate the type-specific struct */
188-
hstack_expr *hnode = (hstack_expr *) calloc(1, sizeof(hstack_expr));
189+
hstack_expr *hnode = (hstack_expr *) SP_CALLOC(1, sizeof(hstack_expr));
189190
expr *node = &hnode->base;
190191
init_expr(node, args[0]->d1, d2, n_vars, forward, jacobian_init_impl,
191192
eval_jacobian, is_affine, wsum_hess_init_impl, wsum_hess_eval,
192193
free_type_data);
193194

194195
/* Set type-specific fields (deep copy args array) */
195-
hnode->args = (expr **) calloc(n_args, sizeof(expr *));
196+
hnode->args = (expr **) SP_CALLOC(n_args, sizeof(expr *));
196197
hnode->n_args = n_args;
197198
for (int i = 0; i < n_args; i++)
198199
{

src/atoms/affine/index.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
#include "atoms/affine.h"
1919
#include "subexpr.h"
20+
#include "utils/tracked_alloc.h"
2021
#include <assert.h>
2122
#include <stdio.h>
2223
#include <stdlib.h>
@@ -28,7 +29,7 @@
2829
* Returns true if duplicates exist, false otherwise. */
2930
static bool check_for_duplicates(const int *indices, int n_idxs, int max_idx)
3031
{
31-
bool *seen = (bool *) calloc(max_idx, sizeof(bool));
32+
bool *seen = (bool *) SP_CALLOC(max_idx, sizeof(bool));
3233
bool has_dup = false;
3334
for (int i = 0; i < n_idxs && !has_dup; i++)
3435
{
@@ -104,7 +105,7 @@ static void wsum_hess_init_impl(expr *node)
104105
wsum_hess_init(x);
105106

106107
/* for setting weight vector to evaluate hessian of child */
107-
node->work->dwork = (double *) calloc(x->size, sizeof(double));
108+
node->work->dwork = (double *) SP_CALLOC(x->size, sizeof(double));
108109

109110
/* in the implementation of eval_wsum_hess we evaluate the
110111
child's hessian with a weight vector that has w[i] = 0
@@ -163,7 +164,7 @@ expr *new_index(expr *child, int d1, int d2, const int *indices, int n_idxs)
163164
{
164165
assert(d1 * d2 == n_idxs);
165166
/* allocate type-specific struct */
166-
index_expr *idx = (index_expr *) calloc(1, sizeof(index_expr));
167+
index_expr *idx = (index_expr *) SP_CALLOC(1, sizeof(index_expr));
167168
expr *node = &idx->base;
168169

169170
init_expr(node, d1, d2, child->n_vars, forward, jacobian_init_impl,
@@ -174,7 +175,7 @@ expr *new_index(expr *child, int d1, int d2, const int *indices, int n_idxs)
174175
expr_retain(child);
175176

176177
/* copy indices */
177-
idx->indices = (int *) malloc(n_idxs * sizeof(int));
178+
idx->indices = (int *) SP_MALLOC(n_idxs * sizeof(int));
178179
memcpy(idx->indices, indices, n_idxs * sizeof(int));
179180
idx->n_idxs = n_idxs;
180181

0 commit comments

Comments
 (0)