| 1 |
/* |
| 2 |
* mmap.h |
| 3 |
* |
| 4 |
* This file is a part of NSIS. |
| 5 |
* |
| 6 |
* Copyright (C) 1999-2026 Nullsoft and Contributors |
| 7 |
* |
| 8 |
* Licensed under the zlib/libpng license (the "License"); |
| 9 |
* you may not use this file except in compliance with the License. |
| 10 |
* |
| 11 |
* Licence details can be found in the file COPYING. |
| 12 |
* |
| 13 |
* This software is provided 'as-is', without any express or implied |
| 14 |
* warranty. |
| 15 |
* |
| 16 |
* Unicode support by Jim Park -- 08/13/2007 |
| 17 |
*/ |
| 18 |
|
| 19 |
#ifndef __MMAP_H_ |
| 20 |
#define __MMAP_H_ |
| 21 |
|
| 22 |
#include "Platform.h" |
| 23 |
#include "growbuf.h" |
| 24 |
#include "tchar.h" |
| 25 |
|
| 26 |
#ifndef _WIN32 |
| 27 |
#include <cstdio> // for FILE* |
| 28 |
#include <fstream> // (some systems have FILE* in here) |
| 29 |
#endif |
| 30 |
|
| 31 |
class IMMap |
| 32 |
{ |
| 33 |
public: |
| 34 |
typedef DWORD maxfilesizetype; // TODO: Change to UINT64 |
| 35 |
static inline UINT64 getmaxoffset() { return INT_MAX; } // NSIS offset limit is 31 bits right now |
| 36 |
|
| 37 |
virtual void resize(int newlen)=0; |
| 38 |
virtual int getsize() const=0; |
| 39 |
virtual void *get(int offset, size_t size) const=0; |
| 40 |
virtual void *get(int offset, size_t *size) const=0; |
| 41 |
virtual void *getmore(int offset, size_t size) const=0; |
| 42 |
virtual void release()=0; |
| 43 |
virtual void release(void *view, size_t size)=0; |
| 44 |
virtual void clear()=0; |
| 45 |
virtual void setro(BOOL bRO)=0; |
| 46 |
virtual void flush(size_t num)=0; |
| 47 |
virtual ~IMMap() {} |
| 48 |
|
| 49 |
virtual bool write_to_external_file(FILE*file, UINT64 size = ~(UINT64)0); |
| 50 |
}; |
| 51 |
|
| 52 |
class MMapFile : public IMMap |
| 53 |
{ |
| 54 |
private: // don't copy instances |
| 55 |
MMapFile(const MMapFile&); |
| 56 |
void operator=(const MMapFile&); |
| 57 |
|
| 58 |
public: |
| 59 |
static DWORD getmaxfilesize(); |
| 60 |
|
| 61 |
MMapFile(); |
| 62 |
virtual ~MMapFile(); |
| 63 |
|
| 64 |
/** |
| 65 |
* Closes the memory map and the file handle. |
| 66 |
*/ |
| 67 |
void clear(); |
| 68 |
|
| 69 |
/** |
| 70 |
* Set read-only. |
| 71 |
* @param bRO Boolean value to set read-only. |
| 72 |
*/ |
| 73 |
void setro(BOOL bRO); |
| 74 |
|
| 75 |
/** |
| 76 |
* Creates the memory mapping object of the file with a mapping size. |
| 77 |
* |
| 78 |
* @param hFile The handle to the opened file. |
| 79 |
* @param size The size of the memory mapped object. You cannot set |
| 80 |
* this value to zero like with CreateFileMapping() because it will |
| 81 |
* immediately return. Most likely, you want to set it to the size |
| 82 |
* of the file unless you want to only map a part of the file on |
| 83 |
* purpose. |
| 84 |
* @return Returns 1 on success, 0 on failure. |
| 85 |
*/ |
| 86 |
#ifdef _WIN32 |
| 87 |
bool setfile(HANDLE hFile, UINT64 size); |
| 88 |
private: |
| 89 |
int internalsetfile(HANDLE hFile, DWORD dwSize); |
| 90 |
#else |
| 91 |
bool setfile(FILE*hFile, UINT64 size); |
| 92 |
private: |
| 93 |
int internalsetfile(int hFile, DWORD dwSize); |
| 94 |
#endif |
| 95 |
|
| 96 |
public: |
| 97 |
UINT64 setfile(const TCHAR*fpath); |
| 98 |
|
| 99 |
#ifdef _WIN32 |
| 100 |
static HANDLE openfilehelper(const TCHAR*fpath, UINT64 &size); |
| 101 |
#else |
| 102 |
static FILE* openfilehelper(const TCHAR*fpath, UINT64 &size); |
| 103 |
#endif |
| 104 |
|
| 105 |
/** |
| 106 |
* Resize the memory mapping of the file. Used when the filesize has |
| 107 |
* changed. When setfile has not been called previously, then it will |
| 108 |
* create a temporary file and use it to create a memory map. This is |
| 109 |
* what's used by MMapBuf to create a Memory Mapped Buffer. |
| 110 |
* |
| 111 |
* @param newsize The new size of the file. Limited to 32-bits. |
| 112 |
*/ |
| 113 |
void resize(int newsize); |
| 114 |
|
| 115 |
/** |
| 116 |
* Size of the memory map object. |
| 117 |
*/ |
| 118 |
int getsize() const; |
| 119 |
|
| 120 |
/** |
| 121 |
* Set the memory map to a particular offset in the file and return the |
| 122 |
* memory mapped pointer to it. Internally it may have to align to a |
| 123 |
* certain page size. |
| 124 |
* |
| 125 |
* @param offset The offset from the beginning of the file. |
| 126 |
* @param size The size of the memory map window. |
| 127 |
*/ |
| 128 |
void *get(int offset, size_t size) const; |
| 129 |
|
| 130 |
/** |
| 131 |
* Set the memory map to a particular offset in the file and return the |
| 132 |
* memory mapped pointer to it. Internally it may have to align to a |
| 133 |
* certain page size. |
| 134 |
* |
| 135 |
* @param offset The offset from the beginning of the file. |
| 136 |
* @param sizep [in/out] The size of the memory map window. (In non-Win32 |
| 137 |
* systems, the new size is written back out.) |
| 138 |
*/ |
| 139 |
void *get(int offset, size_t *sizep) const; |
| 140 |
|
| 141 |
/** |
| 142 |
* This function sets memory map and just hands you the pointer and |
| 143 |
* it expects you to manage it. So you need to call release(pView, size) |
| 144 |
* yourself or you will leak memory. |
| 145 |
* |
| 146 |
* Warning: This breaks encapsulation. The user should probably just |
| 147 |
* create a new map. |
| 148 |
* |
| 149 |
* @param offset The offset from the beginning of the file. |
| 150 |
* @param size The size of the memory map window. |
| 151 |
*/ |
| 152 |
void *getmore(int offset, size_t size) const; |
| 153 |
|
| 154 |
/** |
| 155 |
* Releases the memory map currently being used. Calls UnMapViewOfFile(). |
| 156 |
*/ |
| 157 |
void release(); |
| 158 |
|
| 159 |
/** |
| 160 |
* Releases the memory map pointed to by pView. In Win32 systems |
| 161 |
* eventually calls UnmapViewOfFile(). Interestingly, the function |
| 162 |
* tries to align the pointer value back to the beginning of the |
| 163 |
* paged memory which is necessary because of the way get() works. |
| 164 |
* |
| 165 |
* This looks like it should only be used in conjunction with |
| 166 |
* getmore(). Otherwise, just call release(). |
| 167 |
* |
| 168 |
* @param pView The pointer to somewhere in a MemMapped object. |
| 169 |
* @param size The size of the object. Used only in non-Win32 systems. |
| 170 |
*/ |
| 171 |
void release(void *pView, size_t size); |
| 172 |
|
| 173 |
/** |
| 174 |
* Flushes the contents of the current memory map to disk. Set size to 0 |
| 175 |
* if you want to flush everything. |
| 176 |
* |
| 177 |
* @param num The number of bytes to flush. 0 for everything. |
| 178 |
*/ |
| 179 |
void flush(size_t num); |
| 180 |
|
| 181 |
private: |
| 182 |
#ifdef _WIN32 |
| 183 |
HANDLE m_hFile, m_hFileMap; |
| 184 |
#else |
| 185 |
FILE *m_hFile; |
| 186 |
int m_hFileDesc; |
| 187 |
mutable int m_iMappedSize; |
| 188 |
#endif |
| 189 |
mutable void *m_pView; |
| 190 |
mutable int m_iSize; |
| 191 |
BOOL m_bReadOnly; |
| 192 |
BOOL m_bTempHandle; |
| 193 |
|
| 194 |
static int m_iAllocationGranularity; |
| 195 |
}; |
| 196 |
|
| 197 |
class MMapFake : public IMMap |
| 198 |
{ |
| 199 |
private: // don't copy instances |
| 200 |
MMapFake(const MMapFake&); |
| 201 |
void operator=(const MMapFake&); |
| 202 |
public: |
| 203 |
MMapFake(); |
| 204 |
|
| 205 |
void set(const char *pMem, int iSize); |
| 206 |
int getsize() const; |
| 207 |
void *get(int offset, size_t size) const; |
| 208 |
void *get(int offset, size_t *size) const; |
| 209 |
void *getmore(int offset, size_t size) const; |
| 210 |
|
| 211 |
void resize(int n); |
| 212 |
void release(); |
| 213 |
void release(void *p, size_t size); |
| 214 |
void clear(); |
| 215 |
void setro(BOOL b); |
| 216 |
void flush(size_t b); |
| 217 |
|
| 218 |
private: |
| 219 |
const char *m_pMem; |
| 220 |
int m_iSize; |
| 221 |
}; |
| 222 |
|
| 223 |
/** |
| 224 |
* A data structure that can be used to create a scratch file to do |
| 225 |
* work in. When it's smaller than 16mb, it's all in memory using the |
| 226 |
* GrowBuf class. But when it gets larger than 16mb, then it uses |
| 227 |
* the MMapFile class to create a memory map to a temporary file and |
| 228 |
* then uses it. This reduces memory overhead of the installer. |
| 229 |
* |
| 230 |
* This is sort of our virtual memory manager. |
| 231 |
*/ |
| 232 |
class MMapBuf : public IGrowBuf, public IMMap |
| 233 |
{ |
| 234 |
private: // don't copy instances |
| 235 |
MMapBuf(const MMapBuf&); |
| 236 |
void operator=(const MMapBuf&); |
| 237 |
|
| 238 |
public: |
| 239 |
MMapBuf(); |
| 240 |
virtual ~MMapBuf(); |
| 241 |
|
| 242 |
int add(const void *data, int len); |
| 243 |
void setro(BOOL bRO); |
| 244 |
void resize(int newlen); |
| 245 |
int getsize() const; |
| 246 |
int getlen() const; |
| 247 |
void *get() const; |
| 248 |
void *get(int offset, size_t *sizep) const; |
| 249 |
void *get(int offset, size_t size) const; |
| 250 |
void *getmore(int offset, size_t size) const; |
| 251 |
void release(); |
| 252 |
void release(void *pView, size_t size); |
| 253 |
void clear(); |
| 254 |
void flush(size_t num); |
| 255 |
|
| 256 |
protected: |
| 257 |
static inline int getmodethreshold() { return 16 << 20; } |
| 258 |
|
| 259 |
private: |
| 260 |
GrowBuf m_gb; |
| 261 |
MMapFile m_fm; |
| 262 |
|
| 263 |
int m_gb_u; |
| 264 |
int m_alloc, m_used; |
| 265 |
}; |
| 266 |
|
| 267 |
#endif//__MMAP_H_ |
| 268 |
|