diff --git a/src/utils/dma_alloc.cpp b/src/utils/dma_alloc.cpp index 50cf645..7326ff6 100644 --- a/src/utils/dma_alloc.cpp +++ b/src/utils/dma_alloc.cpp @@ -175,18 +175,37 @@ DmaBufferPtr DmaAlloc(size_t alloc_size) { nullptr }; - int heap_fd = -1; + static bool first_success = true; + + int dma_fd = -1; const char* used_heap = nullptr; + int last_errno = 0; for (int i = 0; heap_paths[i] != nullptr; ++i) { - heap_fd = open(heap_paths[i], O_RDWR | O_CLOEXEC); - if (heap_fd >= 0) { + int heap_fd = open(heap_paths[i], O_RDWR | O_CLOEXEC); + if (heap_fd < 0) { + last_errno = errno; + continue; + } + + dma_heap_allocation_data alloc_data{}; + alloc_data.len = alloc_size; + alloc_data.fd_flags = O_RDWR | O_CLOEXEC; + alloc_data.heap_flags = 0; + + if (ioctl(heap_fd, DMA_HEAP_IOCTL_ALLOC, &alloc_data) == 0) { + dma_fd = static_cast(alloc_data.fd); used_heap = heap_paths[i]; + close(heap_fd); break; } + + last_errno = errno; + close(heap_fd); + continue; } - if (heap_fd < 0) { - std::cerr << "[DmaAlloc] failed to open dma_heap device: " << strerror(errno) << "\n"; + if (dma_fd < 0) { + std::cerr << "[DmaAlloc] DMA_HEAP_IOCTL_ALLOC failed on all heaps: " << strerror(last_errno) << "\n"; std::cerr << "[DmaAlloc] tried: "; for (int i = 0; heap_paths[i] != nullptr; ++i) { std::cerr << heap_paths[i] << " "; @@ -195,27 +214,11 @@ DmaBufferPtr DmaAlloc(size_t alloc_size) { return nullptr; } - static bool first_alloc = true; - if (first_alloc) { + if (first_success) { std::cout << "[DmaAlloc] using heap: " << used_heap << "\n"; - first_alloc = false; + first_success = false; } - dma_heap_allocation_data alloc_data{}; - alloc_data.len = alloc_size; - alloc_data.fd_flags = O_RDWR | O_CLOEXEC; - alloc_data.heap_flags = 0; - - if (ioctl(heap_fd, DMA_HEAP_IOCTL_ALLOC, &alloc_data) < 0) { - std::cerr << "[DmaAlloc] DMA_HEAP_IOCTL_ALLOC failed: " << strerror(errno) << "\n"; - close(heap_fd); - return nullptr; - } - - close(heap_fd); - - int dma_fd = static_cast(alloc_data.fd); - void* ptr = mmap(nullptr, alloc_size, PROT_READ | PROT_WRITE, MAP_SHARED, dma_fd, 0); if (ptr == MAP_FAILED) { std::cerr << "[DmaAlloc] mmap failed: " << strerror(errno) << "\n";