xinyiZzz commented on code in PR #9301:
URL: https://github.com/apache/incubator-doris/pull/9301#discussion_r863565901
##########
be/src/runtime/tcmalloc_hook.h:
##########
@@ -23,32 +23,40 @@
#include "runtime/thread_context.h"
-// Notice: modify the command in New/Delete Hook should be careful enough!,
-// and should be as simple as possible, otherwise it may cause weird errors.
E.g:
-// 1. The first New Hook call of the process may be before some variables of
-// the process are initialized.
-// 2. Allocating memory in the Hook command causes the Hook to be entered
again,
-// infinite recursion.
-// 3. TCMalloc hook will be triggered during the process of
initializing/Destructor
-// memtracker shared_ptr, Using the object pointed to by this memtracker
shared_ptr
-// in TCMalloc hook may cause crash.
-// 4. Modifying additional thread local variables in ThreadContext
construction and
-// destructor to control the behavior of consume can lead to unexpected
behavior,
-// like this: if (LIKELY(doris::start_thread_mem_tracker)) {
-void new_hook(const void* ptr, size_t size) {
- doris::tls_ctx()->consume_mem(tc_nallocx(size, 0));
-}
-
-void delete_hook(const void* ptr) {
- doris::tls_ctx()->release_mem(tc_malloc_size(const_cast<void*>(ptr)));
-}
-
-void init_hook() {
- MallocHook::AddNewHook(&new_hook);
- MallocHook::AddDeleteHook(&delete_hook);
-}
-
-void destroy_hook() {
- MallocHook::RemoveNewHook(&new_hook);
- MallocHook::RemoveDeleteHook(&delete_hook);
-}
+namespace doris {
+
+class TcmallocHook {
+public:
+ // Notice: modify the command in New/Delete Hook should be careful enough!,
+ // and should be as simple as possible, otherwise it may cause weird
errors. E.g:
+ // 1. The first New Hook call of the process may be before some variables
of
+ // the process are initialized.
+ // 2. Allocating memory in the Hook command causes the Hook to be entered
again,
+ // infinite recursion.
+ // 3. TCMalloc hook will be triggered during the process of
initializing/Destructor
+ // memtracker shared_ptr, Using the object pointed to by this memtracker
shared_ptr
+ // in TCMalloc hook may cause crash.
+ // 4. Modifying additional thread local variables in ThreadContext
construction and
+ // destructor to control the behavior of consume can lead to unexpected
behavior,
+ // like this: if (LIKELY(doris::start_thread_mem_tracker)) {
+ static void new_hook(const void* ptr, size_t size) {
Review Comment:
In order to avoid function redefinition in multiple includes, the function
is moved to the `TcmallocHook` class.
I will recover before. I thought about it, and there will be no redefinition
for the time being.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]