11
11
* Platform abstraction layer to allow individual platform libraries to override
12
12
* symbols in ExecuTorch. PAL functions are defined as C functions so a platform
13
13
* library implementer can use C in lieu of C++.
14
+ *
15
+ * The et_pal_ methods should not be called directly. Use the corresponding methods
16
+ * in the executorch::runtime namespace instead to appropriately dispatch through
17
+ * the PAL function table.
14
18
*/
15
19
16
20
#pragma once
@@ -53,19 +57,22 @@ typedef struct {
53
57
* to initialize any global state. Typically overridden by PAL implementer.
54
58
*/
55
59
void et_pal_init (void ) ET_INTERNAL_PLATFORM_WEAKNESS;
60
+ typedef void (*et_pal_init_t )(void );
56
61
57
62
/* *
58
63
* Immediately abort execution, setting the device into an error state, if
59
64
* available.
60
65
*/
61
66
ET_NORETURN void et_pal_abort (void ) ET_INTERNAL_PLATFORM_WEAKNESS;
67
+ typedef void (*et_pal_abort_t )(void );
62
68
63
69
/* *
64
70
* Return a monotonically non-decreasing timestamp in system ticks.
65
71
*
66
72
* @retval Timestamp value in system ticks.
67
73
*/
68
74
et_timestamp_t et_pal_current_ticks (void ) ET_INTERNAL_PLATFORM_WEAKNESS;
75
+ typedef et_timestamp_t (*et_pal_current_ticks_t )(void );
69
76
70
77
/* *
71
78
* Return the conversion rate from system ticks to nanoseconds as a fraction.
@@ -79,6 +86,7 @@ et_timestamp_t et_pal_current_ticks(void) ET_INTERNAL_PLATFORM_WEAKNESS;
79
86
*
80
87
* @retval The ratio of nanoseconds to system ticks.
81
88
*/
89
+ typedef et_tick_ratio_t (*et_pal_ticks_to_ns_multiplier_t )(void );
82
90
et_tick_ratio_t et_pal_ticks_to_ns_multiplier (void )
83
91
ET_INTERNAL_PLATFORM_WEAKNESS;
84
92
@@ -114,6 +122,14 @@ void et_pal_emit_log_message(
114
122
size_t line,
115
123
const char * message,
116
124
size_t length) ET_INTERNAL_PLATFORM_WEAKNESS;
125
+ typedef void (*et_pal_emit_log_message_t )(
126
+ et_timestamp_t timestamp,
127
+ et_pal_log_level_t level,
128
+ const char * filename,
129
+ const char * function,
130
+ size_t line,
131
+ const char * message,
132
+ size_t length);
117
133
118
134
/* *
119
135
* NOTE: Core runtime code must not call this directly. It may only be called by
@@ -126,12 +142,107 @@ void et_pal_emit_log_message(
126
142
* et_pal_free().
127
143
*/
128
144
void * et_pal_allocate (size_t size) ET_INTERNAL_PLATFORM_WEAKNESS;
145
+ typedef void * (*et_pal_allocate_t )(size_t size);
129
146
130
147
/* *
131
148
* Frees memory allocated by et_pal_allocate().
132
149
*
133
150
* @param[in] ptr Pointer to memory to free. May be nullptr.
134
151
*/
135
152
void et_pal_free (void * ptr) ET_INTERNAL_PLATFORM_WEAKNESS;
153
+ typedef void (*et_pal_free_t )(void * ptr);
136
154
137
155
} // extern "C"
156
+
157
+ namespace executorch {
158
+ namespace runtime {
159
+
160
+ /* *
161
+ * Table of pointers to platform abstraction layer functions.
162
+ */
163
+ struct pal_table {
164
+ et_pal_init_t init;
165
+ et_pal_abort_t abort;
166
+ et_pal_current_ticks_t current_ticks;
167
+ et_pal_ticks_to_ns_multiplier_t ticks_to_ns_multiplier;
168
+ et_pal_emit_log_message_t emit_log_message;
169
+ et_pal_allocate_t allocate;
170
+ et_pal_free_t free;
171
+ };
172
+
173
+ /* *
174
+ * Retrieve a pointer to the singleton instance of the PAL function table. This
175
+ * can be used to override the default implementations of the PAL functions.
176
+ */
177
+ pal_table* get_pal_table (void );
178
+
179
+ /* *
180
+ * Initialize the platform abstraction layer.
181
+ *
182
+ * This function should be called before any other function provided by the PAL
183
+ * to initialize any global state. Typically overridden by PAL implementer.
184
+ */
185
+ void pal_init ();
186
+
187
+ /* *
188
+ * Immediately abort execution, setting the device into an error state, if
189
+ * available.
190
+ */
191
+ ET_NORETURN void pal_abort ();
192
+
193
+ /* *
194
+ * Return a monotonically non-decreasing timestamp in system ticks.
195
+ *
196
+ * @retval Timestamp value in system ticks.
197
+ */
198
+ et_timestamp_t pal_current_ticks ();
199
+
200
+ /* *
201
+ * Return the conversion rate from system ticks to nanoseconds as a fraction.
202
+ * To convert a system ticks to nanoseconds, multiply the tick count by the
203
+ * numerator and then divide by the denominator:
204
+ * nanoseconds = ticks * numerator / denominator
205
+ *
206
+ * The utility method executorch::runtime::ticks_to_ns(et_timestamp_t) can also
207
+ * be used to perform the conversion for a given tick count. It is defined in
208
+ * torch/executor/runtime/platform/clock.h.
209
+ *
210
+ * @retval The ratio of nanoseconds to system ticks.
211
+ */
212
+ et_tick_ratio_t pal_ticks_to_ns_multiplier ();
213
+
214
+ /* *
215
+ * Severity level of a log message. Values must map to printable 7-bit ASCII
216
+ * uppercase letters.
217
+ */
218
+ void pal_emit_log_message (
219
+ et_timestamp_t timestamp,
220
+ et_pal_log_level_t level,
221
+ const char * filename,
222
+ const char * function,
223
+ size_t line,
224
+ const char * message,
225
+ size_t length);
226
+
227
+ /* *
228
+ * NOTE: Core runtime code must not call this directly. It may only be called by
229
+ * a MemoryAllocator wrapper.
230
+ *
231
+ * Allocates size bytes of memory.
232
+ *
233
+ * @param[in] size Number of bytes to allocate.
234
+ * @returns the allocated memory, or nullptr on failure. Must be freed using
235
+ * et_pal_free().
236
+ */
237
+ void * pal_allocate (size_t size);
238
+
239
+ /* *
240
+ * Frees memory allocated by et_pal_allocate().
241
+ *
242
+ * @param[in] ptr Pointer to memory to free. May be nullptr.
243
+ */
244
+ void pal_free (void * ptr);
245
+
246
+
247
+ } // namespace runtime
248
+ } // namespace executorch
0 commit comments