btrfs: [] on the end of a struct field is a variable length array.
[haiku.git] / headers / private / net / DynamicBuffer.h
blobddbd3c0a235ee4b9cf089ae6149db62c15b33361
1 /*
2 * Copyright 2009-2014, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Bruno Albuquerque, bga@bug-br.org.br
7 */
9 #ifndef _DYNAMIC_BUFFER_H
10 #define _DYNAMIC_BUFFER_H
12 #include <DataIO.h>
13 #include <SupportDefs.h>
16 class DynamicBuffer : public BDataIO {
17 public:
18 DynamicBuffer(size_t initialSize = 0);
19 ~DynamicBuffer();
21 DynamicBuffer(const DynamicBuffer& buffer);
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;
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);
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);
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;
41 // Returns the actual buffer size. This is the amount of memory allocated
42 // for the buffer.
43 size_t Size() const;
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;
49 // Dump some buffer statistics to stdout.
50 void PrintToStream();
52 private:
53 status_t _GrowToFit(size_t size, bool exact = false);
55 unsigned char* fBuffer;
56 size_t fBufferSize;
57 size_t fDataStart;
58 size_t fDataEnd;
60 status_t fInit;
63 #endif // _DYNAMIC_BUFFER_H