-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathxmem.h
66 lines (50 loc) · 2.08 KB
/
xmem.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
#ifndef XMEM_H
#define XMEM_H
#ifdef __cplusplus
extern "C" {
#endif
#ifndef MEMORY_SIZE_KB
#error MEMORY_SIZE_KB definition is required with total dynamic memory size in kB
#endif
typedef unsigned char byte;
/* global MEMORY[] array */
__attribute__ ((aligned(8)))
byte MEMORY[1024ul * MEMORY_SIZE_KB];
#include <stdlib.h> /* needed for the size_t definition */
/* initialise or reinitialise the entire memory */
/* needs to be called initially before referring to any other xmem function */
/* will return the size of the memory */
size_t x_meminit(void);
/* allocate block or change size of already allocated one */
/*
when calling for initial allocation the variable must be previously initialised with NULL
the new memory block is cleared
if the block is being extended, the extension area is cleared
will update the supplied variable with the pointer, or NULL in case of unsuccessful allocation
The function usually returns pointer to the memory block with the exception of cases when a
block is already allocated and needs changing its size. If unsuccessful, the function will
return NULL but the original block will not be affected
*/
void *x_malloc(byte **var, size_t sz);
/* free allocated block */
/*
will do nothing if the parameter doesn't point to a valid allocated block
will update the variable with NULL
return 0 if successful, or -1 for error
*/
int x_free(byte **var);
/* return the actual size of an allocated block */
size_t x_blksize(byte *v);
/* return the size of the largest continuous currently available block */
size_t x_avail(void);
/* return the total size of the currently available memory (could be fragmented in many separate blocks) */
size_t x_total(void);
/* list all currently allocated blocks (FOR DEBUG) */
void x_list_alloc(void);
/* optimise the memory and try to free up a block with size (sz) or more */
/* NOTE: this is typically an internal function, but can be called externally in case of a need */
void x_defrag(size_t sz);
#ifdef __cplusplus
}
#endif
#endif /* XMEM_H */