xref: /haiku/headers/private/net/DynamicBuffer.h (revision 922e7ba1f3228e6f28db69b0ded8f86eb32dea17)
1 /*
2  * Copyright 2009, 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 <SupportDefs.h>
13 
14 
15 class DynamicBuffer {
16 public:
17 	DynamicBuffer(size_t initialSize = 0);
18 	~DynamicBuffer();
19 
20 	DynamicBuffer(const DynamicBuffer& buffer);
21 
22 	// InitCheck() should be called to guarantee the object initialization
23 	// didn't fail. If it does not return B_OK, the initialization failed.
24 	status_t InitCheck() const;
25 
26 	// Insert data at the end of the buffer. The buffer will be increased to
27 	// accomodate the data if needed.
28 	status_t Insert(const void* data, size_t size);
29 
30 	// Remove data from the start of the buffer. Move the buffer start
31 	// pointer to point to the data following it.
32 	status_t Remove(void* data, size_t size);
33 
34 	// Return a pointer to the underlying buffer. Note this will not
35 	// necessarily be a pointer to the start of the allocated memory as the
36 	// initial data may be positioned at an offset inside the buffer. In other
37 	// words, this returns a pointer to the start of data inside the buffer.
38 	unsigned char* Data() const;
39 
40 	// Returns the actual buffer size. This is the amount of memory allocated
41 	// for the buffer.
42 	size_t Size() const;
43 
44 	// Number of bytes of data still present in the buffer that can be
45 	// extracted through calls to Remove().
46 	size_t BytesRemaining() const;
47 
48 	// Dump some buffer statistics to stdout.
49 	void PrintToStream();
50 
51 private:
52 	status_t _GrowToFit(size_t size, bool exact = false);
53 
54 	unsigned char* fBuffer;
55 	size_t fBufferSize;
56 	size_t fDataStart;
57 	size_t fDataEnd;
58 
59 	status_t fInit;
60 };
61 
62 #endif  // _DYNAMIC_BUFFER_H
63