Files
test/intern/guardedalloc/intern/mallocn_intern_function_pointers.hh
Bastien Montagne 63016ad965 MEM management: Add data storage only destructed after memleak detection.
Add a new API to store data that is guaranteed to not be freed
before the memleak detector has run.

This will be used in next commit by the readfile code to improve
reporting on leaks from blendfile readingi process.

This is done by a two-layer approach:

A new templated `MEM_construct_leak_detection_data` allows to
create any type of data. Its ownership and lifetime are handled
internally, and guaranteed to not be destroyed before the memleak
detector has run.

Add a new template-based 'allocation string storage' system to
`intern/memutil`. This uses the new `Guardedalloc Persistent Storage`
system to store all 'complex' allocation messages, that cannot be
defined as literals.

Internally, the storage is done through an owning reference (a
`shared_ptr`) of the created data into a mutex-protected static
vector.

`MEM_init_memleak_detection` code ensures that this static storage
is created before the memleak detection data, so that it is destructed
after the memleak detector has ran.

The main container (`AllocStringStorageContainer`) is wrapping a
map of `{string -> AllocStringStorage<key_type, hash_type>}`.
The key is a storage identifier.

Each storage is also a map wrapped into a simple templated API
class (`AllocStringStorage`), where the values are the alloc strings,
and the keys type is defined by the user code.

Pull Request: https://projects.blender.org/blender/blender/pulls/125320
2024-07-29 11:47:04 +02:00

47 lines
1.7 KiB
C++

/* SPDX-FileCopyrightText: 2024 Blender Authors
*
* SPDX-License-Identifier: GPL-2.0-or-later */
/** \file
* \ingroup intern_mem
*/
#pragma once
namespace mem_guarded::internal {
enum class AllocationType {
/** Allocation is handled through 'C type' alloc/free calls. */
ALLOC_FREE,
/** Allocation is handled through 'C++ type' new/delete calls. */
NEW_DELETE,
};
/** Internal implementation of #MEM_freeN, exposed because #MEM_delete needs access to it. */
extern void (*mem_freeN_ex)(void *vmemh, AllocationType allocation_type);
/** Internal implementation of #MEM_mallocN_aligned, exposed because #MEM_new needs access to it.
*/
extern void *(*mem_mallocN_aligned_ex)(size_t len,
size_t alignment,
const char *str,
AllocationType allocation_type);
/**
* Store a std::any into a static opaque storage vector. The only purpose of this call is to
* control the lifetime of the given data, there is no way to access it from here afterwards. User
* code is expected to keep its own reference to the data contained in the `std::any` as long as it
* needs it.
*
* Typically, this `any` should contain a `shared_ptr` to the actual data, to ensure that the data
* itself is not duplicated, and that the static storage does become an owner of it.
*
* That way, the memleak data does not get destructed before the static storage is. Since this
* storage is created before the memleak detection data (see the implementation of
* #MEM_init_memleak_detection), it is guaranteed to happen after the execution and destruction of
* the memleak detector.
*/
void add_memleak_data(std::any data);
} // namespace mem_guarded::internal