-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcheap.h
69 lines (51 loc) · 2.55 KB
/
mcheap.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
MCHEAP Dynamic memory allocator.
Configuration
*************
The following symbols may be defined to configure heap features:
MCHEAP_SIZE
The heap size in bytes. If this is not defined the default value of 1024 will be used.
MCHEAP_ALIGNMENT
Ensure all allocations are aligned to the specified byte boundary.
If this is not defined, the default is __BIGGEST_ALIGNMENT__
MCHEAP_ADDRESS
Specify a fixed memory address for the heap. This is useful for parts which may have external RAM not covered by the linker script.
If this is not defined, the heap space will simply be a static uint8_t[] within the BSS section.
**CAUTION** If this is used, the address provided MUST respect the MCHEAP_ALIGNMENT provided, or an alignment of __BIGGEST_ALIGNMENT__
*/
#ifndef _MCHEAP_H_
#define _MCHEAP_H_
#include <stdbool.h>
#include <stddef.h>
//********************************************************************************************************
// Public defines
//********************************************************************************************************
//********************************************************************************************************
// Public variables
//********************************************************************************************************
//********************************************************************************************************
// Public prototypes
//********************************************************************************************************
// Allocate memory and return it's address, or NULL on failure.
void* mcheap_allocate(size_t size);
/* Reallocate the memory at *ptr to be a new size.
If ptr is NULL, attempt a new allocation.
If size is 0, free the allocation and return NULL.
Preferred reallocate methods from 1st to last are:
* relocate to a lower address
* extend down (or shift down if new size is smaller)
* shrink in place
* extend up
* relocate to a higher address.
If heap_reallocate() fails, it will return NULL.*/
void* mcheap_reallocate(void* ptr, size_t size);
// Free the allocation, always returns NULL
void* mcheap_free(void* ptr);
// Return largest possible allocation that can currently be made.
size_t mcheap_largest_free(void);
// Return true if all the heap meta data is valid and intact.
bool mcheap_is_intact(void);
// If the heap is broken, this can re-initialize it.
// This is used after test cases which break the heap on purpose.
void mcheap_reinit(void);
#endif