xref: /haiku/headers/os/support/BlockCache.h (revision 087882c26e81eabf9a459e634a33f8e978d8a66b)
12fc06b87Sbeveloper /*
22fc06b87Sbeveloper  * Copyright (c) 2003 Marcus Overhagen
32fc06b87Sbeveloper  *
42fc06b87Sbeveloper  * Permission is hereby granted, free of charge, to any person obtaining a
52fc06b87Sbeveloper  * copy of this software and associated documentation files (the "Software"),
62fc06b87Sbeveloper  * to deal in the Software without restriction, including without limitation
72fc06b87Sbeveloper  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
82fc06b87Sbeveloper  * and/or sell copies of the Software, and to permit persons to whom the
92fc06b87Sbeveloper  * Software is furnished to do so, subject to the following conditions:
102fc06b87Sbeveloper  *
112fc06b87Sbeveloper  * The above copyright notice and this permission notice shall be included in
122fc06b87Sbeveloper  * all copies or substantial portions of the Software.
132fc06b87Sbeveloper  *
142fc06b87Sbeveloper  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
152fc06b87Sbeveloper  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
162fc06b87Sbeveloper  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
172fc06b87Sbeveloper  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
182fc06b87Sbeveloper  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
192fc06b87Sbeveloper  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
202fc06b87Sbeveloper  * DEALINGS IN THE SOFTWARE.
212fc06b87Sbeveloper  */
22e75560e6Sejakowatz #ifndef _BLOCK_CACHE_H
23e75560e6Sejakowatz #define _BLOCK_CACHE_H
24e75560e6Sejakowatz 
25*087882c2SAxel Dörfler 
26e75560e6Sejakowatz #include <BeBuild.h>
27e75560e6Sejakowatz #include <Locker.h>
28e75560e6Sejakowatz 
292fc06b87Sbeveloper 
30*087882c2SAxel Dörfler // The allocation type to be used in the constructor
312fc06b87Sbeveloper enum {
32e75560e6Sejakowatz 	B_OBJECT_CACHE = 0,
33e75560e6Sejakowatz 	B_MALLOC_CACHE = 1
34e75560e6Sejakowatz };
35e75560e6Sejakowatz 
36*087882c2SAxel Dörfler class BBlockCache {
37e75560e6Sejakowatz 	public:
38*087882c2SAxel Dörfler 		BBlockCache(uint32 blockCount, size_t blockSize,
392fc06b87Sbeveloper 			uint32 allocationType);
402fc06b87Sbeveloper 		virtual	~BBlockCache();
41e75560e6Sejakowatz 
422fc06b87Sbeveloper 		void*	Get(size_t blockSize);
432fc06b87Sbeveloper 		void	Save(void *pointer, size_t blockSize);
44e75560e6Sejakowatz 
45e75560e6Sejakowatz 	private:
46e75560e6Sejakowatz 		virtual	void _ReservedBlockCache1();
47e75560e6Sejakowatz 		virtual	void _ReservedBlockCache2();
48e75560e6Sejakowatz 
49e75560e6Sejakowatz 		BBlockCache(const BBlockCache &);
50e75560e6Sejakowatz 		BBlockCache	&operator=(const BBlockCache &);
51e75560e6Sejakowatz 
52340eed61SIngo Weinhold 		struct _FreeBlock;
53e75560e6Sejakowatz 
542fc06b87Sbeveloper 		_FreeBlock*	fFreeList;
552fc06b87Sbeveloper 		size_t		fBlockSize;
56369eef13SIngo Weinhold 		int32		fFreeBlocks;
57369eef13SIngo Weinhold 		int32		fBlockCount;
582fc06b87Sbeveloper 		BLocker		fLocker;
592fc06b87Sbeveloper 		void*		(*fAlloc)(size_t size);
602fc06b87Sbeveloper 		void		(*fFree)(void *pointer);
61369eef13SIngo Weinhold 		uint32		_reserved[2];
622fc06b87Sbeveloper };
632fc06b87Sbeveloper 
64*087882c2SAxel Dörfler #endif	// _BLOCK_CACHE_H
65