GithubHelp home page GithubHelp logo

hutorny / infiniray Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 9 KB

Infinite Array - A memory mirrored ring buffer

License: MIT License

C 3.02% C++ 96.98%
android circular-buffer mmap ring-buffer

infiniray's Introduction

infiniray - Infinite Array

A memory mirrored ring buffer

Introduction

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.

Features

infiniray follows design of std::array/std::vector, featuring:

  • header-only library, requiring only stdlib, sys/mman.h and unistd.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)

Requirements

  • C++17 capable compiler
  • For Android: Android NDK

Usage

  1. Obtain the sources

  2. Add infiniray/include to the include path

  3. 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
    
  4. 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);
    }
    

References

  1. https://abhinavag.medium.com/a-fast-circular-ring-buffer-4d102ef4d4a3
  2. https://github.com/smcho-kr/magic-ring-buffer
  3. https://fgiesen.wordpress.com/2012/07/21/the-magic-ring-buffer/
  4. https://gist.github.com/rygorous/3158316
  5. https://github.com/lava/linear_ringbuffer
  6. https://lo.calho.st/posts/black-magic-buffer/
  7. https://github.com/tmick0/toy-queue/
  8. https://github.com/google/wuffs/blob/main/script/mmap-ring-buffer.c

infiniray's People

Contributors

hutorny avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.