// Copyright (2019-2024) Paul Scherrer Institute #ifdef JFJOCH_USE_NUMA #include #endif #include #include "SendBuffer.h" #include "JFJochException.h" SendBuffer::SendBuffer(size_t in_buffer_size) : buffer_size(in_buffer_size) { #ifdef JFJOCH_USE_NUMA buffer = (uint8_t *) numa_alloc_interleaved(buffer_size); #else buffer = (uint8_t *) mmap (nullptr, buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) ; #endif if (buffer == nullptr) throw JFJochException(JFJochExceptionCategory::MemAllocFailed, "Failed to allocate communication buffer"); memset(buffer, 0, in_buffer_size); } SendBuffer::~SendBuffer() { #ifdef JFJOCH_USE_NUMA numa_free(buffer, buffer_size); #else munmap(buffer, buffer_size); #endif } void SendBuffer::SetBufferLocationSize(size_t in_location_size) { if (buffer_size < in_location_size) throw JFJochException(JFJochExceptionCategory::InputParameterInvalid, "Buffer is to small to hold even 1 image"); location_size = in_location_size; location_number = buffer_size / in_location_size; } size_t SendBuffer::GetBufferLocationSize() const { return location_size; } size_t SendBuffer::GetBufferLocationID(const uint8_t *ptr) const { if (location_size == 0) throw JFJochException(JFJochExceptionCategory::WrongNumber, "Buffer not initialized"); if (ptr < buffer) throw JFJochException(JFJochExceptionCategory::WrongNumber, "Pointer outside of buffer location"); if (ptr >= buffer + buffer_size) throw JFJochException(JFJochExceptionCategory::WrongNumber, "Pointer outside of buffer location"); return (ptr - buffer) / location_size; } size_t SendBuffer::GetNumOfLocations() const { if (location_size == 0) throw JFJochException(JFJochExceptionCategory::WrongNumber, "Buffer not initialized"); return location_number; } uint8_t *SendBuffer::GetBufferLocation(size_t id) { if (location_size == 0) throw JFJochException(JFJochExceptionCategory::WrongNumber, "Buffer not initialized"); if (id >= location_number) throw JFJochException(JFJochExceptionCategory::ArrayOutOfBounds, "Image entry out of buffer bounds"); return buffer + location_size * id; }