From: Dejan L. <de...@us...> - 2004-04-20 18:54:10
|
Update of /cvsroot/rtk/rtk/src/core/platform/linux In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv28961/src/core/platform/linux Added Files: Mmap.cpp Log Message: Mmap.cpp added to /src/core/platform/linux --- NEW FILE: Mmap.cpp --- /** * * RTK * Fast and easy cross-platform GUI ToolKit. * * Copyright (C) 2001-200x RTK Development Team * * This library is free software; you can redistribute it and/or modify it * under the terms of the slightly modified (see the "EXCEPTION NOTICE" part * of RTK Library License) GNU Lesser General Public License as published * by the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public License * and along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA . * * Also you should have received a copy of RTK Library License, if not please * write an e-mail to some of RTK authors (listed in file AUTHORS). * * Bug reports: bu...@rt... * Suggestions: rf...@rt... ***************************************************************************/ /** * $Source: /cvsroot/rtk/rtk/src/core/platform/linux/Mmap.cpp,v $ ***** * Authors (chronological order): * Dejan Lekic, de...@nu... (dejan§rtk.cx) * Contributors (chronological order): * $fname $lname, $email ***** * T0D0 List: * - ***************************************************************************/ #include <rtk/Mmap.h> namespace Rtk { Mmap::Mmap() { _file_opened = false; _filename = 0; _len = 0; _fd = -1; _prot = PROT_READ|PROT_WRITE; _shared = MAP_SHARED; _mmapAddress = (caddr_t) -1; } // Mmap() constructor Mmap::Mmap(const String& fname, bool bc) { if (SetFilename(fname.c_str(), bc)) _file_opened = true; else _file_opened = false; _len = 0; _fd = -1; _prot = PROT_READ|PROT_WRITE; _shared = MAP_SHARED; _mmapAddress = (caddr_t) -1; } // Mmap() constructor Mmap::~Mmap() { // free filename if (_filename) delete _filename; // close file if (_fd >= 0) close(_fd); // unmap file if (_mmap_address) munmap(_mmap_address, _len); } // ~Mmap() destructor bool Mmap::SetFilename(const char *filename, bool create) { int len = strlen(filename)+1; _filename = new RCHAR [len]; strcpy(_filename, filename); struct stat statbuf; if (!create) { _fd = open(_filename, O_RDWR) ; } else { _fd = open(_filename, O_RDWR|O_CREAT, 00600); } // Return -1 as result if opening went wrong if (_fd < 0) return false; int rc = stat(_filename, &statbuf); if (rc < 0) { return false; } _len = (size_t) statbuf.st_size; return true; } // SetFilename() void Mmap::SetSize(uint size) { if (_len != size) { lseek(_fd, (off_t) size, SEEK_SET); write(_fd, "", 1); ftruncate(_fd, size); } _len = size; } void* Mmap::Map() { _mmap_address = (void*)mmap(0, _len, _prot, _shared, _fd, 0); return _mmap_address; } // Map() void Mmap::Bzero() { if (_mmap_address != (void*)-1) memset(_mmap_address, 0, _len); } // Bzero() }; // Rtk /** * $Id: Mmap.cpp,v 1.1 2004/04/20 18:54:00 dejan Exp $ ***************************************************************************/ |