Skip to content

Commit 9bdd5ec

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

File tree

4 files changed

+20
-38
lines changed

4 files changed

+20
-38
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: 5 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,7 @@ 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);
187189
utils_mutex_unlock(&pool->metadata.lock);
188190

189191
return ptr;
@@ -206,6 +208,7 @@ int umf_ba_linear_free(umf_ba_linear_pool_t *pool, void *ptr) {
206208
if (pool_contains_ptr(pool, pool->metadata.pool_size, pool->data, ptr)) {
207209
pool->metadata.pool_n_allocs--;
208210
_DEBUG_EXECUTE(pool->metadata.global_n_allocs--);
211+
VALGRIND_DO_FREELIKE_BLOCK(ptr, 0);
209212
size_t page_size = ba_os_get_page_size();
210213
if ((pool->metadata.pool_n_allocs == 0) && pool->next_pool &&
211214
(pool->metadata.pool_size > page_size)) {
@@ -241,6 +244,7 @@ int umf_ba_linear_free(umf_ba_linear_pool_t *pool, void *ptr) {
241244
}
242245
_DEBUG_EXECUTE(ba_debug_checks(pool));
243246
utils_mutex_unlock(&pool->metadata.lock);
247+
VALGRIND_DO_FREELIKE_BLOCK(ptr, 0);
244248
return 0;
245249
}
246250
prev_pool = next_pool;

src/pool/pool_disjoint.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,7 @@ umf_result_t disjoint_pool_finalize(void *pool) {
10121012
destroy_bucket(hPool->buckets[i]);
10131013
}
10141014

1015+
umf_ba_global_free(hPool->buckets);
10151016
VALGRIND_DO_DESTROY_MEMPOOL(hPool);
10161017
ret = umfDisjointPoolSharedLimitsDestroy(hPool->default_shared_limits);
10171018
if (ret != UMF_RESULT_SUCCESS) {

test/coarse_lib.cpp

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,6 @@ static umf_result_t alloc_cb_fails(void *provider, size_t size,
5757
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
5858
}
5959

60-
static umf_result_t free_cb_fails(void *provider, void *ptr, size_t size) {
61-
(void)provider; //unused
62-
(void)ptr; //unused
63-
(void)size; //unused
64-
return UMF_RESULT_ERROR_USER_SPECIFIC;
65-
}
66-
6760
static umf_result_t split_cb_fails(void *provider, void *ptr, size_t totalSize,
6861
size_t firstSize) {
6962
(void)provider; //unused
@@ -615,34 +608,6 @@ TEST_P(CoarseWithMemoryStrategyTest, coarseTest_basic_alloc_cb_fails) {
615608
umfMemoryProviderDestroy(malloc_memory_provider);
616609
}
617610

618-
TEST_P(CoarseWithMemoryStrategyTest, coarseTest_basic_free_cb_fails) {
619-
umf_memory_provider_handle_t malloc_memory_provider;
620-
umf_result = umfMemoryProviderCreate(&UMF_MALLOC_MEMORY_PROVIDER_OPS, NULL,
621-
&malloc_memory_provider);
622-
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
623-
ASSERT_NE(malloc_memory_provider, nullptr);
624-
625-
coarse_params.provider = malloc_memory_provider;
626-
coarse_params.cb.free = free_cb_fails;
627-
628-
umf_result = coarse_new(&coarse_params, &coarse_handle);
629-
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
630-
ASSERT_NE(coarse_handle, nullptr);
631-
632-
coarse_t *ch = coarse_handle;
633-
const size_t alloc_size = 20 * MB;
634-
635-
umf_result = coarse_add_memory_from_provider(ch, alloc_size);
636-
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
637-
638-
ASSERT_EQ(coarse_get_stats(ch).used_size, 0 * MB);
639-
ASSERT_EQ(coarse_get_stats(ch).alloc_size, alloc_size);
640-
ASSERT_EQ(coarse_get_stats(ch).num_all_blocks, (size_t)1);
641-
642-
coarse_delete(ch);
643-
umfMemoryProviderDestroy(malloc_memory_provider);
644-
}
645-
646611
TEST_P(CoarseWithMemoryStrategyTest, coarseTest_split_cb_fails) {
647612
if (coarse_params.allocation_strategy ==
648613
UMF_COARSE_MEMORY_STRATEGY_FASTEST) {

0 commit comments

Comments
 (0)