Path: blob/main/contrib/llvm-project/compiler-rt/include/orc_rt/c_api.h
35233 views
/*===- c_api.h - C API for the ORC runtime ------------------------*- C -*-===*\1|* *|2|* Part of the LLVM Project, under the Apache License v2.0 with LLVM *|3|* Exceptions. *|4|* See https://llvm.org/LICENSE.txt for license information. *|5|* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception *|6|* *|7|*===----------------------------------------------------------------------===*|8|* *|9|* This file defines the C API for the ORC runtime *|10|* *|11\*===----------------------------------------------------------------------===*/1213#ifndef ORC_RT_C_API_H14#define ORC_RT_C_API_H1516#include <assert.h>17#include <stdio.h>18#include <stdlib.h>19#include <string.h>2021/* Helper to suppress strict prototype warnings. */22#ifdef __clang__23#define ORC_RT_C_STRICT_PROTOTYPES_BEGIN \24_Pragma("clang diagnostic push") \25_Pragma("clang diagnostic error \"-Wstrict-prototypes\"")26#define ORC_RT_C_STRICT_PROTOTYPES_END _Pragma("clang diagnostic pop")27#else28#define ORC_RT_C_STRICT_PROTOTYPES_BEGIN29#define ORC_RT_C_STRICT_PROTOTYPES_END30#endif3132/* Helper to wrap C code for C++ */33#ifdef __cplusplus34#define ORC_RT_C_EXTERN_C_BEGIN \35extern "C" { \36ORC_RT_C_STRICT_PROTOTYPES_BEGIN37#define ORC_RT_C_EXTERN_C_END \38ORC_RT_C_STRICT_PROTOTYPES_END \39}40#else41#define ORC_RT_C_EXTERN_C_BEGIN ORC_RT_C_STRICT_PROTOTYPES_BEGIN42#define ORC_RT_C_EXTERN_C_END ORC_RT_C_STRICT_PROTOTYPES_END43#endif4445ORC_RT_C_EXTERN_C_BEGIN4647typedef union {48char *ValuePtr;49char Value[sizeof(char *)];50} orc_rt_CWrapperFunctionResultDataUnion;5152/**53* orc_rt_CWrapperFunctionResult is a kind of C-SmallVector with an54* out-of-band error state.55*56* If Size == 0 and Data.ValuePtr is non-zero then the value is in the57* 'out-of-band error' state, and Data.ValuePtr points at a malloc-allocated,58* null-terminated string error message.59*60* If Size <= sizeof(orc_rt_CWrapperFunctionResultData) then the value is in61* the 'small' state and the content is held in the first Size bytes of62* Data.Value.63*64* If Size > sizeof(OrtRTCWrapperFunctionResultData) then the value is in the65* 'large' state and the content is held in the first Size bytes of the66* memory pointed to by Data.ValuePtr. This memory must have been allocated by67* malloc, and will be freed with free when this value is destroyed.68*/69typedef struct {70orc_rt_CWrapperFunctionResultDataUnion Data;71size_t Size;72} orc_rt_CWrapperFunctionResult;7374/**75* Zero-initialize an orc_rt_CWrapperFunctionResult.76*/77static inline void78orc_rt_CWrapperFunctionResultInit(orc_rt_CWrapperFunctionResult *R) {79R->Size = 0;80R->Data.ValuePtr = 0;81}8283/**84* Create an orc_rt_CWrapperFunctionResult with an uninitialized buffer of85* size Size. The buffer is returned via the DataPtr argument.86*/87static inline orc_rt_CWrapperFunctionResult88orc_rt_CWrapperFunctionResultAllocate(size_t Size) {89orc_rt_CWrapperFunctionResult R;90R.Size = Size;91// If Size is 0 ValuePtr must be 0 or it is considered an out-of-band error.92R.Data.ValuePtr = 0;93if (Size > sizeof(R.Data.Value))94R.Data.ValuePtr = (char *)malloc(Size);95return R;96}9798/**99* Create an orc_rt_WrapperFunctionResult from the given data range.100*/101static inline orc_rt_CWrapperFunctionResult102orc_rt_CreateCWrapperFunctionResultFromRange(const char *Data, size_t Size) {103orc_rt_CWrapperFunctionResult R;104R.Size = Size;105if (R.Size > sizeof(R.Data.Value)) {106char *Tmp = (char *)malloc(Size);107memcpy(Tmp, Data, Size);108R.Data.ValuePtr = Tmp;109} else110memcpy(R.Data.Value, Data, Size);111return R;112}113114/**115* Create an orc_rt_CWrapperFunctionResult by copying the given string,116* including the null-terminator.117*118* This function copies the input string. The client is responsible for freeing119* the ErrMsg arg.120*/121static inline orc_rt_CWrapperFunctionResult122orc_rt_CreateCWrapperFunctionResultFromString(const char *Source) {123return orc_rt_CreateCWrapperFunctionResultFromRange(Source,124strlen(Source) + 1);125}126127/**128* Create an orc_rt_CWrapperFunctionResult representing an out-of-band129* error.130*131* This function copies the input string. The client is responsible for freeing132* the ErrMsg arg.133*/134static inline orc_rt_CWrapperFunctionResult135orc_rt_CreateCWrapperFunctionResultFromOutOfBandError(const char *ErrMsg) {136orc_rt_CWrapperFunctionResult R;137R.Size = 0;138char *Tmp = (char *)malloc(strlen(ErrMsg) + 1);139strcpy(Tmp, ErrMsg);140R.Data.ValuePtr = Tmp;141return R;142}143144/**145* This should be called to destroy orc_rt_CWrapperFunctionResult values146* regardless of their state.147*/148static inline void149orc_rt_DisposeCWrapperFunctionResult(orc_rt_CWrapperFunctionResult *R) {150if (R->Size > sizeof(R->Data.Value) ||151(R->Size == 0 && R->Data.ValuePtr))152free(R->Data.ValuePtr);153}154155/**156* Get a pointer to the data contained in the given157* orc_rt_CWrapperFunctionResult.158*/159static inline char *160orc_rt_CWrapperFunctionResultData(orc_rt_CWrapperFunctionResult *R) {161assert((R->Size != 0 || R->Data.ValuePtr == NULL) &&162"Cannot get data for out-of-band error value");163return R->Size > sizeof(R->Data.Value) ? R->Data.ValuePtr : R->Data.Value;164}165166/**167* Safely get the size of the given orc_rt_CWrapperFunctionResult.168*169* Asserts that we're not trying to access the size of an error value.170*/171static inline size_t172orc_rt_CWrapperFunctionResultSize(const orc_rt_CWrapperFunctionResult *R) {173assert((R->Size != 0 || R->Data.ValuePtr == NULL) &&174"Cannot get size for out-of-band error value");175return R->Size;176}177178/**179* Returns 1 if this value is equivalent to a value just initialized by180* orc_rt_CWrapperFunctionResultInit, 0 otherwise.181*/182static inline size_t183orc_rt_CWrapperFunctionResultEmpty(const orc_rt_CWrapperFunctionResult *R) {184return R->Size == 0 && R->Data.ValuePtr == 0;185}186187/**188* Returns a pointer to the out-of-band error string for this189* orc_rt_CWrapperFunctionResult, or null if there is no error.190*191* The orc_rt_CWrapperFunctionResult retains ownership of the error192* string, so it should be copied if the caller wishes to preserve it.193*/194static inline const char *orc_rt_CWrapperFunctionResultGetOutOfBandError(195const orc_rt_CWrapperFunctionResult *R) {196return R->Size == 0 ? R->Data.ValuePtr : 0;197}198199ORC_RT_C_EXTERN_C_END200201#endif /* ORC_RT_C_API_H */202203204