quic/qbox
Loading...
Searching...
No Matches
memory_services.h
1/*
2 * Copyright (c) 2022-2024 Qualcomm Innovation Center, Inc. All Rights Reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#ifndef _GREENSOCS_BASE_COMPONENTS_MEMORY_SERVICES_H
8#define _GREENSOCS_BASE_COMPONENTS_MEMORY_SERVICES_H
9
10#include <fstream>
11#include <memory>
12
13#include <cci_configuration>
14#include <systemc>
15
16#include <scp/report.h>
17
18#if defined(_WIN32)
19#include <windows.h>
20#else
21#include <fcntl.h>
22#include <sys/mman.h>
23#include <sys/stat.h>
24#endif
25
26#include <csignal>
27#include <cstdlib>
28#include <uutils.h>
29#include <cerrno>
30#include <cstring>
31
32namespace gs {
33// Singleton class that handles memory allocation, alignment, file mapping and shared memory
34
35#define ALIGNEDBITS 12
36
37/* Structure to hold information about allocated memory address and alignment.
38Alignment flag is needed on Windows where memory allocated using _aligned_malloc
39must be freed using _aligned_free.
40 */
42 uint8_t* ptr = nullptr;
43 bool is_aligned = false;
44};
45
46#ifndef _WIN32
47// Memory cleaner service is not needed on Windows
48#define MAX_SHM_STR_LENGTH 255
49#define MAX_SHM_SEGS_NUM 1024
50
60 uint32_t count;
61 char name[MAX_SHM_SEGS_NUM][MAX_SHM_STR_LENGTH];
62};
63
65
67{
68 SCP_LOGGER((), "ShmemCleanerService");
69
70public:
72 ~ShmemCleanerService() = default;
73
74private:
75 /* Function called from the fork. Insure that shared memory is cleaned before exit*/
76 void cleanup_routine();
77
78 ProcAliveHandler pahandler;
79 pid_t m_cpid = -1;
80 SharedMemoryCleaner* m_shm_registry_mgr;
81};
82
95{
96 SCP_LOGGER((), "SharedMemoryCleaner");
97
98public:
100
102
103 void add_shared_memory_region(const std::string& memname);
104
105 void cleanup();
106
107private:
108 bool is_full() const { return get_count() == MAX_SHM_SEGS_NUM; }
109
110 bool is_name_valid(const std::string& name) const { return name.size() < MAX_SHM_STR_LENGTH; }
111
112 uint32_t get_count() const { return m_named_shm_registry->count; }
113
114 void ensure_shm_cleaner_service()
115 {
116 // Initialize the shared memory cleaner manager if not already initialized
117 if (m_shm_cleaner_service == nullptr) m_shm_cleaner_service = std::make_unique<ShmemCleanerService>(this);
118 }
119
120 NameRegistry* m_named_shm_registry;
121 std::unique_ptr<ShmemCleanerService> m_shm_cleaner_service;
122};
123#endif // ifndef WIN32
124
126{
127 SCP_LOGGER((), "MemoryServices");
128
129private:
131
143 void start_shm_cleaner_proc();
144
145 void cleanup();
146
147 static void cleanupexit();
148
149 void die_sys_api(int error, const std::string& memname, const std::string& die_msg);
150
151 struct SharedMemoryDescriptor {
152 SharedMemoryDescriptor(int file_descriptor, uint8_t* addr, size_t size)
153 : file_descriptor(file_descriptor), addr(addr), size(size)
154 {
155 }
156
157 const int file_descriptor;
158 uint8_t* const addr;
159 const size_t size;
160 };
161
162 std::string m_name;
163 std::map<std::string, SharedMemoryDescriptor> m_shmem_desc_map;
164
165#ifndef _WIN32
166 void initialize_shm_cleaner_service();
167 uint8_t* map_fd_to_mem(const std::string& memname, int fd, size_t size, int mmap_flags);
168
169 SharedMemoryCleaner m_shm_registry_mgr;
170#endif
171
172public:
174
175 const char* name() const;
176
177 size_t get_shmem_seg_num() const;
178
179 void init();
180
181 static MemoryServices& get();
182
183 MemoryServices(MemoryServices const&) = delete;
184 void operator=(MemoryServices const&) = delete;
185
186 int get_shmem_fd(const std::string& memname);
187
188 uint8_t* map_file(const std::string& mapfile, uint64_t size, uint64_t offset);
189
190 void unmap_file(uint8_t* ptr, size_t size);
191
192 uint8_t* map_mem_create(const std::string& memname, uint64_t size);
193
194 uint8_t* map_mem_create(const std::string& memname, uint64_t size, int* fd);
195
196 uint8_t* map_mem_join(const std::string& memname, size_t size);
197
198 uint8_t* map_mem_join_new(const std::string& memname, size_t size);
199
200 AllocatedMemory alloc(uint64_t size);
201};
202
203} // namespace gs
204
205#endif // #_GREENSOCS_BASE_COMPONENTS_MEMORY_SERVICES_H
Definition target.h:160
Definition memory_services.h:126
Definition uutils.h:169
Forks a process to ensure shared memory cleanup on crash or abnormal exit.
Definition memory_services.h:95
Definition memory_services.h:67
Tool which reads a Lua configuration file and sets parameters.
Definition biflow.cc:10
Definition memory_services.h:41
Structure to hold an array of names for shared memory usage.
Definition memory_services.h:59