Skip to content

Commit

Permalink
Merge pull request #412 from apache/cpc_teardown
Browse files Browse the repository at this point in the history
register cpc teardown functions for std::atexit
  • Loading branch information
jmalkin authored Dec 21, 2023
2 parents 04346bb + 7386601 commit f58c622
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
8 changes: 7 additions & 1 deletion cpc/include/cpc_compressor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ template<typename A> class cpc_compressor;
template<typename A>
inline cpc_compressor<A>& get_compressor();

// function called atexit to clean up compression tables
template<typename A>
void destroy_compressor();

template<typename A>
class cpc_compressor {
public:
Expand Down Expand Up @@ -109,8 +113,10 @@ class cpc_compressor {
};

cpc_compressor();
template<typename T> friend cpc_compressor<T>& get_compressor();
friend cpc_compressor& get_compressor<A>();

~cpc_compressor();
friend void destroy_compressor<A>();

void make_decoding_tables(); // call this at startup
void free_decoding_tables(); // call this at the end
Expand Down
10 changes: 10 additions & 0 deletions cpc/include/cpc_compressor_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
#ifndef CPC_COMPRESSOR_IMPL_HPP_
#define CPC_COMPRESSOR_IMPL_HPP_

#include <cstdlib>
#include <memory>
#include <stdexcept>

#include "common_defs.hpp"
#include "compression_data.hpp"
#include "cpc_util.hpp"
#include "cpc_common.hpp"
Expand All @@ -36,9 +38,17 @@ namespace datasketches {
template<typename A>
cpc_compressor<A>& get_compressor() {
static cpc_compressor<A>* instance = new cpc_compressor<A>(); // use new for global initialization
static int reg_result = std::atexit(destroy_compressor<A>); // just to clean up a little more nicely; don't worry if it fails
unused(reg_result);
return *instance;
}

// register to call compressor destructor at exit
template<typename A>
void destroy_compressor() {
delete std::addressof(get_compressor<A>());
}

template<typename A>
cpc_compressor<A>::cpc_compressor() {
make_decoding_tables();
Expand Down

0 comments on commit f58c622

Please sign in to comment.