A memory mirrored ring buffer
Using mmap
to create a mirrored region for a ring buffer is not a new idea,
there are several solution available [1], [2], [3], [4], [5], [6], [7], [8].
Although, each of infiniray features is not unique, their collection is believed
to be unique.
infiniray follows design of std::array
/std::vector
, featuring:
- header-only library, requiring only
stdlib
,sys/mman.h
andunistd.h
- memory allocation abstracted into
allocator
- element access operators
being
/end
iterators- initialization bypass for trivially constructible data type
- mirrored_region abstraction
fno-exceptions
compatibility- Android support (at NDK level)
- C++17 capable compiler
- For Android: Android NDK
-
Obtain the sources
-
Add
infiniray/include
to the include path -
Use it in your code:
#include <infiniray.h> // Include the main header infinite::array<long long> buffer(1024); // Declare an buffer of desired capacity //... buffer.append(data); // Append data at the end buffer.push_back(value); // push back an element buffer.emplace_back(value); // emplace an element //... buffer.erase(512); // Erase elements in the front
-
To use on Android, provide a path to a temporary directory, accessible to the app with
infinite::ashmem::region::settmpdir
, as in the following example:extern "C" JNIEXPORT void JNICALL Java_Infiniray_setTempDir(JNIEnv *env, jobject thiz, jstring jPath) { jboolean is_path_copy{}; const char* const path = env->GetStringUTFChars(jPath, &is_path_copy); infinite::ashmem::region::settmpdir(path); if (is_path_copy) env->ReleaseStringUTFChars(jPath, path); }
- https://abhinavag.medium.com/a-fast-circular-ring-buffer-4d102ef4d4a3
- https://github.com/smcho-kr/magic-ring-buffer
- https://fgiesen.wordpress.com/2012/07/21/the-magic-ring-buffer/
- https://gist.github.com/rygorous/3158316
- https://github.com/lava/linear_ringbuffer
- https://lo.calho.st/posts/black-magic-buffer/
- https://github.com/tmick0/toy-queue/
- https://github.com/google/wuffs/blob/main/script/mmap-ring-buffer.c