1 /******************************************************************************
2  *
3  *  Copyright (C) 2014 Google, Inc.
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 #pragma once
20 
21 #include <stdbool.h>
22 #include <stddef.h>
23 #include <stdint.h>
24 
25 typedef struct allocation_tracker_t allocation_tracker_t;
26 typedef uint8_t allocator_id_t;
27 
28 // Initialize the allocation tracker. If you do not call this function,
29 // the allocation tracker functions do nothing but are still safe to call.
30 void allocation_tracker_init(void);
31 
32 // Reset the allocation tracker. Don't call this in the normal course of
33 // operations. Useful mostly for testing.
34 void allocation_tracker_reset(void);
35 
36 // Expects that there are no allocations at the time of this call. Dumps
37 // information about unfreed allocations to the log. Returns the amount of
38 // unallocated memory.
39 size_t allocation_tracker_expect_no_allocations(void);
40 
41 // Notify the tracker of a new allocation belonging to |allocator_id|.
42 // If |ptr| is NULL, this function does nothing. |requested_size| is the
43 // size of the allocation without any canaries. The caller must allocate
44 // enough memory for canaries; the total allocation size can be determined
45 // by calling |allocation_tracker_resize_for_canary|. Returns |ptr| offset
46 // to the the beginning of the uncanaried region.
47 void *allocation_tracker_notify_alloc(allocator_id_t allocator_id, void *ptr, size_t requested_size);
48 
49 // Notify the tracker of an allocation that is being freed. |ptr| must be a
50 // pointer returned by a call to |allocation_tracker_notify_alloc| with the
51 // same |allocator_id|. If |ptr| is NULL, this function does nothing. Returns
52 // |ptr| offset to the real beginning of the allocation including any canary
53 // space.
54 void *allocation_tracker_notify_free(allocator_id_t allocator_id, void *ptr);
55 
56 // Get the full size for an allocation, taking into account the size of canaries.
57 size_t allocation_tracker_resize_for_canary(size_t size);
58