Added library target, changed some build settings, and fixed 64-to-32-bit conversion warnings.
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.
10 #include "MemoryMap.h"
18 MemoryMap::~MemoryMap() {
19 for (int i=0; i<_nRegions; i++)
24 void MemoryMap::mapRegion (off_t pos, size_t length) {
25 off_t end = pos+length;
26 for (int i=0; i<_nRegions; i++) {
27 Region *region = _regions[i];
28 if (region->position() <= pos) {
29 if (end <= region->end())
30 return; // found an existing region covering this range
31 else if (region->setLength((size_t)(end - region->position())))
32 return; // able to grow the existing region
36 // No existing region, so add a new one:
37 Region *region = new Region(_file, pos,length);
38 _regions = (Region**) realloc(_regions, (_nRegions+1)*sizeof(*_regions));
39 _regions[_nRegions++] = region;
42 const void* MemoryMap::_at (off_t pos) const {
43 for (int i=0; i<_nRegions; i++) {
44 const void *ptr = _regions[i]->mappedPosition(pos);
51 const void* MemoryMap::mappedPosition (off_t pos) const {
52 const void *result = _at(pos);
54 throw File::Error("No memory mapped at this position");
61 MemoryMap::Region::Region (File* file, off_t position, size_t length)
65 _start( (const uint8_t*) ::mmap(NULL, length, PROT_READ, MAP_PRIVATE, file->_fd, position) )
67 printf("File#%i: Mapped (%6llu -- %6llu) --> %p\n", file->_fd, _position, end(), _start);
68 if (_start==NULL || _start == MAP_FAILED) {
70 throw File::Error(errno, strerror(errno));
74 MemoryMap::Region::~Region() {
76 printf("File#%i: Unmapped (%6llu -- %6llu) from %p\n", _file->_fd, _position, end(), _start);
77 ::munmap((void*)_start,_length);
81 bool MemoryMap::Region::setLength (size_t length) {
82 if (length != _length) {
83 printf("File#%i: Resiging (%6llu -- %6llu) from %lu to %lu ...",
84 _file->_fd, _position, end(), _length,length);
85 if (::mmap((void*)_start, length, PROT_READ, MAP_PRIVATE | MAP_FIXED, _file->_fd, _position) == MAP_FAILED) {
86 printf("failed! errno=%i\n", errno);
95 const void* MemoryMap::Region::mappedPosition (off_t pos) {
96 if (pos >= _position && pos < end())
97 return _start + (pos-_position);