initial code commit
This commit is contained in:
commit
ff78a4668c
|
@ -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
|
||||
|
|
@ -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)
|
|
@ -0,0 +1,87 @@
|
|||
#include "ringbuffer.h"
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <cstddef>
|
||||
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;
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
#ifndef RINGBUFFER_H
|
||||
#define RINGBUFFER_H
|
||||
|
||||
#include <cstddef>
|
||||
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
|
Loading…
Reference in New Issue