2 * Copyright (c) 2003 Marcus Overhagen
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
23 #include <BlockCache.h>
30 #define MAGIC1 0x9183f4d9
31 #define MAGIC2 0xa6b3c87d
34 struct BBlockCache::_FreeBlock
{
35 DEBUG_ONLY( uint32 magic1
; )
37 DEBUG_ONLY( uint32 magic2
; )
41 // The requirements set by the BeBook's description of the destructor,
42 // as well as Get() function, allowing the caller to dispose of the
43 // memory, do not allow to allocate one large block to be used as pool.
44 // Thus we need to create multiple small ones.
45 // We maintain a list of free blocks.
47 BBlockCache::BBlockCache(uint32 blockCount
, size_t blockSize
,
48 uint32 allocationType
)
51 fBlockSize(blockSize
),
53 fBlockCount(blockCount
),
54 fLocker("some BBlockCache lock"),
58 switch (allocationType
) {
60 fAlloc
= &operator new[];
61 fFree
= &operator delete[];
70 // To properly maintain a list of free buffers, a buffer must be
71 // large enough to contain the _FreeBlock struct that is used.
72 if (blockSize
< sizeof(_FreeBlock
))
73 blockSize
= sizeof(_FreeBlock
);
75 // should have at least one block
79 // create blocks and put them into the free list
80 while (blockCount
--) {
81 _FreeBlock
*block
= reinterpret_cast<_FreeBlock
*>(fAlloc(blockSize
));
85 block
->next
= fFreeList
;
87 DEBUG_ONLY(block
->magic1
= MAGIC1
);
88 DEBUG_ONLY(block
->magic2
= MAGIC2
+ (uint32
)(addr_t
)block
->next
);
93 BBlockCache::~BBlockCache()
95 // walk the free list and deallocate all blocks
98 ASSERT(fFreeList
->magic1
== MAGIC1
);
99 ASSERT(fFreeList
->magic2
== MAGIC2
+ (uint32
)(addr_t
)fFreeList
->next
);
100 void *pointer
= fFreeList
;
101 fFreeList
= fFreeList
->next
;
102 DEBUG_ONLY(memset(pointer
, 0xCC, sizeof(_FreeBlock
)));
110 BBlockCache::Get(size_t blockSize
)
115 if (blockSize
== fBlockSize
&& fFreeList
!= 0) {
116 // we can take a block from the list
117 ASSERT(fFreeList
->magic1
== MAGIC1
);
118 ASSERT(fFreeList
->magic2
== MAGIC2
+ (uint32
)(addr_t
)fFreeList
->next
);
120 fFreeList
= fFreeList
->next
;
122 DEBUG_ONLY(memset(pointer
, 0xCC, sizeof(_FreeBlock
)));
124 if (blockSize
< sizeof(_FreeBlock
))
125 blockSize
= sizeof(_FreeBlock
);
126 pointer
= fAlloc(blockSize
);
127 DEBUG_ONLY(if (pointer
) memset(pointer
, 0xCC, sizeof(_FreeBlock
)));
135 BBlockCache::Save(void *pointer
, size_t blockSize
)
139 if (blockSize
== fBlockSize
&& fFreeBlocks
< fBlockCount
) {
140 // the block needs to be returned to the cache
141 _FreeBlock
*block
= reinterpret_cast<_FreeBlock
*>(pointer
);
142 block
->next
= fFreeList
;
145 DEBUG_ONLY(block
->magic1
= MAGIC1
);
146 DEBUG_ONLY(block
->magic2
= MAGIC2
+ (uint32
)(addr_t
)block
->next
);
148 DEBUG_ONLY(memset(pointer
, 0xCC, sizeof(_FreeBlock
)));
155 void BBlockCache::_ReservedBlockCache1() {}
156 void BBlockCache::_ReservedBlockCache2() {}