Loading...
Searching...
No Matches
OptimAbi.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4#pragma once
5
6// Layout stamp between gprn/gprd and libgpridge.
7//
8// GPNEBResult and OCINEBResult are filled by the library and destroyed by
9// the driver. A driver built against an older header frees the wrong
10// offsets and segfaults. The byte counts below are compiled into the
11// driver from this header, and libraryResultLayout() returns the counts
12// compiled into libgpridge. They disagree exactly when the two were not
13// built together.
14//
15// Bump kResultLayoutVersion when either struct's layout changes, including
16// a reorder that keeps the size. A size change is caught even if the
17// version is left alone.
18
19#include <cstddef>
20#include <cstdint>
21#include <stdexcept>
22#include <string>
23
25
26namespace gpr::optim_abi {
27
30inline constexpr std::uint32_t kResultLayoutVersion = 2;
31
33 std::uint32_t version = 0;
34 std::uint32_t gpneb_result_bytes = 0;
35 std::uint32_t ocineb_result_bytes = 0;
36};
37
38[[nodiscard]] inline bool operator==(const ResultLayoutStamp& a,
39 const ResultLayoutStamp& b) noexcept
40{
41 return a.version == b.version &&
42 a.gpneb_result_bytes == b.gpneb_result_bytes &&
43 a.ocineb_result_bytes == b.ocineb_result_bytes;
44}
45
47[[nodiscard]] inline ResultLayoutStamp compiledResultLayout() noexcept
48{
50 static_cast<std::uint32_t>(sizeof(neb::GPNEBResult)),
51 static_cast<std::uint32_t>(sizeof(neb::OCINEBResult))};
52}
53
56[[nodiscard]] ResultLayoutStamp libraryResultLayout();
57
58[[nodiscard]] inline std::string describe(const ResultLayoutStamp& stamp)
59{
60 return std::to_string(stamp.version) + "/" +
61 std::to_string(stamp.gpneb_result_bytes) + "/" +
62 std::to_string(stamp.ocineb_result_bytes);
63}
64
66[[nodiscard]] inline std::string resultLayoutMismatch(
67 const ResultLayoutStamp& driver, const ResultLayoutStamp& library)
68{
69 if (driver == library) return {};
70 return "libgpridge result layout mismatch: this program was built "
71 "against layout " +
72 std::to_string(driver.version) + " (GPNEBResult " +
73 std::to_string(driver.gpneb_result_bytes) + " bytes, OCINEBResult " +
74 std::to_string(driver.ocineb_result_bytes) +
75 " bytes) but the loaded library reports layout " +
76 std::to_string(library.version) + " (GPNEBResult " +
77 std::to_string(library.gpneb_result_bytes) +
78 " bytes, OCINEBResult " +
79 std::to_string(library.ocineb_result_bytes) +
80 " bytes). Rebuild gprn and gprd against this libgpridge; a "
81 "stale driver frees the wrong offsets in OCINEBResult's "
82 "destructor.";
83}
84
86 const ResultLayoutStamp& library)
87{
88 const std::string message = resultLayoutMismatch(driver, library);
89 if (!message.empty()) throw std::runtime_error(message);
90}
91
96
97} // namespace gpr::optim_abi
GP-OCINEB controller – faithful port of the eOn ~NEBOcinebController~ pattern (eOn/client/ NEBOcinebC...
bool operator==(const ResultLayoutStamp &a, const ResultLayoutStamp &b) noexcept
Definition OptimAbi.h:38
void requireResultLayout()
Definition OptimAbi.h:92
constexpr std::uint32_t kResultLayoutVersion
Definition OptimAbi.h:30
void refuseMismatchedResultLayout(const ResultLayoutStamp &driver, const ResultLayoutStamp &library)
Definition OptimAbi.h:85
std::string describe(const ResultLayoutStamp &stamp)
Definition OptimAbi.h:58
ResultLayoutStamp compiledResultLayout() noexcept
What this translation unit compiled against.
Definition OptimAbi.h:47
std::string resultLayoutMismatch(const ResultLayoutStamp &driver, const ResultLayoutStamp &library)
Empty when the stamps agree. Otherwise a message naming both.
Definition OptimAbi.h:66
ResultLayoutStamp libraryResultLayout()
Definition OptimAbi.cpp:9