Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#include "core/session/plugin_ep/ep_allocator_utils.h"
 
#include <functional>
#include <memory>
#include <string_view>
 
#include "core/common/common.h"
#include "core/framework/error_code_helper.h"
#include "core/session/allocator_adapters.h"
 
namespace onnxruntime {
namespace ep_allocator_utils {
 
Status CreateAndWrapEpAllocator(OrtEp* ep,
                                OrtEpFactory& ep_factory,
                                const OrtMemoryInfo& memory_info,
                                const OrtKeyValuePairs* ep_factory_allocator_options,
                                AllocatorPtr& allocator_out,
                                OrtAllocator** raw_allocator_out) {
  const bool has_ep_create = ep != nullptr && ep->CreateAllocator != nullptr;
  const bool has_factory_create = ep_factory.CreateAllocator != nullptr;
 
  ORT_RETURN_IF_NOT((!has_ep_create && !has_factory_create) || ep_factory.ReleaseAllocator != nullptr,
                    "OrtEpFactory must implement ReleaseAllocator when OrtEp::CreateAllocator or "
                    "OrtEpFactory::CreateAllocator is implemented.");
 
  if (!has_ep_create && !has_factory_create) {
    allocator_out = nullptr;
    if (raw_allocator_out != nullptr) {
      *raw_allocator_out = nullptr;
    }
    return Status::OK();
  }
 
  std::function<void(OrtAllocator*)> allocator_deleter = [&ep_factory](OrtAllocator* allocator) {
    ep_factory.ReleaseAllocator(&ep_factory, allocator);
  };
 
  OrtAllocator* raw_allocator = nullptr;
  OrtStatus* creation_ort_status =
      has_ep_create
          ? ep->CreateAllocator(ep, &memory_info, &raw_allocator)
          : ep_factory.CreateAllocator(&ep_factory, &memory_info, ep_factory_allocator_options, &raw_allocator);
  OrtAllocatorUniquePtr owned_allocator{raw_allocator, std::move(allocator_deleter)};
 
  if (creation_ort_status != nullptr) {
    return ToStatusAndRelease(creation_ort_status);
  }
 
  AllocatorPtr wrapped_allocator;
  if (owned_allocator != nullptr) {
    const std::string_view creator = has_ep_create ? "OrtEp" : "OrtEpFactory";
 
    const OrtMemoryInfo* allocator_memory_info = owned_allocator->Info(owned_allocator.get());
    ORT_RETURN_IF_NOT(allocator_memory_info != nullptr, creator, " returned an allocator with null memory info.");
 
    ORT_RETURN_IF_NOT(allocator_memory_info->alloc_type != OrtAllocatorType::OrtArenaAllocator,
                      creator,
                      " returned an allocator with OrtAllocatorType of OrtArenaAllocator. "
                      "This type is reserved for ONNX Runtime internal usage only, as any arena usage by the "
                      "EP library should be opaque to ORT");
 
    constexpr uint32_t kOrtAllocatorShrinkMinVersion = 25;
    if (owned_allocator->version >= kOrtAllocatorShrinkMinVersion && owned_allocator->Shrink != nullptr) {
      wrapped_allocator = std::make_shared<IArenaImplWrappingOrtAllocator>(std::move(owned_allocator));
    } else {
      wrapped_allocator = std::make_shared<IAllocatorImplWrappingOrtAllocator>(std::move(owned_allocator));
    }
  }
 
  if (raw_allocator_out != nullptr) {
    *raw_allocator_out = raw_allocator;
  }
  allocator_out = std::move(wrapped_allocator);
  return Status::OK();
}
 
}  // namespace ep_allocator_utils
}  // namespace onnxruntime