-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdma.c
71 lines (61 loc) · 1.28 KB
/
dma.c
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
70
71
#include "config.h"
#include "dma.h"
#include "types.h"
#if H8_NO_DMA
static h8_u8 h8_heap[H8_NO_DMA_SIZE];
static unsigned h8_heap_alloc = 0;
void h8_dma_oom_cb_default(void)
{
printf("Out of memory error! (%u of %u)\n", h8_heap_alloc, H8_NO_DMA_SIZE);
exit(1);
}
static void (*h8_dma_oom_cb)(void) = h8_dma_oom_cb_default;
#else
#include <stdlib.h>
#endif
void *h8_dma_alloc(unsigned size, unsigned zero)
{
/**
* Implements very simple DMA for embedded systems that have no access to
* malloc. Uses a static-sized heap, allocates purely linearly, and does not
* actually free values. Use only if absolutely necessary.
*/
#if H8_NO_DMA
if (size + h8_heap_alloc >= H8_NO_DMA_SIZE)
{
if (h8_dma_oom_cb)
h8_dma_oom_cb();
return NULL;
}
else
{
h8_u8 *allocated_value = &h8_heap[h8_heap_alloc];
if (zero)
{
unsigned offset;
for (offset = h8_heap_alloc; offset < h8_heap_alloc + size; offset++)
h8_heap[offset] = 0;
}
h8_heap_alloc += size;
return allocated_value;
}
#else
return zero ? calloc(size, 1) : malloc(size);
#endif
}
void h8_dma_free(void *value)
{
#if H8_NO_DMA
H8_UNUSED(value);
#else
free(value);
#endif
}
void h8_dma_set_oom_cb(void *cb)
{
#if H8_NO_DMA
h8_dma_oom_cb = cb;
#else
H8_UNUSED(cb);
#endif
}