//------------------------------------------------------------------------------ // Copyright (c) 2001-2005, Haiku // // 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: // // 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: ServerMemIO.cpp // Author(s): Stefano Ceccherini (burton666@libero.it) // The Storage Team // DarkWyrm // // Description: Adaptation of BMemoryIO code to allow for client-side usage // of shared memory which is allocated by the server. Model is // similar to BBitmap in this respect. // //------------------------------------------------------------------------------ #include #include #include #include #include #include #include "ServerMemIO.h" ServerMemIO::ServerMemIO(size_t size) : fLen(0), fPhys(0), fPos(0) { if (size == 0) return; BPrivate::AppServerLink link; link.StartMessage(AS_ACQUIRE_SERVERMEM); link.Attach(size); int32 code; if (link.FlushWithReply(code) == B_OK && code == SERVER_TRUE) { area_info info; link.Read(&fSourceArea); link.Read(&fOffset); if (fSourceArea >= B_OK && get_area_info(fSourceArea, &info) == B_OK) { fPhys = size; fArea = clone_area("ServerMemIO area", (void **)&fBuf, B_CLONE_ADDRESS, B_READ_AREA|B_WRITE_AREA, fSourceArea); fBuf += fOffset; } else { debugger("PANIC: bad data or something in ServerMemIO constructor"); } } } // Destruction ServerMemIO::~ServerMemIO() { BPrivate::AppServerLink link; link.StartMessage(AS_RELEASE_SERVERMEM); link.Attach(fSourceArea); link.Attach(fOffset); link.Flush(); } // ReadAt ssize_t ServerMemIO::ReadAt(off_t pos, void *buffer, size_t size) { if (buffer == NULL || pos < 0) return B_BAD_VALUE; ssize_t sizeRead = 0; if (pos < fLen) { sizeRead = min_c(static_cast(size), fLen - pos); memcpy(buffer, fBuf + pos, sizeRead); } return sizeRead; } // WriteAt ssize_t ServerMemIO::WriteAt(off_t pos, const void *buffer, size_t size) { if (buffer == NULL || pos < 0) return B_BAD_VALUE; ssize_t sizeWritten = 0; if (pos < fPhys) { sizeWritten = min_c(static_cast(size), fPhys - pos); memcpy(fBuf + pos, buffer, sizeWritten); } if (pos + sizeWritten > fLen) fLen = pos + sizeWritten; return sizeWritten; } // Seek off_t ServerMemIO::Seek(off_t position, uint32 seek_mode) { switch (seek_mode) { case SEEK_SET: fPos = position; break; case SEEK_CUR: fPos += position; break; case SEEK_END: fPos = fLen + position; break; default: break; } return fPos; } // Position off_t ServerMemIO::Position() const { return fPos; } // SetSize status_t ServerMemIO::SetSize(off_t size) { return B_NOT_ALLOWED; } size_t ServerMemIO::GetSize(void) const { return fLen; } void * ServerMemIO::GetPointer(void) const { return (void*)fBuf; } // Private or Reserved ServerMemIO::ServerMemIO(const ServerMemIO &) { //Copying not allowed } ServerMemIO & ServerMemIO::operator=(const ServerMemIO &) { //Copying not allowed return *this; }