From c109ab72ae91a87fdc67499b0089ac0804003065 Mon Sep 17 00:00:00 2001 From: Stefano Ceccherini Date: Mon, 28 Oct 2002 09:13:37 +0000 Subject: [PATCH] Style changes git-svn-id: file:///srv/svn/repos/haiku/trunk/current@1731 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/kits/support/BlockCache.cpp | 121 ++++++++++++++++++++------------ 1 file changed, 76 insertions(+), 45 deletions(-) diff --git a/src/kits/support/BlockCache.cpp b/src/kits/support/BlockCache.cpp index 1e2fe5de2c..5d581c3c45 100644 --- a/src/kits/support/BlockCache.cpp +++ b/src/kits/support/BlockCache.cpp @@ -1,42 +1,67 @@ +//------------------------------------------------------------------------------ +// Copyright (c) 2001-2002, OpenBeOS // -// File: BlockCache.cpp +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: // -// Description: Handles a cache of memory blocks of the same size +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. // -// Copyright (c) 2002 Massimiliano Origgi +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. // -// License: See MIT license +// File Name: String.cpp +// Author(s): Massimiliano Origgi // +// Description: Handles a cache of memory blocks of the same size +//------------------------------------------------------------------------------ -#include - -#include - +// Standard Includes ----------------------------------------------------------- #include +// System Includes ------------------------------------------------------------- +#include +#include + // Private functions - -void *object_alloc(size_t Size) +static void * +object_alloc(size_t Size) { return (void *)(new char[Size]); } -void object_free(void *Data) + +static void +object_free(void *Data) { delete[] Data; } -void *malloc_alloc(size_t Size) + +static void* +malloc_alloc(size_t Size) { return malloc(Size); } -void malloc_free(void *Data) + +static void +malloc_free(void *Data) { free(Data); } + // Private structure for cache struct _Block { @@ -46,36 +71,38 @@ struct _Block bool InUse; }; + _Block::_Block(void) : Data(NULL), InUse(false) { } + _Block::~_Block(void) { } -BBlockCache::BBlockCache(size_t CacheSize, size_t BlockSize, uint32 Type) : - fCacheSize(CacheSize), - fBlockSize(BlockSize), - fMark(0) +BBlockCache::BBlockCache(size_t CacheSize, size_t BlockSize, uint32 Type) + : fCacheSize(CacheSize), + fBlockSize(BlockSize), + fMark(0) { // Setup function pointers based on Type - if(Type==B_OBJECT_CACHE) { - fAlloc=&object_alloc; - fFree=&object_free; + if(Type == B_OBJECT_CACHE) { + fAlloc = &object_alloc; + fFree = &object_free; } else { - fAlloc=&malloc_alloc; - fFree=&malloc_free; + fAlloc = &malloc_alloc; + fFree = &malloc_free; } // Allocate cache - fCache=(void *)new _Block[CacheSize]; - for(size_t i=0; iInUse==false) { - block->InUse=true; + for(size_t i = fMark; i < fCacheSize; i++) { + block = &((_Block *)fCache)[i]; + if(block->InUse == false) { + block->InUse = true; ++fMark; - if(fMark==fCacheSize) fMark=0; + if(fMark == fCacheSize) + fMark = 0; return block->Data; } } - if(fMark==0) return fAlloc(BlockSize); + if(fMark == 0) + return fAlloc(BlockSize); - for(size_t i=0; iInUse==false) { - block->InUse=true; + for(size_t i = 0; i < fMark; i++) { + block = &((_Block *)fCache)[i]; + if(block->InUse == false) { + block->InUse = true; ++fMark; - if(fMark==fCacheSize) fMark=0; + if(fMark == fCacheSize) + fMark = 0; return block->Data; } } @@ -119,22 +149,23 @@ BBlockCache::Get(size_t BlockSize) return fAlloc(BlockSize); } + void BBlockCache::Save(void *Data, size_t BlockSize) { BAutolock lock(fLock); - if(BlockSize!=fBlockSize) { + if(BlockSize != fBlockSize) { fFree(Data); return; } _Block *block; - for(size_t i=0; iData==Data) { - block->InUse=false; - fMark=i; + for(size_t i = 0; i < fCacheSize; i++) { + block = &((_Block *)fCache)[i]; + if(block->Data == Data) { + block->InUse = false; + fMark = i; return; } }