Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#pragma once
 
#include <charconv>
#include <mutex>
#include <optional>
#include <stdexcept>
#include <string>
#include <tuple>
 
// Users of this header can explicitly define ORT_PLUGIN_EP_SKIP_API_MANUAL_INIT to skip manual ORT C++ API
// initialization.
//
// A plugin EP built as its own shared library must not call OrtGetApiBase() itself. It receives the OrtApiBase* from
// ORT and initializes the C++ API manually, so ORT_API_MANUAL_INIT is forced on for onnxruntime_cxx_api.h. This is
// the default because it is the safe choice for a separately built plugin.
//
// A build that links the plugin EP into the ORT binary defines ORT_PLUGIN_EP_SKIP_API_MANUAL_INIT: OrtGetApiBase() is
// then available in-process and the rest of the binary is compiled without ORT_API_MANUAL_INIT. MSVC's detect_mismatch
// guard in onnxruntime_cxx_api.h requires every translation unit in a binary to agree on ORT_API_MANUAL_INIT, so in
// that case leave it alone and let the C++ API initialize itself.
#if !defined(ORT_PLUGIN_EP_SKIP_API_MANUAL_INIT)
#pragma push_macro("ORT_API_MANUAL_INIT")
#undef ORT_API_MANUAL_INIT
#define ORT_API_MANUAL_INIT
#include "onnxruntime_cxx_api.h"
#pragma pop_macro("ORT_API_MANUAL_INIT")
#else
#include "onnxruntime_cxx_api.h"
#endif
 
namespace onnxruntime {
namespace ep {
 
struct ApiPtrs {
  ApiPtrs(const OrtApi& ort_, const OrtEpApi& ep_, const OrtModelEditorApi& model_editor_)
      : ort(ort_), ep(ep_), model_editor(model_editor_) {}
  const OrtApi& ort;
  const OrtEpApi& ep;
  const OrtModelEditorApi& model_editor;
};
 
namespace detail {
inline std::optional<ApiPtrs> g_api_ptrs{};
inline uint32_t g_current_ort_api_version{};
 
// Strictly parse a "MAJOR.MINOR.PATCH" version string. Each component must be a non-empty sequence of decimal digits.
// Returns false for null/empty input, missing or extra components, non-numeric components, or any trailing characters
// (including a pre-release suffix).
inline bool TryParseVersion(const char* version_str, uint32_t& major, uint32_t& minor, uint32_t& patch) {
  if (version_str == nullptr || version_str[0] == '\0') {
    return false;
  }
 
  uint32_t values[3] = {0, 0, 0};
  const char* p = version_str;
  for (int i = 0; i < 3; ++i) {
    if (*p < '0' || *p > '9') {
      return false;
    }
    const char* end = p;
    while (*end >= '0' && *end <= '9') {
      ++end;
    }
    auto [next, ec] = std::from_chars(p, end, values[i]);
    if (ec != std::errc{} || next != end) {
      return false;
    }
    p = end;
    if (i < 2) {
      if (*p != '.') {
        return false;
      }
      ++p;
    } else if (*p != '\0') {
      // Last component must consume the whole string.
      return false;
    }
  }
  major = values[0];
  minor = values[1];
  patch = values[2];
  return true;
}
 
}  // namespace detail
 
/// <summary>
/// Get the global instance of ApiPtrs.
/// </summary>
inline const ApiPtrs& Api() {
  if (!detail::g_api_ptrs.has_value()) {
    throw std::logic_error("onnxruntime::ep::Api() called before ApiInit().");
  }
  return *detail::g_api_ptrs;
}
 
/// <summary>
/// Initialize the EP API pointers and global OrtEnv if not already done.
/// Thread-safe via std::call_once.
///
/// If `min_ort_version` is non-null, it is parsed as a strict "MAJOR.MINOR.PATCH" version string and compared against
/// the runtime ORT version reported by `ort_api_base->GetVersionString()`.
/// The runtime version must be at least `min_ort_version`.
///
/// If initialization fails, this function throws an exception.
/// </summary>
inline void ApiInit(const OrtApiBase* ort_api_base, const char* min_ort_version = nullptr) {
  static std::once_flag init_flag;
  std::call_once(init_flag, [&]() {
    const char* version_str = ort_api_base->GetVersionString();
    if (version_str == nullptr) {
      throw std::runtime_error("Failed to initialize EP API: ort_api_base->GetVersionString() returned null.");
    }
 
    uint32_t runtime_major = 0, runtime_minor = 0, runtime_patch = 0;
    if (!detail::TryParseVersion(version_str, runtime_major, runtime_minor, runtime_patch)) {
      throw std::runtime_error(std::string("Failed to initialize EP API: could not parse ORT version \"") +
                               version_str + "\". Expected format: \"MAJOR.MINOR.PATCH\".");
    }
 
    // If a minimum ORT version was specified by the EP, enforce it before any other checks.
    // This is also what defines the floor for the API version below.
    if (min_ort_version != nullptr) {
      uint32_t min_major = 0, min_minor = 0, min_patch = 0;
      if (!detail::TryParseVersion(min_ort_version, min_major, min_minor, min_patch)) {
        throw std::runtime_error(std::string("Failed to parse minimum required ORT version \"") +
                                 min_ort_version + "\". Expected format: \"MAJOR.MINOR.PATCH\".");
      }
      if (std::tie(runtime_major, runtime_minor, runtime_patch) < std::tie(min_major, min_minor, min_patch)) {
        throw std::runtime_error(std::string("ORT runtime version \"") + version_str +
                                 "\" is below the minimum required version \"" + min_ort_version + "\".");
      }
    }
 
    // Assume ORT versions of the form "1.<API version>.PATCH".
    if (runtime_major != 1) {
      throw std::runtime_error(std::string("Failed to initialize EP API: unsupported ORT major version in \"") +
                               version_str + "\" (expected major version 1).");
    }
 
    const uint32_t current_ort_api_version = runtime_minor;
 
    const OrtApi* ort_api = ort_api_base->GetApi(current_ort_api_version);
    if (!ort_api) {
      throw std::runtime_error(
          "Failed to initialize EP API: the current ORT version is \"" + std::string(version_str) +
          "\" but it does not support the parsed API version " + std::to_string(current_ort_api_version) + ".");
    }
 
    const OrtEpApi* ep_api = ort_api->GetEpApi();
    const OrtModelEditorApi* model_editor_api = ort_api->GetModelEditorApi();
    if (!ep_api || !model_editor_api) {
      throw std::runtime_error("Failed to initialize EP API: GetEpApi or GetModelEditorApi returned null.");
    }
 
#if !defined(ORT_PLUGIN_EP_SKIP_API_MANUAL_INIT)
    // Manual init for the C++ API. When the EP is linked into the ORT binary, the C++ API initializes itself from
    // OrtGetApiBase() with ORT_API_VERSION, which is the same API pointer because the EP and ORT are the same binary.
    Ort::InitApi(ort_api);
#endif
 
    // Initialize globals
    detail::g_api_ptrs.emplace(*ort_api, *ep_api, *model_editor_api);
    detail::g_current_ort_api_version = current_ort_api_version;
  });
}
 
/// <summary>
/// Get the current ORT API version that the EP API has been initialized with.
///
/// This function should be called after ApiInit() to get the actual API version.
/// </summary>
inline uint32_t CurrentOrtApiVersion() {
  if (!detail::g_api_ptrs.has_value()) {
    throw std::logic_error("onnxruntime::ep::CurrentOrtApiVersion() called before ApiInit().");
  }
  return detail::g_current_ort_api_version;
}
 
}  // namespace ep
}  // namespace onnxruntime