Skip to content

Commit 6af1247

Browse files
authored
[orc-rt] Rename WrapperFunctionBuffer and headers. (#156799)
Renames WrapperFunctionResult to WrapperFunctionBuffer. This reflects intended usage as a buffer for both arguments and results. The WrapperFunctionResult.h headers are renamed to WrapperFunction.h as they will be extended with further wrapper-function related APIs in an upcoming patch.
1 parent 93785ff commit 6af1247

File tree

6 files changed

+228
-230
lines changed

6 files changed

+228
-230
lines changed
Lines changed: 64 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*===----- WrapperFunctionResult.h - blob-of-bytes container -----*- C -*-===*\
1+
/*===--------- WrapperFunction.h - Wrapper function utils ---------*- C -*-===*\
22
|* *|
33
|* Part of the LLVM Project, under the Apache License v2.0 with LLVM *|
44
|* Exceptions. *|
@@ -7,12 +7,12 @@
77
|* *|
88
|*===----------------------------------------------------------------------===*|
99
|* *|
10-
|* Defines orc_rt_WrapperFunctionResult and related APIs. *|
10+
|* Defines orc_rt_WrapperFunctionBuffer and related APIs. *|
1111
|* *|
1212
\*===----------------------------------------------------------------------===*/
1313

14-
#ifndef ORC_RT_C_WRAPPERFUNCTIONRESULT_H
15-
#define ORC_RT_C_WRAPPERFUNCTIONRESULT_H
14+
#ifndef ORC_RT_C_WRAPPERFUNCTION_H
15+
#define ORC_RT_C_WRAPPERFUNCTION_H
1616

1717
#include "orc-rt-c/ExternC.h"
1818

@@ -25,154 +25,154 @@ ORC_RT_C_EXTERN_C_BEGIN
2525
typedef union {
2626
char *ValuePtr;
2727
char Value[sizeof(char *)];
28-
} orc_rt_WrapperFunctionResultDataUnion;
28+
} orc_rt_WrapperFunctionBufferDataUnion;
2929

3030
/**
31-
* orc_rt_WrapperFunctionResult is a kind of C-SmallVector with an
31+
* orc_rt_WrapperFunctionBuffer is a kind of C-SmallVector with an
3232
* out-of-band error state.
3333
*
3434
* If Size == 0 and Data.ValuePtr is non-zero then the value is in the
3535
* 'out-of-band error' state, and Data.ValuePtr points at a malloc-allocated,
3636
* null-terminated string error message.
3737
*
38-
* If Size <= sizeof(orc_rt_WrapperFunctionResultData) then the value is in
38+
* If Size <= sizeof(orc_rt_WrapperFunctionBufferData) then the value is in
3939
* the 'small' state and the content is held in the first Size bytes of
4040
* Data.Value.
4141
*
42-
* If Size > sizeof(orc_rt_WrapperFunctionResultData) then the value is in the
42+
* If Size > sizeof(orc_rt_WrapperFunctionBufferData) then the value is in the
4343
* 'large' state and the content is held in the first Size bytes of the
4444
* memory pointed to by Data.ValuePtr. This memory must have been allocated by
4545
* malloc, and will be freed with free when this value is destroyed.
4646
*/
4747
typedef struct {
48-
orc_rt_WrapperFunctionResultDataUnion Data;
48+
orc_rt_WrapperFunctionBufferDataUnion Data;
4949
size_t Size;
50-
} orc_rt_WrapperFunctionResult;
50+
} orc_rt_WrapperFunctionBuffer;
5151

5252
/**
53-
* Zero-initialize an orc_rt_WrapperFunctionResult.
53+
* Zero-initialize an orc_rt_WrapperFunctionBuffer.
5454
*/
5555
static inline void
56-
orc_rt_WrapperFunctionResultInit(orc_rt_WrapperFunctionResult *R) {
57-
R->Size = 0;
58-
R->Data.ValuePtr = 0;
56+
orc_rt_WrapperFunctionBufferInit(orc_rt_WrapperFunctionBuffer *B) {
57+
B->Size = 0;
58+
B->Data.ValuePtr = 0;
5959
}
6060

6161
/**
62-
* Create an orc_rt_WrapperFunctionResult with an uninitialized buffer of
62+
* Create an orc_rt_WrapperFunctionBuffer with an uninitialized buffer of
6363
* size Size. The buffer is returned via the DataPtr argument.
6464
*/
65-
static inline orc_rt_WrapperFunctionResult
66-
orc_rt_WrapperFunctionResultAllocate(size_t Size) {
67-
orc_rt_WrapperFunctionResult R;
68-
R.Size = Size;
65+
static inline orc_rt_WrapperFunctionBuffer
66+
orc_rt_WrapperFunctionBufferAllocate(size_t Size) {
67+
orc_rt_WrapperFunctionBuffer B;
68+
B.Size = Size;
6969
// 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;
70+
B.Data.ValuePtr = 0;
71+
if (Size > sizeof(B.Data.Value))
72+
B.Data.ValuePtr = (char *)malloc(Size);
73+
return B;
7474
}
7575

7676
/**
77-
* Create an orc_rt_WrapperFunctionResult from the given data range.
77+
* Create an orc_rt_WrapperFunctionBuffer from the given data range.
7878
*/
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)) {
79+
static inline orc_rt_WrapperFunctionBuffer
80+
orc_rt_CreateWrapperFunctionBufferFromRange(const char *Data, size_t Size) {
81+
orc_rt_WrapperFunctionBuffer B;
82+
B.Size = Size;
83+
if (B.Size > sizeof(B.Data.Value)) {
8484
char *Tmp = (char *)malloc(Size);
8585
memcpy(Tmp, Data, Size);
86-
R.Data.ValuePtr = Tmp;
86+
B.Data.ValuePtr = Tmp;
8787
} else
88-
memcpy(R.Data.Value, Data, Size);
89-
return R;
88+
memcpy(B.Data.Value, Data, Size);
89+
return B;
9090
}
9191

9292
/**
93-
* Create an orc_rt_WrapperFunctionResult by copying the given string,
93+
* Create an orc_rt_WrapperFunctionBuffer by copying the given string,
9494
* including the null-terminator.
9595
*
9696
* This function copies the input string. The client is responsible for freeing
9797
* the ErrMsg arg.
9898
*/
99-
static inline orc_rt_WrapperFunctionResult
100-
orc_rt_CreateWrapperFunctionResultFromString(const char *Source) {
101-
return orc_rt_CreateWrapperFunctionResultFromRange(Source,
99+
static inline orc_rt_WrapperFunctionBuffer
100+
orc_rt_CreateWrapperFunctionBufferFromString(const char *Source) {
101+
return orc_rt_CreateWrapperFunctionBufferFromRange(Source,
102102
strlen(Source) + 1);
103103
}
104104

105105
/**
106-
* Create an orc_rt_WrapperFunctionResult representing an out-of-band
106+
* Create an orc_rt_WrapperFunctionBuffer representing an out-of-band
107107
* error.
108108
*
109109
* This function copies the input string. The client is responsible for freeing
110110
* the ErrMsg arg.
111111
*/
112-
static inline orc_rt_WrapperFunctionResult
113-
orc_rt_CreateWrapperFunctionResultFromOutOfBandError(const char *ErrMsg) {
114-
orc_rt_WrapperFunctionResult R;
115-
R.Size = 0;
112+
static inline orc_rt_WrapperFunctionBuffer
113+
orc_rt_CreateWrapperFunctionBufferFromOutOfBandError(const char *ErrMsg) {
114+
orc_rt_WrapperFunctionBuffer B;
115+
B.Size = 0;
116116
char *Tmp = (char *)malloc(strlen(ErrMsg) + 1);
117117
strcpy(Tmp, ErrMsg);
118-
R.Data.ValuePtr = Tmp;
119-
return R;
118+
B.Data.ValuePtr = Tmp;
119+
return B;
120120
}
121121

122122
/**
123-
* This should be called to destroy orc_rt_WrapperFunctionResult values
123+
* This should be called to destroy orc_rt_WrapperFunctionBuffer values
124124
* regardless of their state.
125125
*/
126126
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);
127+
orc_rt_WrapperFunctionBufferDispose(orc_rt_WrapperFunctionBuffer *B) {
128+
if (B->Size > sizeof(B->Data.Value) || (B->Size == 0 && B->Data.ValuePtr))
129+
free(B->Data.ValuePtr);
130130
}
131131

132132
/**
133133
* Get a pointer to the data contained in the given
134-
* orc_rt_WrapperFunctionResult.
134+
* orc_rt_WrapperFunctionBuffer.
135135
*/
136136
static inline char *
137-
orc_rt_WrapperFunctionResultData(orc_rt_WrapperFunctionResult *R) {
138-
assert((R->Size != 0 || R->Data.ValuePtr == NULL) &&
137+
orc_rt_WrapperFunctionBufferData(orc_rt_WrapperFunctionBuffer *B) {
138+
assert((B->Size != 0 || B->Data.ValuePtr == NULL) &&
139139
"Cannot get data for out-of-band error value");
140-
return R->Size > sizeof(R->Data.Value) ? R->Data.ValuePtr : R->Data.Value;
140+
return B->Size > sizeof(B->Data.Value) ? B->Data.ValuePtr : B->Data.Value;
141141
}
142142

143143
/**
144-
* Safely get the size of the given orc_rt_WrapperFunctionResult.
144+
* Safely get the size of the given orc_rt_WrapperFunctionBuffer.
145145
*
146146
* Asserts that we're not trying to access the size of an error value.
147147
*/
148148
static inline size_t
149-
orc_rt_WrapperFunctionResultSize(const orc_rt_WrapperFunctionResult *R) {
150-
assert((R->Size != 0 || R->Data.ValuePtr == NULL) &&
149+
orc_rt_WrapperFunctionBufferSize(const orc_rt_WrapperFunctionBuffer *B) {
150+
assert((B->Size != 0 || B->Data.ValuePtr == NULL) &&
151151
"Cannot get size for out-of-band error value");
152-
return R->Size;
152+
return B->Size;
153153
}
154154

155155
/**
156156
* Returns 1 if this value is equivalent to a value just initialized by
157-
* orc_rt_WrapperFunctionResultInit, 0 otherwise.
157+
* orc_rt_WrapperFunctionBufferInit, 0 otherwise.
158158
*/
159159
static inline size_t
160-
orc_rt_WrapperFunctionResultEmpty(const orc_rt_WrapperFunctionResult *R) {
161-
return R->Size == 0 && R->Data.ValuePtr == 0;
160+
orc_rt_WrapperFunctionBufferEmpty(const orc_rt_WrapperFunctionBuffer *B) {
161+
return B->Size == 0 && B->Data.ValuePtr == 0;
162162
}
163163

164164
/**
165165
* Returns a pointer to the out-of-band error string for this
166-
* orc_rt_WrapperFunctionResult, or null if there is no error.
166+
* orc_rt_WrapperFunctionBuffer, or null if there is no error.
167167
*
168-
* The orc_rt_WrapperFunctionResult retains ownership of the error
168+
* The orc_rt_WrapperFunctionBuffer retains ownership of the error
169169
* string, so it should be copied if the caller wishes to preserve it.
170170
*/
171-
static inline const char *orc_rt_WrapperFunctionResultGetOutOfBandError(
172-
const orc_rt_WrapperFunctionResult *R) {
173-
return R->Size == 0 ? R->Data.ValuePtr : 0;
171+
static inline const char *orc_rt_WrapperFunctionBufferGetOutOfBandError(
172+
const orc_rt_WrapperFunctionBuffer *B) {
173+
return B->Size == 0 ? B->Data.ValuePtr : 0;
174174
}
175175

176176
ORC_RT_C_EXTERN_C_END
177177

178-
#endif /* ORC_RT_WRAPPERFUNCTIONRESULT_H */
178+
#endif /* ORC_RT_WRAPPERFUNCTION_H */
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
//===-------- WrapperFunction.h - Wrapper function utils --------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM 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+
// Defines WrapperFunctionBuffer and related APIs.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef ORC_RT_WRAPPERFUNCTION_H
14+
#define ORC_RT_WRAPPERFUNCTION_H
15+
16+
#include "orc-rt-c/WrapperFunction.h"
17+
18+
#include <utility>
19+
20+
namespace orc_rt {
21+
22+
/// A C++ convenience wrapper for orc_rt_WrapperFunctionBuffer. Auto-disposes
23+
/// the contained result on destruction.
24+
class WrapperFunctionBuffer {
25+
public:
26+
/// Create a default WrapperFunctionBuffer.
27+
WrapperFunctionBuffer() { orc_rt_WrapperFunctionBufferInit(&B); }
28+
29+
/// Create a WrapperFunctionBuffer from a WrapperFunctionBuffer. This
30+
/// instance takes ownership of the result object and will automatically
31+
/// call dispose on the result upon destruction.
32+
WrapperFunctionBuffer(orc_rt_WrapperFunctionBuffer B) : B(B) {}
33+
34+
WrapperFunctionBuffer(const WrapperFunctionBuffer &) = delete;
35+
WrapperFunctionBuffer &operator=(const WrapperFunctionBuffer &) = delete;
36+
37+
WrapperFunctionBuffer(WrapperFunctionBuffer &&Other) {
38+
orc_rt_WrapperFunctionBufferInit(&B);
39+
std::swap(B, Other.B);
40+
}
41+
42+
WrapperFunctionBuffer &operator=(WrapperFunctionBuffer &&Other) {
43+
orc_rt_WrapperFunctionBufferDispose(&B);
44+
orc_rt_WrapperFunctionBufferInit(&B);
45+
std::swap(B, Other.B);
46+
return *this;
47+
}
48+
49+
~WrapperFunctionBuffer() { orc_rt_WrapperFunctionBufferDispose(&B); }
50+
51+
/// Relinquish ownership of and return the
52+
/// orc_rt_WrapperFunctionBuffer.
53+
orc_rt_WrapperFunctionBuffer release() {
54+
orc_rt_WrapperFunctionBuffer Tmp;
55+
orc_rt_WrapperFunctionBufferInit(&Tmp);
56+
std::swap(B, Tmp);
57+
return Tmp;
58+
}
59+
60+
/// Get a pointer to the data contained in this instance.
61+
char *data() { return orc_rt_WrapperFunctionBufferData(&B); }
62+
63+
/// Returns the size of the data contained in this instance.
64+
size_t size() const { return orc_rt_WrapperFunctionBufferSize(&B); }
65+
66+
/// Returns true if this value is equivalent to a default-constructed
67+
/// WrapperFunctionBuffer.
68+
bool empty() const { return orc_rt_WrapperFunctionBufferEmpty(&B); }
69+
70+
/// Create a WrapperFunctionBuffer with the given size and return a pointer
71+
/// to the underlying memory.
72+
static WrapperFunctionBuffer allocate(size_t Size) {
73+
return orc_rt_WrapperFunctionBufferAllocate(Size);
74+
}
75+
76+
/// Copy from the given char range.
77+
static WrapperFunctionBuffer copyFrom(const char *Source, size_t Size) {
78+
return orc_rt_CreateWrapperFunctionBufferFromRange(Source, Size);
79+
}
80+
81+
/// Copy from the given null-terminated string (includes the null-terminator).
82+
static WrapperFunctionBuffer copyFrom(const char *Source) {
83+
return orc_rt_CreateWrapperFunctionBufferFromString(Source);
84+
}
85+
86+
/// Create an out-of-band error by copying the given string.
87+
static WrapperFunctionBuffer createOutOfBandError(const char *Msg) {
88+
return orc_rt_CreateWrapperFunctionBufferFromOutOfBandError(Msg);
89+
}
90+
91+
/// If this value is an out-of-band error then this returns the error message,
92+
/// otherwise returns nullptr.
93+
const char *getOutOfBandError() const {
94+
return orc_rt_WrapperFunctionBufferGetOutOfBandError(&B);
95+
}
96+
97+
private:
98+
orc_rt_WrapperFunctionBuffer B;
99+
};
100+
101+
} // namespace orc_rt
102+
103+
#endif // ORC_RT_WRAPPERFUNCTION_H

0 commit comments

Comments
 (0)