include/MemoryMap.h
author Jens Alfke <jens@mooseyard.com>
Sun Sep 20 15:14:12 2009 -0700 (2009-09-20)
changeset 0 31a43d94cc26
permissions -rw-r--r--
First official checkin.
     1 /*
     2  *  MemoryMap.h
     3  *  Ottoman
     4  *
     5  *  Created by Jens Alfke on 9/17/09.
     6  *  Copyright 2009 Jens Alfke. All rights reserved.
     7  *  BSD-Licensed: See the file "LICENSE.txt" for details.
     8  */
     9 
    10 #include "File.h"
    11 
    12 namespace Mooseyard {
    13     
    14     /** A flexible memory-map on a file, which can handle multiple discontiguous mapped regions.
    15         Clients usually access this functionality through File, which delegates to it. */
    16     class MemoryMap {
    17     public:
    18         MemoryMap (File* file)                          :_file(file), _nRegions(0), _regions(NULL) { }
    19         ~MemoryMap();
    20         
    21         void mapRegion (off_t position, size_t length);
    22         const void* mappedPosition (off_t position) const;
    23         
    24     private:
    25         UNCOPYABLE(MemoryMap);
    26         class Region;
    27         const void* _at (off_t) const;
    28         
    29         File *_file;
    30         int _nRegions;
    31         Region **_regions;
    32     };
    33     
    34     
    35     class MemoryMap::Region {
    36     public:
    37         Region (File*, off_t position, size_t length);
    38         ~Region();
    39         off_t position() const                  {return _position;}
    40         size_t length() const                   {return _length;}
    41         off_t end() const                       {return _position + _length;}
    42         bool setLength (size_t);          // Returns false if it failed
    43         const void* mappedPosition(off_t);
    44     private:
    45         UNCOPYABLE(Region);
    46         File* const _file;
    47         const off_t _position;
    48         size_t _length;
    49         const uint8_t *_start;
    50     };
    51     
    52     
    53 }