Project
Loading...
Searching...
No Matches
ShmAllocator.h
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
12/*
13 * ShmAllocator.h
14 *
15 * Created on: Jun 18, 2018
16 * Author: swenzel
17 */
18
19#ifndef COMMON_UTILS_INCLUDE_COMMONUTILS_SHMALLOCATOR_H_
20#define COMMON_UTILS_INCLUDE_COMMONUTILS_SHMALLOCATOR_H_
21
23#include <cassert>
24
25namespace o2
26{
27namespace utils
28{
29
30// An allocator placing objects in shared memory as organized by ShmManager.
31// Allocator used in a few std::vector so that they create stuff in shared mem
32// The only non-trivial things are in methods allocate/deallocate
33template <typename T>
35{
36 public:
37 typedef T value_type;
38 typedef std::size_t size_type;
39 typedef std::ptrdiff_t difference_type;
40
41 typedef T* pointer;
42 typedef const T* const_pointer;
43
44 typedef T& reference;
45 typedef const T& const_reference;
46
47 public:
48 inline ShmAllocator() noexcept = default;
49
50 template <typename T2>
51 inline ShmAllocator(const ShmAllocator<T2>&) noexcept
52 {
53 }
54
55 inline ~ShmAllocator() noexcept = default;
56
57 inline pointer adress(reference r) { return &r; }
58
59 inline const_pointer adress(const_reference r) const { return &r; }
60
61 // the actually important functions:
63 {
64 auto& instance = ShmManager::Instance();
65 if (instance.readyToAllocate()) {
67 }
68 return (pointer)malloc(sizeof(value_type) * n);
69 }
70 inline void deallocate(pointer p, size_type s)
71 {
72 auto& instance = ShmManager::Instance();
73 if (instance.readyToAllocate()) {
75 } else {
76 free(p);
77 }
78 }
79
80 inline void construct(pointer p, const value_type& value)
81 {
82 new (p) value_type(value);
83 }
84
85 template <class U, class... Args>
86 void construct(U* p, Args&&... args)
87 {
88 ::new ((void*)p) U(std::forward<Args>(args)...);
89 }
90
91 inline void destroy(pointer p) { p->~value_type(); }
92
93 inline size_type max_size() const noexcept { return size_type(-1) / sizeof(value_type); }
94
95 template <typename T2>
96 struct rebind {
98 };
99
100 bool operator!=(const ShmAllocator<T>& other) const { return !(*this == other); }
101
102 // Returns true if and only if storage allocated from *this
103 // can be deallocated from other, and vice versa.
104 // Always returns true for stateless allocators.
105 bool operator==(const ShmAllocator<T>& /*other*/) const { return true; }
106};
107
108template <typename T>
109std::vector<T>* createSimVector()
110{
111 using vector_t = std::vector<T>;
112 auto& instance = o2::utils::ShmManager::Instance();
113 if (instance.isOperational() && instance.readyToAllocate()) {
114 auto placement = instance.hasSegment() ? instance.getmemblock(sizeof(vector_t)) : malloc(sizeof(vector_t));
115 return new (placement) vector_t;
116 } else {
117 return new vector_t;
118 }
119}
120
121template <typename T>
122void freeSimVector(std::vector<T>* ptr)
123{
124 return;
125 using vector_t = std::vector<T>;
126#ifdef USESHM
127 auto& instance = o2::utils::ShmManager::Instance();
128 ptr->clear();
129 ptr->shrink_to_fit();
130 if (instance.hasSegment() && instance.isPointerOk(ptr)) {
131 instance.freememblock(ptr);
132 } else {
133 free(ptr);
134 }
135// at this moment we have to trust that std::
136#else
137 delete ptr;
138#endif
139}
140
141} // end namespace utils
142} // end namespace o2
143#endif /* COMMON_UTILS_INCLUDE_COMMONUTILS_SHMALLOCATOR_H_ */
TBranch * ptr
const_pointer adress(const_reference r) const
~ShmAllocator() noexcept=default
pointer adress(reference r)
void deallocate(pointer p, size_type s)
std::ptrdiff_t difference_type
bool operator==(const ShmAllocator< T > &) const
void construct(pointer p, const value_type &value)
ShmAllocator() noexcept=default
void destroy(pointer p)
bool operator!=(const ShmAllocator< T > &other) const
pointer allocate(size_type n)
void construct(U *p, Args &&... args)
size_type max_size() const noexcept
static ShmManager & Instance()
Definition ShmManager.h:61
void * getmemblock(size_t size)
void freememblock(void *, std::size_t=1)
GLdouble n
Definition glcorearb.h:1982
GLsizei const GLfloat * value
Definition glcorearb.h:819
GLboolean r
Definition glcorearb.h:1233
void freeSimVector(std::vector< T > *ptr)
std::vector< T > * createSimVector()
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
Common utility functions.
VectorOfTObjectPtrs other