pyembed 0.24.0

Embed a Python interpreter
Documentation
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
215
216
217
218
219
220
221
222
223
224
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

/*! Custom Python memory allocators.

This module holds code for customizing Python's memory allocators.

# Python Memory Allocators

The canonical documentation for Python's memory allocators is
<https://docs.python.org/3/c-api/memory.html>.

Important parts have been reproduced below for easy reference.

Python declares memory allocators via the `PyMemAllocatorEx` struct.
This holds pointers to functions which perform allocation, reallocation,
releasing, etc.

There are 3 _domains_ within the Python interpreter: raw, memory, and object.
The _raw_ domain is effectively the global allocator for Python. The
_memory_ and _object_ domains often wrap the _raw_ domain with custom logic,
such as arena allocation.

By default, the _raw_ domain uses malloc()/free(). The other domains
use _pymalloc_, which is an arena-based allocator backed by
malloc()/VirtualAlloc(). It is possible to customize the allocator used
by _pymalloc_ or to replace _pymalloc_ with your own `PyMemAllocatorEx`,
bypassing _pymalloc_ completely.

Here is the documentation for the various `PyMemAllocatorEx` members:

`void* malloc(void *ctx, size_t size)`
> Allocates n bytes and returns a pointer of type void* to the allocated
> memory, or NULL if the request fails.
>
> Requesting zero bytes returns a distinct non-NULL pointer if possible,
> as if PyMem_Malloc(1) had been called instead. The memory will not have
> been initialized in any way.

`void* PyMem_Calloc(size_t nelem, size_t elsize)`
> Allocates nelem elements each whose size in bytes is elsize and returns
> a pointer of type void* to the allocated memory, or NULL if the request
> fails. The memory is initialized to zeros.
>
> Requesting zero elements or elements of size zero bytes returns a
> distinct non-NULL pointer if possible, as if PyMem_RawCalloc(1, 1) had
> been called instead.

`void* PyMem_RawRealloc(void *p, size_t n)`
> Resizes the memory block pointed to by p to n bytes. The contents will be
> unchanged to the minimum of the old and the new sizes.
>
> If p is NULL, the call is equivalent to PyMem_RawMalloc(n); else if n is
> equal to zero, the memory block is resized but is not freed, and the
> returned pointer is non-NULL.
>
> Unless p is NULL, it must have been returned by a previous call to
> PyMem_RawMalloc(), PyMem_RawRealloc() or PyMem_RawCalloc().

`void PyMem_RawFree(void *p)`
> Frees the memory block pointed to by p, which must have been returned by
> a previous call to PyMem_RawMalloc(), PyMem_RawRealloc() or
> PyMem_RawCalloc(). Otherwise, or if PyMem_RawFree(p) has been called before,
> undefined behavior occurs.
>
> If p is NULL, no operation is performed.

(Documentation for the `PyMem_Raw*()` functions was used. However, the semantics
are the same regardless of which domain the `PyMemAllocatorEx` is installed
to.)

# Support for Custom Allocators

We support `jemalloc`, `mimalloc`, `snmalloc`, and Rust's global allocator as
custom Python allocators.

Rust's global allocator can independently also be set to one of the aforementioned
custom allocators via external Rust code.

Our `jemalloc`, `mimalloc`, and `snmalloc` Python allocator bindings speak
directly to the underlying C APIs provided by these allocators. By contrast,
going through the Rust global allocator introduces an abstraction layer. This
abstraction layer adds overhead (as we need to track allocation sizes to appease
Rust's allocator API). So even if Rust's global allocator is set to a custom
allocator, it is preferred to install the Python allocator because its bindings
to the allocator will be more efficient.

*/

use {
    core::ffi::c_void,
    pyo3::ffi as pyffi,
    python_packaging::interpreter::MemoryAllocatorBackend,
    std::{
        alloc,
        collections::HashMap,
        ops::{Deref, DerefMut},
        sync::Mutex,
    },
};

const MIN_ALIGN: usize = 16;

/// Tracks allocations from an allocator.
///
/// Some allocators need to pass the original allocation size and alignment
/// to various functions. But this information isn't passed as part of Python's
/// allocator APIs.
///
/// This type exists to facilitate tracking the allocation metadata
/// out-of-band. Essentially, we create an instance of this on the heap
/// and store a pointer to it via the allocator "context" C structs.
///
/// The Python raw domain allocator doesn't hold the GIL. So operations
/// against this data structure called from the context of a raw domain
/// allocator must be thread safe.
///
/// Our current solution to this is a Mutex around the inner data structure.
/// Although this is inefficient: many calling functions perform multiple
/// container operations, requiring a lock for each one. It would be better
/// to have a RAII guard for scoped logical operation.
struct AllocationTracker {
    allocations: Mutex<HashMap<*mut c_void, alloc::Layout>>,
}

impl AllocationTracker {
    /// Construct a new instance.
    ///
    /// It is automatically boxed because it needs to live on the heap.
    fn new() -> Box<Self> {
        Box::new(Self {
            allocations: Mutex::new(HashMap::with_capacity(128)),
        })
    }

    /// Construct an instance from a pointer owned by someone else.
    fn from_owned_ptr(ptr: *mut c_void) -> BorrowedAllocationTracker {
        if ptr.is_null() {
            panic!("must not pass NULL pointer");
        }

        BorrowedAllocationTracker {
            inner: Some(unsafe { Box::from_raw(ptr as *mut AllocationTracker) }),
        }
    }

    /// Obtain an allocation record in this tracker.
    #[inline]
    fn get_allocation(&self, ptr: *mut c_void) -> Option<alloc::Layout> {
        self.allocations.lock().unwrap().get(&ptr).cloned()
    }

    /// Record an allocation in this tracker.
    ///
    /// An existing allocation for the specified memory address will be replaced.
    #[inline]
    fn insert_allocation(&mut self, ptr: *mut c_void, layout: alloc::Layout) {
        self.allocations.lock().unwrap().insert(ptr, layout);
    }

    /// Remove an allocation from this tracker.
    #[inline]
    fn remove_allocation(&mut self, ptr: *mut c_void) -> alloc::Layout {
        self.allocations
            .lock()
            .unwrap()
            .remove(&ptr)
            .expect("memory address not tracked")
    }
}

/// An `AllocationTracker` associated with a borrowed raw pointer.
///
/// Instances can be derefed to `AllocationTracker` and are "leaked"
/// when they are dropped.
struct BorrowedAllocationTracker {
    inner: Option<Box<AllocationTracker>>,
}

impl Deref for BorrowedAllocationTracker {
    type Target = AllocationTracker;

    fn deref(&self) -> &Self::Target {
        self.inner.as_ref().unwrap()
    }
}

impl DerefMut for BorrowedAllocationTracker {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.inner.as_mut().unwrap()
    }
}

impl Drop for BorrowedAllocationTracker {
    fn drop(&mut self) {
        Box::into_raw(self.inner.take().unwrap());
    }
}

/// Represents an interface to Rust's memory allocator.
pub(crate) struct TrackingAllocator {
    pub allocator: pyffi::PyMemAllocatorEx,
    pub arena: pyffi::PyObjectArenaAllocator,
    _state: Box<AllocationTracker>,
}

extern "C" fn rust_malloc(ctx: *mut c_void, size: usize) -> *mut c_void {
    let size = match size {
        0 => 1,
        val => val,
    };

    let mut tracker = AllocationTracker::from_owned_ptr(ctx);

    let layout = unsafe { alloc::Layout::from_size_align_unchecked(size, MIN_ALIGN) };
    let res = unsafe { alloc::alloc(layout) } as *mut _;

    tracker.insert_allocation(res, layout);

    res
}

#[cfg(feature = "jemalloc-sys")]
extern "C" fn jemalloc_malloc(_ctx: *mut c_void, size: usize) -> *mut c_void {
    let size = match size {
        0 => 1,
        val => val,
    };

    unsafe { jemalloc_sys::mallocx(size, 0) }
}

#[cfg(feature = "libmimalloc-sys")]
extern "C" fn mimalloc_malloc(_ctx: *mut c_void, size: usize) -> *mut c_void {
    let size = match size {
        0 => 1,
        val => val,
    };

    unsafe { libmimalloc_sys::mi_malloc(size) as *mut _ }
}

#[cfg(feature = "snmalloc-sys")]
extern "C" fn snmalloc_malloc(_ctx: *mut c_void, size: usize) -> *mut c_void {
    let size = match size {
        0 => 1,
        val => val,
    };

    unsafe { snmalloc_sys::sn_malloc(size) as *mut _ }
}

extern "C" fn rust_calloc(ctx: *mut c_void, nelem: usize, elsize: usize) -> *mut c_void {
    let size = match nelem * elsize {
        0 => 1,
        val => val,
    };

    let mut tracker = AllocationTracker::from_owned_ptr(ctx);

    let layout = unsafe { alloc::Layout::from_size_align_unchecked(size, MIN_ALIGN) };
    let res = unsafe { alloc::alloc_zeroed(layout) } as *mut _;

    tracker.insert_allocation(res, layout);

    res
}

#[cfg(feature = "jemalloc-sys")]
extern "C" fn jemalloc_calloc(_ctx: *mut c_void, nelem: usize, elsize: usize) -> *mut c_void {
    let size = match nelem * elsize {
        0 => 1,
        val => val,
    };

    unsafe { jemalloc_sys::mallocx(size, jemalloc_sys::MALLOCX_ZERO) }
}

#[cfg(feature = "libmimalloc-sys")]
extern "C" fn mimalloc_calloc(_ctx: *mut c_void, nelem: usize, elsize: usize) -> *mut c_void {
    let size = match nelem * elsize {
        0 => 1,
        val => val,
    };

    unsafe { libmimalloc_sys::mi_calloc(nelem, size) as *mut _ }
}

#[cfg(feature = "snmalloc-sys")]
extern "C" fn snmalloc_calloc(_ctx: *mut c_void, nelem: usize, elsize: usize) -> *mut c_void {
    let size = match nelem * elsize {
        0 => 1,
        val => val,
    };

    unsafe { snmalloc_sys::sn_calloc(nelem, size) as *mut _ }
}

extern "C" fn rust_realloc(ctx: *mut c_void, ptr: *mut c_void, new_size: usize) -> *mut c_void {
    if ptr.is_null() {
        return rust_malloc(ctx, new_size);
    }

    let new_size = match new_size {
        0 => 1,
        val => val,
    };

    let mut tracker = AllocationTracker::from_owned_ptr(ctx);

    let layout = unsafe { alloc::Layout::from_size_align_unchecked(new_size, MIN_ALIGN) };

    let old_layout = tracker.remove_allocation(ptr);

    let res = unsafe { alloc::realloc(ptr as *mut _, old_layout, new_size) } as *mut _;

    tracker.insert_allocation(res, layout);

    res
}

#[cfg(feature = "jemalloc-sys")]
extern "C" fn jemalloc_realloc(ctx: *mut c_void, ptr: *mut c_void, new_size: usize) -> *mut c_void {
    if ptr.is_null() {
        return jemalloc_malloc(ctx, new_size);
    }

    let new_size = match new_size {
        0 => 1,
        val => val,
    };

    unsafe { jemalloc_sys::rallocx(ptr, new_size, 0) }
}

#[cfg(feature = "libmimalloc-sys")]
extern "C" fn mimalloc_realloc(ctx: *mut c_void, ptr: *mut c_void, new_size: usize) -> *mut c_void {
    if ptr.is_null() {
        return mimalloc_malloc(ctx, new_size);
    }

    let new_size = match new_size {
        0 => 1,
        val => val,
    };

    unsafe { libmimalloc_sys::mi_realloc(ptr as *mut _, new_size) as *mut _ }
}

#[cfg(feature = "snmalloc-sys")]
extern "C" fn snmalloc_realloc(ctx: *mut c_void, ptr: *mut c_void, new_size: usize) -> *mut c_void {
    if ptr.is_null() {
        return snmalloc_malloc(ctx, new_size);
    }
    let new_size = match new_size {
        0 => 1,
        val => val,
    };

    unsafe { snmalloc_sys::sn_realloc(ptr as *mut _, new_size) as *mut _ }
}

extern "C" fn rust_free(ctx: *mut c_void, ptr: *mut c_void) {
    if ptr.is_null() {
        return;
    }

    let mut tracker = AllocationTracker::from_owned_ptr(ctx);

    let layout = tracker
        .get_allocation(ptr)
        .unwrap_or_else(|| panic!("could not find allocated memory record: {:?}", ptr));

    unsafe {
        alloc::dealloc(ptr as *mut _, layout);
    }

    tracker.remove_allocation(ptr);
}

#[cfg(feature = "jemalloc-sys")]
extern "C" fn jemalloc_free(_ctx: *mut c_void, ptr: *mut c_void) {
    if ptr.is_null() {
        return;
    }

    unsafe { jemalloc_sys::dallocx(ptr, 0) }
}

#[cfg(feature = "libmimalloc-sys")]
extern "C" fn mimalloc_free(_ctx: *mut c_void, ptr: *mut c_void) {
    if ptr.is_null() {
        return;
    }

    unsafe { libmimalloc_sys::mi_free(ptr as *mut _) }
}

#[cfg(feature = "snmalloc-sys")]
extern "C" fn snmalloc_free(_ctx: *mut c_void, ptr: *mut c_void) {
    if ptr.is_null() {
        return;
    }

    unsafe { snmalloc_sys::sn_free(ptr as *mut _) }
}

extern "C" fn rust_arena_free(ctx: *mut c_void, ptr: *mut c_void, _size: usize) {
    if ptr.is_null() {
        return;
    }

    let mut tracker = AllocationTracker::from_owned_ptr(ctx);

    let layout = tracker
        .get_allocation(ptr)
        .unwrap_or_else(|| panic!("could not find allocated memory record: {:?}", ptr));

    unsafe {
        alloc::dealloc(ptr as *mut _, layout);
    }

    tracker.remove_allocation(ptr);
}

#[cfg(feature = "jemalloc-sys")]
extern "C" fn jemalloc_arena_free(_ctx: *mut c_void, ptr: *mut c_void, _size: usize) {
    if ptr.is_null() {
        return;
    }

    unsafe { jemalloc_sys::dallocx(ptr, 0) }
}

#[cfg(feature = "libmimalloc-sys")]
extern "C" fn mimalloc_arena_free(_ctx: *mut c_void, ptr: *mut c_void, _size: usize) {
    if ptr.is_null() {
        return;
    }

    unsafe { libmimalloc_sys::mi_free(ptr as *mut _) }
}

#[cfg(feature = "snmalloc-sys")]
extern "C" fn snmalloc_arena_free(_ctx: *mut c_void, ptr: *mut c_void, _size: usize) {
    if ptr.is_null() {
        return;
    }

    unsafe { snmalloc_sys::sn_free(ptr as *mut _) }
}

/// Represents a `PyMemAllocatorEx` that can be installed as a memory allocator.
enum AllocatorInstance {
    /// Backed by a `PyMemAllocatorEx` struct.
    #[allow(dead_code)]
    Simple(pyffi::PyMemAllocatorEx, pyffi::PyObjectArenaAllocator),

    /// Backed by a custom wrapper type.
    Tracking(TrackingAllocator),
}

/// Represents a custom memory allocator that can be registered with Python.
pub struct PythonMemoryAllocator {
    /// The allocator being used (for identification purposes).
    backend: MemoryAllocatorBackend,

    /// Holds reference to data structures needed by the Python interpreter.
    instance: AllocatorInstance,
}

impl PythonMemoryAllocator {
    /// Construct an instance from a `MemoryAllocatorBackend`.
    ///
    /// Returns `None` if the backend shouldn't be defined.
    pub fn from_backend(backend: MemoryAllocatorBackend) -> Option<Self> {
        match backend {
            MemoryAllocatorBackend::Default => None,
            MemoryAllocatorBackend::Jemalloc => Some(Self::jemalloc()),
            MemoryAllocatorBackend::Mimalloc => Some(Self::mimalloc()),
            MemoryAllocatorBackend::Snmalloc => Some(Self::snmalloc()),
            MemoryAllocatorBackend::Rust => Some(Self::rust()),
        }
    }

    /// Construct a new instance using jemalloc.
    #[cfg(feature = "jemalloc-sys")]
    pub fn jemalloc() -> Self {
        Self {
            backend: MemoryAllocatorBackend::Jemalloc,
            instance: AllocatorInstance::Simple(
                pyffi::PyMemAllocatorEx {
                    ctx: std::ptr::null_mut(),
                    malloc: Some(jemalloc_malloc),
                    calloc: Some(jemalloc_calloc),
                    realloc: Some(jemalloc_realloc),
                    free: Some(jemalloc_free),
                },
                pyffi::PyObjectArenaAllocator {
                    ctx: std::ptr::null_mut(),
                    alloc: Some(jemalloc_malloc),
                    free: Some(jemalloc_arena_free),
                },
            ),
        }
    }

    #[cfg(not(feature = "jemalloc-sys"))]
    pub fn jemalloc() -> Self {
        panic!("jemalloc allocator requested but it isn't compiled into this build configuration; try `cargo build --features allocator-jemalloc`");
    }

    /// Construct a new instance using mimalloc.
    #[cfg(feature = "libmimalloc-sys")]
    pub fn mimalloc() -> Self {
        Self {
            backend: MemoryAllocatorBackend::Mimalloc,
            instance: AllocatorInstance::Simple(
                pyffi::PyMemAllocatorEx {
                    ctx: std::ptr::null_mut(),
                    malloc: Some(mimalloc_malloc),
                    calloc: Some(mimalloc_calloc),
                    realloc: Some(mimalloc_realloc),
                    free: Some(mimalloc_free),
                },
                pyffi::PyObjectArenaAllocator {
                    ctx: std::ptr::null_mut(),
                    alloc: Some(mimalloc_malloc),
                    free: Some(mimalloc_arena_free),
                },
            ),
        }
    }

    #[cfg(not(feature = "libmimalloc-sys"))]
    pub fn mimalloc() -> Self {
        panic!("mimalloc allocator requested but it isn't compiled into this build configuration; try `cargo build --features allocator-mimalloc`");
    }

    /// Construct a new instance using Rust's global allocator.
    pub fn rust() -> Self {
        // We temporarily convert the box to a raw pointer to workaround
        // borrow issues.
        let state = Box::into_raw(AllocationTracker::new());

        let allocator = pyffi::PyMemAllocatorEx {
            ctx: state as *mut c_void,
            malloc: Some(rust_malloc),
            calloc: Some(rust_calloc),
            realloc: Some(rust_realloc),
            free: Some(rust_free),
        };

        Self {
            backend: MemoryAllocatorBackend::Rust,
            instance: AllocatorInstance::Tracking(TrackingAllocator {
                allocator,
                arena: pyffi::PyObjectArenaAllocator {
                    ctx: state as *mut c_void,
                    alloc: Some(rust_malloc),
                    free: Some(rust_arena_free),
                },
                _state: unsafe { Box::from_raw(state) },
            }),
        }
    }

    /// Construct a new instance using snmalloc.
    #[cfg(feature = "snmalloc-sys")]
    pub fn snmalloc() -> Self {
        Self {
            backend: MemoryAllocatorBackend::Snmalloc,
            instance: AllocatorInstance::Simple(
                pyffi::PyMemAllocatorEx {
                    ctx: std::ptr::null_mut(),
                    malloc: Some(snmalloc_malloc),
                    calloc: Some(snmalloc_calloc),
                    realloc: Some(snmalloc_realloc),
                    free: Some(snmalloc_free),
                },
                pyffi::PyObjectArenaAllocator {
                    ctx: std::ptr::null_mut(),
                    alloc: Some(snmalloc_malloc),
                    free: Some(snmalloc_arena_free),
                },
            ),
        }
    }

    #[cfg(not(feature = "snmalloc-sys"))]
    pub fn snmalloc() -> Self {
        panic!("snmalloc allocator requested but it isn't compiled into this build configuration; try `cargo build --features allocator-snmalloc`");
    }

    /// Obtain the backend used for this instance.
    #[allow(unused)]
    pub fn backend(&self) -> MemoryAllocatorBackend {
        self.backend
    }

    /// Set this allocator to be the allocator for a certain "domain" in a Python interpreter.
    ///
    /// This should be called before `Py_Initialize*()`.
    pub fn set_allocator(&self, domain: pyffi::PyMemAllocatorDomain) {
        unsafe {
            pyffi::PyMem_SetAllocator(domain, self.as_memory_allocator() as *mut _);
        }
    }

    /// Set the arena allocator used by the `pymalloc` allocator.
    ///
    /// This only has an effect if the `pymalloc` allocator is registered to the
    /// `mem` or `object` allocator domains.
    #[allow(dead_code)]
    pub fn set_arena_allocator(&self) {
        unsafe { pyffi::PyObject_SetArenaAllocator(self.as_arena_allocator()) }
    }

    /// Obtain the pointer to the `PyMemAllocatorEx` for this allocator.
    fn as_memory_allocator(&self) -> *const pyffi::PyMemAllocatorEx {
        match &self.instance {
            AllocatorInstance::Simple(alloc, _) => alloc as *const _,
            AllocatorInstance::Tracking(alloc) => &alloc.allocator as *const _,
        }
    }

    #[allow(dead_code)]
    fn as_arena_allocator(&self) -> *mut pyffi::PyObjectArenaAllocator {
        match &self.instance {
            AllocatorInstance::Simple(_, arena) => arena as *const _ as *mut _,
            AllocatorInstance::Tracking(alloc) => &alloc.arena as *const _ as *mut _,
        }
    }
}