|
15 | 15 | #include <OpenImageIO/unordered_map_concurrent.h> |
16 | 16 | #include <OpenImageIO/ustring.h> |
17 | 17 |
|
| 18 | + |
| 19 | + |
| 20 | +OIIO_NAMESPACE_BEGIN |
| 21 | +namespace pvt { |
| 22 | + |
| 23 | +// If nonzero, the ustring table will be freed at process exit. This is off by |
| 24 | +// default because cleanup is unnecessary (the OS reclaims the memory) and can |
| 25 | +// add measurable time at exit for large tables. Enable it when using valgrind |
| 26 | +// or other leak detectors to suppress false positives. Settable via |
| 27 | +// OIIO::attribute("ustring:cleanup",1) or the OIIO_USTRING_CLEANUP |
| 28 | +// environment variable. |
| 29 | +OIIO_UTIL_API int oiio_ustring_cleanup = Strutil::stoi( |
| 30 | + Sysutil::getenv("OIIO_USTRING_CLEANUP")); |
| 31 | + |
| 32 | +} // namespace pvt |
| 33 | +OIIO_NAMESPACE_END |
| 34 | + |
| 35 | + |
18 | 36 | OIIO_NAMESPACE_3_1_BEGIN |
19 | 37 |
|
20 | 38 | // Use rw spin locks |
@@ -47,26 +65,21 @@ template<unsigned BASE_CAPACITY, unsigned POOL_SIZE> struct TableRepMap { |
47 | 65 | } |
48 | 66 |
|
49 | 67 | ~TableRepMap() |
50 | | - { /* just let memory leak */ |
51 | | - } |
52 | | - |
53 | | - // Free all allocated resources. Note that this leaves the TableRepMap |
54 | | - // in an unusable state. |
55 | | - void free_resources() |
56 | 68 | { |
57 | | - ustring_write_lock_t lock(mutex); |
58 | | - // Destroy TableRep objects FIRST, while the pool/large-alloc memory |
59 | | - // they live in is still valid. Their destructors may read/write the |
60 | | - // surrounding allocation and (on some platforms) the std::string |
61 | | - // subobject owns a separate heap buffer that only gets freed here. |
62 | | - destroy_entries(); |
63 | | - entries.clear(); |
64 | | - entries.shrink_to_fit(); |
65 | | - // Now safe to free the backing buffers. |
66 | | - all_pools.clear(); |
67 | | - all_pools.shrink_to_fit(); |
68 | | - large_allocs.clear(); |
69 | | - large_allocs.shrink_to_fit(); |
| 69 | + if (OIIO::pvt::oiio_ustring_cleanup) { |
| 70 | + // If requested, take the time to properly destroy all the |
| 71 | + // entries, and also the unique_ptr arrays all_pools and |
| 72 | + // large_allocs will naturally free their contents after this |
| 73 | + // destructor body ends. |
| 74 | + destroy_entries(); |
| 75 | + } else { |
| 76 | + // If no ustring cleanup was requested, take the fastest possible |
| 77 | + // route to exit, just release the pointers and let them leak! |
| 78 | + for (auto& p : all_pools) |
| 79 | + (void)p.release(); |
| 80 | + for (auto& p : large_allocs) |
| 81 | + (void)p.release(); |
| 82 | + } |
70 | 83 | } |
71 | 84 |
|
72 | 85 | size_t get_memory_usage() |
@@ -309,12 +322,6 @@ struct UstringTable { |
309 | 322 | return num; |
310 | 323 | } |
311 | 324 |
|
312 | | - void free_resources() |
313 | | - { |
314 | | - for (auto& bin : bins) |
315 | | - bin.free_resources(); |
316 | | - } |
317 | | - |
318 | 325 | # ifdef USTRING_TRACK_NUM_LOOKUPS |
319 | 326 | size_t get_num_lookups() |
320 | 327 | { |
@@ -744,32 +751,3 @@ ustring::memory() |
744 | 751 | } |
745 | 752 |
|
746 | 753 | OIIO_NAMESPACE_3_1_END |
747 | | - |
748 | | - |
749 | | -OIIO_NAMESPACE_BEGIN |
750 | | -namespace pvt { |
751 | | - |
752 | | -// If nonzero, the ustring table will be freed at process exit. This is off by |
753 | | -// default because cleanup is unnecessary (the OS reclaims the memory) and can |
754 | | -// add measurable time at exit for large tables. Enable it when using valgrind |
755 | | -// or other leak detectors to suppress false positives. Settable via |
756 | | -// OIIO::attribute("ustring:cleanup",1) or the OIIO_USTRING_CLEANUP |
757 | | -// environment variable. |
758 | | -OIIO_UTIL_API int oiio_ustring_cleanup = Strutil::stoi( |
759 | | - Sysutil::getenv("OIIO_USTRING_CLEANUP")); |
760 | | - |
761 | | -// Register an atexit handler once at startup. The handler checks the flag at |
762 | | -// exit time, so it covers both the env-var path (flag set here) and the |
763 | | -// OIIO::attribute() path (flag set later at runtime). |
764 | | -static int ustring_cleanup_atexit_registered = []() { |
765 | | - std::atexit([]() { |
766 | | - if (pvt::oiio_ustring_cleanup) { |
767 | | - v3_1::ustring_table().free_resources(); |
768 | | - v3_1::reverse_map().clear(); |
769 | | - } |
770 | | - }); |
771 | | - return 0; |
772 | | -}(); |
773 | | - |
774 | | -} // namespace pvt |
775 | | -OIIO_NAMESPACE_END |
0 commit comments