@@ -198,176 +198,6 @@ class MemoryAllocator {
198198  int32_t  prof_id_ = -1 ;
199199};
200200
201- #if  ET_HAVE_GNU_STATEMENT_EXPRESSIONS
202- /* *
203-  * Tries allocating from the specified MemoryAllocator*. 
204-  * 
205-  * - On success, returns a pointer to the allocated buffer. 
206-  * - On failure, executes the provided code block, which must return or panic. 
207-  * 
208-  * Example: 
209-  * @code 
210-  *   char* buf = ET_TRY_ALLOCATE_OR( 
211-  *       memory_allocator, bufsize, { 
212-  *         *out_err = Error::MemoryAllocationFailed; 
213-  *         return nullopt; 
214-  *       }); 
215-  * @endcode 
216-  */  
217- #define  ET_TRY_ALLOCATE_OR (memory_allocator__, nbytes__, ...)              \
218-   ({                                                                       \
219-     void * et_try_allocate_result = memory_allocator__->allocate (nbytes__); \
220-     if  (et_try_allocate_result == nullptr  && nbytes__ > 0 ) {               \
221-       __VA_ARGS__                                                          \
222-       /*  The args must return. */                                            \
223-       ET_UNREACHABLE ();                                                    \
224-     }                                                                      \
225-     et_try_allocate_result;                                                \
226-   })
227- 
228- /* *
229-  * Tries allocating an instance of type__ from the specified MemoryAllocator*. 
230-  * 
231-  * - On success, returns a pointer to the allocated buffer. Note that the memory 
232-  *   will not be initialized. 
233-  * - On failure, executes the provided code block, which must return or panic. 
234-  * 
235-  * Example: 
236-  * @code 
237-  *   char* buf = ET_TRY_ALLOCATE_INSTANCE_OR( 
238-  *       memory_allocator, 
239-  *       MyType, 
240-  *       { *out_err = Error::MemoryAllocationFailed; return nullopt; }); 
241-  * @endcode 
242-  */  
243- #define  ET_TRY_ALLOCATE_INSTANCE_OR (memory_allocator__, type__, ...) \
244-   ({                                                                 \
245-     type__* et_try_allocate_result =                                 \
246-         memory_allocator__->allocateInstance <type__>();              \
247-     if  (et_try_allocate_result == nullptr ) {                         \
248-       __VA_ARGS__                                                    \
249-       /*  The args must return. */                                      \
250-       ET_UNREACHABLE ();                                              \
251-     }                                                                \
252-     et_try_allocate_result;                                          \
253-   })
254- 
255- /* *
256-  * Tries allocating multiple elements of a given type from the specified 
257-  * MemoryAllocator*. 
258-  * 
259-  * - On success, returns a pointer to the allocated buffer. 
260-  * - On failure, executes the provided code block, which must return or panic. 
261-  * 
262-  * Example: 
263-  * @code 
264-  *   Tensor* tensor_list = ET_TRY_ALLOCATE_LIST_OR( 
265-  *       memory_allocator, Tensor, num_tensors, { 
266-  *         *out_err = Error::MemoryAllocationFailed; 
267-  *         return nullopt; 
268-  *       }); 
269-  * @endcode 
270-  */  
271- #define  ET_TRY_ALLOCATE_LIST_OR (memory_allocator__, type__, nelem__, ...) \
272-   ({                                                                      \
273-     type__* et_try_allocate_result =                                      \
274-         memory_allocator__->allocateList <type__>(nelem__);                \
275-     if  (et_try_allocate_result == nullptr  && nelem__ > 0 ) {               \
276-       __VA_ARGS__                                                         \
277-       /*  The args must return. */                                           \
278-       ET_UNREACHABLE ();                                                   \
279-     }                                                                     \
280-     et_try_allocate_result;                                               \
281-   })
282- #else  //  !ET_HAVE_GNU_STATEMENT_EXPRESSIONS
283- /* *
284-  * The recommended alternative for statement expression-incompatible compilers 
285-  * is to directly allocate the memory. 
286-  * e.g. memory_allocator__->allocate(nbytes__); 
287-  */  
288- #define  ET_TRY_ALLOCATE_OR (memory_allocator__, nbytes__, ...) \
289-   static_assert (                                              \
290-       false ,                                                  \
291-       " ET_TRY_ALLOCATE_OR uses statement expressions and \
292-       thus is not available for use with this compiler."  );
293- 
294- /* *
295-  * The recommended alternative for statement expression-incompatible compilers 
296-  * is to directly allocate the memory. 
297-  * e.g. memory_allocator__->allocateInstance<type__>(); 
298-  */  
299- #define  ET_TRY_ALLOCATE_INSTANCE_OR (memory_allocator__, type__, ...) \
300-   static_assert (                                                     \
301-       false ,                                                         \
302-       " ET_TRY_ALLOCATE_INSTANCE_OR uses statement \
303-     expressions and thus is not available for use with this compiler."  );
304- 
305- /* *
306-  * The recommended alternative for statement expression-incompatible compilers 
307-  * is to directly use allocate the memory. 
308-  * e.g. memory_allocator__->allocateList<type__>(nelem__); 
309-  */  
310- #define  ET_TRY_ALLOCATE_LIST_OR (memory_allocator__, type__, nelem__, ...) \
311-   static_assert (                                                          \
312-       false ,                                                              \
313-       " ET_TRY_ALLOCATE_LIST_OR uses statement \
314-     expressions and thus is not available for use with this compiler."  );
315- #endif  //  !ET_HAVE_GNU_STATEMENT_EXPRESSIONS
316- 
317- /* *
318-  * Tries allocating from the specified MemoryAllocator*. 
319-  * 
320-  * - On success, returns a pointer to the allocated buffer. 
321-  * - On failure, returns `Error::MemoryAllocationFailed` from the calling 
322-  *   function, which must be declared to return `executorch::runtime::Error`. 
323-  * 
324-  * Example: 
325-  * @code 
326-  *   char* buf = ET_ALLOCATE_OR_RETURN_ERROR(memory_allocator, bufsize); 
327-  * @endcode 
328-  */  
329- #define  ET_ALLOCATE_OR_RETURN_ERROR (memory_allocator__, nbytes__ ) \
330-   ET_TRY_ALLOCATE_OR (memory_allocator__, nbytes__, {              \
331-     return  ::executorch::runtime::Error::MemoryAllocationFailed;  \
332-   })
333- 
334- /* *
335-  * Tries allocating an instance of type__ from the specified MemoryAllocator*. 
336-  * 
337-  * - On success, returns a pointer to the allocated buffer. Note that the memory 
338-  *   will not be initialized. 
339-  * - On failure, returns `Error::MemoryAllocationFailed` from the calling 
340-  *   function, which must be declared to return `executorch::runtime::Error`. 
341-  * 
342-  * Example: 
343-  * @code 
344-  *   char* buf = ET_ALLOCATE_INSTANCE_OR_RETURN_ERROR(memory_allocator, MyType); 
345-  * @endcode 
346-  */  
347- #define  ET_ALLOCATE_INSTANCE_OR_RETURN_ERROR (memory_allocator__, type__ ) \
348-   ET_TRY_ALLOCATE_INSTANCE_OR (memory_allocator__, type__, {              \
349-     return  ::executorch::runtime::Error::MemoryAllocationFailed;         \
350-   })
351- 
352- /* *
353-  * Tries allocating multiple elements of a given type from the specified 
354-  * MemoryAllocator*. 
355-  * 
356-  * - On success, returns a pointer to the allocated buffer. 
357-  * - On failure, returns `Error::MemoryAllocationFailed` from the calling 
358-  *   function, which must be declared to return `executorch::runtime::Error`. 
359-  * 
360-  * Example: 
361-  * @code 
362-  *   Tensor* tensor_list = ET_ALLOCATE_LIST_OR_RETURN_ERROR( 
363-  *       memory_allocator, Tensor, num_tensors); 
364-  * @endcode 
365-  */  
366- #define  ET_ALLOCATE_LIST_OR_RETURN_ERROR (memory_allocator__, type__, nelem__ ) \
367-   ET_TRY_ALLOCATE_LIST_OR (memory_allocator__, type__, nelem__, {              \
368-     return  ::executorch::runtime::Error::MemoryAllocationFailed;              \
369-   })
370- 
371201} //  namespace runtime
372202} //  namespace executorch
373203
0 commit comments