-
Notifications
You must be signed in to change notification settings - Fork 522
/
Copy pathinterface.cpp
70 lines (54 loc) · 1.84 KB
/
interface.cpp
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
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <executorch/runtime/backend/interface.h>
namespace executorch {
namespace ET_RUNTIME_NAMESPACE {
// Pure-virtual dtors still need an implementation.
BackendInterface::~BackendInterface() {}
namespace {
// The max number of backends that can be registered globally.
constexpr size_t kMaxRegisteredBackends = 16;
// TODO(T128866626): Remove global static variables. We want to be able to run
// multiple Executor instances and having a global registration isn't a viable
// solution in the long term.
/// Global table of registered backends.
Backend registered_backends[kMaxRegisteredBackends];
/// The number of backends registered in the table.
size_t num_registered_backends = 0;
} // namespace
BackendInterface* get_backend_class(const char* name) {
for (size_t i = 0; i < num_registered_backends; i++) {
Backend backend = registered_backends[i];
if (strcmp(backend.name, name) == 0) {
return backend.backend;
}
}
return nullptr;
}
Error register_backend(const Backend& backend) {
if (num_registered_backends >= kMaxRegisteredBackends) {
return Error::Internal;
}
// Check if the name already exists in the table
if (get_backend_class(backend.name) != nullptr) {
return Error::InvalidArgument;
}
registered_backends[num_registered_backends++] = backend;
return Error::Ok;
}
size_t get_num_registered_backends() {
return num_registered_backends;
}
Result<const char*> get_backend_name(size_t index) {
if (index >= num_registered_backends) {
return Error::InvalidArgument;
}
return registered_backends[index].name;
}
} // namespace ET_RUNTIME_NAMESPACE
} // namespace executorch