src/lib/TransactionalBuffer.h
branchv_0
changeset 1 68a281aefa76
equal deleted inserted replaced
0:28294b895e5e 1:68a281aefa76
       
     1 /**
       
     2  * Relational pipes
       
     3  * Copyright © 2021 František Kučera (Frantovo.cz, GlobalCode.info)
       
     4  *
       
     5  * This program is free software: you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation, version 3 of the License.
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
       
    16  */
       
    17 #pragma once
       
    18 
       
    19 #include <string>
       
    20 #include <exception>
       
    21 #include <cstring>
       
    22 
       
    23 #include "AbstractParser.h"
       
    24 
       
    25 namespace relpipe {
       
    26 namespace in {
       
    27 namespace asn1 {
       
    28 namespace lib {
       
    29 
       
    30 class TransactionalBuffer {
       
    31 private:
       
    32 	uint8_t* buffer;
       
    33 	size_t bufferSize;
       
    34 	bool hasData = false;
       
    35 	size_t writePosition = 0;
       
    36 	// size_t writePositionCommited = 0; // TODO: transactions also on the writing side?
       
    37 	size_t readPosition = 0;
       
    38 	size_t readPositionCommited = 0;
       
    39 
       
    40 	size_t bytesRead = 0;
       
    41 	size_t bytesReadCommited = 0;
       
    42 
       
    43 	size_t availableForReading() {
       
    44 		if (readPosition < writePosition) return writePosition - readPosition;
       
    45 		else if (readPosition > writePosition) return bufferSize - readPosition + writePosition;
       
    46 		else if (hasData) return bufferSize;
       
    47 		else return 0;
       
    48 	}
       
    49 
       
    50 	size_t availableForWriting() {
       
    51 		if (readPositionCommited < writePosition) return bufferSize - writePosition + readPositionCommited;
       
    52 		else if (readPositionCommited > writePosition) return readPositionCommited - writePosition;
       
    53 		else if (hasData) return 0;
       
    54 		else return bufferSize;
       
    55 	}
       
    56 
       
    57 public:
       
    58 
       
    59 	/**
       
    60 	 * Is thrown from read() and peak() methods if there are not enough data.
       
    61 	 * Interrupts current update() cycle and causes rollback.
       
    62 	 */
       
    63 	class ReadBufferUnderflowException {
       
    64 		// TODO: common super-class for exceptions, hidden implementation
       
    65 	};
       
    66 
       
    67 	/**
       
    68 	 * Is thrown from write() method of the buffer when there is not enough space.
       
    69 	 * Interrupts whole process.
       
    70 	 */
       
    71 	class WriteBufferOverflowException {
       
    72 	};
       
    73 
       
    74 	TransactionalBuffer(size_t initialSize = 4096) : bufferSize(initialSize) {
       
    75 		// TODO: initial size + resize + hard upper limit
       
    76 		buffer = (uint8_t*) malloc(bufferSize);
       
    77 	}
       
    78 
       
    79 	virtual ~TransactionalBuffer() {
       
    80 		free(buffer);
       
    81 	}
       
    82 
       
    83 	void write(const uint8_t* inputBuffer, const size_t length) {
       
    84 		if (length == 0) return;
       
    85 		if (length > availableForWriting()) throw WriteBufferOverflowException(); // TODO: optional resize
       
    86 
       
    87 		hasData = true;
       
    88 
       
    89 		const size_t a = std::min(length, bufferSize - writePosition);
       
    90 		const size_t b = length - a;
       
    91 
       
    92 		// TODO: map buffer twice in the memory in order to create a continuous area and avoid the second memcpy()?
       
    93 		memcpy(buffer + writePosition, inputBuffer, a);
       
    94 		memcpy(buffer, inputBuffer + a, b);
       
    95 
       
    96 		writePosition = (writePosition + length) % bufferSize;
       
    97 	}
       
    98 
       
    99 	void read(uint8_t* outputBuffer, const size_t length) {
       
   100 		if (length == 0) return;
       
   101 
       
   102 		peek(outputBuffer, length);
       
   103 		readPosition = (readPosition + length) % bufferSize;
       
   104 		hasData = readPosition != writePosition;
       
   105 		bytesRead += length;
       
   106 	}
       
   107 
       
   108 	void peek(uint8_t* outputBuffer, const size_t length) {
       
   109 		if (length == 0) return;
       
   110 		if (length > availableForReading()) throw ReadBufferUnderflowException();
       
   111 
       
   112 		const size_t a = std::min(length, bufferSize - readPosition);
       
   113 		const size_t b = length - a;
       
   114 
       
   115 		memcpy(outputBuffer, buffer + readPosition, a);
       
   116 		memcpy(outputBuffer + a, buffer, b);
       
   117 	}
       
   118 
       
   119 	void commitRead() {
       
   120 		readPositionCommited = readPosition;
       
   121 		bytesReadCommited = bytesRead;
       
   122 	}
       
   123 
       
   124 	void rollbackRead() {
       
   125 		readPosition = readPositionCommited;
       
   126 		bytesRead = bytesReadCommited;
       
   127 	}
       
   128 
       
   129 	size_t getBytesRead() {
       
   130 		return bytesRead;
       
   131 	}
       
   132 
       
   133 };
       
   134 
       
   135 
       
   136 }
       
   137 }
       
   138 }
       
   139 }