Improved AreaPool to have an initial size as well as well as a name that is

used for new area.
MemPool::AddToPool() now gracefully deals with NULL pointers (or a size of 0).
BitmapManager was deleting the area it transferred to AreaPool before - it
no longer needs an extra area, though.
Minor other cleanup.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@13260 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2005-06-24 04:01:16 +00:00
parent b5436616a3
commit 0ec4af2233
6 changed files with 75 additions and 88 deletions
@@ -44,7 +44,6 @@ class BitmapManager {
void DeleteBitmap(ServerBitmap* bitmap); void DeleteBitmap(ServerBitmap* bitmap);
protected: protected:
BList fBitmapList; BList fBitmapList;
area_id fBitmapArea;
int8* fBuffer; int8* fBuffer;
TokenHandler fTokenizer; TokenHandler fTokenizer;
BLocker fLock; BLocker fLock;
+42 -41
View File
@@ -468,47 +468,44 @@ MemPool::ReleaseBuffer(void *buf)
// Add a region of memory to the buffer pool. // Add a region of memory to the buffer pool.
void void
MemPool::AddToPool(void *buf, ssize_t len) MemPool::AddToPool(void *buffer, ssize_t length)
{ {
struct bfhead *b = BFH(buf); if (buffer == NULL || length <= 0)
struct bhead *bn; return;
len &= ~(SizeQuant - 1); length &= ~(SizeQuant - 1);
if (fPoolLength == 0) if (fPoolLength == 0)
{ fPoolLength = length;
fPoolLength = len; else if (length != fPoolLength)
}
else
if (len != fPoolLength)
{
fPoolLength = -1; fPoolLength = -1;
}
// Number of block acquisitions // Number of block acquisitions
fNumpget++; fNumpget++;
// Number of blocks total
fNumpblk++;
assert(fNumpblk == fNumpget - fNumprel);
// Since the block is initially occupied by a single free buffer, // Number of blocks total
// it had better not be (much) larger than the largest buffer fNumpblk++;
// whose size we can store in bhead.bsize. assert(fNumpblk == fNumpget - fNumprel);
assert(len - sizeof(struct bhead) <= -((ssize_t) ESent + 1));
// Clear the backpointer at the start of the block to indicate that // Since the block is initially occupied by a single free buffer,
// there is no free block prior to this one. That blocks // it had better not be (much) larger than the largest buffer
// recombination when the first block in memory is released. // whose size we can store in bhead.bsize.
b->bh.prevfree = 0; assert(length - sizeof(struct bhead) <= -((ssize_t) ESent + 1));
// Chain the new block to the free list. struct bfhead *b = BFH(buffer);
assert(fFreeList.ql.blink->ql.flink == &fFreeList);
assert(fFreeList.ql.flink->ql.blink == &fFreeList); // Clear the backpointer at the start of the block to indicate that
b->ql.flink = &fFreeList; // there is no free block prior to this one. That blocks
b->ql.blink = fFreeList.ql.blink; // recombination when the first block in memory is released.
fFreeList.ql.blink = b; b->bh.prevfree = 0;
b->ql.blink->ql.flink = b;
// Chain the new block to the free list.
assert(fFreeList.ql.blink->ql.flink == &fFreeList);
assert(fFreeList.ql.flink->ql.blink == &fFreeList);
b->ql.flink = &fFreeList;
b->ql.blink = fFreeList.ql.blink;
fFreeList.ql.blink = b;
b->ql.blink->ql.flink = b;
// Create a dummy allocated buffer at the end of the pool. This dummy // Create a dummy allocated buffer at the end of the pool. This dummy
// buffer is seen when a buffer at the end of the pool is released and // buffer is seen when a buffer at the end of the pool is released and
@@ -518,14 +515,14 @@ MemPool::AddToPool(void *buf, ssize_t len)
// routines (this specific value is not counted on by the actual // routines (this specific value is not counted on by the actual
// allocation and release functions). // allocation and release functions).
len -= sizeof(struct bhead); length -= sizeof(struct bhead);
b->bh.bsize = (ssize_t) len; b->bh.bsize = length;
memset(((char *) b) + sizeof(struct bfhead), 0x55,(int) (len - sizeof(struct bfhead))); memset(((char *)b) + sizeof(struct bfhead), 0x55, (int)(length - sizeof(struct bfhead)));
struct bhead *bn = BH(((char *) b) + length);
bn->prevfree = length;
bn = BH(((char *) b) + len);
bn->prevfree = (ssize_t) len;
// Definition of ESent assumes two's complement! // Definition of ESent assumes two's complement!
assert((~0) == -1); assert((~0) == -1);
bn->bsize = ESent; bn->bsize = ESent;
@@ -765,8 +762,12 @@ MemPool::ReleaseMem(void *buffer)
// #pragma mark - // #pragma mark -
AreaPool::AreaPool() AreaPool::AreaPool(const char* name, size_t initialSize)
:
fName(name)
{ {
if (initialSize > 0)
AddToPool(AcquireMem(initialSize), initialSize);
} }
@@ -784,7 +785,7 @@ AreaPool::AcquireMem(ssize_t size)
// make size a multiple of B_PAGE_SIZE // make size a multiple of B_PAGE_SIZE
size = (size + B_PAGE_SIZE - 1) & ~(B_PAGE_SIZE - 1); size = (size + B_PAGE_SIZE - 1) & ~(B_PAGE_SIZE - 1);
area = create_area("AreaPool_area", &address, B_ANY_ADDRESS, size, area = create_area(fName, &address, B_ANY_ADDRESS, size,
B_NO_LOCK, B_READ_AREA | B_WRITE_AREA); B_NO_LOCK, B_READ_AREA | B_WRITE_AREA);
if (area < B_OK) { if (area < B_OK) {
+13 -10
View File
@@ -1,13 +1,13 @@
/* /*
* Copyright 2001-2005, Haiku, Inc. All rights reserved. * Copyright 2001-2005, Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*
* Authors: John Walker <[email protected]> * Authors:
* DarkWyrm <[email protected]> * John Walker <[email protected]>
* Stephan Aßmus <[email protected]> * DarkWyrm <[email protected]>
* Stephan Aßmus <[email protected]>
* *
* BGET pool allocator * BGET pool allocator
*
*/ */
#ifndef MEM_POOL_H #ifndef MEM_POOL_H
@@ -93,12 +93,15 @@ class MemPool {
}; };
class AreaPool : public MemPool { class AreaPool : public MemPool {
public: public:
AreaPool(); AreaPool(const char* name, size_t initialSize = 0);
virtual ~AreaPool(); virtual ~AreaPool();
virtual void* AcquireMem(ssize_t size); virtual void* AcquireMem(ssize_t size);
virtual void ReleaseMem(void* buffer); virtual void ReleaseMem(void* buffer);
private:
const char* fName;
}; };
#endif #endif
+10 -23
View File
@@ -29,23 +29,13 @@ BitmapManager *gBitmapManager = NULL;
//! Sets up stuff to be ready to allocate space for bitmaps //! Sets up stuff to be ready to allocate space for bitmaps
BitmapManager::BitmapManager() BitmapManager::BitmapManager()
: fBitmapList(1024), :
fBitmapArea(B_ERROR), fBitmapList(1024),
fBuffer(NULL), fBuffer(NULL),
fTokenizer(), fTokenizer(),
fLock("BitmapManager Lock"), fLock("BitmapManager Lock"),
fMemPool() fMemPool("bitmap pool", BITMAP_AREA_SIZE)
{ {
// When create_area is passed the B_ANY_ADDRESS flag, the address of the area
// is stored in the pointer to a pointer.
fBitmapArea = create_area("bitmap_area", (void**)&fBuffer,
B_ANY_ADDRESS, BITMAP_AREA_SIZE,
B_NO_LOCK, B_READ_AREA | B_WRITE_AREA);
if (fBitmapArea < B_OK)
printf("PANIC: BitmapManager couldn't allocate area: %s\n", strerror(fBitmapArea));
fMemPool.AddToPool(fBuffer, BITMAP_AREA_SIZE);
} }
//! Deallocates everything associated with the manager //! Deallocates everything associated with the manager
@@ -58,8 +48,6 @@ BitmapManager::~BitmapManager()
delete bitmap; delete bitmap;
} }
} }
delete_area(fBitmapArea);
} }
/*! /*!
@@ -93,12 +81,11 @@ BitmapManager::CreateBitmap(BRect bounds, color_space space, int32 flags,
bitmap->fBuffer = buffer; bitmap->fBuffer = buffer;
bitmap->fToken = fTokenizer.GetToken(); bitmap->fToken = fTokenizer.GetToken();
bitmap->fInitialized = true; bitmap->fInitialized = true;
// calculate area offset
area_info ai;
get_area_info(bitmap->fArea, &ai);
bitmap->fOffset = buffer - (uint8*)ai.address;
// calculate area offset
area_info info;
get_area_info(bitmap->fArea, &info);
bitmap->fOffset = buffer - (uint8*)info.address;
} else { } else {
// Allocation failed for buffer or bitmap list // Allocation failed for buffer or bitmap list
fMemPool.ReleaseBuffer(buffer); fMemPool.ReleaseBuffer(buffer);
+5 -9
View File
@@ -91,7 +91,7 @@ ServerApp::ServerApp(port_id clientReplyPort, port_id clientLooperPort,
fCursorHidden(false), fCursorHidden(false),
fIsActive(false), fIsActive(false),
//fHandlerToken(handlerID), //fHandlerToken(handlerID),
fSharedMem(new AreaPool), fSharedMem("shared memory"),
fQuitting(false) fQuitting(false)
{ {
if (fSignature == "") if (fSignature == "")
@@ -198,12 +198,8 @@ ServerApp::~ServerApp(void)
// there should be a way that this ServerApp be attached to a particular // there should be a way that this ServerApp be attached to a particular
// RootLayer to know which RootLayer's cursor to modify. // RootLayer to know which RootLayer's cursor to modify.
gDesktop->ActiveRootLayer()->GetCursorManager().RemoveAppCursors(fClientTeam); gDesktop->ActiveRootLayer()->GetCursorManager().RemoveAppCursors(fClientTeam);
STRACE(("#ServerApp %s:~ServerApp()\n", fSignature.String()));
delete fSharedMem; STRACE(("ServerApp %s::~ServerApp(): Exiting\n", Signature()));
STRACE(("ServerApp %s::~ServerApp(): Exiting\n", fSignature.String()));
} }
@@ -587,7 +583,7 @@ ServerApp::_DispatchMessage(int32 code, BPrivate::LinkReceiver &link)
// This is a very special case in the sense that when ServerMemIO is used for this // This is a very special case in the sense that when ServerMemIO is used for this
// purpose, it will be set to NOT automatically free the memory which it had // purpose, it will be set to NOT automatically free the memory which it had
// requested. This is the server's job once the message has been dispatched. // requested. This is the server's job once the message has been dispatched.
fSharedMem->ReleaseBuffer(msgpointer); fSharedMem.ReleaseBuffer(msgpointer);
break; break;
} }
case AS_ACQUIRE_SERVERMEM: case AS_ACQUIRE_SERVERMEM:
@@ -607,7 +603,7 @@ ServerApp::_DispatchMessage(int32 code, BPrivate::LinkReceiver &link)
// TODO: I wonder if ACQUIRE_SERVERMEM should have a minimum size requirement? // TODO: I wonder if ACQUIRE_SERVERMEM should have a minimum size requirement?
void *sharedmem = fSharedMem->GetBuffer(memsize); void *sharedmem = fSharedMem.GetBuffer(memsize);
if (memsize < 1 || sharedmem == NULL) { if (memsize < 1 || sharedmem == NULL) {
fLink.StartMessage(SERVER_FALSE); fLink.StartMessage(SERVER_FALSE);
@@ -653,7 +649,7 @@ ServerApp::_DispatchMessage(int32 code, BPrivate::LinkReceiver &link)
STRACE(("Successfully freed shared memory\n")); STRACE(("Successfully freed shared memory\n"));
void *sharedmem = ((int32*)areaInfo.address) + areaoffset; void *sharedmem = ((int32*)areaInfo.address) + areaoffset;
fSharedMem->ReleaseBuffer(sharedmem); fSharedMem.ReleaseBuffer(sharedmem);
break; break;
} }
+5 -4
View File
@@ -18,6 +18,7 @@
#include <PortLink.h> #include <PortLink.h>
#include "SubWindowList.h" #include "SubWindowList.h"
#include "BGet++.h"
class AreaPool; class AreaPool;
class BMessage; class BMessage;
@@ -72,7 +73,7 @@ public:
int32 CountPictures() const; int32 CountPictures() const;
ServerPicture *FindPicture(int32 token) const; ServerPicture *FindPicture(int32 token) const;
AreaPool *AppAreaPool() { return fSharedMem; } AreaPool *AppAreaPool() { return &fSharedMem; }
SubWindowList fAppSubWindowList; SubWindowList fAppSubWindowList;
@@ -117,9 +118,9 @@ private:
// Used for BMessage target specification // Used for BMessage target specification
// TODO: Is it still needed ? We aren't using it. // TODO: Is it still needed ? We aren't using it.
//int32 fHandlerToken; //int32 fHandlerToken;
AreaPool *fSharedMem; AreaPool fSharedMem;
bool fQuitting; bool fQuitting;
}; };