1*2fc06b87Sbeveloper /* 2*2fc06b87Sbeveloper * Copyright (c) 2003 Marcus Overhagen 3*2fc06b87Sbeveloper * 4*2fc06b87Sbeveloper * Permission is hereby granted, free of charge, to any person obtaining a 5*2fc06b87Sbeveloper * copy of this software and associated documentation files (the "Software"), 6*2fc06b87Sbeveloper * to deal in the Software without restriction, including without limitation 7*2fc06b87Sbeveloper * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8*2fc06b87Sbeveloper * and/or sell copies of the Software, and to permit persons to whom the 9*2fc06b87Sbeveloper * Software is furnished to do so, subject to the following conditions: 10*2fc06b87Sbeveloper * 11*2fc06b87Sbeveloper * The above copyright notice and this permission notice shall be included in 12*2fc06b87Sbeveloper * all copies or substantial portions of the Software. 13*2fc06b87Sbeveloper * 14*2fc06b87Sbeveloper * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15*2fc06b87Sbeveloper * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16*2fc06b87Sbeveloper * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17*2fc06b87Sbeveloper * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18*2fc06b87Sbeveloper * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19*2fc06b87Sbeveloper * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20*2fc06b87Sbeveloper * DEALINGS IN THE SOFTWARE. 21*2fc06b87Sbeveloper */ 22e75560e6Sejakowatz #ifndef _BLOCK_CACHE_H 23e75560e6Sejakowatz #define _BLOCK_CACHE_H 24e75560e6Sejakowatz 25e75560e6Sejakowatz #include <BeBuild.h> 26e75560e6Sejakowatz #include <Locker.h> 27e75560e6Sejakowatz 28*2fc06b87Sbeveloper /* The OpenBeOS implementation of the BBLockCache. 29*2fc06b87Sbeveloper * A class used to manage a pool of memory blocks. 30*2fc06b87Sbeveloper */ 31*2fc06b87Sbeveloper 32*2fc06b87Sbeveloper /* The allocation type to be used in the constructor 33*2fc06b87Sbeveloper */ 34*2fc06b87Sbeveloper enum { 35e75560e6Sejakowatz B_OBJECT_CACHE = 0, 36e75560e6Sejakowatz B_MALLOC_CACHE = 1 37e75560e6Sejakowatz }; 38e75560e6Sejakowatz 39*2fc06b87Sbeveloper /* The BBlockCache class: 40*2fc06b87Sbeveloper */ 41e75560e6Sejakowatz class BBlockCache 42e75560e6Sejakowatz { 43e75560e6Sejakowatz public: 44*2fc06b87Sbeveloper BBlockCache(size_t cacheSize, 45*2fc06b87Sbeveloper size_t blockSize, 46*2fc06b87Sbeveloper uint32 allocationType); 47*2fc06b87Sbeveloper virtual ~BBlockCache(); 48e75560e6Sejakowatz 49*2fc06b87Sbeveloper void * Get(size_t blockSize); 50*2fc06b87Sbeveloper void Save(void *pointer, size_t blockSize); 51e75560e6Sejakowatz 52*2fc06b87Sbeveloper /* Private or reserved functions and data 53*2fc06b87Sbeveloper */ 54e75560e6Sejakowatz private: 55e75560e6Sejakowatz virtual void _ReservedBlockCache1(); 56e75560e6Sejakowatz virtual void _ReservedBlockCache2(); 57e75560e6Sejakowatz 58e75560e6Sejakowatz BBlockCache(const BBlockCache &); 59e75560e6Sejakowatz BBlockCache &operator=(const BBlockCache &); 60e75560e6Sejakowatz 61*2fc06b87Sbeveloper struct _FreeBlock 62*2fc06b87Sbeveloper { 63*2fc06b87Sbeveloper uint32 magic1; 64*2fc06b87Sbeveloper _FreeBlock *next; 65*2fc06b87Sbeveloper uint32 magic2; 66e75560e6Sejakowatz }; 67e75560e6Sejakowatz 68*2fc06b87Sbeveloper _FreeBlock *fFreeList; 69*2fc06b87Sbeveloper size_t fBlockSize; 70*2fc06b87Sbeveloper int32 fExcessBlocks; 71*2fc06b87Sbeveloper BLocker fLocker; 72*2fc06b87Sbeveloper void * (*fAlloc)(size_t size); 73*2fc06b87Sbeveloper void (*fFree)(void *pointer); 74*2fc06b87Sbeveloper uint32 _reserved[3]; 75*2fc06b87Sbeveloper }; 76*2fc06b87Sbeveloper 77*2fc06b87Sbeveloper #endif /* _BLOCK_CACHE_H */ 78