1// Copyright 2016 The Fuchsia Authors
2// Copyright (c) 2008-2015 Travis Geiselbrecht
3//
4// Use of this source code is governed by a MIT-style
5// license that can be found in the LICENSE file or at
6// https://opensource.org/licenses/MIT
7
8#pragma once
9
10#include <stddef.h>
11#include <sys/types.h>
12#include <zircon/compiler.h>
13
14__BEGIN_CDECLS
15
16// define this to enable collection of all unique call sites with unique sizes
17#define HEAP_COLLECT_STATS 0
18
19// standard heap definitions
20void *malloc(size_t size) __MALLOC;
21void *memalign(size_t boundary, size_t size) __MALLOC;
22void *calloc(size_t count, size_t size) __MALLOC;
23void *realloc(void *ptr, size_t size);
24void free(void *ptr);
25
26// inner function used when stats gathering is enabled
27void *malloc_debug_caller_(size_t size, void *caller);
28
29// alternate version of malloc where the caller is passed in
30__MALLOC static inline void *malloc_debug_caller(size_t size, void *caller) {
31    if (HEAP_COLLECT_STATS) {
32        return malloc_debug_caller_(size, caller);
33    } else {
34        return malloc(size);
35    }
36}
37
38// tell the heap to return any free pages it can find
39void heap_trim(void);
40
41// internal apis used by the heap implementation to get/return pages to the VM
42void *heap_page_alloc(size_t pages);
43void heap_page_free(void *ptr, size_t pages);
44
45// Gets stats about the heap.
46// |size_bytes| is the total size of the heap, |free_bytes| is the free portion.
47void heap_get_info(size_t *size_bytes, size_t *free_bytes);
48
49// called once at kernel initialization
50void heap_init(void);
51
52__END_CDECLS
53