1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*!
This crate provides a method to count and test the number of allocations while running some code.

# Example
Add as a dependency - since including the trait replaces the global memory allocator, you most likely want it gated behind a feature.

```toml
[features]
count-allocations = ["allocation-counter"]

[dependencies]
allocation-counter = { version = "0", optional = true }
```

Tests can now be written to assert that the number of desired memory allocations are not exceeded.

```
#[cfg(feature = "count-allocations")]
#[test]
pub fn no_memory_allocations() {
    # fn code_that_should_not_allocate_memory() {}
    # fn code_that_should_not_allocate_much() {}
    let allocations = allocation_counter::count(|| {
        code_that_should_not_allocate_memory();
    });
    assert_eq!(allocations, 0);

    // Or use this utility method in this case:
    allocation_counter::assert_no_allocations(|| {
        code_that_should_not_allocate_memory();
    });

    // Can also allow a certain number of allocations:
    allocation_counter::assert_max_allocations(10 || {
        code_that_should_not_allocate_much();
    });

    // Can also assert on a range, useful to adjust
    // test expectations over time:
    allocation_counter::assert_num_allocations(500..600 || {
        code_that_should_not_allocate_much();
    });

    // It's possible to opt out of counting allocations
    // for certain parts of the code flow:
    allocation_counter::assert_no_allocations(|| {
        code_that_should_not_allocate();
        allocation_counter::avoid_counting(|| {
            external_code_that_should_not_be_tested();
        });
        code_that_should_not_allocate();
    });
}
```

Run the tests with the necessary feature enabled.

```sh
cargo test --features count-allocations
```
*/
use std::alloc::{GlobalAlloc, Layout, System};
use std::cell::RefCell;

/// Run a closure while counting the performed memory allocations.
///
/// Will only measure those done by the current thread, so take care when
/// interpreting the returned count for multithreaded code.
///
/// # Arguments
///
/// - `run_while_counting` - The code to run while counting allocations
///
/// # Examples
///
/// ```
/// # fn code_that_should_not_allocate_memory() {}
/// let allocations = allocation_counter::count(|| {
///      "hello, world".to_string();
/// });
/// assert_eq!(allocations, 1);
/// ```
pub fn count<F: FnOnce()>(run_while_counting: F) -> u64 {
    let initial_count = ALLOCATIONS.with(|f| *f.borrow());

    run_while_counting();

    ALLOCATIONS.with(|f| *f.borrow()) - initial_count
}

/// Run a closure and assert that no memory allocations were made.
///
/// Will only measure those done by the current thread, so take care when
/// testing the memory allocations of multithreaded code.
///
/// # Arguments
///
/// - `run_while_counting` - The code to run while counting allocations
///
/// # Examples
///
/// ```
/// # fn code_that_should_not_allocate_memory() {}
/// allocation_counter::assert_no_allocations(|| {
///     code_that_should_not_allocate_memory();
/// });
/// ```
pub fn assert_no_allocations<F: FnOnce()>(run_while_counting: F) {
    assert_max_allocations(0, run_while_counting);
}

/// Run a closure and assert that the number of memory allocations are below a limit.
///
/// Will only measure those done by the current thread, so take care when
/// testing the memory allocations of multithreaded code.
///
/// # Arguments
///
/// - `max_allocations` - The maximum number of allocations allowed
/// - `run_while_counting` - The code to run while counting allocations
///
/// # Examples
///
/// ```
/// # fn code_that_should_not_allocate_much() {}
/// allocation_counter::assert_max_allocations(12, || {
///     code_that_should_not_allocate_much();
/// });
/// ```
pub fn assert_max_allocations<F: FnOnce()>(max_allocations: u64, run_while_counting: F) {
    let initial_count = ALLOCATIONS.with(|f| *f.borrow());

    run_while_counting();

    let num_allocations = ALLOCATIONS.with(|f| *f.borrow()) - initial_count;
    assert!(
        num_allocations <= max_allocations,
        "Unexpected memory allocations (more than {}): {}",
        max_allocations,
        num_allocations
    );
}

/// Run a closure and assert that the number of memory allocations are inside a range.
///
/// Will only measure those done by the current thread, so take care when
/// testing the memory allocations of multithreaded code.
///
/// # Arguments
///
/// - `allowed_allocations` - The range of allocations allowed
/// - `run_while_counting` - The code to run while counting allocations
///
/// # Examples
///
/// ```
/// # fn code_that_should_not_allocate_much() {}
/// allocation_counter::assert_max_allocations(12, || {
///     code_that_should_not_allocate_much();
/// });
/// ```
pub fn assert_num_allocations<F: FnOnce()>(
    allowed_allocations: std::ops::Range<u64>,
    run_while_counting: F,
) {
    let initial_count = ALLOCATIONS.with(|f| *f.borrow());

    run_while_counting();

    let num_allocations = ALLOCATIONS.with(|f| *f.borrow()) - initial_count;
    assert!(
        allowed_allocations.contains(&num_allocations),
        "Unexpected memory allocations (outside of {:?}): {}",
        allowed_allocations,
        num_allocations
    );
}

/// Opt out of counting allocations while running some code.
///
/// Useful to avoid certain parts of the code flow that should not be counted.
///
/// # Arguments
///
/// - `run_while_not_counting` - The code to run while not counting allocations
///
/// # Examples
///
/// ```
/// # fn code_that_should_not_allocate() {}
/// # fn external_code_that_should_not_be_tested() {}
/// allocation_counter::assert_no_allocations(|| {
///     code_that_should_not_allocate();
///     allocation_counter::avoid_counting(|| {
///         external_code_that_should_not_be_tested();
///     });
///     code_that_should_not_allocate();
/// });
/// ```
pub fn avoid_counting<F: FnOnce()>(run_while_not_counting: F) {
    DO_COUNT.with(|b| {
        *b.borrow_mut() += 1;
        run_while_not_counting();
        *b.borrow_mut() -= 1;
    });
}

thread_local! {
    static ALLOCATIONS: RefCell<u64> = RefCell::new(0);
}
thread_local! {
    static DO_COUNT: RefCell<u32> = RefCell::new(0);
}

struct CountingAllocator;

unsafe impl GlobalAlloc for CountingAllocator {
    unsafe fn alloc(&self, l: Layout) -> *mut u8 {
        DO_COUNT.with(|b| {
            if *b.borrow() == 0 {
                ALLOCATIONS.with(|f| {
                    *f.borrow_mut() += 1;
                });
            }
        });

        System.alloc(l)
    }

    unsafe fn dealloc(&self, ptr: *mut u8, l: Layout) {
        System.dealloc(ptr, l);
    }
}

#[global_allocator]
static GLOBAL: CountingAllocator = CountingAllocator {};

#[test]
fn test_basic() {
    let allocations = count(|| {
        // Do nothing.
    });
    assert_eq!(allocations, 0);

    let allocations = count(|| {
        let v: Vec<u32> = vec![12];
        assert_eq!(v.len(), 1);
    });
    assert_eq!(allocations, 1);

    let allocations = count(|| {
        let v: Vec<u32> = vec![12];
        assert_eq!(v.len(), 1);
    });
    assert_eq!(allocations, 1);

    let allocations = count(|| {
        let v: Vec<u32> = vec![12];
        assert_eq!(v.len(), 1);
        let v: Vec<u32> = vec![12];
        assert_eq!(v.len(), 1);
    });
    assert_eq!(allocations, 2);

    assert_no_allocations(|| {
        // Do nothing
    });

    assert_max_allocations(2, || {
        // Do nothing
    });

    assert_max_allocations(2, || {
        let v: Vec<u32> = vec![12];
        assert_eq!(v.len(), 1);
        let v: Vec<u32> = vec![12];
        assert_eq!(v.len(), 1);
    });

    assert_num_allocations(1..3, || {
        let v: Vec<u32> = vec![12];
        assert_eq!(v.len(), 1);
        let v: Vec<u32> = vec![12];
        assert_eq!(v.len(), 1);
    });

    assert_num_allocations(2..3, || {
        let v: Vec<u32> = vec![12];
        assert_eq!(v.len(), 1);
        let v: Vec<u32> = vec![12];
        assert_eq!(v.len(), 1);
    });
}

#[test]
#[should_panic(expected = "Unexpected memory allocations (more than 0): 1")]
fn test_assert_no_allocations_panic() {
    assert_no_allocations(|| {
        let v: Vec<u32> = vec![12];
        assert_eq!(v.len(), 1);
    });
}

#[test]
#[should_panic(expected = "Unexpected memory allocations (more than 1): 2")]
fn test_assert_max_allocations_panic() {
    assert_max_allocations(1, || {
        let v: Vec<u32> = vec![12];
        assert_eq!(v.len(), 1);
        let v: Vec<u32> = vec![12];
        assert_eq!(v.len(), 1);
    });
}

#[test]
#[should_panic(expected = "Unexpected memory allocations (outside of 10..12): 2")]
fn test_assert_num_allocations_panic() {
    assert_num_allocations(10..12, || {
        let v: Vec<u32> = vec![12];
        assert_eq!(v.len(), 1);
        let v: Vec<u32> = vec![12];
        assert_eq!(v.len(), 1);
    });
}

#[test]
fn test_avoid_counting() {
    let allocations = count(|| {
        // Do nothing.
    });
    assert_eq!(allocations, 0);

    let allocations = count(|| {
        let v: Vec<u32> = vec![12];
        assert_eq!(v.len(), 1);
        avoid_counting(|| {
            let v: Vec<u32> = vec![12];
            assert_eq!(v.len(), 1);
            avoid_counting(|| {
                let v: Vec<u32> = vec![12];
                assert_eq!(v.len(), 1);
            });
        });
        let v: Vec<u32> = vec![12];
        assert_eq!(v.len(), 1);
        let v: Vec<u32> = vec![12];
        assert_eq!(v.len(), 1);
    });
    assert_eq!(allocations, 3);

    assert_no_allocations(|| {
        avoid_counting(|| {
            let v: Vec<u32> = vec![12];
            assert_eq!(v.len(), 1);
        });
    });
}