1/*
2 * Copyright 2014, NICTA
3 *
4 * This software may be distributed and modified according to the terms of
5 * the BSD 2-Clause license. Note that NO WARRANTY is provided.
6 * See "LICENSE_BSD2.txt" for details.
7 *
8 * @TAG(NICTA_BSD)
9 */
10
11#ifndef _ALLOC_H_
12#define _ALLOC_H_
13
14#ifndef NULL
15#define NULL ((void *)0)
16#endif
17
18/* System word size. */
19typedef unsigned long word_t;
20
21/*
22 * Allocator memory node.
23 *
24 * Used as a node in a linked list tracking free memory regions.
25 */
26struct mem_node {
27    word_t size;
28    struct mem_node *next;
29};
30
31/*
32 * Heap object.
33 *
34 * Contains a pointer to the first node in the chain, and also keeps
35 * track of the number of allocations performed, so we know when the
36 * entire heap is free.
37 */
38struct heap {
39    word_t num_allocs;
40    struct mem_node *head;
41};
42
43/* Minimum granuality of the allocator (log2 of number of bytes). */
44#define ALLOC_CHUNK_SIZE_BITS 3
45
46/* Minimum alignment that the allocator will return. */
47#define DEFAULT_ALIGNMENT_BITS 3
48
49void *ualloc(struct heap* heap, word_t size);
50
51void *alloc(struct heap *heap, word_t size);
52
53void udealloc(struct heap* heap, void* ptr);
54
55void dealloc(struct heap *heap, void *ptr, word_t size);
56
57void add_mem_pool(struct heap *heap, void *ptr, word_t size);
58
59void init_allocator(struct heap *init_heap, struct mem_node *init_mem_node);
60
61#endif /* _ALLOC_H_ */
62