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"
class GoogleInitializer {
public:
typedef void (*VoidFunction)(void);
GoogleInitializer(const char* name, VoidFunction ctor, VoidFunction dtor)
: name_(name), destructor_(dtor) {
RAW_VLOG(10, "<GoogleModuleObject> constructing: %s\n", name_);
if (ctor)
ctor();
}
~GoogleInitializer() {
RAW_VLOG(10, "<GoogleModuleObject> destroying: %s\n", name_);
if (destructor_)
destructor_();
}
public:
typedef void (*VoidFunction)(void);
private:
const char* const name_;
const VoidFunction destructor_;
GoogleInitializer(const char *name, VoidFunction ctor, VoidFunction dtor) : name_(name), destructor_(dtor)
{
RAW_VLOG(10, "<GoogleModuleObject> constructing: %s\n", name_);
if (ctor) ctor();
}
~GoogleInitializer()
{
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_;
};
#define REGISTER_MODULE_INITIALIZER(name, body) \
namespace { \
static void google_init_module_##name () { body; } \
GoogleInitializer google_initializer_module_##name(#name, \
google_init_module_##name, NULL); \
}
#define REGISTER_MODULE_DESTRUCTOR(name, body) \
namespace { \
static void google_destruct_module_##name () { body; } \
GoogleInitializer google_destructor_module_##name(#name, \
NULL, google_destruct_module_##name); \
}
#define REGISTER_MODULE_INITIALIZER(name, body) \
namespace { \
static void google_init_module_##name() __attribute__((constructor)) { body; } \
GoogleInitializer google_initializer_module_##name(#name, google_init_module_##name, NULL); \
}
#define REGISTER_MODULE_DESTRUCTOR(name, body) \
namespace { \
static void google_destruct_module_##name() __attribute__((constructor)) { body; } \
GoogleInitializer google_destructor_module_##name(#name, NULL, google_destruct_module_##name); \
}
#endif /* _GOOGLEINIT_H */

View File

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