Skip to content

Commit 1b2b04f

Browse files
author
elsa
committed
fix: memory management for thread local variable
* include/datadog/tls_storage.h * include/datadog/tracer.h * src/datadog/tracer.cpp
1 parent fe712b5 commit 1b2b04f

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

include/datadog/tls_storage.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
#include <array>
66
#include <cstdint>
77

8+
// Global struct used to exposed thread-specific information.
9+
// https://github.com/elastic/apm/blob/149cd3e39a77a58002344270ed2ad35357bdd02d/specs/agents/universal-profiling-integration.md#thread-local-storage-layout
10+
811
namespace datadog {
912
namespace tracing {
1013
struct __attribute__((packed)) TLSStorage {
@@ -17,5 +20,6 @@ struct __attribute__((packed)) TLSStorage {
1720
uint64_t span_id;
1821
uint64_t transaction_id;
1922
};
23+
2024
} // namespace tracing
2125
} // namespace datadog

include/datadog/tracer.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
// `tracer_config.h`.
1212

1313
#include <datadog/tls_storage.h>
14-
#include <threads.h>
1514

1615
#include <cstddef>
1716
#include <memory>

src/datadog/tracer.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
const void* elastic_apm_profiling_correlation_process_storage_v1 = nullptr;
3434
thread_local struct datadog::tracing::TLSStorage*
3535
elastic_apm_profiling_correlation_tls_v1 = nullptr;
36+
thread_local std::unique_ptr<datadog::tracing::TLSStorage> tls_info_holder =
37+
nullptr;
3638

3739
namespace datadog {
3840
namespace tracing {
@@ -118,11 +120,9 @@ Tracer::Tracer(const FinalizedTracerConfig& config,
118120
}
119121

120122
void Tracer::correlate(const Span& span) {
121-
// TODO: update this variablle with data
122-
// See Layout:
123-
// https://github.com/elastic/apm/blob/149cd3e39a77a58002344270ed2ad35357bdd02d/specs/agents/universal-profiling-integration.md#thread-local-storage-layout
124-
auto tls_storage_ptr = std::make_unique<struct TLSStorage>();
125-
elastic_apm_profiling_correlation_tls_v1 = tls_storage_ptr.get();
123+
tls_info_holder = std::make_unique<datadog::tracing::TLSStorage>();
124+
elastic_apm_profiling_correlation_tls_v1 = tls_info_holder.get();
125+
126126
struct TLSStorage* tls_data = elastic_apm_profiling_correlation_tls_v1;
127127
tls_data->valid = 0;
128128

0 commit comments

Comments
 (0)