-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathdma_buf_alloc.cpp
More file actions
34 lines (27 loc) · 793 Bytes
/
dma_buf_alloc.cpp
File metadata and controls
34 lines (27 loc) · 793 Bytes
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
#include "dma_buf_alloc.h"
#include <stdexcept>
#include <fcntl.h>
#include <unistd.h>
#include <linux/dma-buf.h>
#include <linux/dma-heap.h>
#include <sys/ioctl.h>
DmaBufAlloc::DmaBufAlloc(const std::string &heap_name) {
int heap_fd = open(heap_name.c_str(), O_RDWR, 0);
if (heap_fd < 0) {
throw std::runtime_error("failed to open dma_heap");
}
m_heap_fd = heap_fd;
}
int DmaBufAlloc::alloc_buf(std::size_t len) {
struct dma_heap_allocation_data alloc = {};
alloc.len = len;
alloc.fd_flags = O_CLOEXEC | O_RDWR;
int success = ioctl(m_heap_fd, DMA_HEAP_IOCTL_ALLOC, &alloc);
if (success < 0) {
throw std::runtime_error("failed to allocate dma-heap");
}
return alloc.fd;
}
void DmaBufAlloc::free_buf(int fd) {
close(fd);
}