Browse Source

Convert JPC_BroadPhaseLayerInterface to be opaque type, fixing segfault

Lucien Greathouse 1 year ago
parent
commit
44d4469309
3 changed files with 19 additions and 16 deletions
  1. 1 4
      HelloWorld/main.cpp
  2. 6 5
      JoltC/Functions.h
  3. 12 7
      JoltC/JoltC.cpp

+ 1 - 4
HelloWorld/main.cpp

@@ -88,10 +88,7 @@ int main() {
 
 	JPC_JobSystemThreadPool* job_system = JPC_JobSystemThreadPool_new2(JPC_MAX_PHYSICS_JOBS, JPC_MAX_PHYSICS_BARRIERS);
 
-	JPC_BroadPhaseLayerInterface broad_phase_layer_interface = {
-		.self = nullptr,
-		.fns = Hello_BPL,
-	};
+	JPC_BroadPhaseLayerInterface* broad_phase_layer_interface = JPC_BroadPhaseLayerInterface_new(nullptr, Hello_BPL);
 
 	JPC_ObjectVsBroadPhaseLayerFilter object_vs_broad_phase_layer_filter = {
 		.self = nullptr,

+ 6 - 5
JoltC/Functions.h

@@ -112,10 +112,11 @@ typedef struct JPC_BroadPhaseLayerInterfaceFns {
 	JPC_BroadPhaseLayer (*GetBroadPhaseLayer)(const void *self, JPC_ObjectLayer inLayer);
 } JPC_BroadPhaseLayerInterfaceFns;
 
-typedef struct JPC_BroadPhaseLayerInterface {
-	const void *self;
-	JPC_BroadPhaseLayerInterfaceFns fns;
-} JPC_BroadPhaseLayerInterface;
+typedef struct JPC_BroadPhaseLayerInterface JPC_BroadPhaseLayerInterface;
+
+JPC_API JPC_BroadPhaseLayerInterface* JPC_BroadPhaseLayerInterface_new(
+	const void *self,
+	JPC_BroadPhaseLayerInterfaceFns fns);
 
 ////////////////////////////////////////////////////////////////////////////////
 // ObjectVsBroadPhaseLayerFilter
@@ -232,7 +233,7 @@ JPC_API void JPC_PhysicsSystem_Init(
 	uint inNumBodyMutexes,
 	uint inMaxBodyPairs,
 	uint inMaxContactConstraints,
-	JPC_BroadPhaseLayerInterface inBroadPhaseLayerInterface,
+	JPC_BroadPhaseLayerInterface* inBroadPhaseLayerInterface,
 	JPC_ObjectVsBroadPhaseLayerFilter inObjectVsBroadPhaseLayerFilter,
 	JPC_ObjectLayerPairFilter inObjectLayerPairFilter);
 

+ 12 - 7
JoltC/JoltC.cpp

@@ -126,9 +126,9 @@ JPC_API JPC_JobSystemThreadPool* JPC_JobSystemThreadPool_new3(
 ////////////////////////////////////////////////////////////////////////////////
 // BroadPhaseLayerInterface
 
-class JPC_BroadPhaseLayerInterface_Impl final : public JPH::BroadPhaseLayerInterface {
+class JPC_BroadPhaseLayerInterfaceBridge final : public JPH::BroadPhaseLayerInterface {
 public:
-	explicit JPC_BroadPhaseLayerInterface_Impl(JPC_BroadPhaseLayerInterface in) : self(in.self), fns(in.fns) {}
+	explicit JPC_BroadPhaseLayerInterfaceBridge(const void *self, JPC_BroadPhaseLayerInterfaceFns fns) : self(self), fns(fns) {}
 
 	virtual uint GetNumBroadPhaseLayers() const override {
 		return fns.GetNumBroadPhaseLayers(self);
@@ -149,8 +149,13 @@ private:
 	JPC_BroadPhaseLayerInterfaceFns fns;
 };
 
-static JPC_BroadPhaseLayerInterface_Impl to_jph(JPC_BroadPhaseLayerInterface in) {
-	return JPC_BroadPhaseLayerInterface_Impl(in);
+OPAQUE_WRAPPER(JPC_BroadPhaseLayerInterface, JPC_BroadPhaseLayerInterfaceBridge)
+
+JPC_API JPC_BroadPhaseLayerInterface* JPC_BroadPhaseLayerInterface_new(
+	const void *self,
+	JPC_BroadPhaseLayerInterfaceFns fns)
+{
+	return to_jpc(new JPC_BroadPhaseLayerInterfaceBridge(self, fns));
 }
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -293,11 +298,11 @@ JPC_API void JPC_PhysicsSystem_Init(
 	uint inNumBodyMutexes,
 	uint inMaxBodyPairs,
 	uint inMaxContactConstraints,
-	JPC_BroadPhaseLayerInterface inBroadPhaseLayerInterface,
+	JPC_BroadPhaseLayerInterface* inBroadPhaseLayerInterface,
 	JPC_ObjectVsBroadPhaseLayerFilter inObjectVsBroadPhaseLayerFilter,
 	JPC_ObjectLayerPairFilter inObjectLayerPairFilter)
 {
-	auto impl_inBroadPhaseLayerInterface = to_jph(inBroadPhaseLayerInterface);
+	JPC_BroadPhaseLayerInterfaceBridge* impl_inBroadPhaseLayerInterface = to_jph(inBroadPhaseLayerInterface);
 	auto impl_inObjectVsBroadPhaseLayerFilter = to_jph(inObjectVsBroadPhaseLayerFilter);
 	auto impl_inObjectLayerPairFilter = to_jph(inObjectLayerPairFilter);
 
@@ -306,7 +311,7 @@ JPC_API void JPC_PhysicsSystem_Init(
 		inNumBodyMutexes,
 		inMaxBodyPairs,
 		inMaxContactConstraints,
-		impl_inBroadPhaseLayerInterface,
+		*impl_inBroadPhaseLayerInterface,
 		impl_inObjectVsBroadPhaseLayerFilter,
 		impl_inObjectLayerPairFilter);
 }