commit ff78a4668c5d23ec3bd048c1e015f058f9c4353e Author: kingecg Date: Sat Apr 8 22:35:17 2023 +0800 initial code commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4a0b530 --- /dev/null +++ b/.gitignore @@ -0,0 +1,74 @@ +# This file is used to ignore files which are generated +# ---------------------------------------------------------------------------- + +*~ +*.autosave +*.a +*.core +*.moc +*.o +*.obj +*.orig +*.rej +*.so +*.so.* +*_pch.h.cpp +*_resource.rc +*.qm +.#* +*.*# +core +!core/ +tags +.DS_Store +.directory +*.debug +Makefile* +*.prl +*.app +moc_*.cpp +ui_*.h +qrc_*.cpp +Thumbs.db +*.res +*.rc +/.qmake.cache +/.qmake.stash + +# qtcreator generated files +*.pro.user* +CMakeLists.txt.user* + +# xemacs temporary files +*.flc + +# Vim temporary files +.*.swp + +# Visual Studio generated files +*.ib_pdb_index +*.idb +*.ilk +*.pdb +*.sln +*.suo +*.vcproj +*vcproj.*.*.user +*.ncb +*.sdf +*.opensdf +*.vcxproj +*vcxproj.* + +# MinGW generated files +*.Debug +*.Release + +# Python byte code +*.pyc + +# Binaries +# -------- +*.dll +*.exe + diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..9464a17 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.14) + +project(ringbuffer LANGUAGES CXX) + +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +add_library(ringbuffer STATIC + ringbuffer.cpp + ringbuffer.h +) + +target_compile_definitions(ringbuffer PRIVATE RINGBUFFER_LIBRARY) diff --git a/ringbuffer.cpp b/ringbuffer.cpp new file mode 100644 index 0000000..96107e2 --- /dev/null +++ b/ringbuffer.cpp @@ -0,0 +1,87 @@ +#include "ringbuffer.h" +#include +#include +#include +Ringbuffer::Ringbuffer() +{ + Ringbuffer(1024*1024); +} + +Ringbuffer::Ringbuffer(size_t size) +{ + this->headptr = (unsigned char*)(malloc(size)); + this->pSize = size; + this->endptr = this->headptr + size; + this->readptr = this->headptr; + this->writeptr = this->headptr; +} + +Ringbuffer::~Ringbuffer() +{ + free(headptr); + headptr=endptr=0; + readptr=writeptr=0; +} + +size_t Ringbuffer::size() const +{ + return pSize; +} + +size_t Ringbuffer::size_free() +{ + return pSize - size_used(); +} + +size_t Ringbuffer::size_used() +{ + if(writeptr>=readptr){ + return writeptr - readptr; + } + return (writeptr-headptr) + (endptr - readptr); +} + +size_t Ringbuffer::read(void *buf, size_t bufSize) +{ + size_t currentStored = size_used(); + if(currentStored == 0){ + return 0; + } + + size_t read = currentStored>=bufSize?bufSize:currentStored; + if(readptr + read <= endptr){ + memcpy(buf, readptr, read); + readptr = readptr + read; + } else { + // read with two step + size_t first_size = endptr - readptr; + size_t second_size = read - first_size; + memcpy(buf, readptr, first_size); + unsigned char* buf_n = (unsigned char*)(buf) + first_size; + memcpy(buf_n, headptr, second_size); + readptr = headptr + second_size; + } + + return read; +} + +bool Ringbuffer::write(void *buf, size_t bufSize) +{ + size_t free = size_free(); + if(free < bufSize){ + return false; + } + + if(writeptr + bufSize <= endptr){ + memcpy(writeptr, buf, bufSize); + writeptr = writeptr + bufSize; + } else { + //write with two step + size_t first_size = endptr - writeptr; + size_t second_size = bufSize - first_size; + memcpy(writeptr, buf, first_size); + memcpy(headptr, (unsigned char*)buf+first_size, second_size); + writeptr = headptr + second_size; + } + return true; +} diff --git a/ringbuffer.h b/ringbuffer.h new file mode 100644 index 0000000..dbfb1c3 --- /dev/null +++ b/ringbuffer.h @@ -0,0 +1,25 @@ +#ifndef RINGBUFFER_H +#define RINGBUFFER_H + +#include +class Ringbuffer +{ +public: + Ringbuffer(); + Ringbuffer(size_t size); + ~Ringbuffer(); + size_t size() const; + size_t size_free(); + size_t size_used(); + size_t read(void *buf, size_t bufSize); + bool write(void *buf, size_t bufSize); + +private: + size_t pSize; + unsigned char* headptr; + unsigned char* endptr; + unsigned char* readptr; + unsigned char* writeptr; +}; + +#endif // RINGBUFFER_H