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