Differences From Artifact [5df33e3bf5]:
- File src/OFSecureData.m — part of check-in [7413a728a7] at 2024-04-03 02:16:03 on branch trunk — Change license to LGPLv3 only (user: js, size: 15153) [annotate] [blame] [check-ins using] [more...]
To Artifact [d0c4634748]:
- File
src/OFSecureData.m
— part of check-in
[96f946d7ad]
at
2024-05-09 10:21:24
on branch trunk
— Use unsigned long for bit sets
This should be more efficient on some architectures.
As a nice side effect, this is also what Linux does in some userspace
APIs, so after this change, these macros can be used for this as well
(and Linux interestingly does not provide similar macros in the
userspace headers). (user: js, size: 15198) [annotate] [blame] [check-ins using] [more...]
︙ | ︙ | |||
40 41 42 43 44 45 46 | #import "OFOutOfRangeException.h" #if defined(HAVE_MMAP) && defined(HAVE_MLOCK) && defined(MAP_ANON) static const size_t chunkSize = 16; struct Page { struct Page *next, *previous; | | | 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | #import "OFOutOfRangeException.h" #if defined(HAVE_MMAP) && defined(HAVE_MLOCK) && defined(MAP_ANON) static const size_t chunkSize = 16; struct Page { struct Page *next, *previous; unsigned long *map; unsigned char *page; }; # if defined(OF_HAVE_COMPILER_TLS) static thread_local struct Page *firstPage = NULL; static thread_local struct Page *lastPage = NULL; static thread_local struct Page **preallocatedPages = NULL; |
︙ | ︙ | |||
98 99 100 101 102 103 104 | munmap(pointer, numPages * pageSize); } static struct Page * addPage(bool allowPreallocated) { size_t pageSize = [OFSystemInfo pageSize]; | | | | 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | munmap(pointer, numPages * pageSize); } static struct Page * addPage(bool allowPreallocated) { size_t pageSize = [OFSystemInfo pageSize]; size_t mapSize = OFRoundUpToPowerOf2(OF_ULONG_BIT, pageSize / chunkSize) / OF_ULONG_BIT; struct Page *page; # if !defined(OF_HAVE_COMPILER_TLS) && defined(OF_HAVE_THREADS) struct Page *lastPage; # endif if (allowPreallocated) { # if !defined(OF_HAVE_COMPILER_TLS) && defined(OF_HAVE_THREADS) |
︙ | ︙ | |||
140 141 142 143 144 145 146 | return page; } } page = OFAllocMemory(1, sizeof(*page)); @try { | | | 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | return page; } } page = OFAllocMemory(1, sizeof(*page)); @try { page->map = OFAllocZeroedMemory(mapSize, sizeof(unsigned long)); } @catch (id e) { OFFreeMemory(page); @throw e; } @try { page->page = mapPages(1); } @catch (id e) { |
︙ | ︙ | |||
182 183 184 185 186 187 188 | return page; } static void removePageIfEmpty(struct Page *page) { | | | | | 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | return page; } static void removePageIfEmpty(struct Page *page) { unsigned long *map = page->map; size_t pageSize = [OFSystemInfo pageSize]; size_t mapSize = OFRoundUpToPowerOf2(OF_ULONG_BIT, pageSize / chunkSize) / OF_ULONG_BIT; for (size_t i = 0; i < mapSize; i++) if (map[i] != 0) return; unmapPages(page->page, 1); OFFreeMemory(page->map); |
︙ | ︙ | |||
225 226 227 228 229 230 231 | bytes = OFRoundUpToPowerOf2(chunkSize, bytes); chunks = chunksLeft = bytes / chunkSize; firstChunk = 0; pageSize = [OFSystemInfo pageSize]; for (i = 0; i < pageSize / chunkSize; i++) { | | | | | 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | bytes = OFRoundUpToPowerOf2(chunkSize, bytes); chunks = chunksLeft = bytes / chunkSize; firstChunk = 0; pageSize = [OFSystemInfo pageSize]; for (i = 0; i < pageSize / chunkSize; i++) { if (OFBitSetIsSet(page->map, i)) { chunksLeft = chunks; firstChunk = i + 1; continue; } if (--chunksLeft == 0) break; } if (chunksLeft == 0) { for (size_t j = firstChunk; j < firstChunk + chunks; j++) OFBitSetSet(page->map, j); return page->page + (chunkSize * firstChunk); } return NULL; } static void freeMemory(struct Page *page, void *pointer, size_t bytes) { size_t chunks, chunkIndex; bytes = OFRoundUpToPowerOf2(chunkSize, bytes); chunks = bytes / chunkSize; chunkIndex = ((uintptr_t)pointer - (uintptr_t)page->page) / chunkSize; OFZeroMemory(pointer, bytes); for (size_t i = 0; i < chunks; i++) OFBitSetClear(page->map, chunkIndex + i); } #endif @implementation OFSecureData @synthesize allowsSwappableMemory = _allowsSwappableMemory; #if defined(HAVE_MMAP) && defined(HAVE_MLOCK) && defined(MAP_ANON) && \ |
︙ | ︙ |