src/lib/TransactionalBuffer.h
branchv_0
changeset 10 6904e4448807
child 13 d5e2cb9e31f1
equal deleted inserted replaced
9:7a6abdd00ab5 10:6904e4448807
       
     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 	char* 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 availableForReading() {
       
    41 		if (readPosition < writePosition) return writePosition - readPosition;
       
    42 		else if (readPosition > writePosition) return bufferSize - readPosition + writePosition;
       
    43 		else if (hasData) return bufferSize;
       
    44 		else return 0;
       
    45 	}
       
    46 
       
    47 	size_t availableForWriting() {
       
    48 		if (readPositionCommited < writePosition) return bufferSize - writePosition + readPositionCommited;
       
    49 		else if (readPositionCommited > writePosition) return readPositionCommited - writePosition;
       
    50 		else if (hasData) return 0;
       
    51 		else return bufferSize;
       
    52 	}
       
    53 
       
    54 public:
       
    55 
       
    56 		/**
       
    57 	 * Is thrown from read() and peak() methods if there are not enough data.
       
    58 	 * Interrupts current update() cycle and causes rollback.
       
    59 	 */
       
    60 	class ReadBufferUnderflowException {
       
    61 		// TODO: common super-class for exceptions, hidden implementation
       
    62 	};
       
    63 
       
    64 	/**
       
    65 	 * Is thrown from write() method of the buffer when there is not enough space.
       
    66 	 * Interrupts whole process.
       
    67 	 */
       
    68 	class WriteBufferOverflowException {
       
    69 	};
       
    70 
       
    71 	TransactionalBuffer(size_t initialSize = 4096) : bufferSize(initialSize) {
       
    72 		// TODO: initial size + resize + hard upper limit
       
    73 		buffer = (char*) malloc(bufferSize);
       
    74 	}
       
    75 
       
    76 	virtual ~TransactionalBuffer() {
       
    77 		free(buffer);
       
    78 	}
       
    79 
       
    80 	void write(const char* inputBuffer, const size_t length) {
       
    81 		if (length == 0) return;
       
    82 		if (length > availableForWriting()) throw WriteBufferOverflowException(); // TODO: optional resize
       
    83 
       
    84 		hasData = true;
       
    85 
       
    86 		const size_t a = std::min(length, bufferSize - writePosition);
       
    87 		const size_t b = length - a;
       
    88 
       
    89 		// TODO: map buffer twice in the memory in order to create a continuous area and avoid the second memcpy()?
       
    90 		memcpy(buffer + writePosition, inputBuffer, a);
       
    91 		memcpy(buffer, inputBuffer + a, b);
       
    92 
       
    93 		writePosition = (writePosition + length) % bufferSize;
       
    94 	}
       
    95 
       
    96 	void read(char* outputBuffer, const size_t length) {
       
    97 		if (length == 0) return;
       
    98 
       
    99 		peek(outputBuffer, length);
       
   100 		readPosition = (readPosition + length) % bufferSize;
       
   101 		hasData = readPosition != writePosition;
       
   102 	}
       
   103 
       
   104 	void peek(char* outputBuffer, const size_t length) {
       
   105 		if (length == 0) return;
       
   106 		if (length > availableForReading()) throw ReadBufferUnderflowException();
       
   107 
       
   108 		const size_t a = std::min(length, bufferSize - readPosition);
       
   109 		const size_t b = length - a;
       
   110 
       
   111 		memcpy(outputBuffer, buffer + readPosition, a);
       
   112 		memcpy(outputBuffer + a, buffer, b);
       
   113 	}
       
   114 
       
   115 	void commitRead() {
       
   116 		readPositionCommited = readPosition;
       
   117 	}
       
   118 
       
   119 	void rollbackRead() {
       
   120 		readPosition = readPositionCommited;
       
   121 	}
       
   122 };
       
   123 
       
   124 
       
   125 }
       
   126 }
       
   127 }
       
   128 }