1 /* 2 * Copyright 2009-2014, Haiku, Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Bruno Albuquerque, bga@bug-br.org.br 7 */ 8 9 #ifndef _DYNAMIC_BUFFER_H 10 #define _DYNAMIC_BUFFER_H 11 12 #include <DataIO.h> 13 #include <SupportDefs.h> 14 15 16 class DynamicBuffer : public BDataIO { 17 public: 18 DynamicBuffer(size_t initialSize = 0); 19 ~DynamicBuffer(); 20 21 DynamicBuffer(const DynamicBuffer& buffer); 22 23 // InitCheck() should be called to guarantee the object initialization 24 // didn't fail. If it does not return B_OK, the initialization failed. 25 status_t InitCheck() const; 26 27 // Insert data at the end of the buffer. The buffer will be increased to 28 // accomodate the data if needed. 29 virtual ssize_t Write(const void* data, size_t size); 30 31 // Remove data from the start of the buffer. Move the buffer start 32 // pointer to point to the data following it. 33 virtual ssize_t Read(void* data, size_t size); 34 35 // Return a pointer to the underlying buffer. Note this will not 36 // necessarily be a pointer to the start of the allocated memory as the 37 // initial data may be positioned at an offset inside the buffer. In other 38 // words, this returns a pointer to the start of data inside the buffer. 39 unsigned char* Data() const; 40 41 // Returns the actual buffer size. This is the amount of memory allocated 42 // for the buffer. 43 size_t Size() const; 44 45 // Number of bytes of data still present in the buffer that can be 46 // extracted through calls to Remove(). 47 size_t BytesRemaining() const; 48 49 // Dump some buffer statistics to stdout. 50 void PrintToStream(); 51 52 private: 53 status_t _GrowToFit(size_t size, bool exact = false); 54 55 unsigned char* fBuffer; 56 size_t fBufferSize; 57 size_t fDataStart; 58 size_t fDataEnd; 59 60 status_t fInit; 61 }; 62 63 #endif // _DYNAMIC_BUFFER_H 64