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
17namespace BPackageKit {
18
19namespace BHPKG {
20
21namespace BPrivate {
22
23
24class PoolBuffer : public DoublyLinkedListLinkImpl<PoolBuffer> {
25public:
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
41private:
42			PoolBuffer**		fOwner;
43			void*				fBuffer;
44			size_t				fSize;
45			bool				fCached;
46};
47
48
49class PoolBufferPutter {
50public:
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
77private:
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