Selaa lähdekoodia

Use static storage instead of heap for forever-objects (squelches ASan)

rdb 5 päivää sitten
vanhempi
sitoutus
4b98910789

+ 2 - 1
panda/src/pgraph/renderAttrib.cxx

@@ -478,7 +478,8 @@ init_attribs() {
   // _attribs_lock without a startup race condition.  For the meantime, this
   // is OK because we guarantee that this method is called at static init
   // time, presumably when there is still only one thread in the world.
-  _attribs_lock = new LightReMutex("RenderAttrib::_attribs_lock");
+  alignas(LightReMutex) static char storage[sizeof(LightReMutex)];
+  _attribs_lock = new (storage) LightReMutex("RenderAttrib::_attribs_lock");
   nassertv(Thread::get_current_thread() == Thread::get_main_thread());
 }
 

+ 4 - 2
panda/src/pgraph/renderState.cxx

@@ -1841,13 +1841,15 @@ init_states() {
   // _states_lock without a startup race condition.  For the meantime, this is
   // OK because we guarantee that this method is called at static init time,
   // presumably when there is still only one thread in the world.
-  _states_lock = new LightReMutex("RenderState::_states_lock");
+  alignas(LightReMutex) static char storage[sizeof(LightReMutex)];
+  _states_lock = new (storage) LightReMutex("RenderState::_states_lock");
   _cache_stats.init();
   nassertv(Thread::get_current_thread() == Thread::get_main_thread());
 
   // Initialize the empty state object as well.  It is used so often that it
   // is declared globally, and lives forever.
-  RenderState *state = new RenderState;
+  alignas(RenderState) static char state_storage[sizeof(RenderState)];
+  RenderState *state = new (state_storage) RenderState;
   state->local_object();
   state->cache_ref_only();
   state->_saved_entry = _states.store(state, nullptr);

+ 2 - 1
panda/src/pgraph/transformState.cxx

@@ -1387,7 +1387,8 @@ init_states() {
   // _states_lock without a startup race condition.  For the meantime, this is
   // OK because we guarantee that this method is called at static init time,
   // presumably when there is still only one thread in the world.
-  _states_lock = new LightReMutex("TransformState::_states_lock");
+  alignas(LightReMutex) static char storage[sizeof(LightReMutex)];
+  _states_lock = new (storage) LightReMutex("TransformState::_states_lock");
   _cache_stats.init();
   nassertv(Thread::get_current_thread() == Thread::get_main_thread());
 

+ 12 - 2
panda/src/pstatclient/pStatClient.cxx

@@ -1116,9 +1116,19 @@ add_collector(PStatClient::Collector *collector) {
     // We need to grow the array.  We have to be careful here, because there
     // might be clients accessing the array right now who are not protected by
     // the lock.
-    size_t new_collectors_size = (_collectors_size == 0) ? 128 : _collectors_size * 2;
     CollectorPointer *old_collectors = _collectors.load(std::memory_order_relaxed);
-    CollectorPointer *new_collectors = new CollectorPointer[new_collectors_size];
+    CollectorPointer *new_collectors;
+
+    static const size_t initial_collectors_size = 256;
+    static CollectorPointer initial_collectors[initial_collectors_size];
+    size_t new_collectors_size;
+    if (_collectors_size == 0) {
+      new_collectors_size = initial_collectors_size;
+      new_collectors = initial_collectors;
+    } else {
+      new_collectors_size = _collectors_size * 2;
+      new_collectors = new CollectorPointer[new_collectors_size];
+    }
     if (old_collectors != nullptr) {
       memcpy(new_collectors, old_collectors, num_collectors * sizeof(CollectorPointer));
     }