feat add __attribute__((construcotr))

This commit is contained in:
tqcq 2024-04-29 21:45:37 +08:00
parent fc1ad57bbb
commit 563a1bfcfe
2 changed files with 36 additions and 37 deletions

View File

@ -37,38 +37,38 @@
#include "base/logging.h" #include "base/logging.h"
class GoogleInitializer { class GoogleInitializer {
public: public:
typedef void (*VoidFunction)(void); typedef void (*VoidFunction)(void);
GoogleInitializer(const char* name, VoidFunction ctor, VoidFunction dtor)
: name_(name), destructor_(dtor) { GoogleInitializer(const char *name, VoidFunction ctor, VoidFunction dtor) : name_(name), destructor_(dtor)
{
RAW_VLOG(10, "<GoogleModuleObject> constructing: %s\n", name_); RAW_VLOG(10, "<GoogleModuleObject> constructing: %s\n", name_);
if (ctor) if (ctor) ctor();
ctor();
}
~GoogleInitializer() {
RAW_VLOG(10, "<GoogleModuleObject> destroying: %s\n", name_);
if (destructor_)
destructor_();
} }
private: ~GoogleInitializer()
const char* const name_; {
RAW_VLOG(10, "<GoogleModuleObject> destroying: %s\n", name_);
if (destructor_) destructor_();
}
const char *name() const { return name_; }
private:
const char *const name_;
const VoidFunction destructor_; const VoidFunction destructor_;
}; };
#define REGISTER_MODULE_INITIALIZER(name, body) \ #define REGISTER_MODULE_INITIALIZER(name, body) \
namespace { \ namespace { \
static void google_init_module_##name () { body; } \ static void google_init_module_##name() __attribute__((constructor)) { body; } \
GoogleInitializer google_initializer_module_##name(#name, \ GoogleInitializer google_initializer_module_##name(#name, google_init_module_##name, NULL); \
google_init_module_##name, NULL); \
} }
#define REGISTER_MODULE_DESTRUCTOR(name, body) \ #define REGISTER_MODULE_DESTRUCTOR(name, body) \
namespace { \ namespace { \
static void google_destruct_module_##name () { body; } \ static void google_destruct_module_##name() __attribute__((constructor)) { body; } \
GoogleInitializer google_destructor_module_##name(#name, \ GoogleInitializer google_destructor_module_##name(#name, NULL, google_destruct_module_##name); \
NULL, google_destruct_module_##name); \
} }
#endif /* _GOOGLEINIT_H */ #endif /* _GOOGLEINIT_H */

View File

@ -30,9 +30,11 @@ target_include_directories(test_main PUBLIC src/)
target_include_directories(benchmark_main PUBLIC src/) target_include_directories(benchmark_main PUBLIC src/)
add_library(sled STATIC "") add_library(sled STATIC "")
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
add_subdirectory(3party/gperftools EXCLUDE_FROM_ALL)
endif()
add_subdirectory(3party/minilua EXCLUDE_FROM_ALL) add_subdirectory(3party/minilua EXCLUDE_FROM_ALL)
add_subdirectory(3party/gperftools EXCLUDE_FROM_ALL)
add_subdirectory(3party/asyncplusplus EXCLUDE_FROM_ALL) add_subdirectory(3party/asyncplusplus EXCLUDE_FROM_ALL)
# add_subdirectory(3party/cppuprofile EXCLUDE_FROM_ALL) # add_subdirectory(3party/cppuprofile EXCLUDE_FROM_ALL)
if(SLED_WITH_PROTOBUF) if(SLED_WITH_PROTOBUF)
@ -104,15 +106,12 @@ target_sources(
target_link_libraries( target_link_libraries(
sled sled
PUBLIC rpc_core PUBLIC rpc_core fmt marl Async++ minilua protobuf::libprotobuf
fmt
marl
Async++
minilua
protobuf::libprotobuf
tcmalloc_and_profiler_static
# protobuf::libprotoc # protobuf::libprotoc
PRIVATE dl) PRIVATE dl)
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
target_link_libraries(sled PRIVATE tcmalloc_and_profiler_static)
endif()
if(SLED_WITH_PROTOBUF) if(SLED_WITH_PROTOBUF)
target_link_libraries(sled PUBLIC protobuf::libprotobuf) target_link_libraries(sled PUBLIC protobuf::libprotobuf)
endif() endif()