From 0c31e332dd6ba4f8def51ad49a0f8d8fcebf1069 Mon Sep 17 00:00:00 2001 From: moto <855818+mthrok@users.noreply.github.com> Date: Sat, 3 Jun 2023 10:28:44 -0400 Subject: [PATCH] const --- torchaudio/csrc/sox/stub.cpp | 67 ++++++++++++++++++------------------ torchaudio/csrc/sox/stub.h | 2 +- 2 files changed, 35 insertions(+), 34 deletions(-) diff --git a/torchaudio/csrc/sox/stub.cpp b/torchaudio/csrc/sox/stub.cpp index c83b6d3c440..08fb12a16cc 100644 --- a/torchaudio/csrc/sox/stub.cpp +++ b/torchaudio/csrc/sox/stub.cpp @@ -6,43 +6,44 @@ namespace torchaudio::sox::detail { namespace { +LibSoxStub get_stub(at::DynamicLibrary& handle) { + // Validate version: we only support 14.4.2 + auto fn = (const sox_version_info_t* (*)())handle.sym("sox_version_info"); + if (SOX_LIB_VERSION_CODE != fn()->version_code) { + auto ver = ((const char* (*)())handle.sym("sox_version"))(); + TORCH_CHECK(false, "Need libsox 14.4.2, but found ", ver); + } + +#define get_symbol(X) (decltype(LibSoxStub::X))handle.sym(#X) + return LibSoxStub { + get_symbol(sox_add_effect), + get_symbol(sox_close), + get_symbol(sox_create_effect), + get_symbol(sox_create_effects_chain), + get_symbol(sox_delete_effect), + get_symbol(sox_delete_effects_chain), + get_symbol(sox_effect_options), + get_symbol(sox_find_effect), + get_symbol(sox_flow_effects), + get_symbol(sox_get_effect_fns), + get_symbol(sox_get_format_fns), + get_symbol(sox_get_globals), + get_symbol(sox_open_read), + get_symbol(sox_open_write), + get_symbol(sox_strerror), + get_symbol(sox_write) + }; +#undef get_symbol +} + // Handle to the dlopen-ed libsox class StubImpl { at::DynamicLibrary handle; public: - LibSoxStub stub{}; - - StubImpl(const char* name) : handle(name) { - // check version: we only support 14.4.2 - { - auto f = (const sox_version_info_t* (*)())handle.sym("sox_version_info"); - if (SOX_LIB_VERSION_CODE != f()->version_code) { - auto ver = ((const char* (*)())handle.sym("sox_version"))(); - TORCH_CHECK(false, "Need libsox 14.4.2, but found ", ver); - } - } - - // Register fanction pointers on public-facing interface -#define set(X) stub.X = (decltype(LibSoxStub::X))handle.sym(#X) - set(sox_add_effect); - set(sox_close); - set(sox_create_effect); - set(sox_create_effects_chain); - set(sox_delete_effect); - set(sox_delete_effects_chain); - set(sox_effect_options); - set(sox_find_effect); - set(sox_flow_effects); - set(sox_get_effect_fns); - set(sox_get_format_fns); - set(sox_get_globals); - set(sox_open_read); - set(sox_open_write); - set(sox_strerror); - set(sox_write); -#undef set + const LibSoxStub stub; + StubImpl(const char* name) : handle(name), stub(get_stub(handle)) { // Global config auto config = stub.sox_get_globals(); config->verbosity = 0; @@ -69,8 +70,8 @@ class StubImpl { #else #define EXT "so" #endif -LibSoxStub& libsox_stub() { - static StubImpl s{"libsox." EXT}; +const LibSoxStub& libsox_stub() { + static const StubImpl s{"libsox." EXT}; return s.stub; } #undef EXT diff --git a/torchaudio/csrc/sox/stub.h b/torchaudio/csrc/sox/stub.h index 9e1517d1348..d2b3f70e64a 100644 --- a/torchaudio/csrc/sox/stub.h +++ b/torchaudio/csrc/sox/stub.h @@ -60,7 +60,7 @@ struct LibSoxStub { size_t (*sox_write)(sox_format_t* ft, const sox_sample_t* buf, size_t len); }; -LibSoxStub& libsox_stub(); +const LibSoxStub& libsox_stub(); } // namespace torchaudio::sox