54
|
1 #include <stdio.h>
|
|
2 #include <stdlib.h>
|
70
|
3 #include "types.h"
|
54
|
4 #include "DmaBuffer.h"
|
|
5
|
88
|
6 #define ALIGN_DOWN(v, align) ((v)&(-align))
|
|
7 #define ALIGN_UP(v, align) ALIGN_DOWN((v)+((align)-1), align)
|
|
8
|
|
9 void*
|
|
10 new_align(int alignment, int size)
|
|
11 {
|
|
12 char *nonalign;
|
|
13 char *align;
|
|
14
|
|
15 nonalign = new char[size + sizeof(void*) + alignment - 1];
|
|
16 align = (char*)(ALIGN_UP((unsigned int)nonalign+sizeof(void*),alignment));
|
|
17 *((void**)align - 1) = (void*)nonalign;
|
|
18
|
|
19 return align;
|
|
20 }
|
|
21
|
|
22 void
|
|
23 free_align(void *ptr)
|
|
24 {
|
|
25 char *real;
|
|
26
|
|
27 if (ptr != NULL) {
|
|
28 real = (char*)(*((void**)(ptr)-1));
|
|
29 delete real;
|
|
30 }
|
|
31 }
|
|
32
|
54
|
33 DmaBuffer::DmaBuffer(int size)
|
|
34 {
|
86
|
35 buffer[0] = malloc(size);
|
|
36 buffer[1] = malloc(size);
|
70
|
37
|
86
|
38 //posix_memalign(&buffer[0], DEFAULT_ALIGNMENT, size);
|
|
39 //posix_memalign(&buffer[1], DEFAULT_ALIGNMENT, size);
|
70
|
40
|
88
|
41 //buffer[0] = new_align(DEFAULT_ALIGNMENT, size);
|
|
42 //buffer[1] = new_align(DEFAULT_ALIGNMENT, size);
|
|
43
|
54
|
44 flag = 0;
|
|
45 }
|
|
46
|
|
47 DmaBuffer::~DmaBuffer(void)
|
|
48 {
|
88
|
49 //free(buffer[0]);
|
|
50 //free(buffer[1]);
|
|
51 free_align(buffer[0]);
|
|
52 free_align(buffer[1]);
|
54
|
53 }
|
|
54
|
|
55 void
|
|
56 DmaBuffer::swap_buffer(void)
|
|
57 {
|
|
58 flag ^= 1;
|
|
59 }
|
|
60
|
|
61 void
|
|
62 DmaBuffer::get_buffer(void *_dst)
|
|
63 {
|
|
64 void **dst = (void**)_dst;
|
|
65 *dst = buffer[flag];
|
|
66 }
|