Skip to content

Commit 90f1523

Browse files
committed
Add opaque context pointer to SwiftRemoteMirror C API Reader API
Memory readers on the C-side of the API may actually have an object- oriented design, so they may want to pass an instance to revive when callbacks make it to the other side of the API boundary.
1 parent 936d4e5 commit 90f1523

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

include/swift/Remote/CMemoryReader.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,27 @@ class CMemoryReader final : public MemoryReader {
3434
assert(this->Impl.getPointerSize && "No getPointerSize implementation");
3535
assert(this->Impl.getStringLength && "No stringLength implementation");
3636
assert(this->Impl.readBytes && "No readBytes implementation");
37-
assert(this->Impl.getPointerSize() != 0 && "Invalid target pointer size");
37+
assert(this->Impl.getPointerSize(this->Impl.reader_context) != 0 &&
38+
"Invalid target pointer size");
3839
}
3940

4041
uint8_t getPointerSize() override {
41-
return Impl.getPointerSize();
42+
return Impl.getPointerSize(Impl.reader_context);
4243
}
4344

4445
uint8_t getSizeSize() override {
45-
return Impl.getSizeSize();
46+
return Impl.getSizeSize(Impl.reader_context);
4647
}
4748

4849
RemoteAddress getSymbolAddress(const std::string &name) override {
49-
auto addressData = Impl.getSymbolAddress(name.c_str(), name.size());
50+
auto addressData = Impl.getSymbolAddress(Impl.reader_context,
51+
name.c_str(), name.size());
5052
return RemoteAddress(addressData);
5153
}
5254

5355
uint64_t getStringLength(RemoteAddress address) {
54-
return Impl.getStringLength(address.getAddressData());
56+
return Impl.getStringLength(Impl.reader_context,
57+
address.getAddressData());
5558
}
5659

5760
bool readString(RemoteAddress address, std::string &dest) override {
@@ -68,7 +71,8 @@ class CMemoryReader final : public MemoryReader {
6871
}
6972

7073
bool readBytes(RemoteAddress address, uint8_t *dest, uint64_t size) override {
71-
return Impl.readBytes(address.getAddressData(), dest, size);
74+
return Impl.readBytes(Impl.reader_context,
75+
address.getAddressData(), dest, size);
7276
}
7377
};
7478

include/swift/SwiftRemoteMirror/MemoryReaderInterface.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,21 @@ extern "C" {
2828

2929
typedef uint64_t addr_t;
3030

31-
typedef uint8_t (*PointerSizeFunction)();
32-
typedef uint8_t (*SizeSizeFunction)();
33-
typedef bool (*ReadBytesFunction)(addr_t address, uint8_t *dest, uint64_t size);
34-
typedef uint64_t (*GetStringLengthFunction)(addr_t address);
35-
typedef addr_t (*GetSymbolAddressFunction)(const char *name, uint64_t name_length);
31+
typedef uint8_t (*PointerSizeFunction)(void *reader_context);
32+
typedef uint8_t (*SizeSizeFunction)(void * reader_context);
33+
typedef bool (*ReadBytesFunction)(void * reader_context, addr_t address,
34+
uint8_t *dest, uint64_t size);
35+
typedef uint64_t (*GetStringLengthFunction)(void * reader_context,
36+
addr_t address);
37+
typedef addr_t (*GetSymbolAddressFunction)(void * reader_context,
38+
const char *name,
39+
uint64_t name_length);
3640

3741
typedef struct MemoryReaderImpl {
42+
/// An opaque context that the implementor can specify to
43+
/// be passed to each of the APIs below.
44+
void *reader_context;
45+
3846
/// Get the size in bytes of the target's pointer type.
3947
PointerSizeFunction getPointerSize;
4048

stdlib/public/SwiftRemoteMirror/SwiftRemoteMirror.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ using NativeReflectionContext
2323
= ReflectionContext<External<RuntimeTarget<sizeof(uintptr_t)>>>;
2424

2525
SwiftReflectionContextRef
26-
swift_reflection_createReflectionContext(PointerSizeFunction getPointerSize,
26+
swift_reflection_createReflectionContext(void *reader_context,
27+
PointerSizeFunction getPointerSize,
2728
SizeSizeFunction getSizeSize,
2829
ReadBytesFunction readBytes,
2930
GetStringLengthFunction getStringLength,
3031
GetSymbolAddressFunction getSymbolAddress) {
3132
MemoryReaderImpl ReaderImpl {
33+
reader_context,
3234
getPointerSize,
3335
getSizeSize,
3436
readBytes,

0 commit comments

Comments
 (0)