Skip to content

Commit 6404127

Browse files
committed
backend: frontend: Make BackEnd and FrontEnd standard layout types
To allow the BackEnd and FrontEnd objects to be safely shared between processes, it must be a standard layout type. Add a static_assert to ensure this is the case. Remove the const reference PiSPVariant members from both BackEnd and FrontEnd and replace with a const PiSPVariant member. Remove all BackEnd default config std::map types. These are replaced by std::vector<std::pair<.,.>> types which are standard layout types. Signed-off-by: Naushir Patuck <[email protected]>
1 parent 5d3ab55 commit 6404127

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

src/libpisp/backend/backend.hpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
*/
88
#pragma once
99

10-
#include <map>
1110
#include <string>
11+
#include <type_traits>
12+
#include <utility>
1213
#include <vector>
1314

1415
#include "common/shm_mutex.hpp"
@@ -157,7 +158,7 @@ class BackEnd final
157158
void initialiseDefaultConfig(const std::string &filename);
158159

159160
Config config_;
160-
const PiSPVariant &variant_;
161+
const PiSPVariant variant_;
161162
pisp_be_config be_config_;
162163
pisp_image_format_config max_input_;
163164
bool retile_;
@@ -169,12 +170,16 @@ class BackEnd final
169170
uint32_t smart_resize_dirty_;
170171

171172
// Default config
172-
std::map<std::string, pisp_be_ccm_config> ycbcr_map_;
173-
std::map<std::string, pisp_be_ccm_config> inverse_ycbcr_map_;
174-
std::map<std::string, pisp_be_resample_config> resample_filter_map_;
173+
// We use std::vector<std::pair<.,.>> insead of std::map<.,.> to ensure this object provides a standard layout.
174+
std::vector<std::pair<std::string, pisp_be_ccm_config>> ycbcr_map_;
175+
std::vector<std::pair<std::string, pisp_be_ccm_config>> inverse_ycbcr_map_;
176+
std::vector<std::pair<std::string, pisp_be_resample_config>> resample_filter_map_;
175177
std::vector<std::pair<double, std::string>> resample_select_list_;
176178
pisp_be_sharpen_config default_sharpen_;
177179
pisp_be_sh_fc_combine_config default_shfc_;
178180
};
179181

182+
// This is required to ensure we can safely share a BackEnd object across multiple processes.
183+
static_assert(std::is_standard_layout<BackEnd>::value, "BackEnd must be a standard layout type");
184+
180185
} // namespace libpisp

src/libpisp/backend/backend_default_config.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
#include "backend.hpp"
99

10+
#include <algorithm>
1011
#include <dlfcn.h>
1112
#include <elf.h>
1213
#include <fstream>
@@ -132,7 +133,7 @@ void initialise_gamma(pisp_be_gamma_config &gamma, const json &root)
132133
}
133134
}
134135

135-
void read_resample(std::map<std::string, pisp_be_resample_config> &resample_filter_map,
136+
void read_resample(std::vector<std::pair<std::string, pisp_be_resample_config>> &resample_filter_map,
136137
std::vector<std::pair<double, std::string>> &resample_select_list, const json &root)
137138
{
138139
auto &filters = root["resample"]["filters"];
@@ -147,7 +148,7 @@ void read_resample(std::map<std::string, pisp_be_resample_config> &resample_filt
147148
throw std::runtime_error("read_resample: Incorrect number of filter coefficients");
148149

149150
memcpy(r.coef, coefs.data(), sizeof(r.coef));
150-
resample_filter_map.emplace(name, r);
151+
resample_filter_map.emplace_back(name, r);
151152
}
152153

153154
auto &smart = root["resample"]["smart_selection"];
@@ -221,8 +222,8 @@ void read_sharpen(pisp_be_sharpen_config &sharpen, pisp_be_sh_fc_combine_config
221222
shfc.y_factor = params["shfc_y_factor"].get<double>() * (1 << 8);
222223
}
223224

224-
void read_ycbcr(std::map<std::string, pisp_be_ccm_config> &ycbcr_map,
225-
std::map<std::string, pisp_be_ccm_config> &inverse_ycbcr_map, const json &root)
225+
void read_ycbcr(std::vector<std::pair<std::string, pisp_be_ccm_config>> &ycbcr_map,
226+
std::vector<std::pair<std::string, pisp_be_ccm_config>> &inverse_ycbcr_map, const json &root)
226227
{
227228
auto encoding = root["colour_encoding"];
228229

@@ -246,20 +247,20 @@ void read_ycbcr(std::map<std::string, pisp_be_ccm_config> &ycbcr_map,
246247
memcpy(ccm.offsets, offsets.data(), sizeof(ccm.offsets));
247248

248249
if (key == "ycbcr")
249-
ycbcr_map.emplace(format, ccm);
250+
ycbcr_map.emplace_back(format, ccm);
250251
else
251-
inverse_ycbcr_map.emplace(format, ccm);
252+
inverse_ycbcr_map.emplace_back(format, ccm);
252253
}
253254
}
254255
}
255256

256-
void get_matrix(pisp_be_ccm_config &matrix, const std::map<std::string, pisp_be_ccm_config> &map,
257+
void get_matrix(pisp_be_ccm_config &matrix, const std::vector<std::pair<std::string, pisp_be_ccm_config>> &map,
257258
const std::string &colour_space)
258259
{
259260
memset(matrix.coeffs, 0, sizeof(matrix.coeffs));
260261
memset(matrix.offsets, 0, sizeof(matrix.offsets));
261262

262-
auto it = map.find(colour_space);
263+
auto it = std::find_if(map.begin(), map.end(), [&colour_space](const auto &m) { return m.first == colour_space; });
263264
if (it != map.end())
264265
{
265266
memcpy(matrix.coeffs, it->second.coeffs, sizeof(matrix.coeffs));
@@ -286,7 +287,8 @@ void BackEnd::InitialiseResample(pisp_be_resample_config &resample, const std::s
286287
{
287288
memset(resample.coef, 0, sizeof(resample.coef));
288289

289-
auto it = resample_filter_map_.find(filter);
290+
auto it = std::find_if(resample_filter_map_.begin(), resample_filter_map_.end(),
291+
[&filter](const auto &m) { return m.first == filter; });
290292
if (it != resample_filter_map_.end())
291293
memcpy(resample.coef, it->second.coef, sizeof(resample.coef));
292294
}

src/libpisp/frontend/frontend.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#pragma once
99

1010
#include <cstring>
11+
#include <type_traits>
1112

1213
#include "common/pisp_types.h"
1314
#include "common/shm_mutex.hpp"
@@ -73,10 +74,13 @@ class FrontEnd final
7374
private:
7475
void getOutputSize(unsigned int output_num, uint16_t &width, uint16_t &height) const;
7576

76-
const PiSPVariant &variant_;
77+
const PiSPVariant variant_;
7778
pisp_fe_config fe_config_;
7879
int align_;
7980
mutable ShmMutex mutex_;
8081
};
8182

83+
// This is required to ensure we can safely share a FrontEnd object across multiple processes.
84+
static_assert(std::is_standard_layout<FrontEnd>::value, "FrontEnd must be a standard layout type");
85+
8286
} // namespace libpisp

0 commit comments

Comments
 (0)