Compare commits

...

1 Commits

Author SHA1 Message Date
Bartosz Podrygajlo
32471f94ba Wraparound buffer for modulo-free mapping/demapping in FFT buffers.
Added an implementation of a wraparound buffer using virtual address mapping.
With the wraparound buffer a physical address is mapped a second time to virtual
memory right after the initial mapping. That makes it so that:

  buf[i] == buff[i + size]

The offset operation is undertaken by the processor MMU instead.
2025-12-16 16:53:32 +01:00
6 changed files with 201 additions and 0 deletions

View File

@@ -23,3 +23,4 @@ endif()
add_subdirectory(barrier)
add_subdirectory(actor)
add_subdirectory(shm_iq_channel)
add_subdirectory(wraparound_buffer)

View File

@@ -0,0 +1,6 @@
add_library(wraparound_buffer wraparound_buffer.cpp)
target_include_directories(wraparound_buffer PUBLIC ./)
target_link_libraries(wraparound_buffer PUBLIC LOG)
if (ENABLE_TESTS)
add_subdirectory(tests)
endif()

View File

@@ -0,0 +1,5 @@
add_executable(test_wraparound_buffer test_wraparound_buffer.cpp)
add_dependencies(tests test_wraparound_buffer)
target_link_libraries(test_wraparound_buffer PRIVATE wraparound_buffer pthread LOG minimal_lib GTest::gtest)
add_test(NAME test_wraparound_buffer
COMMAND ./test_wraparound_buffer)

View File

@@ -0,0 +1,67 @@
/*
* Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The OpenAirInterface Software Alliance licenses this file to You under
* the OAI Public License, Version 1.1 (the "License"); you may not use this file
* except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.openairinterface.org/?page_id=698
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*-------------------------------------------------------------------------------
* For more information about the OpenAirInterface (OAI) Software Alliance:
* contact@openairinterface.org
*/
#include <gtest/gtest.h>
#include "wraparound_buffer.h"
#include "log.h"
#include <unistd.h>
// Thread safety can be ensured through core affinity - if two actors
// are running on the same core they are thread safe
TEST(wraparound_buffer, basic_logic)
{
size_t buf_size = getpagesize() * 2;
void *buffer = malloc_wraparound_buffer(buf_size);
ASSERT_NE(buffer, nullptr);
uint8_t *buffptr = static_cast<uint8_t *>(buffer);
memset(buffer, 0, buf_size);
buffptr[0] = 1;
EXPECT_EQ(buffptr[buf_size], 1);
free_wraparound_buffer(buffer);
}
TEST(wraparound_buffer, full_range)
{
size_t buf_size = getpagesize() * 2;
void *buffer = malloc_wraparound_buffer(buf_size);
ASSERT_NE(buffer, nullptr);
uint8_t *buffptr = static_cast<uint8_t *>(buffer);
memset(buffer, 0, buf_size);
for (auto i = 0U; i < buf_size; i++) {
buffptr[i] = (uint8_t)i;
EXPECT_EQ(buffptr[i + buf_size], (uint8_t)i);
}
free_wraparound_buffer(buffer);
}
TEST(wraparound_buffer, size)
{
size_t buf_size = getpagesize() * 2 + 1;
void *buffer = malloc_wraparound_buffer(buf_size);
ASSERT_EQ(buffer, nullptr);
}
int main(int argc, char **argv)
{
logInit();
g_log->log_component[UTIL].level = OAILOG_DEBUG;
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@@ -0,0 +1,86 @@
/*
* Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The OpenAirInterface Software Alliance licenses this file to You under
* the OAI Public License, Version 1.1 (the "License"); you may not use this file
* except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.openairinterface.org/?page_id=698
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*-------------------------------------------------------------------------------
* For more information about the OpenAirInterface (OAI) Software Alliance:
* contact@openairinterface.org
*/
#include "wraparound_buffer.h"
#include <unistd.h>
#include <sys/mman.h>
#include <map>
#include <mutex>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include "log.h"
#include "assertions.h"
struct {
std::map<void*, size_t> map;
std::mutex mtx;
} internal_state;
extern "C" void* malloc_wraparound_buffer(size_t size)
{
size_t pagesize = getpagesize();
if (size % pagesize != 0) {
return NULL;
}
int fd = memfd_create("wraparound", MFD_CLOEXEC);
if (fd == -1) return NULL;
if (ftruncate(fd, size) != 0) {
close(fd);
return NULL;
}
void* region = mmap(NULL, size * 2, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (region == MAP_FAILED) {
close(fd);
return NULL;
}
void* buf = mmap(region, size, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_SHARED, fd, 0);
if (buf == MAP_FAILED) {
munmap(region, size * 2);
close(fd);
return NULL;
}
uint8_t *second_half = (uint8_t *)region + size;
void* mirror = mmap(second_half, size, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_SHARED, fd, 0);
if (mirror == MAP_FAILED) {
munmap(region, size * 2);
close(fd);
return NULL;
}
close(fd);
std::lock_guard<std::mutex> lock(internal_state.mtx);
internal_state.map[buf] = size;
return buf;
}
extern "C" void free_wraparound_buffer(void* buf) {
if (buf == nullptr) return;
std::lock_guard<std::mutex> lock(internal_state.mtx);
size_t size = internal_state.map[buf];
if (munmap(buf, size * 2) != 0) {
LOG_W(UTIL, "munmap failed\n");
}
}

View File

@@ -0,0 +1,36 @@
/*
* Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The OpenAirInterface Software Alliance licenses this file to You under
* the OAI Public License, Version 1.1 (the "License"); you may not use this file
* except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.openairinterface.org/?page_id=698
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*-------------------------------------------------------------------------------
* For more information about the OpenAirInterface (OAI) Software Alliance:
* contact@openairinterface.org
*/
#ifndef __WRAPAROUND_BUFFER_H__
#define __WRAPAROUND_BUFFER_H__
#include <memory.h>
#ifdef __cplusplus
extern "C" {
#endif
void *malloc_wraparound_buffer(size_t size);
void free_wraparound_buffer(void *buf);
#ifdef __cplusplus
}
#endif
#endif