|
| 1 | +/*===----- WrapperFunctionResult.h - blob-of-bytes container -----*- C -*-===*\ |
| 2 | +|* *| |
| 3 | +|* Part of the LLVM Project, under the Apache License v2.0 with LLVM *| |
| 4 | +|* Exceptions. *| |
| 5 | +|* See https://llvm.org/LICENSE.txt for license information. *| |
| 6 | +|* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception *| |
| 7 | +|* *| |
| 8 | +|*===----------------------------------------------------------------------===*| |
| 9 | +|* *| |
| 10 | +|* Defines orc_rt_WrapperFunctionResult and related APIs. *| |
| 11 | +|* *| |
| 12 | +\*===----------------------------------------------------------------------===*/ |
| 13 | + |
| 14 | +#ifndef ORC_RT_C_WRAPPERFUNCTIONRESULT_H |
| 15 | +#define ORC_RT_C_WRAPPERFUNCTIONRESULT_H |
| 16 | + |
| 17 | +#include "orc-rt-c/ExternC.h" |
| 18 | + |
| 19 | +#include <assert.h> |
| 20 | +#include <stdlib.h> |
| 21 | +#include <string.h> |
| 22 | + |
| 23 | +ORC_RT_C_EXTERN_C_BEGIN |
| 24 | + |
| 25 | +typedef union { |
| 26 | + char *ValuePtr; |
| 27 | + char Value[sizeof(char *)]; |
| 28 | +} orc_rt_WrapperFunctionResultDataUnion; |
| 29 | + |
| 30 | +/** |
| 31 | + * orc_rt_WrapperFunctionResult is a kind of C-SmallVector with an |
| 32 | + * out-of-band error state. |
| 33 | + * |
| 34 | + * If Size == 0 and Data.ValuePtr is non-zero then the value is in the |
| 35 | + * 'out-of-band error' state, and Data.ValuePtr points at a malloc-allocated, |
| 36 | + * null-terminated string error message. |
| 37 | + * |
| 38 | + * If Size <= sizeof(orc_rt_WrapperFunctionResultData) then the value is in |
| 39 | + * the 'small' state and the content is held in the first Size bytes of |
| 40 | + * Data.Value. |
| 41 | + * |
| 42 | + * If Size > sizeof(orc_rt_WrapperFunctionResultData) then the value is in the |
| 43 | + * 'large' state and the content is held in the first Size bytes of the |
| 44 | + * memory pointed to by Data.ValuePtr. This memory must have been allocated by |
| 45 | + * malloc, and will be freed with free when this value is destroyed. |
| 46 | + */ |
| 47 | +typedef struct { |
| 48 | + orc_rt_WrapperFunctionResultDataUnion Data; |
| 49 | + size_t Size; |
| 50 | +} orc_rt_WrapperFunctionResult; |
| 51 | + |
| 52 | +/** |
| 53 | + * Zero-initialize an orc_rt_WrapperFunctionResult. |
| 54 | + */ |
| 55 | +static inline void |
| 56 | +orc_rt_WrapperFunctionResultInit(orc_rt_WrapperFunctionResult *R) { |
| 57 | + R->Size = 0; |
| 58 | + R->Data.ValuePtr = 0; |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Create an orc_rt_WrapperFunctionResult with an uninitialized buffer of |
| 63 | + * size Size. The buffer is returned via the DataPtr argument. |
| 64 | + */ |
| 65 | +static inline orc_rt_WrapperFunctionResult |
| 66 | +orc_rt_WrapperFunctionResultAllocate(size_t Size) { |
| 67 | + orc_rt_WrapperFunctionResult R; |
| 68 | + R.Size = Size; |
| 69 | + // If Size is 0 ValuePtr must be 0 or it is considered an out-of-band error. |
| 70 | + R.Data.ValuePtr = 0; |
| 71 | + if (Size > sizeof(R.Data.Value)) |
| 72 | + R.Data.ValuePtr = (char *)malloc(Size); |
| 73 | + return R; |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * Create an orc_rt_WrapperFunctionResult from the given data range. |
| 78 | + */ |
| 79 | +static inline orc_rt_WrapperFunctionResult |
| 80 | +orc_rt_CreateWrapperFunctionResultFromRange(const char *Data, size_t Size) { |
| 81 | + orc_rt_WrapperFunctionResult R; |
| 82 | + R.Size = Size; |
| 83 | + if (R.Size > sizeof(R.Data.Value)) { |
| 84 | + char *Tmp = (char *)malloc(Size); |
| 85 | + memcpy(Tmp, Data, Size); |
| 86 | + R.Data.ValuePtr = Tmp; |
| 87 | + } else |
| 88 | + memcpy(R.Data.Value, Data, Size); |
| 89 | + return R; |
| 90 | +} |
| 91 | + |
| 92 | +/** |
| 93 | + * Create an orc_rt_WrapperFunctionResult by copying the given string, |
| 94 | + * including the null-terminator. |
| 95 | + * |
| 96 | + * This function copies the input string. The client is responsible for freeing |
| 97 | + * the ErrMsg arg. |
| 98 | + */ |
| 99 | +static inline orc_rt_WrapperFunctionResult |
| 100 | +orc_rt_CreateWrapperFunctionResultFromString(const char *Source) { |
| 101 | + return orc_rt_CreateWrapperFunctionResultFromRange(Source, |
| 102 | + strlen(Source) + 1); |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * Create an orc_rt_WrapperFunctionResult representing an out-of-band |
| 107 | + * error. |
| 108 | + * |
| 109 | + * This function copies the input string. The client is responsible for freeing |
| 110 | + * the ErrMsg arg. |
| 111 | + */ |
| 112 | +static inline orc_rt_WrapperFunctionResult |
| 113 | +orc_rt_CreateWrapperFunctionResultFromOutOfBandError(const char *ErrMsg) { |
| 114 | + orc_rt_WrapperFunctionResult R; |
| 115 | + R.Size = 0; |
| 116 | + char *Tmp = (char *)malloc(strlen(ErrMsg) + 1); |
| 117 | + strcpy(Tmp, ErrMsg); |
| 118 | + R.Data.ValuePtr = Tmp; |
| 119 | + return R; |
| 120 | +} |
| 121 | + |
| 122 | +/** |
| 123 | + * This should be called to destroy orc_rt_WrapperFunctionResult values |
| 124 | + * regardless of their state. |
| 125 | + */ |
| 126 | +static inline void |
| 127 | +orc_rt_DisposeWrapperFunctionResult(orc_rt_WrapperFunctionResult *R) { |
| 128 | + if (R->Size > sizeof(R->Data.Value) || (R->Size == 0 && R->Data.ValuePtr)) |
| 129 | + free(R->Data.ValuePtr); |
| 130 | +} |
| 131 | + |
| 132 | +/** |
| 133 | + * Get a pointer to the data contained in the given |
| 134 | + * orc_rt_WrapperFunctionResult. |
| 135 | + */ |
| 136 | +static inline char * |
| 137 | +orc_rt_WrapperFunctionResultData(orc_rt_WrapperFunctionResult *R) { |
| 138 | + assert((R->Size != 0 || R->Data.ValuePtr == NULL) && |
| 139 | + "Cannot get data for out-of-band error value"); |
| 140 | + return R->Size > sizeof(R->Data.Value) ? R->Data.ValuePtr : R->Data.Value; |
| 141 | +} |
| 142 | + |
| 143 | +/** |
| 144 | + * Safely get the size of the given orc_rt_WrapperFunctionResult. |
| 145 | + * |
| 146 | + * Asserts that we're not trying to access the size of an error value. |
| 147 | + */ |
| 148 | +static inline size_t |
| 149 | +orc_rt_WrapperFunctionResultSize(const orc_rt_WrapperFunctionResult *R) { |
| 150 | + assert((R->Size != 0 || R->Data.ValuePtr == NULL) && |
| 151 | + "Cannot get size for out-of-band error value"); |
| 152 | + return R->Size; |
| 153 | +} |
| 154 | + |
| 155 | +/** |
| 156 | + * Returns 1 if this value is equivalent to a value just initialized by |
| 157 | + * orc_rt_WrapperFunctionResultInit, 0 otherwise. |
| 158 | + */ |
| 159 | +static inline size_t |
| 160 | +orc_rt_WrapperFunctionResultEmpty(const orc_rt_WrapperFunctionResult *R) { |
| 161 | + return R->Size == 0 && R->Data.ValuePtr == 0; |
| 162 | +} |
| 163 | + |
| 164 | +/** |
| 165 | + * Returns a pointer to the out-of-band error string for this |
| 166 | + * orc_rt_WrapperFunctionResult, or null if there is no error. |
| 167 | + * |
| 168 | + * The orc_rt_WrapperFunctionResult retains ownership of the error |
| 169 | + * string, so it should be copied if the caller wishes to preserve it. |
| 170 | + */ |
| 171 | +static inline const char *orc_rt_WrapperFunctionResultGetOutOfBandError( |
| 172 | + const orc_rt_WrapperFunctionResult *R) { |
| 173 | + return R->Size == 0 ? R->Data.ValuePtr : 0; |
| 174 | +} |
| 175 | + |
| 176 | +ORC_RT_C_EXTERN_C_END |
| 177 | + |
| 178 | +#endif /* ORC_RT_WRAPPERFUNCTIONRESULT_H */ |
0 commit comments