comparison TaskManager/kernel/ppe/DmaBuffer.cc @ 88:504899860e66

*** empty log message ***
author gongo
date Wed, 27 Feb 2008 17:16:29 +0900
parents 61bacd3b4405
children 1733f3cbfa28
comparison
equal deleted inserted replaced
87:265fb700dd4c 88:504899860e66
1 #include <stdio.h> 1 #include <stdio.h>
2 #include <stdlib.h> 2 #include <stdlib.h>
3 #include "types.h" 3 #include "types.h"
4 #include "DmaBuffer.h" 4 #include "DmaBuffer.h"
5
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 }
5 32
6 DmaBuffer::DmaBuffer(int size) 33 DmaBuffer::DmaBuffer(int size)
7 { 34 {
8 buffer[0] = malloc(size); 35 buffer[0] = malloc(size);
9 buffer[1] = malloc(size); 36 buffer[1] = malloc(size);
10 37
11 //posix_memalign(&buffer[0], DEFAULT_ALIGNMENT, size); 38 //posix_memalign(&buffer[0], DEFAULT_ALIGNMENT, size);
12 //posix_memalign(&buffer[1], DEFAULT_ALIGNMENT, size); 39 //posix_memalign(&buffer[1], DEFAULT_ALIGNMENT, size);
13 40
41 //buffer[0] = new_align(DEFAULT_ALIGNMENT, size);
42 //buffer[1] = new_align(DEFAULT_ALIGNMENT, size);
43
14 flag = 0; 44 flag = 0;
15 } 45 }
16 46
17 DmaBuffer::~DmaBuffer(void) 47 DmaBuffer::~DmaBuffer(void)
18 { 48 {
19 free(buffer[0]); 49 //free(buffer[0]);
20 free(buffer[1]); 50 //free(buffer[1]);
51 free_align(buffer[0]);
52 free_align(buffer[1]);
21 } 53 }
22 54
23 void 55 void
24 DmaBuffer::swap_buffer(void) 56 DmaBuffer::swap_buffer(void)
25 { 57 {