summaryrefslogtreecommitdiff
path: root/source/utilities/MemMappedFile.cpp
blob: 21eea7b1624280f9ca441c5aa006d562e2f0a048 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*******************************************************************************
 * Copyright (C) ST-Ericsson SA 2011
 * License terms: 3-clause BSD license
 ******************************************************************************/

#include "lcdriver_error_codes.h"
#include "MemMappedFile.h"
#if defined(_WIN32)
#include <windows.h>
#elif (defined(__linux__) || defined(__APPLE__))
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <string.h>
using namespace std;
#else
#error "Unknown target"
#endif

#ifdef __APPLE__
#define lseek64 lseek
#endif

MemMappedFile::MemMappedFile(uint32 alignmentLength):
    size_(0),
    isMapped_(false),
    mappedData_(0),
    error_(0),
    alignmentLength_(alignmentLength),
#ifdef _WIN32
    handle_(INVALID_HANDLE_VALUE),
    memmap_(INVALID_HANDLE_VALUE)
#else
    descriptor_(-1)
#endif
{
}

MemMappedFile::~MemMappedFile()
{
#ifdef _WIN32

    if (0 != mappedData_) {
        UnmapViewOfFile(mappedData_);
    }

    if (INVALID_HANDLE_VALUE != memmap_) {
        CloseHandle(memmap_);
    }

    if (INVALID_HANDLE_VALUE != handle_) {
        CloseHandle(handle_);
    }

#else

    if (0 != mappedData_) {
        munmap(mappedData_, size_);
    }

    if (-1 != descriptor_) {
        close(descriptor_);
    }

#endif
}

int MemMappedFile::LoadFileData(const char *path)
{
    path_ = path;
#ifdef _WIN32
    handle_ = CreateFile(path_.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

    if (INVALID_HANDLE_VALUE == handle_) {
        return FILE_OPENING_ERROR;
    }

    uint64 SizeHigh = 0;

    size_ = ::GetFileSize(handle_, (LPDWORD)&SizeHigh);
    size_ = (SizeHigh << 32) | size_;

    if (0 == size_) {
        return 0;
    }

    memmap_ = CreateFileMapping(handle_, NULL, PAGE_READONLY, 0, 0, NULL);

    if (INVALID_HANDLE_VALUE == memmap_) {
        return FILE_CREATE_MAPPING_ERROR;
    }

    mappedData_ = static_cast<uint8 *>(MapViewOfFile(memmap_, FILE_MAP_READ, 0, 0, 0));

    if (0 != mappedData_) {
        isMapped_ = true;
        volatile char tmp;

        for (uint64 i = 0; i < size_; i += 4096) {
            tmp = mappedData_[i];
        }
    } else {
        isMapped_ = false;
        CloseHandle(memmap_);
        memmap_ = INVALID_HANDLE_VALUE;
    }

#else
    descriptor_ = open(path_.c_str(), O_RDONLY);

    if (-1 == descriptor_) {
        return FILE_OPENING_ERROR;
    }

    struct stat fileStat;

    if (0 != fstat(descriptor_, &fileStat)) {
        return FILE_FAILED_TO_GET_SIZE;
    }

    size_ = fileStat.st_size;

    /* Map file in memory, BUT DON"T Reserve SWAP memory, use only physical memory */
    mappedData_ = static_cast<uint8 *>(mmap(0, size_, PROT_READ, MAP_PRIVATE | MAP_NORESERVE /*| MAP_POPULATE*/, descriptor_, 0));

    if (MAP_FAILED != mappedData_) {
        isMapped_ = true;
    } else {
        isMapped_ = false;
    }

#endif
    return 0;
}

uint8 *MemMappedFile::AllocateFileData(uint64 offset, uint64 size)
{
    if (size_ < offset + size) {
        error_ = FILE_READ_INVALID_OFFSET;
        return 0;
    }

    uint32 alignedSize = (static_cast<uint32>(size) + (alignmentLength_ - 1)) & (~(alignmentLength_ - 1));

    if (isMapped_) {
        if (size_ < offset + alignedSize) {
            uint8 *data = new uint8[alignedSize];
            memcpy(data, mappedData_ + offset, static_cast<size_t>(size));
            return data;
        } else {
            return mappedData_ + offset;
        }
    } else {
        // file is not memory mapped fall back to plain read
        uint32 readSize = static_cast<uint32>(size);
        uint8 *data = new uint8[alignedSize];
#ifdef _WIN32
        LARGE_INTEGER liOffset;
        liOffset.QuadPart = offset;

        if (!SetFilePointerEx(handle_, liOffset, NULL, FILE_BEGIN)) {
            delete[] data;
            error_ = FILE_READ_INVALID_OFFSET;
            return 0;
        }

        uint32 bytesRead;

        if (ReadFile(handle_, data, readSize, (LPDWORD)&bytesRead, NULL) && bytesRead == size) {
            return data;
        } else {
            delete[] data;
            error_ = FILE_READ_ERROR;
            return 0;
        }

#else

        if (-1 == lseek64(descriptor_, offset, SEEK_SET)) {
            delete[] data;
            error_ = FILE_READ_INVALID_OFFSET;
            return 0;
        }

        if (readSize == (uint32)read(descriptor_, data, readSize)) {
            return data;
        } else {
            delete[] data;
            error_ = FILE_READ_ERROR;
            return 0;
        }

#endif
    }
}

void MemMappedFile::ReleaseFileData(uint8 *data, uint64 offset, uint64 size)
{
    uint32 alignedSize = (static_cast<uint32>(size) + (alignmentLength_ - 1)) & (~(alignmentLength_ - 1));

    if (!isMapped_ || size_ < offset + alignedSize) {
        delete[] data;
    }
}

uint64 MemMappedFile::GetFileSize()
{
    return size_;
}

int MemMappedFile::GetError()
{
    return error_;
}