Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions cpp/src/arrow/util/io_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2278,4 +2278,24 @@ Result<void*> GetSymbol(void* handle, const char* name) {
#endif
}

Status CloseDynamicLibrary(void* handle) {
if (handle == nullptr) {
return Status::Invalid("Attempting to close null library handle");
}
#ifdef _WIN32
if (FreeLibrary(reinterpret_cast<HMODULE>(handle))) {
return Status::OK();
}
// win32 api doc: "If the function fails, the return value is zero."
return IOErrorFromWinError(GetLastError(), "FreeLibrary() failed");
#else
if (dlclose(handle) == 0) {
return Status::OK();
}
// dlclose(3) man page: "On success, dlclose() returns 0; on error, it returns a nonzero value."
auto* error = dlerror();
return Status::IOError("dlclose() failed: ", error ? error : "unknown error");
#endif
}

} // namespace arrow::internal
7 changes: 7 additions & 0 deletions cpp/src/arrow/util/io_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,13 @@ ARROW_EXPORT Result<void*> LoadDynamicLibrary(const char* path);
/// returned; instead an error will be raised.
ARROW_EXPORT Result<void*> GetSymbol(void* handle, const char* name);

/// \brief Close a dynamic library
///
/// This wraps dlclose() except on Windows, where FreeLibrary() is called.
///
/// \return Status::OK() if the library was closed successfully, otherwise an error is returned.
ARROW_EXPORT Status CloseDynamicLibrary(void* handle);

template <typename T>
Result<T*> GetSymbolAs(void* handle, const char* name) {
ARROW_ASSIGN_OR_RAISE(void* sym, GetSymbol(handle, name));
Expand Down
Loading