diff --git a/headers/private/app/PortLink.h b/headers/private/app/PortLink.h index 00b1daa5a8..7fc1b811f8 100644 --- a/headers/private/app/PortLink.h +++ b/headers/private/app/PortLink.h @@ -7,9 +7,26 @@ #include #include -class PortLinkData; class PortMessage; +/*! + \class PortLinkData PortLink.cpp + \brief Internal data storage class + + PortLinkData objects serve to hold attached data whilst it is waiting to be Flattened() + and then Flushed(). There is no need for this to be used outside the PortLink class. +*/ +class PortLinkData +{ +public: + PortLinkData(void); + ~PortLinkData(void); + status_t Set(const void *data, size_t size); + char *buffer; + size_t buffersize; +}; + + class PortLink { public: @@ -35,7 +52,29 @@ public: status_t FlushWithReply(PortMessage *msg,bigtime_t timeout=B_INFINITE_TIMEOUT); status_t Attach(const void *data, size_t size); - template status_t Attach(Type data); + template status_t Attach(Type data) + { + int32 size=sizeof(Type); + + #ifdef CAPACITY_CHECKING + if(bufferlength+size>capacity) + return B_NO_MEMORY; + #endif + + // create a new storage object and stash the data + PortLinkData *pld=new PortLinkData; + if(pld->Set(&data,size)==B_OK) + { + attachlist->AddItem(pld); + bufferlength+=size; + } + else + { + delete pld; + return B_ERROR; + } + return B_OK; + } void MakeEmpty(void); protected: void FlattenData(int8 **buffer,int32 *size); diff --git a/headers/private/app/PortMessage.h b/headers/private/app/PortMessage.h index 4d6c5e96c2..687979f16e 100644 --- a/headers/private/app/PortMessage.h +++ b/headers/private/app/PortMessage.h @@ -48,7 +48,23 @@ public: ssize_t BufferSize(void) { return _buffersize; } status_t Read(void *data, ssize_t size); - template status_t Read(Type *data); + template status_t Read(Type *data) + { + int32 size = sizeof(Type); + + if(!data) + return B_BAD_VALUE; + + if( !_buffer || + (_buffersize < size) || + (_index+size > _buffer+_buffersize) ) + return B_NO_MEMORY; + + *data=*((Type*)_index); + _index+=size; + + return B_OK; + } void Rewind(void);