[Assorted-commits] SF.net SVN: assorted:[1355] cpp-commons/trunk/src/commons/mmap.h
Brought to you by:
yangzhang
From: <yan...@us...> - 2009-04-29 05:25:50
|
Revision: 1355 http://assorted.svn.sourceforge.net/assorted/?rev=1355&view=rev Author: yangzhang Date: 2009-04-29 05:25:41 +0000 (Wed, 29 Apr 2009) Log Message: ----------- added mmap abstractions Added Paths: ----------- cpp-commons/trunk/src/commons/mmap.h Added: cpp-commons/trunk/src/commons/mmap.h =================================================================== --- cpp-commons/trunk/src/commons/mmap.h (rev 0) +++ cpp-commons/trunk/src/commons/mmap.h 2009-04-29 05:25:41 UTC (rev 1355) @@ -0,0 +1,44 @@ +#ifndef COMMONS_MMAP_H +#define COMMONS_MMAP_H + +#include <commons/check.h> +#include <commons/closing.h> +#include <commons/nullptr.h> +#include <commons/files.h> +#include <sys/mman.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <fcntl.h> + +namespace commons { + +enum { prot_read = PROT_READ }; +enum { map_private = MAP_PRIVATE }; +const void *map_failed = reinterpret_cast<void*>(-1); + +class mmapping +{ +public: + explicit mmapping(const char *path) + : fd_(checknnegerr(open(path, O_RDONLY))) + { + len_ = file_size(fd_); + map_ = checkpass(mmap(nullptr, len_, prot_read, map_private, fd_, 0)); + checkneq(map_, map_failed); + } + + ~mmapping() { if (map_ != nullptr) check0x(munmap(map_, len_)); } + int fd() const { return fd_; } + void *get() const { return map_; } + size_t len() const { return len_; } + void release() { map_ = nullptr; fd_.release(); } + +private: + closingfd fd_; + size_t len_; + void *map_; +}; + +} + +#endif This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |