1/*
2 * Copyright 2015, Michael Lotz <mmlr@mlotz.ch>.
3 * Distributed under the terms of the MIT License.
4 */
5#ifndef MALLOC_DEBUG_API_H
6#define MALLOC_DEBUG_API_H
7
8#include <OS.h>
9
10
11struct heap_implementation {
12	status_t	(*init)();
13	void		(*terminate_after)();
14
15	// Mandatory hooks
16	void*		(*memalign)(size_t alignment, size_t size);
17	void*		(*malloc)(size_t size);
18	void		(*free)(void* address);
19	void*		(*realloc)(void* address, size_t newSize);
20
21	// Hooks with default implementations
22	void*		(*calloc)(size_t numElements, size_t size);
23	void*		(*valloc)(size_t size);
24	int			(*posix_memalign)(void** pointer, size_t alignment,
25					size_t size);
26
27	// Heap Debug API
28	status_t	(*start_wall_checking)(int msInterval);
29	status_t	(*stop_wall_checking)();
30	void		(*set_paranoid_validation)(bool enabled);
31	void		(*set_memory_reuse)(bool enabled);
32	void		(*set_debugger_calls)(bool enabled);
33	void		(*set_default_alignment)(size_t defaultAlignment);
34	void		(*validate_heaps)();
35	void		(*validate_walls)();
36	void		(*dump_allocations)(bool statsOnly, thread_id thread);
37	void		(*dump_heaps)(bool dumpAreas, bool dumpBins);
38	void*		(*malloc_with_guard_page)(size_t size);
39	status_t	(*get_allocation_info)(void* address, size_t *size,
40					thread_id *thread);
41	status_t	(*set_dump_allocations_on_exit)(bool enabled);
42	status_t	(*set_stack_trace_depth)(size_t stackTraceDepth);
43};
44
45
46extern heap_implementation __mallocDebugHeap;
47extern heap_implementation __mallocGuardedHeap;
48
49
50static inline bool
51is_valid_alignment(size_t number)
52{
53	// this cryptic line accepts zero and all powers of two
54	return ((~number + 1) | ((number << 1) - 1)) == ~0UL;
55}
56
57#endif // MALLOC_DEBUG_API_H
58