|
| 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/platform/log.h> |
| 10 | +#include <executorch/runtime/platform/platform.h> |
| 11 | +#include <cstdlib> |
| 12 | + |
| 13 | +namespace executorch::runtime { |
| 14 | + |
| 15 | +namespace { |
| 16 | +/** |
| 17 | + * The singleton instance of the PAL function table. |
| 18 | + */ |
| 19 | +PalImpl pal_impl = { |
| 20 | + et_pal_init, |
| 21 | + et_pal_abort, |
| 22 | + et_pal_current_ticks, |
| 23 | + et_pal_ticks_to_ns_multiplier, |
| 24 | + et_pal_emit_log_message, |
| 25 | + et_pal_allocate, |
| 26 | + et_pal_free, |
| 27 | + __FILE__}; |
| 28 | + |
| 29 | +/** |
| 30 | + * Tracks whether the PAL has been overridden. This is used to warn when |
| 31 | + * multiple callers override the PAL. |
| 32 | + */ |
| 33 | +bool is_pal_overridden = false; |
| 34 | +} // namespace |
| 35 | + |
| 36 | +PalImpl PalImpl::create( |
| 37 | + pal_emit_log_message_method emit_log_message, |
| 38 | + const char* source_filename) { |
| 39 | + return PalImpl::create( |
| 40 | + nullptr, // init |
| 41 | + nullptr, // abort |
| 42 | + nullptr, // current_ticks |
| 43 | + nullptr, // ticks_to_ns_multiplier |
| 44 | + emit_log_message, |
| 45 | + nullptr, // allocate |
| 46 | + nullptr, // free |
| 47 | + source_filename); |
| 48 | +} |
| 49 | + |
| 50 | +PalImpl PalImpl::create( |
| 51 | + pal_init_method init, |
| 52 | + pal_abort_method abort, |
| 53 | + pal_current_ticks_method current_ticks, |
| 54 | + pal_ticks_to_ns_multiplier_method ticks_to_ns_multiplier, |
| 55 | + pal_emit_log_message_method emit_log_message, |
| 56 | + pal_allocate_method allocate, |
| 57 | + pal_free_method free, |
| 58 | + const char* source_filename) { |
| 59 | + return PalImpl{ |
| 60 | + init, |
| 61 | + abort, |
| 62 | + current_ticks, |
| 63 | + ticks_to_ns_multiplier, |
| 64 | + emit_log_message, |
| 65 | + allocate, |
| 66 | + free, |
| 67 | + source_filename}; |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Override the PAL functions with user implementations. Any null entries in the |
| 72 | + * table are unchanged and will keep the default implementation. |
| 73 | + */ |
| 74 | +bool register_pal(PalImpl impl) { |
| 75 | + if (is_pal_overridden) { |
| 76 | + ET_LOG( |
| 77 | + Error, |
| 78 | + "register_pal() called multiple times. Subsequent calls will override the previous implementation. Previous implementation was registered from %s.", |
| 79 | + impl.source_filename != nullptr ? impl.source_filename : "unknown"); |
| 80 | + } |
| 81 | + is_pal_overridden = true; |
| 82 | + |
| 83 | + if (impl.abort != nullptr) { |
| 84 | + pal_impl.abort = impl.abort; |
| 85 | + } |
| 86 | + |
| 87 | + if (impl.current_ticks != nullptr) { |
| 88 | + pal_impl.current_ticks = impl.current_ticks; |
| 89 | + } |
| 90 | + |
| 91 | + if (impl.ticks_to_ns_multiplier != nullptr) { |
| 92 | + pal_impl.ticks_to_ns_multiplier = impl.ticks_to_ns_multiplier; |
| 93 | + } |
| 94 | + |
| 95 | + if (impl.emit_log_message != nullptr) { |
| 96 | + pal_impl.emit_log_message = impl.emit_log_message; |
| 97 | + } |
| 98 | + |
| 99 | + if (impl.allocate != nullptr) { |
| 100 | + pal_impl.allocate = impl.allocate; |
| 101 | + } |
| 102 | + |
| 103 | + if (impl.free != nullptr) { |
| 104 | + pal_impl.free = impl.free; |
| 105 | + } |
| 106 | + |
| 107 | + if (impl.init != nullptr) { |
| 108 | + pal_impl.init = impl.init; |
| 109 | + if (pal_impl.init != nullptr) { |
| 110 | + pal_impl.init(); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + return true; |
| 115 | +} |
| 116 | + |
| 117 | +const PalImpl* get_pal_impl() { |
| 118 | + return &pal_impl; |
| 119 | +} |
| 120 | + |
| 121 | +void pal_init() { |
| 122 | + pal_impl.init(); |
| 123 | +} |
| 124 | + |
| 125 | +ET_NORETURN void pal_abort() { |
| 126 | + pal_impl.abort(); |
| 127 | + // This should be unreachable, but in case the PAL implementation doesn't |
| 128 | + // abort, force it here. |
| 129 | + std::abort(); |
| 130 | +} |
| 131 | + |
| 132 | +et_timestamp_t pal_current_ticks() { |
| 133 | + return pal_impl.current_ticks(); |
| 134 | +} |
| 135 | + |
| 136 | +et_tick_ratio_t pal_ticks_to_ns_multiplier() { |
| 137 | + return pal_impl.ticks_to_ns_multiplier(); |
| 138 | +} |
| 139 | + |
| 140 | +void pal_emit_log_message( |
| 141 | + et_timestamp_t timestamp, |
| 142 | + et_pal_log_level_t level, |
| 143 | + const char* filename, |
| 144 | + const char* function, |
| 145 | + size_t line, |
| 146 | + const char* message, |
| 147 | + size_t length) { |
| 148 | + pal_impl.emit_log_message( |
| 149 | + timestamp, level, filename, function, line, message, length); |
| 150 | +} |
| 151 | + |
| 152 | +void* pal_allocate(size_t size) { |
| 153 | + return pal_impl.allocate(size); |
| 154 | +} |
| 155 | + |
| 156 | +void pal_free(void* ptr) { |
| 157 | + pal_impl.free(ptr); |
| 158 | +} |
| 159 | + |
| 160 | +} // namespace executorch::runtime |
0 commit comments