Style changes

git-svn-id: file:///srv/svn/repos/haiku/trunk/current@1731 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stefano Ceccherini 2002-10-28 09:13:37 +00:00
parent a4d15367fd
commit c109ab72ae

View File

@ -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 <BlockCache.h>
#include <Autolock.h>
// Standard Includes -----------------------------------------------------------
#include <stdlib.h>
// System Includes -------------------------------------------------------------
#include <Autolock.h>
#include <BlockCache.h>
// 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; i<CacheSize; i++) {
((_Block *)fCache)[i].Data=fAlloc(BlockSize);
}
fCache = (void *)new _Block[CacheSize];
for(size_t i = 0; i < CacheSize; i++)
((_Block *)fCache)[i].Data = fAlloc(BlockSize);
}
@ -84,34 +111,37 @@ BBlockCache::~BBlockCache()
delete[] fCache;
}
void *
BBlockCache::Get(size_t BlockSize)
{
BAutolock lock(fLock);
if(BlockSize!=fBlockSize) {
if(BlockSize != fBlockSize)
return fAlloc(BlockSize);
}
_Block *block;
for(size_t i=fMark; i<fCacheSize; i++) {
block=&((_Block *)fCache)[i];
if(block->InUse==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; i<fMark; i++) {
block=&((_Block *)fCache)[i];
if(block->InUse==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; i<fCacheSize; i++) {
block=&((_Block *)fCache)[i];
if(block->Data==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;
}
}