xref: /haiku/headers/private/package/hpkg/PoolBuffer.h (revision e81a954787e50e56a7f06f72705b7859b6ab06d1)
1 /*
2  * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Copyright 2011, Oliver Tappe <zooey@hirschkaefer.de>
4  * Distributed under the terms of the MIT License.
5  */
6 #ifndef _PACKAGE__HPKG__PRIVATE__POOL_BUFFER_H_
7 #define _PACKAGE__HPKG__PRIVATE__POOL_BUFFER_H_
8 
9 
10 #include <stddef.h>
11 
12 #include <util/DoublyLinkedList.h>
13 
14 #include <package/hpkg/BufferPool.h>
15 
16 
17 namespace BPackageKit {
18 
19 namespace BHPKG {
20 
21 namespace BPrivate {
22 
23 
24 class PoolBuffer : public DoublyLinkedListLinkImpl<PoolBuffer> {
25 public:
26 								PoolBuffer(size_t size);
27 								~PoolBuffer();
28 
29 			void*				Buffer() const	{ return fBuffer; }
30 			size_t				Size() const	{ return fSize; }
31 
32 
33 			// implementation private
34 			PoolBuffer**		Owner() const	{ return fOwner; }
35 			void				SetOwner(PoolBuffer** owner)
36 									{ fOwner = owner; }
37 
38 			void				SetCached(bool cached)	{ fCached = cached; }
39 			bool				IsCached() const		{ return fCached; }
40 
41 private:
42 			PoolBuffer**		fOwner;
43 			void*				fBuffer;
44 			size_t				fSize;
45 			bool				fCached;
46 };
47 
48 
49 class PoolBufferPutter {
50 public:
51 	PoolBufferPutter(BBufferPool* pool, PoolBuffer** owner)
52 		:
53 		fPool(pool),
54 		fOwner(owner),
55 		fBuffer(NULL)
56 	{
57 	}
58 
59 	PoolBufferPutter(BBufferPool* pool, PoolBuffer* buffer)
60 		:
61 		fPool(pool),
62 		fOwner(NULL),
63 		fBuffer(buffer)
64 	{
65 	}
66 
67 	~PoolBufferPutter()
68 	{
69 		if (fPool != NULL) {
70 			if (fOwner != NULL)
71 				fPool->PutBufferAndCache(fOwner);
72 			else if (fBuffer != NULL)
73 				fPool->PutBuffer(&fBuffer);
74 		}
75 	}
76 
77 private:
78 	BBufferPool*	fPool;
79 	PoolBuffer**	fOwner;
80 	PoolBuffer*		fBuffer;
81 };
82 
83 
84 }	// namespace BPrivate
85 
86 }	// namespace BHPKG
87 
88 }	// namespace BPackageKit
89 
90 
91 #endif	// _PACKAGE__HPKG__PRIVATE__POOL_BUFFER_H_
92