Flow-IPC 3.0.0
Flow-IPC project: Full implementation reference.
borrower_allocator_arena.hpp
Go to the documentation of this file.
1/* Flow-IPC: SHM-jemalloc
2 * Copyright (c) 2023 Akamai Technologies, Inc.; and other contributors.
3 * Each commit is copyright by its respective author or author's employer.
4 *
5 * Licensed under the MIT License:
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE. */
24
25/// @file
26#pragma once
27
28#include "ipc/shm/arena_lend/shm_pool_offset_ptr.hpp"
29
31{
32
33/**
34 * When borrowing STL-compliant in-SHM data structures, use this type as the `Arena` template parameter to
35 * shm::stl::Stateless_allocator (or equivalent). It would be unusual for most users to specify this
36 * directly (e.g., session::shm::arena_lend::jemalloc::Shm_session::Borrower_arena_allocator or more generically
37 * session::shm::arena_lend::jemalloc::Session::Borrower_allocator can be used directly instead).
38 *
39 * ### Context ###
40 * Owner-side, a SHM-aware STL-allocator (notably our `Stateless_allocator`) requires an `Arena` type, and this
41 * is intuitively uncontroversial: It needs to allocate/deallocate things, and in SHM-provider-land a SHM-arena
42 * is what does that. `Arena::Pointer` is also an important thing: it is the fancy-pointer type for the
43 * allocator -- and by extension STL-compliant container -- code to use instead of the typical raw `T*` pointer.
44 * So for SHM-jemalloc, a principal arena-lending SHM-provider, `Arena` is jemalloc::Ipc_arena, while
45 * `Arena::Pointer` is `Shm_pool_offset_ptr<T, Owner_shm_pool_repository<Ipc_arena>, int, true>`. With these
46 * plugged into the SHM-aware STL-allocator (compile-time), allocation/deallocation and pointer work in
47 * STL-compliant containers work nicely.
48 *
49 * Borrower-side, however -- given the arena-lending design -- on the one hand one obviously needs to be able
50 * to access in-SHM STL containers; but on the other hand the borrower cannot [de]allocate at all (on behalf
51 * of the borrowed, owner-side-allocated data structure). Nevertheless an `Allocator` arg (to STL types)
52 * is still required borrower-side. The reason is simple: it can't allocate/deallocate anyway, but
53 * the proper, compatible borrower-side pointer type is still required.
54 *
55 * Thus Borrower_allocator_arena is what one shall pass as the `Arena` parameter to `Stateless_allocator`; and
56 * it exclusively provides #Pointer. As no [de]allocation within borrowed containers will ever be
57 * attempted, or even compiled, there is no issue: STL code will only use Stateless_allocator::Pointer which
58 * is simply our #Pointer here.
59 *
60 * No `allocate()` or `deallocate()` methods are provided, as that would be nonsensical.
61 *
62 * @tparam Repository_type
63 * See `Repository_type` parameter to Shm_pool_offset_ptr.
64 * Generally in production use: `detail::Borrower_shm_pool_collection_repository<Shm_arena>`,
65 * where `Shm_arena` is, e.g., jemalloc::Ipc_arena.
66 * The `detail::Borrower_shm_pool_collection_repository` template is not hard-coded
67 * internally but is itself configurable (a tparam) -- as of this writing -- only for testing purposes.
68 * If we were to remove that configurability, hard-coding `detail::Borrower_shm_pool_collection_repository`
69 * internally, then the `Shm_arena` part would become the configurable thing (a template parameter).
70 */
71template<typename Repository_type>
73{
74public:
75 // Types.
76
77 /**
78 * Fancy pointer for stateless allocator use.
79 *
80 * @internal
81 *
82 * The target will usually be `Shm_pool_offset_ptr<T, Borrower_shm_pool_collection_repository<Ipc_arena>, ..., false>`
83 * of which these are of interest:
84 * - `Borrower_shm_pool_collection_repository<Ipc_arena>` is the singleton/central repository of SHM-pool
85 * information so that one can performantly convert from the inner bits of data stored inside a
86 * `Shm_pool_offset_ptr` to a raw `void*` -- `to_address()`; and vice versa (`from_address()`). Thus
87 * STL code can get an actual process-local vaddr from an in-SHM fancy pointer; and vice versa.
88 * - `false` means `bool CAN_STORE_RAW_PTR = false`. Borrower-side, all pointers within an STL data structure
89 * must point into SHM. Owner-side this would be `true`, as it is possible to intermix (say) a stack-stored
90 * container but with the SHM-enabled allocator parameter; so buffers allocated on the container's behalf
91 * do live in SHM, but other parts (at least temporarily) might not and hence must be represented by
92 * raw pointers after all. `false` here forbids the same borrower-side.
93 *
94 * The `int` is not great; it should be grabbed instead from the owner-side equivalent `Pointer` instead of
95 * being repeated here; but please see the to-do about eliminating `Difference_type` configurability in
96 * arena_lend_fwd.hpp. Once that's done, it would be deleted here too; problem solved.
97 *
98 * @endinternal
99 *
100 * @tparam T
101 * Pointee type.
102 */
103 template<typename T>
105}; // class Borrower_allocator_arena
106
107} // namespace ipc::shm::arena_lend
When borrowing STL-compliant in-SHM data structures, use this type as the Arena template parameter to...
ipc::shm sub-module providing the SHM-provider family based on the arena-lending paradigm: each proce...