[4] | 1 | /******************************************************************************** |
---|
| 2 | * Volatile - Volume Visualization Software for SAGE |
---|
| 3 | * Copyright (C) 2004 Electronic Visualization Laboratory, |
---|
| 4 | * University of Illinois at Chicago |
---|
| 5 | * |
---|
| 6 | * All rights reserved. |
---|
| 7 | * |
---|
| 8 | * Redistribution and use in source and binary forms, with or without |
---|
| 9 | * modification, are permitted provided that the following conditions are met: |
---|
| 10 | * |
---|
| 11 | * * Redistributions of source code must retain the above copyright |
---|
| 12 | * notice, this list of conditions and the following disclaimer. |
---|
| 13 | * * Redistributions in binary form must reproduce the above |
---|
| 14 | * copyright notice, this list of conditions and the following disclaimer |
---|
| 15 | * in the documentation and/or other materials provided with the distribution. |
---|
| 16 | * * Neither the name of the University of Illinois at Chicago nor |
---|
| 17 | * the names of its contributors may be used to endorse or promote |
---|
| 18 | * products derived from this software without specific prior written permission. |
---|
| 19 | * |
---|
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
---|
| 24 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
---|
| 25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
---|
| 26 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
---|
| 27 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
---|
| 28 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
---|
| 29 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
| 30 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
| 31 | * |
---|
| 32 | * Direct questions, comments etc about Volatile to www.evl.uic.edu/cavern/forum |
---|
| 33 | *********************************************************************************/ |
---|
| 34 | |
---|
| 35 | #include "mmap.h" |
---|
| 36 | #include <stdio.h> |
---|
| 37 | |
---|
| 38 | Mmap::Mmap() |
---|
| 39 | { |
---|
| 40 | _filename = 0; |
---|
| 41 | _len = 0; |
---|
| 42 | _fd = -1; |
---|
| 43 | //_prot = PROT_READ|PROT_WRITE; |
---|
| 44 | _prot = PROT_READ; |
---|
| 45 | _shared = MAP_PRIVATE; |
---|
| 46 | _mmapAddress = (caddr_t) -1; |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | Mmap::~Mmap() |
---|
| 50 | { |
---|
| 51 | |
---|
| 52 | // free filename |
---|
| 53 | if(_filename) |
---|
| 54 | delete _filename; |
---|
| 55 | |
---|
| 56 | // close file |
---|
| 57 | if(_fd >= 0) |
---|
| 58 | close(_fd); |
---|
| 59 | |
---|
| 60 | // unmap file |
---|
| 61 | if(_mmapAddress) |
---|
| 62 | munmap(_mmapAddress, _len); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | int |
---|
| 66 | Mmap::setFilename(const char *filename, int create) |
---|
| 67 | { |
---|
| 68 | int len = strlen(filename)+1; |
---|
| 69 | _filename = new char [len]; |
---|
| 70 | strcpy(_filename, filename); |
---|
| 71 | |
---|
| 72 | struct stat statbuf; |
---|
| 73 | |
---|
| 74 | |
---|
| 75 | if(!create) |
---|
| 76 | { |
---|
| 77 | // told not to create it -- it must exist already |
---|
| 78 | _fd = open(_filename, O_RDONLY) ; |
---|
| 79 | } |
---|
| 80 | else { |
---|
| 81 | _fd = open(_filename, O_RDONLY|O_CREAT, 00600); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | if(_fd < 0) |
---|
| 85 | return -1; |
---|
| 86 | |
---|
| 87 | int rc = stat(_filename, &statbuf); |
---|
| 88 | if(rc < 0) |
---|
| 89 | { |
---|
| 90 | // Strange -- we could open but not stat???? |
---|
| 91 | return(-1); |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | _len = (size_t) statbuf.st_size; |
---|
| 95 | fprintf(stderr,"File size %d\n",_len); |
---|
| 96 | return 1; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | void Mmap::setSize(size_t size) |
---|
| 100 | { |
---|
| 101 | if(_len != size) { |
---|
| 102 | // Must resize the file |
---|
| 103 | lseek(_fd, (off_t) size, SEEK_SET); |
---|
| 104 | // write one byte to ensure that we're at least this size |
---|
| 105 | write(_fd, "", 1); |
---|
| 106 | // Now truncate the file to be this size |
---|
| 107 | ftruncate(_fd, size); |
---|
| 108 | } |
---|
| 109 | _len = size; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | |
---|
| 113 | |
---|
| 114 | caddr_t |
---|
| 115 | Mmap::doMmap() |
---|
| 116 | { |
---|
| 117 | _mmapAddress = (caddr_t) mmap(0, _len, _prot, _shared, _fd, 0); |
---|
| 118 | |
---|
| 119 | return _mmapAddress; |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | void |
---|
| 123 | Mmap::bzero() |
---|
| 124 | { |
---|
| 125 | if(_mmapAddress != (caddr_t) -1) |
---|
| 126 | memset(_mmapAddress, 0, _len); |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | |
---|