Skip to content

Commit b11534f

Browse files
committed
add valgrind support in base allocator
1 parent cde4cb8 commit b11534f

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/base_alloc/base_alloc_global.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,24 @@ void *umf_ba_global_aligned_alloc(size_t size, size_t alignment) {
186186

187187
int ac_index = size_to_idx(size);
188188
if (ac_index >= NUM_ALLOCATION_CLASSES) {
189-
return add_metadata_and_align(ba_os_alloc(size), size, alignment);
189+
void *ptr = ba_os_alloc(size);
190+
if (!ptr) {
191+
return NULL;
192+
}
193+
VALGRIND_DO_MALLOCLIKE_BLOCK(ptr, size, 0, 0);
194+
return add_metadata_and_align(ptr, size, alignment);
190195
}
191196

192197
if (!BASE_ALLOC.ac[ac_index]) {
193198
// if creating ac failed, fall back to os allocation
194199
LOG_WARN("base_alloc: allocation class not created. Falling "
195200
"back to OS memory allocation.");
196-
return add_metadata_and_align(ba_os_alloc(size), size, alignment);
201+
void *ptr = ba_os_alloc(size);
202+
if (!ptr) {
203+
return NULL;
204+
}
205+
VALGRIND_DO_MALLOCLIKE_BLOCK(ptr, size, 0, 0);
206+
return add_metadata_and_align(ptr, size, alignment);
197207
}
198208

199209
return add_metadata_and_align(umf_ba_alloc(BASE_ALLOC.ac[ac_index]), size,
@@ -220,12 +230,14 @@ void umf_ba_global_free(void *ptr) {
220230

221231
int ac_index = size_to_idx(total_size);
222232
if (ac_index >= NUM_ALLOCATION_CLASSES) {
233+
VALGRIND_DO_FREELIKE_BLOCK(ptr, 0);
223234
ba_os_free(ptr, total_size);
224235
return;
225236
}
226237

227238
if (!BASE_ALLOC.ac[ac_index]) {
228239
// if creating ac failed, memory must have been allocated by os
240+
VALGRIND_DO_FREELIKE_BLOCK(ptr, 0);
229241
ba_os_free(ptr, total_size);
230242
return;
231243
}

src/base_alloc/base_alloc_linear.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2024 Intel Corporation
2+
* Copyright (C) 2024-2025 Intel Corporation
33
*
44
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
55
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
@@ -13,6 +13,7 @@
1313
#include "utils_common.h"
1414
#include "utils_concurrency.h"
1515
#include "utils_log.h"
16+
#include "utils_sanitizers.h"
1617

1718
#ifndef NDEBUG
1819
#define _DEBUG_EXECUTE(expression) DO_WHILE_EXPRS(expression)
@@ -184,6 +185,8 @@ void *umf_ba_linear_alloc(umf_ba_linear_pool_t *pool, size_t size) {
184185
}
185186
_DEBUG_EXECUTE(pool->metadata.global_n_allocs++);
186187
_DEBUG_EXECUTE(ba_debug_checks(pool));
188+
VALGRIND_DO_MALLOCLIKE_BLOCK(ptr, aligned_size, 0, 0);
189+
utils_annotate_memory_undefined(ptr, aligned_size);
187190
utils_mutex_unlock(&pool->metadata.lock);
188191

189192
return ptr;
@@ -206,6 +209,7 @@ int umf_ba_linear_free(umf_ba_linear_pool_t *pool, void *ptr) {
206209
if (pool_contains_ptr(pool, pool->metadata.pool_size, pool->data, ptr)) {
207210
pool->metadata.pool_n_allocs--;
208211
_DEBUG_EXECUTE(pool->metadata.global_n_allocs--);
212+
VALGRIND_DO_FREELIKE_BLOCK(ptr, 0);
209213
size_t page_size = ba_os_get_page_size();
210214
if ((pool->metadata.pool_n_allocs == 0) && pool->next_pool &&
211215
(pool->metadata.pool_size > page_size)) {
@@ -241,6 +245,7 @@ int umf_ba_linear_free(umf_ba_linear_pool_t *pool, void *ptr) {
241245
}
242246
_DEBUG_EXECUTE(ba_debug_checks(pool));
243247
utils_mutex_unlock(&pool->metadata.lock);
248+
VALGRIND_DO_FREELIKE_BLOCK(ptr, 0);
244249
return 0;
245250
}
246251
prev_pool = next_pool;

0 commit comments

Comments
 (0)