1 /* 2 * Copyright (c) Meta Platforms, Inc. and affiliates. 3 * All rights reserved. 4 * 5 * This source code is licensed under the BSD-style license found in the 6 * LICENSE file in the root directory of this source tree. 7 */ 8 9 #include <executorch/runtime/backend/interface.h> 10 11 namespace executorch { 12 namespace runtime { 13 14 // Pure-virtual dtors still need an implementation. ~BackendInterface()15BackendInterface::~BackendInterface() {} 16 17 namespace { 18 19 // The max number of backends that can be registered globally. 20 constexpr size_t kMaxRegisteredBackends = 16; 21 22 // TODO(T128866626): Remove global static variables. We want to be able to run 23 // multiple Executor instances and having a global registration isn't a viable 24 // solution in the long term. 25 26 /// Global table of registered backends. 27 Backend registered_backends[kMaxRegisteredBackends]; 28 29 /// The number of backends registered in the table. 30 size_t num_registered_backends = 0; 31 32 } // namespace 33 get_backend_class(const char * name)34BackendInterface* get_backend_class(const char* name) { 35 for (size_t i = 0; i < num_registered_backends; i++) { 36 Backend backend = registered_backends[i]; 37 if (strcmp(backend.name, name) == 0) { 38 return backend.backend; 39 } 40 } 41 return nullptr; 42 } 43 register_backend(const Backend & backend)44Error register_backend(const Backend& backend) { 45 if (num_registered_backends >= kMaxRegisteredBackends) { 46 return Error::Internal; 47 } 48 49 // Check if the name already exists in the table 50 if (get_backend_class(backend.name) != nullptr) { 51 return Error::InvalidArgument; 52 } 53 54 registered_backends[num_registered_backends++] = backend; 55 return Error::Ok; 56 } 57 58 } // namespace runtime 59 } // namespace executorch 60