Skip to content

Commit

Permalink
CVS-154700, Implement LoadLibrary for windows with GetProcAddress (#2874
Browse files Browse the repository at this point in the history
)

* CVS-154700, Implement LoadLibrary for windows with GetProcAddress - dlopen.

* Node library tests

* Style

* Cleanup

* Spell

---------

Co-authored-by: rasapala <[email protected]>
  • Loading branch information
18582088138 and rasapala authored Jan 7, 2025
1 parent 83987cc commit f530f85
Show file tree
Hide file tree
Showing 14 changed files with 208 additions and 108 deletions.
10 changes: 5 additions & 5 deletions docs/custom_node_development.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Each parameter in such objects can be referenced using an index which you can se

### "execute" function
```
int execute(const struct CustomNodeTensor* inputs, int inputsCount, struct CustomNodeTensor** outputs, int* outputsCount, const struct CustomNodeParam* params, int paramsCount);
DLL_PUBLIC int execute(const struct CustomNodeTensor* inputs, int inputsCount, struct CustomNodeTensor** outputs, int* outputsCount, const struct CustomNodeParam* params, int paramsCount);
```

This function implements the data transformation of the custom node. The input data for the function are passed in the form of
Expand Down Expand Up @@ -90,26 +90,26 @@ node execution. This function should call `free` if `malloc` was used to allocat

### "initialize" function
```
int initialize(void** customNodeLibraryInternalManager, const struct CustomNodeParam* params, int paramsCount);
DLL_PUBLIC int initialize(void** customNodeLibraryInternalManager, const struct CustomNodeParam* params, int paramsCount);
```
This function enables creation of resources to be reused between predictions. Potential use cases include optimized temporary buffers allocation. Using `initialize` is optional and not required for custom node to work. `customNodeLibraryInternalManager` should be instantiated inside this function if initialize is used. On initialize failure, status not equal to `0` should be returned to make OVMS treat it as an error.

When not used, minimal dummy implementation is required. Return `0`, meaning no error:
```
int initialize(void** customNodeLibraryInternalManager, const struct CustomNodeParam* params, int paramsCount) {
DLL_PUBLIC int initialize(void** customNodeLibraryInternalManager, const struct CustomNodeParam* params, int paramsCount) {
return 0;
}
```

### "deinitialize" function
```
int deinitialize(void* customNodeLibraryInternalManager);
DLL_PUBLIC int deinitialize(void* customNodeLibraryInternalManager);
```
This function enables destruction of resources that were used between predictions. Using deinitialize is optional and not required for custom node to work. `customNodeLibraryInternalManager` should be destroyed here if deinitialize is used. On deinitialization failure, status not equal to `0` should be returned to make OVMS treat it as an error.

When not used, minimal dummy implementation is required. Return `0`, meaning no error:
```
int deinitialize(void* customNodeLibraryInternalManager) {
DLL_PUBLIC int deinitialize(void* customNodeLibraryInternalManager) {
return 0;
}
```
Expand Down
20 changes: 14 additions & 6 deletions src/custom_node_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
//*****************************************************************************
#pragma once

#ifdef __linux__
#define DLL_PUBLIC __attribute__((visibility("default")))
#define DLL_LOCAL __attribute__((visibility("hidden")))
#elif _WIN32
#define DLL_PUBLIC __declspec(dllexport)
#define DLL_LOCAL __declspec(dllimport)
#endif

#include <stdint.h>
typedef enum {
UNSPECIFIED,
Expand Down Expand Up @@ -60,18 +68,18 @@ extern "C" {
* CustomNodeLibraryInternalManager should be created here if initialize is used.
* On initialize failure status not equal to zero is returned and error log is printed.
*/
int initialize(void** customNodeLibraryInternalManager, const struct CustomNodeParam* params, int paramsCount);
DLL_PUBLIC int initialize(void** customNodeLibraryInternalManager, const struct CustomNodeParam* params, int paramsCount);
/**
* @brief Custom node library deinitialize enables destruction of resources that were used between predictions.
* Using deinitialize is optional and not required for custom node to work.
* CustomNodeLibraryInternalManager should be destroyed here if deinitialize is used.
* On deinitialize failure only error log is printed.
*/
int deinitialize(void* customNodeLibraryInternalManager);
int execute(const struct CustomNodeTensor* inputs, int inputsCount, struct CustomNodeTensor** outputs, int* outputsCount, const struct CustomNodeParam* params, int paramsCount, void* customNodeLibraryInternalManager);
int getInputsInfo(struct CustomNodeTensorInfo** info, int* infoCount, const struct CustomNodeParam* params, int paramsCount, void* customNodeLibraryInternalManager);
int getOutputsInfo(struct CustomNodeTensorInfo** info, int* infoCount, const struct CustomNodeParam* params, int paramsCount, void* customNodeLibraryInternalManager);
int release(void* ptr, void* customNodeLibraryInternalManager);
DLL_PUBLIC int deinitialize(void* customNodeLibraryInternalManager);
DLL_PUBLIC int execute(const struct CustomNodeTensor* inputs, int inputsCount, struct CustomNodeTensor** outputs, int* outputsCount, const struct CustomNodeParam* params, int paramsCount, void* customNodeLibraryInternalManager);
DLL_PUBLIC int getInputsInfo(struct CustomNodeTensorInfo** info, int* infoCount, const struct CustomNodeParam* params, int paramsCount, void* customNodeLibraryInternalManager);
DLL_PUBLIC int getOutputsInfo(struct CustomNodeTensorInfo** info, int* infoCount, const struct CustomNodeParam* params, int paramsCount, void* customNodeLibraryInternalManager);
DLL_PUBLIC int release(void* ptr, void* customNodeLibraryInternalManager);

#ifdef __cplusplus
}
Expand Down
12 changes: 6 additions & 6 deletions src/custom_nodes/add_one/add_one.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ static int reinitializeInternalManagerIfNeccessary(void** customNodeLibraryInter
return 0;
}

int initialize(void** customNodeLibraryInternalManager, const struct CustomNodeParam* params, int paramsCount) {
DLL_PUBLIC int initialize(void** customNodeLibraryInternalManager, const struct CustomNodeParam* params, int paramsCount) {
auto status = 0;
if (*customNodeLibraryInternalManager == nullptr) {
status = initializeInternalManager(customNodeLibraryInternalManager, params, paramsCount);
Expand All @@ -109,15 +109,15 @@ int initialize(void** customNodeLibraryInternalManager, const struct CustomNodeP
return 0;
}

int deinitialize(void* customNodeLibraryInternalManager) {
DLL_PUBLIC int deinitialize(void* customNodeLibraryInternalManager) {
if (customNodeLibraryInternalManager != nullptr) {
InternalManager* internalManager = static_cast<InternalManager*>(customNodeLibraryInternalManager);
delete internalManager;
}
return 0;
}

int execute(const struct CustomNodeTensor* inputs, int inputsCount, struct CustomNodeTensor** outputs, int* outputsCount, const struct CustomNodeParam* params, int paramsCount, void* customNodeLibraryInternalManager) {
DLL_PUBLIC int execute(const struct CustomNodeTensor* inputs, int inputsCount, struct CustomNodeTensor** outputs, int* outputsCount, const struct CustomNodeParam* params, int paramsCount, void* customNodeLibraryInternalManager) {
InternalManager* internalManager = static_cast<InternalManager*>(customNodeLibraryInternalManager);
NODE_ASSERT(internalManager != nullptr, "internalManager is not initialized");
std::shared_lock<std::shared_timed_mutex> lock(internalManager->getInternalManagerLock());
Expand Down Expand Up @@ -171,7 +171,7 @@ int execute(const struct CustomNodeTensor* inputs, int inputsCount, struct Custo
return 0;
}

int getInputsInfo(struct CustomNodeTensorInfo** info, int* infoCount, const struct CustomNodeParam* params, int paramsCount, void* customNodeLibraryInternalManager) {
DLL_PUBLIC int getInputsInfo(struct CustomNodeTensorInfo** info, int* infoCount, const struct CustomNodeParam* params, int paramsCount, void* customNodeLibraryInternalManager) {
InternalManager* internalManager = static_cast<InternalManager*>(customNodeLibraryInternalManager);
NODE_ASSERT(internalManager != nullptr, "internalManager is not initialized");
std::shared_lock<std::shared_timed_mutex> lock(internalManager->getInternalManagerLock());
Expand All @@ -192,7 +192,7 @@ int getInputsInfo(struct CustomNodeTensorInfo** info, int* infoCount, const stru
return 0;
}

int getOutputsInfo(struct CustomNodeTensorInfo** info, int* infoCount, const struct CustomNodeParam* params, int paramsCount, void* customNodeLibraryInternalManager) {
DLL_PUBLIC int getOutputsInfo(struct CustomNodeTensorInfo** info, int* infoCount, const struct CustomNodeParam* params, int paramsCount, void* customNodeLibraryInternalManager) {
InternalManager* internalManager = static_cast<InternalManager*>(customNodeLibraryInternalManager);
NODE_ASSERT(internalManager != nullptr, "internalManager is not initialized");
std::shared_lock<std::shared_timed_mutex> lock(internalManager->getInternalManagerLock());
Expand All @@ -213,7 +213,7 @@ int getOutputsInfo(struct CustomNodeTensorInfo** info, int* infoCount, const str
return 0;
}

int release(void* ptr, void* customNodeLibraryInternalManager) {
DLL_PUBLIC int release(void* ptr, void* customNodeLibraryInternalManager) {
InternalManager* internalManager = static_cast<InternalManager*>(customNodeLibraryInternalManager);
if (!internalManager->releaseBuffer(ptr)) {
free(ptr);
Expand Down
12 changes: 6 additions & 6 deletions src/custom_nodes/east_ocr/east_ocr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,15 @@ static bool copy_confidences_into_output(struct CustomNodeTensor* output, const
return true;
}

int initialize(void** customNodeLibraryInternalManager, const struct CustomNodeParam* params, int paramsCount) {
DLL_PUBLIC int initialize(void** customNodeLibraryInternalManager, const struct CustomNodeParam* params, int paramsCount) {
return 0;
}

int deinitialize(void* customNodeLibraryInternalManager) {
DLL_PUBLIC int deinitialize(void* customNodeLibraryInternalManager) {
return 0;
}

int execute(const struct CustomNodeTensor* inputs, int inputsCount, struct CustomNodeTensor** outputs, int* outputsCount, const struct CustomNodeParam* params, int paramsCount, void* customNodeLibraryInternalManager) {
DLL_PUBLIC int execute(const struct CustomNodeTensor* inputs, int inputsCount, struct CustomNodeTensor** outputs, int* outputsCount, const struct CustomNodeParam* params, int paramsCount, void* customNodeLibraryInternalManager) {
// Parameters reading
int originalImageHeight = get_int_parameter("original_image_height", params, paramsCount, -1);
int originalImageWidth = get_int_parameter("original_image_width", params, paramsCount, -1);
Expand Down Expand Up @@ -366,7 +366,7 @@ int execute(const struct CustomNodeTensor* inputs, int inputsCount, struct Custo
return 0;
}

int getInputsInfo(struct CustomNodeTensorInfo** info, int* infoCount, const struct CustomNodeParam* params, int paramsCount, void* customNodeLibraryInternalManager) {
DLL_PUBLIC int getInputsInfo(struct CustomNodeTensorInfo** info, int* infoCount, const struct CustomNodeParam* params, int paramsCount, void* customNodeLibraryInternalManager) {
int originalImageHeight = get_int_parameter("original_image_height", params, paramsCount, -1);
int originalImageWidth = get_int_parameter("original_image_width", params, paramsCount, -1);
NODE_ASSERT(originalImageHeight > 0, "original image height must be larger than 0");
Expand Down Expand Up @@ -418,7 +418,7 @@ int getInputsInfo(struct CustomNodeTensorInfo** info, int* infoCount, const stru
return 0;
}

int getOutputsInfo(struct CustomNodeTensorInfo** info, int* infoCount, const struct CustomNodeParam* params, int paramsCount, void* customNodeLibraryInternalManager) {
DLL_PUBLIC int getOutputsInfo(struct CustomNodeTensorInfo** info, int* infoCount, const struct CustomNodeParam* params, int paramsCount, void* customNodeLibraryInternalManager) {
int targetImageHeight = get_int_parameter("target_image_height", params, paramsCount, -1);
int targetImageWidth = get_int_parameter("target_image_width", params, paramsCount, -1);
NODE_ASSERT(targetImageHeight > 0, "target image height must be larger than 0");
Expand Down Expand Up @@ -468,7 +468,7 @@ int getOutputsInfo(struct CustomNodeTensorInfo** info, int* infoCount, const str
return 0;
}

int release(void* ptr, void* customNodeLibraryInternalManager) {
DLL_PUBLIC int release(void* ptr, void* customNodeLibraryInternalManager) {
free(ptr);
return 0;
}
12 changes: 6 additions & 6 deletions src/custom_nodes/face_blur/face_blur.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
static constexpr const char* IMAGE_TENSOR_NAME = "image";
static constexpr const char* DETECTION_TENSOR_NAME = "detection";

int initialize(void** customNodeLibraryInternalManager, const struct CustomNodeParam* params, int paramsCount) {
DLL_PUBLIC int initialize(void** customNodeLibraryInternalManager, const struct CustomNodeParam* params, int paramsCount) {
return 0;
}

int deinitialize(void* customNodeLibraryInternalManager) {
DLL_PUBLIC int deinitialize(void* customNodeLibraryInternalManager) {
return 0;
}

int execute(const struct CustomNodeTensor* inputs, int inputsCount, struct CustomNodeTensor** outputs, int* outputsCount, const struct CustomNodeParam* params, int paramsCount, void* customNodeLibraryInternalManager) {
DLL_PUBLIC int execute(const struct CustomNodeTensor* inputs, int inputsCount, struct CustomNodeTensor** outputs, int* outputsCount, const struct CustomNodeParam* params, int paramsCount, void* customNodeLibraryInternalManager) {
// Parameters reading
int originalImageHeight = get_int_parameter("original_image_height", params, paramsCount, -1);
int originalImageWidth = get_int_parameter("original_image_width", params, paramsCount, -1);
Expand Down Expand Up @@ -184,7 +184,7 @@ int execute(const struct CustomNodeTensor* inputs, int inputsCount, struct Custo
return 0;
}

int getInputsInfo(struct CustomNodeTensorInfo** info, int* infoCount, const struct CustomNodeParam* params, int paramsCount, void* customNodeLibraryInternalManager) {
DLL_PUBLIC int getInputsInfo(struct CustomNodeTensorInfo** info, int* infoCount, const struct CustomNodeParam* params, int paramsCount, void* customNodeLibraryInternalManager) {
int originalImageHeight = get_int_parameter("original_image_height", params, paramsCount, -1);
int originalImageWidth = get_int_parameter("original_image_width", params, paramsCount, -1);
NODE_ASSERT(originalImageHeight > 0, "original image height must be larger than 0");
Expand Down Expand Up @@ -224,7 +224,7 @@ int getInputsInfo(struct CustomNodeTensorInfo** info, int* infoCount, const stru
return 0;
}

int getOutputsInfo(struct CustomNodeTensorInfo** info, int* infoCount, const struct CustomNodeParam* params, int paramsCount, void* customNodeLibraryInternalManager) {
DLL_PUBLIC int getOutputsInfo(struct CustomNodeTensorInfo** info, int* infoCount, const struct CustomNodeParam* params, int paramsCount, void* customNodeLibraryInternalManager) {
int targetImageHeight = get_int_parameter("target_image_height", params, paramsCount, -1);
int targetImageWidth = get_int_parameter("target_image_width", params, paramsCount, -1);
NODE_ASSERT(targetImageHeight > 0, "target image height must be larger than 0");
Expand Down Expand Up @@ -257,7 +257,7 @@ int getOutputsInfo(struct CustomNodeTensorInfo** info, int* infoCount, const str
return 0;
}

int release(void* ptr, void* customNodeLibraryInternalManager) {
DLL_PUBLIC int release(void* ptr, void* customNodeLibraryInternalManager) {
free(ptr);
return 0;
}
12 changes: 6 additions & 6 deletions src/custom_nodes/horizontal_ocr/horizontal_ocr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,15 @@ static bool copy_scores_into_output(struct CustomNodeTensor* output, const std::
return true;
}

int initialize(void** customNodeLibraryInternalManager, const struct CustomNodeParam* params, int paramsCount) {
DLL_PUBLIC int initialize(void** customNodeLibraryInternalManager, const struct CustomNodeParam* params, int paramsCount) {
return 0;
}

int deinitialize(void* customNodeLibraryInternalManager) {
DLL_PUBLIC int deinitialize(void* customNodeLibraryInternalManager) {
return 0;
}

int execute(const struct CustomNodeTensor* inputs, int inputsCount, struct CustomNodeTensor** outputs, int* outputsCount, const struct CustomNodeParam* params, int paramsCount, void* customNodeLibraryInternalManager) {
DLL_PUBLIC int execute(const struct CustomNodeTensor* inputs, int inputsCount, struct CustomNodeTensor** outputs, int* outputsCount, const struct CustomNodeParam* params, int paramsCount, void* customNodeLibraryInternalManager) {
// Parameters reading
int originalImageHeight = get_int_parameter("original_image_height", params, paramsCount, -1);
int originalImageWidth = get_int_parameter("original_image_width", params, paramsCount, -1);
Expand Down Expand Up @@ -267,7 +267,7 @@ int execute(const struct CustomNodeTensor* inputs, int inputsCount, struct Custo
return 0;
}

int getInputsInfo(struct CustomNodeTensorInfo** info, int* infoCount, const struct CustomNodeParam* params, int paramsCount, void* customNodeLibraryInternalManager) {
DLL_PUBLIC int getInputsInfo(struct CustomNodeTensorInfo** info, int* infoCount, const struct CustomNodeParam* params, int paramsCount, void* customNodeLibraryInternalManager) {
int originalImageHeight = get_int_parameter("original_image_height", params, paramsCount, -1);
int originalImageWidth = get_int_parameter("original_image_width", params, paramsCount, -1);
NODE_ASSERT(originalImageHeight > 0, "original image height must be larger than 0");
Expand Down Expand Up @@ -305,7 +305,7 @@ int getInputsInfo(struct CustomNodeTensorInfo** info, int* infoCount, const stru
return 0;
}

int getOutputsInfo(struct CustomNodeTensorInfo** info, int* infoCount, const struct CustomNodeParam* params, int paramsCount, void* customNodeLibraryInternalManager) {
DLL_PUBLIC int getOutputsInfo(struct CustomNodeTensorInfo** info, int* infoCount, const struct CustomNodeParam* params, int paramsCount, void* customNodeLibraryInternalManager) {
int targetImageHeight = get_int_parameter("target_image_height", params, paramsCount, -1);
int targetImageWidth = get_int_parameter("target_image_width", params, paramsCount, -1);
NODE_ASSERT(targetImageHeight > 0, "target image height must be larger than 0");
Expand Down Expand Up @@ -355,7 +355,7 @@ int getOutputsInfo(struct CustomNodeTensorInfo** info, int* infoCount, const str
return 0;
}

int release(void* ptr, void* customNodeLibraryInternalManager) {
DLL_PUBLIC int release(void* ptr, void* customNodeLibraryInternalManager) {
free(ptr);
return 0;
}
Loading

0 comments on commit f530f85

Please sign in to comment.