IsoSpecPy 2.4.0__cp315-cp315-win32.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,622 @@
1
+ #pragma once
2
+
3
+ #include <cstddef> // std::size_t, std::nullptr_t
4
+ #include <cstdint> // std::uintptr_t (only if doing runtime alignment checks)
5
+ #include <cstdlib> // std::aligned_alloc, std::malloc, std::free
6
+ #include <cstring> // std::memcpy
7
+ #include <algorithm> // std::min
8
+ #include <limits> // std::numeric_limits
9
+ #include <memory> // std::assume_aligned (C++20)
10
+ #include <new> // std::bad_alloc
11
+ #include <utility> // std::exchange
12
+ #include <type_traits> // std::is_trivial_v
13
+
14
+ #include "platform.h" // ISOSPEC_WE_ARE_ON_WINDOWS / ISOSPEC_WE_ARE_ON_UNIX_YAY
15
+
16
+ // aligned_unique_ptr's realloc() grows/shrinks without copying once the
17
+ // allocation is big enough to be worth mapping directly from the OS. Below
18
+ // that size it stays on the plain aligned_alloc/copy/free path. Deliberately
19
+ // bypasses this project's own mman.h shim/ISOSPEC_GOT_MMAN machinery (used
20
+ // elsewhere for plain huge-page mmap): that layer has no remap concept at
21
+ // all on Windows and no equivalent of mremap on Darwin, so each platform's
22
+ // real growth primitive is used directly instead:
23
+ // - Linux / other real-mman UNIX: mmap + mremap(MREMAP_MAYMOVE) + munmap.
24
+ // - Apple: mmap/munmap for create/destroy (Darwin has no mremap); growth
25
+ // re-wires pages into a fresh mapping via mach_vm_remap instead of
26
+ // copying, shrink just munmaps the tail in place.
27
+ // - Windows: VirtualAlloc(MEM_RESERVE) a generously oversized address
28
+ // range once, then VirtualAlloc(MEM_COMMIT)/VirtualFree(MEM_DECOMMIT)
29
+ // to grow/shrink within it -- the base address never moves, so there is
30
+ // no copy at all in the common case (only if growth ever outgrows the
31
+ // initial reservation does it fall back to allocating a fresh, larger
32
+ // reservation and copying, same as a real mremap() that has to move).
33
+ // On any other platform (no mmap-family primitive available at all) the
34
+ // VM-backed path is simply never engaged; realloc() keeps using the
35
+ // aligned_alloc/copy/free path regardless of size.
36
+ #if ISOSPEC_WE_ARE_ON_WINDOWS || ISOSPEC_WE_ARE_ON_UNIX_YAY
37
+ #define ISOSPEC_ALIGNED_PTR_HAVE_VM_REALLOC 1
38
+ #else
39
+ #define ISOSPEC_ALIGNED_PTR_HAVE_VM_REALLOC 0
40
+ #endif
41
+
42
+ // The small-allocation backend needs an aligned allocator that can also be
43
+ // deallocated with a plain std::free() (release() promises exactly that).
44
+ // glibc/libc++'s std::aligned_alloc satisfies both at once. MSVC's CRT has
45
+ // no C11 aligned_alloc at all, only _aligned_malloc/_aligned_free -- and
46
+ // memory from those two is NOT free()-compatible (the CRT stores its own
47
+ // offset bookkeeping ahead of the returned pointer, which plain free() does
48
+ // not know how to unwind). So on MSVC the small backend uses
49
+ // _aligned_malloc/_aligned_free for its own storage (still genuinely
50
+ // Alignment-aligned, as marginalTrek++.h's use of this class relies on),
51
+ // and release() falls back to materialising a fresh plain-malloc()'d copy
52
+ // to keep its free()-compatibility promise -- the same thing it already
53
+ // does to escape a VM-backed region.
54
+ #if defined(_MSC_VER)
55
+ #define ISOSPEC_ALIGNED_PTR_SMALL_BACKEND_FREE_COMPATIBLE 0
56
+ #else
57
+ #define ISOSPEC_ALIGNED_PTR_SMALL_BACKEND_FREE_COMPATIBLE 1
58
+ #endif
59
+
60
+ #if ISOSPEC_WE_ARE_ON_WINDOWS
61
+ #include <windows.h>
62
+ #if defined(_MSC_VER)
63
+ #include <malloc.h> // _aligned_malloc / _aligned_free
64
+ #endif
65
+ #elif ISOSPEC_WE_ARE_ON_UNIX_YAY
66
+ #include <sys/mman.h> // real system mmap/munmap (+ mremap on Linux)
67
+ #include <unistd.h> // sysconf
68
+ #if defined(__APPLE__) && defined(__MACH__)
69
+ #include <mach/mach.h>
70
+ #include <mach/mach_vm.h>
71
+ #endif
72
+ #endif
73
+
74
+
75
+ #if ISOSPEC_ALIGNED_PTR_HAVE_VM_REALLOC
76
+ namespace aligned_ptr_detail {
77
+
78
+ // A namespace-scope initializer runs once, single-threaded, before this
79
+ // library is reachable from anywhere else -- unlike a function-local static,
80
+ // whose first-use initialization needs a runtime guard (a lock, on most
81
+ // ABIs) to stay safe if two threads race to initialize it. realloc() below
82
+ // queries this on every call regardless of size, making it the earliest and
83
+ // most frequently hit point such a guard could be contended.
84
+ inline std::size_t compute_os_page_size() noexcept
85
+ {
86
+ #if ISOSPEC_WE_ARE_ON_WINDOWS
87
+ SYSTEM_INFO si;
88
+ GetSystemInfo(&si);
89
+ return static_cast<std::size_t>(si.dwPageSize);
90
+ #else
91
+ return static_cast<std::size_t>(sysconf(_SC_PAGESIZE));
92
+ #endif
93
+ }
94
+
95
+ inline const std::size_t os_page_size_ = compute_os_page_size();
96
+
97
+ inline std::size_t os_page_size() noexcept
98
+ {
99
+ return os_page_size_;
100
+ }
101
+
102
+ inline std::size_t round_up_to_page(std::size_t bytes) noexcept
103
+ {
104
+ const std::size_t ps = os_page_size();
105
+ return (bytes + ps - 1) / ps * ps;
106
+ }
107
+
108
+ // Smallest allocation realloc() will move onto the VM-backed backend.
109
+ //
110
+ // Not simply "one page": mapping fresh anonymous memory makes the kernel hand
111
+ // out zero pages that then fault in one by one on first touch, and for a buffer
112
+ // that is about to be overwritten wholesale that zeroing is pure waste. malloc,
113
+ // by contrast, hands back memory the process already owns and has already
114
+ // faulted -- which, in the loop-over-many-molecules pattern this library is
115
+ // mostly used in, it almost always has. Measured on Alder Lake: routing a
116
+ // ~1 MiB one-shot envelope fill through mmap instead of aligned_alloc cost
117
+ // 1.85x. So the VM backend has to earn its faults, and it only can by saving
118
+ // copies on repeated growth -- which needs the buffer to be big enough for
119
+ // those copies to hurt more than the faults do. The same measurements put the
120
+ // crossover for the doubling-growth fills at a few hundred KiB.
121
+ //
122
+ // Consequently: allocate once, at a known size -> stay on the small backend
123
+ // (reset()); grow repeatedly -> realloc(), and cross over here. Overridable at
124
+ // build time for retuning.
125
+ #ifndef ISOSPEC_ALIGNED_PTR_VM_THRESHOLD
126
+ #define ISOSPEC_ALIGNED_PTR_VM_THRESHOLD (std::size_t(256) << 10) // 256 KiB
127
+ #endif
128
+
129
+ inline std::size_t vm_backend_threshold() noexcept
130
+ {
131
+ const std::size_t ps = os_page_size();
132
+ return ISOSPEC_ALIGNED_PTR_VM_THRESHOLD < ps ? ps : ISOSPEC_ALIGNED_PTR_VM_THRESHOLD;
133
+ }
134
+
135
+ // Releases a VM-backed block given just (ptr, bytes), without the rest of a
136
+ // vm_region's bookkeeping -- used by aligned_unique_ptr::release_with_deleter()
137
+ // once ownership has been handed off to a caller who only has those two
138
+ // values. bytes must be the actual backing size (region_.size / .reserved
139
+ // above), not a logical/unrounded request size; Windows ignores it (a
140
+ // placeholder-free VirtualFree(MEM_RELEASE) always frees the whole
141
+ // reservation from its base).
142
+ inline void vm_release_raw(void* ptr, std::size_t bytes) noexcept
143
+ {
144
+ #if ISOSPEC_WE_ARE_ON_WINDOWS
145
+ (void)bytes;
146
+ ::VirtualFree(ptr, 0, MEM_RELEASE);
147
+ #else
148
+ ::munmap(ptr, bytes);
149
+ #endif
150
+ }
151
+
152
+
153
+ #if ISOSPEC_WE_ARE_ON_WINDOWS
154
+ // Reservation only consumes address space, not RAM or page tables, so we can
155
+ // afford to be wasteful up front in order to (almost) never have to move
156
+ // the block: growth just commits further pages of the same reservation.
157
+ struct vm_region {
158
+ void* base = nullptr;
159
+ std::size_t reserved = 0;
160
+ std::size_t committed = 0;
161
+ };
162
+
163
+ inline std::size_t reservation_size_for(std::size_t bytes) noexcept
164
+ {
165
+ constexpr std::size_t min_reservation = std::size_t{1} << 26; // 64 MiB
166
+ constexpr std::size_t growth_factor = 16;
167
+ std::size_t reserved = round_up_to_page(bytes) * growth_factor;
168
+ if (reserved < min_reservation)
169
+ reserved = min_reservation;
170
+ return reserved;
171
+ }
172
+
173
+ inline vm_region vm_create(std::size_t bytes)
174
+ {
175
+ vm_region r;
176
+ r.committed = round_up_to_page(bytes);
177
+ r.reserved = reservation_size_for(bytes);
178
+
179
+ r.base = ::VirtualAlloc(nullptr, r.reserved, MEM_RESERVE, PAGE_NOACCESS);
180
+ if (!r.base)
181
+ throw std::bad_alloc();
182
+
183
+ if (r.committed > 0 &&
184
+ !::VirtualAlloc(r.base, r.committed, MEM_COMMIT, PAGE_READWRITE)) {
185
+ ::VirtualFree(r.base, 0, MEM_RELEASE);
186
+ throw std::bad_alloc();
187
+ }
188
+ return r;
189
+ }
190
+
191
+ inline void vm_destroy(vm_region& r) noexcept
192
+ {
193
+ if (r.base)
194
+ ::VirtualFree(r.base, 0, MEM_RELEASE);
195
+ r = vm_region{};
196
+ }
197
+
198
+ inline void vm_resize(vm_region& r, std::size_t new_bytes)
199
+ {
200
+ const std::size_t new_committed = round_up_to_page(new_bytes);
201
+ if (new_committed == r.committed)
202
+ return;
203
+
204
+ if (new_committed > r.reserved) {
205
+ // Outgrew the initial reservation (rare): there is no way to extend
206
+ // a VirtualAlloc reservation in place, so relocate -- same as a real
207
+ // mremap() that cannot grow in place and has to move.
208
+ vm_region moved = vm_create(new_bytes);
209
+ std::memcpy(moved.base, r.base, r.committed);
210
+ vm_destroy(r);
211
+ r = moved;
212
+ return;
213
+ }
214
+
215
+ if (new_committed > r.committed) {
216
+ if (!::VirtualAlloc(static_cast<char*>(r.base) + r.committed,
217
+ new_committed - r.committed, MEM_COMMIT, PAGE_READWRITE))
218
+ throw std::bad_alloc();
219
+ } else {
220
+ ::VirtualFree(static_cast<char*>(r.base) + new_committed,
221
+ r.committed - new_committed, MEM_DECOMMIT);
222
+ }
223
+ r.committed = new_committed;
224
+ }
225
+
226
+ #else // ISOSPEC_WE_ARE_ON_UNIX_YAY
227
+
228
+ struct vm_region {
229
+ void* base = nullptr;
230
+ std::size_t size = 0;
231
+ };
232
+
233
+ inline vm_region vm_create(std::size_t bytes)
234
+ {
235
+ vm_region r;
236
+ r.size = round_up_to_page(bytes);
237
+ r.base = ::mmap(nullptr, r.size, PROT_READ | PROT_WRITE,
238
+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
239
+ if (r.base == MAP_FAILED)
240
+ throw std::bad_alloc();
241
+ return r;
242
+ }
243
+
244
+ inline void vm_destroy(vm_region& r) noexcept
245
+ {
246
+ if (r.base)
247
+ ::munmap(r.base, r.size);
248
+ r = vm_region{};
249
+ }
250
+
251
+ #if defined(__APPLE__) && defined(__MACH__)
252
+
253
+ inline void vm_resize(vm_region& r, std::size_t new_bytes)
254
+ {
255
+ const std::size_t new_size = round_up_to_page(new_bytes);
256
+ if (new_size == r.size)
257
+ return;
258
+
259
+ if (new_size < r.size) {
260
+ // Shrink in place: base address is untouched, just drop the tail.
261
+ ::munmap(static_cast<char*>(r.base) + new_size, r.size - new_size);
262
+ r.size = new_size;
263
+ return;
264
+ }
265
+
266
+ // Grow: Darwin has no mremap(). Allocate a fresh region and use
267
+ // mach_vm_remap to re-wire the old pages (not copy them) into its head,
268
+ // then release the old mapping.
269
+ mach_vm_address_t new_addr = 0;
270
+ if (mach_vm_allocate(mach_task_self(), &new_addr, new_size, VM_FLAGS_ANYWHERE) != KERN_SUCCESS)
271
+ throw std::bad_alloc();
272
+
273
+ mach_vm_address_t target = new_addr;
274
+ vm_prot_t cur_prot, max_prot;
275
+ const kern_return_t kr = mach_vm_remap(
276
+ mach_task_self(), &target, r.size, 0, VM_FLAGS_OVERWRITE,
277
+ mach_task_self(), reinterpret_cast<mach_vm_address_t>(r.base),
278
+ FALSE /* share the same physical pages, don't copy */,
279
+ &cur_prot, &max_prot, VM_INHERIT_COPY);
280
+ if (kr != KERN_SUCCESS) {
281
+ mach_vm_deallocate(mach_task_self(), new_addr, new_size);
282
+ throw std::bad_alloc();
283
+ }
284
+
285
+ ::munmap(r.base, r.size);
286
+ r.base = reinterpret_cast<void*>(new_addr);
287
+ r.size = new_size;
288
+ }
289
+
290
+ #else // Linux / other real-mman UNIX with an actual mremap()
291
+
292
+ inline void vm_resize(vm_region& r, std::size_t new_bytes)
293
+ {
294
+ const std::size_t new_size = round_up_to_page(new_bytes);
295
+ if (new_size == r.size)
296
+ return;
297
+
298
+ void* p = ::mremap(r.base, r.size, new_size, MREMAP_MAYMOVE);
299
+ if (p == MAP_FAILED)
300
+ throw std::bad_alloc();
301
+ r.base = p;
302
+ r.size = new_size;
303
+ }
304
+
305
+ #endif // __APPLE__
306
+ #endif // ISOSPEC_WE_ARE_ON_WINDOWS
307
+
308
+ } // namespace aligned_ptr_detail
309
+ #endif // ISOSPEC_ALIGNED_PTR_HAVE_VM_REALLOC
310
+
311
+
312
+ template<class T, std::size_t Alignment>
313
+ class aligned_unique_ptr {
314
+ static_assert((Alignment & (Alignment - 1)) == 0); // meaning: Alignment is a power of two
315
+ static_assert(Alignment >= alignof(T));
316
+ static_assert(Alignment <= 4096, "Alignment must not exceed the smallest page size assumed across supported platforms");
317
+ // Storage is acquired/released as raw bytes (aligned_alloc/free, or
318
+ // mmap/VirtualAlloc) with no constructor/destructor ever run over it --
319
+ // matches how this class has always behaved (it never invoked T's ctor
320
+ // even before this rework, since it called the operator new[] *function*
321
+ // directly rather than a `new T[n]` *expression*), just made explicit.
322
+ static_assert(std::is_trivial_v<T>, "aligned_unique_ptr never runs T's constructor/destructor; T must be trivial");
323
+
324
+ T* ptr_ = nullptr;
325
+ std::size_t capacity_bytes_ = 0;
326
+
327
+ #if ISOSPEC_ALIGNED_PTR_HAVE_VM_REALLOC
328
+ bool is_mapped_ = false;
329
+ aligned_ptr_detail::vm_region region_{};
330
+ #endif
331
+
332
+ // C11 aligned_alloc requires size to be a multiple of alignment.
333
+ static std::size_t round_up_to_alignment(std::size_t bytes) noexcept
334
+ {
335
+ return (bytes + Alignment - 1) / Alignment * Alignment;
336
+ }
337
+
338
+ // n * sizeof(T), then rounding up to Alignment, must not silently wrap:
339
+ // that would turn a request for a huge buffer into a tiny allocation,
340
+ // which the caller (having asked for n elements) would then write past.
341
+ static std::size_t bytes_for(std::size_t n)
342
+ {
343
+ constexpr std::size_t max_size = (std::numeric_limits<std::size_t>::max)();
344
+ if (n > max_size / sizeof(T))
345
+ throw std::bad_alloc();
346
+ const std::size_t bytes = n * sizeof(T);
347
+ if (bytes > max_size - (Alignment - 1))
348
+ throw std::bad_alloc();
349
+ return bytes;
350
+ }
351
+
352
+ static T* allocate_small(std::size_t n)
353
+ {
354
+ if (n == 0)
355
+ return nullptr;
356
+ const std::size_t bytes = bytes_for(n);
357
+ #if ISOSPEC_ALIGNED_PTR_SMALL_BACKEND_FREE_COMPATIBLE
358
+ void* p = std::aligned_alloc(Alignment, round_up_to_alignment(bytes));
359
+ #else
360
+ void* p = ::_aligned_malloc(bytes, Alignment);
361
+ #endif
362
+ if (!p)
363
+ throw std::bad_alloc();
364
+ return static_cast<T*>(p);
365
+ }
366
+
367
+ static void free_small(void* p) noexcept
368
+ {
369
+ #if ISOSPEC_ALIGNED_PTR_SMALL_BACKEND_FREE_COMPATIBLE
370
+ std::free(p);
371
+ #else
372
+ ::_aligned_free(p);
373
+ #endif
374
+ }
375
+
376
+ void free_current() noexcept
377
+ {
378
+ if (!ptr_)
379
+ return;
380
+ #if ISOSPEC_ALIGNED_PTR_HAVE_VM_REALLOC
381
+ if (is_mapped_) {
382
+ aligned_ptr_detail::vm_destroy(region_);
383
+ return;
384
+ }
385
+ #endif
386
+ free_small(ptr_);
387
+ }
388
+
389
+ static void deleter_free(void* p, std::size_t) noexcept
390
+ {
391
+ std::free(p);
392
+ }
393
+
394
+ static void deleter_small(void* p, std::size_t) noexcept
395
+ {
396
+ free_small(p);
397
+ }
398
+
399
+ public:
400
+ static constexpr std::size_t alignment = Alignment;
401
+
402
+ // Returned by release_with_deleter(): the raw pointer plus everything
403
+ // needed to free it correctly, whichever backend actually produced it.
404
+ // Unlike release(), never copies -- deleter(ptr, size) must be called
405
+ // exactly once to avoid leaking it.
406
+ struct release_result {
407
+ T* ptr;
408
+ std::size_t size;
409
+ void (*deleter)(void*, std::size_t) noexcept;
410
+ };
411
+
412
+ T* allocate(std::size_t n)
413
+ {
414
+ return allocate_small(n);
415
+ }
416
+
417
+ explicit aligned_unique_ptr(std::size_t n)
418
+ : ptr_(allocate(n))
419
+ , capacity_bytes_(n * sizeof(T))
420
+ {}
421
+
422
+ aligned_unique_ptr() noexcept
423
+ : ptr_(nullptr)
424
+ {}
425
+
426
+ ~aligned_unique_ptr()
427
+ {
428
+ free_current();
429
+ }
430
+
431
+ aligned_unique_ptr(const aligned_unique_ptr&) = delete;
432
+ aligned_unique_ptr& operator=(const aligned_unique_ptr&) = delete;
433
+
434
+ aligned_unique_ptr(aligned_unique_ptr&& other) noexcept
435
+ : ptr_(std::exchange(other.ptr_, nullptr))
436
+ , capacity_bytes_(std::exchange(other.capacity_bytes_, 0))
437
+ #if ISOSPEC_ALIGNED_PTR_HAVE_VM_REALLOC
438
+ , is_mapped_(std::exchange(other.is_mapped_, false))
439
+ , region_(std::exchange(other.region_, aligned_ptr_detail::vm_region{}))
440
+ #endif
441
+ {}
442
+
443
+ aligned_unique_ptr& operator=(aligned_unique_ptr&& other) noexcept
444
+ {
445
+ if (this != &other) {
446
+ free_current();
447
+ ptr_ = std::exchange(other.ptr_, nullptr);
448
+ capacity_bytes_ = std::exchange(other.capacity_bytes_, 0);
449
+ #if ISOSPEC_ALIGNED_PTR_HAVE_VM_REALLOC
450
+ is_mapped_ = std::exchange(other.is_mapped_, false);
451
+ region_ = std::exchange(other.region_, aligned_ptr_detail::vm_region{});
452
+ #endif
453
+ }
454
+ return *this;
455
+ }
456
+
457
+ T* get() noexcept
458
+ {
459
+ return std::assume_aligned<Alignment>(ptr_);
460
+ }
461
+
462
+ const T* get() const noexcept
463
+ {
464
+ return std::assume_aligned<Alignment>(ptr_);
465
+ }
466
+
467
+ T& operator[](std::size_t i) noexcept
468
+ {
469
+ return get()[i];
470
+ }
471
+
472
+ const T& operator[](std::size_t i) const noexcept
473
+ {
474
+ return get()[i];
475
+ }
476
+
477
+ explicit operator bool() const noexcept
478
+ {
479
+ return ptr_ != nullptr;
480
+ }
481
+
482
+ // Allocate n elements, discarding whatever was held before and keeping none
483
+ // of its contents. Always uses the small backend -- see
484
+ // ISOSPEC_ALIGNED_PTR_VM_THRESHOLD for why a one-shot allocation of a known
485
+ // size does not want the VM one. (Not noexcept: allocation can throw.)
486
+ void reset(std::size_t n)
487
+ {
488
+ free_current();
489
+ #if ISOSPEC_ALIGNED_PTR_HAVE_VM_REALLOC
490
+ is_mapped_ = false;
491
+ region_ = aligned_ptr_detail::vm_region{};
492
+ #endif
493
+ if (n > 0) {
494
+ ptr_ = allocate_small(n);
495
+ capacity_bytes_ = n * sizeof(T);
496
+ } else {
497
+ ptr_ = nullptr;
498
+ capacity_bytes_ = 0;
499
+ }
500
+ }
501
+
502
+ // Grows or shrinks the buffer to hold n elements, preserving the first
503
+ // min(old capacity, new capacity) bytes. Below the page-size threshold
504
+ // this is the same aligned_alloc/copy/free pattern reset() uses; once a
505
+ // given instance has crossed that threshold it switches permanently to
506
+ // a VM-backed region that the OS resizes in place (see the backends
507
+ // above), so further growth/shrinkage stops needing to copy at all.
508
+ void realloc(std::size_t n)
509
+ {
510
+ const std::size_t new_bytes = n == 0 ? 0 : bytes_for(n);
511
+
512
+ if (new_bytes == 0) {
513
+ free_current();
514
+ ptr_ = nullptr;
515
+ capacity_bytes_ = 0;
516
+ #if ISOSPEC_ALIGNED_PTR_HAVE_VM_REALLOC
517
+ is_mapped_ = false;
518
+ region_ = aligned_ptr_detail::vm_region{};
519
+ #endif
520
+ return;
521
+ }
522
+
523
+ #if ISOSPEC_ALIGNED_PTR_HAVE_VM_REALLOC
524
+ if (is_mapped_) {
525
+ aligned_ptr_detail::vm_resize(region_, new_bytes);
526
+ ptr_ = static_cast<T*>(region_.base);
527
+ capacity_bytes_ = new_bytes;
528
+ return;
529
+ }
530
+
531
+ if (new_bytes >= aligned_ptr_detail::vm_backend_threshold()) {
532
+ // Crossing the threshold: switch backend. This one transition
533
+ // still has to copy -- there is nothing yet to remap.
534
+ aligned_ptr_detail::vm_region r = aligned_ptr_detail::vm_create(new_bytes);
535
+ if (ptr_) {
536
+ std::memcpy(r.base, ptr_, std::min(capacity_bytes_, new_bytes));
537
+ free_small(ptr_);
538
+ }
539
+ region_ = r;
540
+ ptr_ = static_cast<T*>(region_.base);
541
+ capacity_bytes_ = new_bytes;
542
+ is_mapped_ = true;
543
+ return;
544
+ }
545
+ #endif
546
+
547
+ T* new_ptr = allocate_small(n);
548
+ if (ptr_) {
549
+ std::memcpy(new_ptr, ptr_, std::min(capacity_bytes_, new_bytes));
550
+ free_small(ptr_);
551
+ }
552
+ ptr_ = new_ptr;
553
+ capacity_bytes_ = new_bytes;
554
+ }
555
+
556
+ // Hands back ownership as a plain T* that the caller can free with a
557
+ // bare std::free() / C free(), regardless of which backend produced it.
558
+ // Whenever the current backend isn't already free()-compatible on its
559
+ // own -- a VM-backed region always needs this, and so does the small
560
+ // backend on MSVC (see ISOSPEC_ALIGNED_PTR_SMALL_BACKEND_FREE_COMPATIBLE
561
+ // above) -- this materialises a fresh plain-malloc()'d copy instead.
562
+ // That copy is the whole cost of calling this in those cases;
563
+ // release_with_deleter() below avoids it when the caller can accept a
564
+ // deleter instead of assuming free().
565
+ T* release()
566
+ {
567
+ if (!ptr_) {
568
+ capacity_bytes_ = 0;
569
+ return nullptr;
570
+ }
571
+
572
+ bool needs_materialize = !ISOSPEC_ALIGNED_PTR_SMALL_BACKEND_FREE_COMPATIBLE;
573
+ #if ISOSPEC_ALIGNED_PTR_HAVE_VM_REALLOC
574
+ needs_materialize = needs_materialize || is_mapped_;
575
+ #endif
576
+ if (needs_materialize) {
577
+ void* copy = std::malloc(capacity_bytes_);
578
+ if (!copy)
579
+ throw std::bad_alloc();
580
+ std::memcpy(copy, ptr_, capacity_bytes_);
581
+ free_current();
582
+ ptr_ = nullptr;
583
+ #if ISOSPEC_ALIGNED_PTR_HAVE_VM_REALLOC
584
+ is_mapped_ = false;
585
+ region_ = aligned_ptr_detail::vm_region{};
586
+ #endif
587
+ capacity_bytes_ = 0;
588
+ return static_cast<T*>(copy);
589
+ }
590
+
591
+ capacity_bytes_ = 0;
592
+ return std::assume_aligned<Alignment>(std::exchange(ptr_, nullptr));
593
+ }
594
+
595
+ // Fast counterpart to release(): never copies. Returns the raw pointer
596
+ // together with the (size, deleter) pair needed to free it correctly --
597
+ // the caller must invoke result.deleter(result.ptr, result.size) exactly
598
+ // once (or not at all, if it hands the triple further on).
599
+ release_result release_with_deleter() noexcept
600
+ {
601
+ T* p = std::exchange(ptr_, nullptr);
602
+ std::size_t bytes = capacity_bytes_;
603
+ capacity_bytes_ = 0;
604
+ #if ISOSPEC_ALIGNED_PTR_HAVE_VM_REALLOC
605
+ if (is_mapped_) {
606
+ is_mapped_ = false;
607
+ #if ISOSPEC_WE_ARE_ON_WINDOWS
608
+ const std::size_t region_bytes = region_.reserved;
609
+ #else
610
+ const std::size_t region_bytes = region_.size;
611
+ #endif
612
+ region_ = aligned_ptr_detail::vm_region{};
613
+ return release_result{p, region_bytes, &aligned_ptr_detail::vm_release_raw};
614
+ }
615
+ #endif
616
+ #if ISOSPEC_ALIGNED_PTR_SMALL_BACKEND_FREE_COMPATIBLE
617
+ return release_result{p, bytes, &deleter_free};
618
+ #else
619
+ return release_result{p, bytes, &deleter_small};
620
+ #endif
621
+ }
622
+ };
@@ -0,0 +1,69 @@
1
+ /*
2
+ * Copyright (C) 2015-2020 Mateusz Łącki and Michał Startek.
3
+ *
4
+ * This file is part of IsoSpec.
5
+ *
6
+ * IsoSpec is free software: you can redistribute it and/or modify
7
+ * it under the terms of the Simplified ("2-clause") BSD licence.
8
+ *
9
+ * IsoSpec is distributed in the hope that it will be useful,
10
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
+ *
13
+ * You should have received a copy of the Simplified BSD Licence
14
+ * along with IsoSpec. If not, see <https://opensource.org/licenses/BSD-2-Clause>.
15
+ */
16
+
17
+ #pragma once
18
+
19
+ #include <cstring>
20
+ #include "conf.h"
21
+ #include "pod_vector.h"
22
+
23
+ namespace IsoSpec
24
+ {
25
+
26
+ template <typename T> inline void copyConf(
27
+ const T* source, T* destination,
28
+ int dim
29
+ ){
30
+ memcpy(destination, source, dim*sizeof(T));
31
+ }
32
+
33
+ template <typename T> class Allocator
34
+ {
35
+ private:
36
+ T* currentTab;
37
+ int currentId;
38
+ const int dim, tabSize;
39
+ pod_vector<T*> prevTabs;
40
+
41
+ public:
42
+ explicit Allocator(const int dim, const int tabSize = 10000);
43
+ ~Allocator();
44
+
45
+ Allocator(const Allocator& other) = delete;
46
+ Allocator& operator=(const Allocator& other) = delete;
47
+
48
+ void shiftTables();
49
+
50
+ inline T* newConf()
51
+ {
52
+ currentId++;
53
+
54
+ if (currentId >= tabSize)
55
+ shiftTables();
56
+
57
+ return &(currentTab[ currentId * dim ]);
58
+ }
59
+
60
+ inline T* makeCopy(const T* conf)
61
+ {
62
+ T* currentPlace = newConf();
63
+ copyConf<T>( conf, currentPlace, dim );
64
+
65
+ return currentPlace;
66
+ }
67
+ };
68
+
69
+ } // namespace IsoSpec