13b72a14c2515c1169d5501ada5499cf232fc643bZach Johnson/****************************************************************************** 23b72a14c2515c1169d5501ada5499cf232fc643bZach Johnson * 33b72a14c2515c1169d5501ada5499cf232fc643bZach Johnson * Copyright (C) 2014 Google, Inc. 43b72a14c2515c1169d5501ada5499cf232fc643bZach Johnson * 53b72a14c2515c1169d5501ada5499cf232fc643bZach Johnson * Licensed under the Apache License, Version 2.0 (the "License"); 63b72a14c2515c1169d5501ada5499cf232fc643bZach Johnson * you may not use this file except in compliance with the License. 73b72a14c2515c1169d5501ada5499cf232fc643bZach Johnson * You may obtain a copy of the License at: 83b72a14c2515c1169d5501ada5499cf232fc643bZach Johnson * 93b72a14c2515c1169d5501ada5499cf232fc643bZach Johnson * http://www.apache.org/licenses/LICENSE-2.0 103b72a14c2515c1169d5501ada5499cf232fc643bZach Johnson * 113b72a14c2515c1169d5501ada5499cf232fc643bZach Johnson * Unless required by applicable law or agreed to in writing, software 123b72a14c2515c1169d5501ada5499cf232fc643bZach Johnson * distributed under the License is distributed on an "AS IS" BASIS, 133b72a14c2515c1169d5501ada5499cf232fc643bZach Johnson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 143b72a14c2515c1169d5501ada5499cf232fc643bZach Johnson * See the License for the specific language governing permissions and 153b72a14c2515c1169d5501ada5499cf232fc643bZach Johnson * limitations under the License. 163b72a14c2515c1169d5501ada5499cf232fc643bZach Johnson * 173b72a14c2515c1169d5501ada5499cf232fc643bZach Johnson ******************************************************************************/ 183b72a14c2515c1169d5501ada5499cf232fc643bZach Johnson 193b72a14c2515c1169d5501ada5499cf232fc643bZach Johnson#pragma once 203b72a14c2515c1169d5501ada5499cf232fc643bZach Johnson 21f947fdddf5de6bd30886688a2b399460a3d95eccZach Johnson#include <stdbool.h> 223b72a14c2515c1169d5501ada5499cf232fc643bZach Johnson#include <stddef.h> 23933926c92e1378cc76bc9c149107e670c4872d4eScott James Remnant#include <stdint.h> 243b72a14c2515c1169d5501ada5499cf232fc643bZach Johnson 253b72a14c2515c1169d5501ada5499cf232fc643bZach Johnsontypedef struct allocation_tracker_t allocation_tracker_t; 264ed68b407ee836df7780a00d6b50b081b334c3abZach Johnsontypedef uint8_t allocator_id_t; 273b72a14c2515c1169d5501ada5499cf232fc643bZach Johnson 283b72a14c2515c1169d5501ada5499cf232fc643bZach Johnson// Initialize the allocation tracker. If you do not call this function, 293b72a14c2515c1169d5501ada5499cf232fc643bZach Johnson// the allocation tracker functions do nothing but are still safe to call. 30c0745da4fb23eea23abac3c3cfd51cc7f1d38f6dSharvil Nanavativoid allocation_tracker_init(void); 313b72a14c2515c1169d5501ada5499cf232fc643bZach Johnson 323b72a14c2515c1169d5501ada5499cf232fc643bZach Johnson// Reset the allocation tracker. Don't call this in the normal course of 333b72a14c2515c1169d5501ada5499cf232fc643bZach Johnson// operations. Useful mostly for testing. 343b72a14c2515c1169d5501ada5499cf232fc643bZach Johnsonvoid allocation_tracker_reset(void); 353b72a14c2515c1169d5501ada5499cf232fc643bZach Johnson 363b72a14c2515c1169d5501ada5499cf232fc643bZach Johnson// Expects that there are no allocations at the time of this call. Dumps 373b72a14c2515c1169d5501ada5499cf232fc643bZach Johnson// information about unfreed allocations to the log. Returns the amount of 383b72a14c2515c1169d5501ada5499cf232fc643bZach Johnson// unallocated memory. 393b72a14c2515c1169d5501ada5499cf232fc643bZach Johnsonsize_t allocation_tracker_expect_no_allocations(void); 403b72a14c2515c1169d5501ada5499cf232fc643bZach Johnson 414ed68b407ee836df7780a00d6b50b081b334c3abZach Johnson// Notify the tracker of a new allocation belonging to |allocator_id|. 424ed68b407ee836df7780a00d6b50b081b334c3abZach Johnson// If |ptr| is NULL, this function does nothing. |requested_size| is the 43c0745da4fb23eea23abac3c3cfd51cc7f1d38f6dSharvil Nanavati// size of the allocation without any canaries. The caller must allocate 44c0745da4fb23eea23abac3c3cfd51cc7f1d38f6dSharvil Nanavati// enough memory for canaries; the total allocation size can be determined 45c0745da4fb23eea23abac3c3cfd51cc7f1d38f6dSharvil Nanavati// by calling |allocation_tracker_resize_for_canary|. Returns |ptr| offset 46c0745da4fb23eea23abac3c3cfd51cc7f1d38f6dSharvil Nanavati// to the the beginning of the uncanaried region. 47c0745da4fb23eea23abac3c3cfd51cc7f1d38f6dSharvil Nanavativoid *allocation_tracker_notify_alloc(allocator_id_t allocator_id, void *ptr, size_t requested_size); 48f947fdddf5de6bd30886688a2b399460a3d95eccZach Johnson 49f947fdddf5de6bd30886688a2b399460a3d95eccZach Johnson// Notify the tracker of an allocation that is being freed. |ptr| must be a 504ed68b407ee836df7780a00d6b50b081b334c3abZach Johnson// pointer returned by a call to |allocation_tracker_notify_alloc| with the 514ed68b407ee836df7780a00d6b50b081b334c3abZach Johnson// same |allocator_id|. If |ptr| is NULL, this function does nothing. Returns 524ed68b407ee836df7780a00d6b50b081b334c3abZach Johnson// |ptr| offset to the real beginning of the allocation including any canary 534ed68b407ee836df7780a00d6b50b081b334c3abZach Johnson// space. 544ed68b407ee836df7780a00d6b50b081b334c3abZach Johnsonvoid *allocation_tracker_notify_free(allocator_id_t allocator_id, void *ptr); 55f947fdddf5de6bd30886688a2b399460a3d95eccZach Johnson 56c0745da4fb23eea23abac3c3cfd51cc7f1d38f6dSharvil Nanavati// Get the full size for an allocation, taking into account the size of canaries. 57f947fdddf5de6bd30886688a2b399460a3d95eccZach Johnsonsize_t allocation_tracker_resize_for_canary(size_t size); 58