2023-01-16 14:10:31 +00:00
|
|
|
#pragma once
|
|
|
|
#include <cstddef>
|
2023-01-16 15:48:44 +00:00
|
|
|
#include <base/defines.h>
|
2023-01-16 14:10:31 +00:00
|
|
|
|
|
|
|
/// This is a structure which is returned by MemoryTracker.
|
|
|
|
/// Methods onAlloc/onFree should be called after actual memory allocation if it succeed.
|
|
|
|
/// For now, it will only collect allocation trace with sample_probability.
|
|
|
|
struct AllocationTrace
|
|
|
|
{
|
|
|
|
AllocationTrace() = default;
|
2023-01-17 14:44:42 +00:00
|
|
|
explicit AllocationTrace(double sample_probability_) : sample_probability(sample_probability_) {}
|
2023-01-16 14:10:31 +00:00
|
|
|
|
2023-01-16 15:48:44 +00:00
|
|
|
ALWAYS_INLINE void onAlloc(void * ptr, size_t size) const
|
|
|
|
{
|
2023-07-20 16:10:56 +00:00
|
|
|
if (likely(sample_probability <= 0))
|
2023-01-16 15:48:44 +00:00
|
|
|
return;
|
2023-01-16 14:10:31 +00:00
|
|
|
|
2023-01-16 15:48:44 +00:00
|
|
|
onAllocImpl(ptr, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE void onFree(void * ptr, size_t size) const
|
|
|
|
{
|
2023-07-20 16:10:56 +00:00
|
|
|
if (likely(sample_probability <= 0))
|
2023-01-16 15:48:44 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
onFreeImpl(ptr, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2023-01-16 14:10:31 +00:00
|
|
|
double sample_probability = 0;
|
2023-01-16 15:48:44 +00:00
|
|
|
|
|
|
|
void onAllocImpl(void * ptr, size_t size) const;
|
|
|
|
void onFreeImpl(void * ptr, size_t size) const;
|
2023-01-16 14:10:31 +00:00
|
|
|
};
|