Skip to content

Commit

Permalink
register cpc teardown functions for std::atexit
Browse files Browse the repository at this point in the history
  • Loading branch information
jmalkin committed Dec 21, 2023
1 parent 04346bb commit 59bc5fb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions 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 @@ -110,6 +114,8 @@ class cpc_compressor {

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

~cpc_compressor();

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

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

Expand All @@ -35,10 +36,21 @@ namespace datasketches {
// construct on first use
template<typename A>
cpc_compressor<A>& get_compressor() {
static bool do_init = true;
static cpc_compressor<A>* instance = new cpc_compressor<A>(); // use new for global initialization
if (do_init) {
std::atexit(destroy_compressor<A>); // just to clean up a little more nicely; don't worry if it fails
do_init = false;
}
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 59bc5fb

Please sign in to comment.