Another cleanup patch by Vasilis Kaoutsis - thanks!

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@19998 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2007-01-28 21:35:08 +00:00
parent 0223d2d0bb
commit 6af9dcbfe5
2 changed files with 188 additions and 187 deletions
+33 -48
View File
@@ -1,50 +1,33 @@
//------------------------------------------------------------------------------ /*
// Copyright (c) 2001-2002, OpenBeOS * Copyright 2001-2007, Haiku, Inc. All Rights Reserved.
// * Distributed under the terms of the MIT License.
// Permission is hereby granted, free of charge, to any person obtaining a *
// copy of this software and associated documentation files (the "Software"), * Authors:
// to deal in the Software without restriction, including without limitation * DarkWyrm, [email protected]
// 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:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// 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.
//
// File Name: AreaLink.h
// Author: DarkWyrm <[email protected]>
// Description: Helper class for managing chunks of small data in an area
//
//------------------------------------------------------------------------------
#ifndef AREALINK_H #ifndef AREALINK_H
#define AREALINK_H #define AREALINK_H
#include <List.h>
#include <InterfaceDefs.h> #include <InterfaceDefs.h>
#include <List.h>
#include <OS.h> #include <OS.h>
#define MAX_ATTACHMENT_SIZE 65535 // in bytes #define MAX_ATTACHMENT_SIZE 65535 // in bytes
#define SIZE_SIZE 2 // size of the size records in an AreaLink area #define SIZE_SIZE 2 // size of the size records in an AreaLink area
class AreaLinkHeader; class AreaLinkHeader;
class AreaLink
{ class AreaLink {
public: public:
AreaLink(area_id area, bool is_arealink_area = false); AreaLink(area_id area, bool is_arealink_area = false);
AreaLink(void); AreaLink();
~AreaLink(void); ~AreaLink();
void SetTarget(area_id area, bool is_arealink_area = false); void SetTarget(area_id area, bool is_arealink_area = false);
area_id GetTarget(void) { return target; } area_id GetTarget() const { return fTarget; }
void Attach(const void *data, size_t size); void Attach(const void *data, size_t size);
inline void Attach(const int32 &data); inline void Attach(const int32 &data);
@@ -56,23 +39,25 @@ public:
inline void Attach(const BRect &data); inline void Attach(const BRect &data);
inline void Attach(const BPoint &data); inline void Attach(const BPoint &data);
void MakeEmpty(void); void MakeEmpty();
void *ItemAt(int32 index) { return attachlist->ItemAt(index); } void *ItemAt(const int32 index) const { return fAttachList->ItemAt(index); }
int32 CountAttachments(void) { return attachlist->CountItems(); } int32 CountAttachments() const { return fAttachList->CountItems(); }
void *BaseAddress(void) { return (void *)baseaddress; } void *BaseAddress() const { return (void *)fBaseAddress; }
void Lock(void); void Lock();
void Unlock(void); void Unlock();
bool IsLocked(void); bool IsLocked();
protected:
void _ReadAttachments(void);
BList *attachlist; private:
area_id target; void _ReadAttachments();
bool area_ok;
bool have_lock; private:
int8 *baseaddress; BList *fAttachList;
AreaLinkHeader *header; area_id fTarget;
bool fAreaIsOk;
bool fHaveLock;
int8 *fBaseAddress;
AreaLinkHeader *fHeader;
}; };
#endif #endif // AREALINK_H
+126 -110
View File
@@ -1,114 +1,106 @@
//------------------------------------------------------------------------------ /*
// Copyright (c) 2001-2002, OpenBeOS * Copyright 2001-2007, Haiku, Inc. All Rights Reserved.
// * Distributed under the terms of the MIT License.
// Permission is hereby granted, free of charge, to any person obtaining a *
// copy of this software and associated documentation files (the "Software"), * Authors:
// to deal in the Software without restriction, including without limitation * DarkWyrm, [email protected]
// 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:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// 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.
//
// File Name: AreaLink.cpp
// Author: DarkWyrm <[email protected]>
// Description: Helper class for managing chunks of small data in an area
//
//------------------------------------------------------------------------------
#include <Rect.h>
#include "AreaLink.h" #include "AreaLink.h"
#include <Rect.h>
#include <string.h> #include <string.h>
//#define AL_DEBUG //#define AL_DEBUG
#ifdef AL_DEBUG #ifdef AL_DEBUG
#include <stdio.h> #include <stdio.h>
#endif #endif
class AreaLinkHeader
{ class AreaLinkHeader {
public: public:
AreaLinkHeader(void) { MakeEmpty(); lock=B_NAME_NOT_FOUND; } AreaLinkHeader() { MakeEmpty(); fLock = B_NAME_NOT_FOUND; }
void SetAttachmentCount(uint32 size) { attachmentcount=size; } void SetAttachmentCount(uint32 size) { fAttachmentCount = size; }
uint32 GetAttachmentCount(void) { return attachmentcount; } uint32 GetAttachmentCount() { return fAttachmentCount; }
void SetAttachmentSize(uint32 size) { attachmentsize=size; } void SetAttachmentSize(uint32 size) { fAttachmentSize = size; }
uint32 GetAttachmentSize(void) { return attachmentsize; } uint32 GetAttachmentSize() { return fAttachmentSize; }
void AddAttachment(uint32 size) { attachmentsize+=size; attachmentcount++; } void AddAttachment(uint32 size) { fAttachmentSize += size; fAttachmentCount++; }
void SetLockSem(sem_id sem) { lock=sem; } void SetLockSem(sem_id sem) { fLock = sem; }
sem_id GetLockSem(void) { return lock; } sem_id GetLockSem() { return fLock; }
void MakeEmpty() { fAttachmentCount = 0; fAttachmentSize = 0; }
area_info GetInfo() { return fInfo; }
void SetInfo(const area_info &newInfo) { fInfo = newInfo; }
void MakeEmpty(void) { attachmentcount=0; attachmentsize=0; }
area_info GetInfo(void) { return info; }
void SetInfo(const area_info &newinfo) { info=newinfo; }
private: private:
uint32 attachmentcount; uint32 fAttachmentCount;
uint32 attachmentsize; uint32 fAttachmentSize;
sem_id lock; sem_id fLock;
area_info info; area_info fInfo;
}; };
AreaLink::AreaLink(area_id area, bool is_arealink_area)
// #pragma mark -
AreaLink::AreaLink(area_id area, bool isAreaLinkArea)
:fAttachList(new BList(0)),
fHaveLock(false)
{ {
attachlist=new BList(0); SetTarget(area, isAreaLinkArea);
have_lock=false;
SetTarget(area,is_arealink_area);
} }
AreaLink::AreaLink(void)
AreaLink::AreaLink()
:fAttachList(new BList(0)),
fTarget(B_NAME_NOT_FOUND),
fAreaIsOk(false),
fHaveLock(false)
{ {
attachlist=new BList(0);
area_ok=false;
have_lock=false;
target=B_NAME_NOT_FOUND;
} }
AreaLink::~AreaLink(void)
AreaLink::~AreaLink()
{ {
delete attachlist; delete fAttachList;
} }
void AreaLink::SetTarget(area_id area,bool is_arealink_area)
{
area_info targetinfo;
if(get_area_info(area,&targetinfo)==B_OK) void
AreaLink::SetTarget(area_id area, bool isAreaLinkArea)
{ {
target=area; area_info targetInfo;
area_ok=true;
header=(AreaLinkHeader *)targetinfo.address;
if(is_arealink_area) if (get_area_info(area, &targetInfo) == B_OK) {
fTarget = area;
fAreaIsOk = true;
fHeader = (AreaLinkHeader *)targetInfo.address;
if (isAreaLinkArea)
_ReadAttachments(); _ReadAttachments();
else else {
{ fHeader->MakeEmpty();
header->MakeEmpty(); fHeader->SetInfo(targetInfo);
header->SetInfo(targetinfo);
} }
baseaddress=(int8*)targetinfo.address; fBaseAddress = (int8*)targetInfo.address;
baseaddress+=sizeof(AreaLinkHeader); fBaseAddress += sizeof(AreaLinkHeader);
} } else {
else fTarget = B_NAME_NOT_FOUND;
{ fAreaIsOk = false;
target=B_NAME_NOT_FOUND;
area_ok=false;
} }
} }
void AreaLink::Attach(const void *data, size_t size)
void
AreaLink::Attach(const void *data, size_t size)
{ {
if (!data || size == 0) if (!data || size == 0)
return; return;
@@ -116,98 +108,122 @@ void AreaLink::Attach(const void *data, size_t size)
Lock(); Lock();
// Will the attachment fit? // Will the attachment fit?
if(header->GetAttachmentSize()+size>header->GetInfo().size) if (fHeader->GetAttachmentSize() + size > fHeader->GetInfo().size) {
{
// Being it won't fit, resize the area to fit the thing // Being it won't fit, resize the area to fit the thing
int32 pagenum=int32(header->GetInfo().size/B_PAGE_SIZE); int32 pageNum = int32(fHeader->GetInfo().size / B_PAGE_SIZE);
resize_area(target,pagenum+1); resize_area(fTarget, pageNum + 1);
} }
// Our attachment will fit, so copy the data into the current location // Our attachment will fit, so copy the data into the current location
// and increment the appropriate header values // and increment the appropriate fHeader values
int8 *currentpointer = (int8*)BaseAddress(); int8 *currentpointer = (int8*)BaseAddress();
currentpointer+=header->GetAttachmentSize(); currentpointer += fHeader->GetAttachmentSize();
memcpy(currentpointer, data, size); memcpy(currentpointer, data, size);
attachlist->AddItem(currentpointer); fAttachList->AddItem(currentpointer);
header->AddAttachment(size); fHeader->AddAttachment(size);
Unlock(); Unlock();
} }
void AreaLink::Attach(const int32 &data)
void
AreaLink::Attach(const int32 &data)
{ {
Attach(&data, sizeof(int32)); Attach(&data, sizeof(int32));
} }
void AreaLink::Attach(const int16 &data)
void
AreaLink::Attach(const int16 &data)
{ {
Attach(&data, sizeof(int16)); Attach(&data, sizeof(int16));
} }
void AreaLink::Attach(const int8 &data)
void
AreaLink::Attach(const int8 &data)
{ {
Attach(&data, sizeof(int8)); Attach(&data, sizeof(int8));
} }
void AreaLink::Attach(const float &data)
void
AreaLink::Attach(const float &data)
{ {
Attach(&data, sizeof(float)); Attach(&data, sizeof(float));
} }
void AreaLink::Attach(const bool &data)
void
AreaLink::Attach(const bool &data)
{ {
Attach(&data, sizeof(bool)); Attach(&data, sizeof(bool));
} }
void AreaLink::Attach(const rgb_color &data)
void
AreaLink::Attach(const rgb_color &data)
{ {
Attach(&data, sizeof(rgb_color)); Attach(&data, sizeof(rgb_color));
} }
void AreaLink::Attach(const BRect &data)
void
AreaLink::Attach(const BRect &data)
{ {
Attach(&data, sizeof(BRect)); Attach(&data, sizeof(BRect));
} }
void AreaLink::Attach(const BPoint &data)
void
AreaLink::Attach(const BPoint &data)
{ {
Attach(&data, sizeof(BPoint)); Attach(&data, sizeof(BPoint));
} }
void AreaLink::MakeEmpty(void)
void
AreaLink::MakeEmpty()
{ {
attachlist->MakeEmpty(); fAttachList->MakeEmpty();
header->MakeEmpty(); fHeader->MakeEmpty();
} }
void AreaLink::_ReadAttachments(void)
void
AreaLink::_ReadAttachments()
{ {
int8 *index=baseaddress; int8 *index = fBaseAddress;
uint16 size; uint16 size;
for(uint32 i=0; i<header->GetAttachmentCount();i++) for (uint32 i = 0; i < fHeader->GetAttachmentCount(); i++) {
{
size = *((int16*)index); size = *((int16*)index);
baseaddress+=SIZE_SIZE; fBaseAddress += SIZE_SIZE;
attachlist->AddItem(baseaddress); fAttachList->AddItem(fBaseAddress);
baseaddress+=size; fBaseAddress += size;
} }
} }
void AreaLink::Lock(void)
void
AreaLink::Lock()
{ {
if(!have_lock) if (!fHaveLock)
acquire_sem(header->GetLockSem()); acquire_sem(fHeader->GetLockSem());
} }
void AreaLink::Unlock(void)
void
AreaLink::Unlock()
{ {
if(have_lock) if (fHaveLock)
release_sem(header->GetLockSem()); release_sem(fHeader->GetLockSem());
} }
bool AreaLink::IsLocked(void)
bool
AreaLink::IsLocked()
{ {
return have_lock; return fHaveLock;
} }