src/MemoryMap.h
changeset 1 6cbad782d16a
parent 0 31a43d94cc26
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/MemoryMap.h	Sun Sep 20 20:39:24 2009 -0700
     1.3 @@ -0,0 +1,53 @@
     1.4 +/*
     1.5 + *  MemoryMap.h
     1.6 + *  Ottoman
     1.7 + *
     1.8 + *  Created by Jens Alfke on 9/17/09.
     1.9 + *  Copyright 2009 Jens Alfke. All rights reserved.
    1.10 + *  BSD-Licensed: See the file "LICENSE.txt" for details.
    1.11 + */
    1.12 +
    1.13 +#include "File.h"
    1.14 +
    1.15 +namespace Mooseyard {
    1.16 +    
    1.17 +    /** A flexible memory-map on a file, which can handle multiple discontiguous mapped regions.
    1.18 +        Clients usually access this functionality through File, which delegates to it. */
    1.19 +    class MemoryMap {
    1.20 +    public:
    1.21 +        MemoryMap (File* file)                          :_file(file), _nRegions(0), _regions(NULL) { }
    1.22 +        ~MemoryMap();
    1.23 +        
    1.24 +        void mapRegion (off_t position, size_t length);
    1.25 +        const void* mappedPosition (off_t position) const;
    1.26 +        
    1.27 +    private:
    1.28 +        UNCOPYABLE(MemoryMap);
    1.29 +        class Region;
    1.30 +        const void* _at (off_t) const;
    1.31 +        
    1.32 +        File *_file;
    1.33 +        int _nRegions;
    1.34 +        Region **_regions;
    1.35 +    };
    1.36 +    
    1.37 +    
    1.38 +    class MemoryMap::Region {
    1.39 +    public:
    1.40 +        Region (File*, off_t position, size_t length);
    1.41 +        ~Region();
    1.42 +        off_t position() const                  {return _position;}
    1.43 +        size_t length() const                   {return _length;}
    1.44 +        off_t end() const                       {return _position + _length;}
    1.45 +        bool setLength (size_t);          // Returns false if it failed
    1.46 +        const void* mappedPosition(off_t);
    1.47 +    private:
    1.48 +        UNCOPYABLE(Region);
    1.49 +        File* const _file;
    1.50 +        const off_t _position;
    1.51 +        size_t _length;
    1.52 +        const uint8_t *_start;
    1.53 +    };
    1.54 +    
    1.55 +    
    1.56 +}