Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/base_alloc/base_alloc_global.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,24 @@ void *umf_ba_global_aligned_alloc(size_t size, size_t alignment) {

int ac_index = size_to_idx(size);
if (ac_index >= NUM_ALLOCATION_CLASSES) {
return add_metadata_and_align(ba_os_alloc(size), size, alignment);
void *ptr = ba_os_alloc(size);
if (!ptr) {
return NULL;
}
VALGRIND_DO_MALLOCLIKE_BLOCK(ptr, size, 0, 0);
return add_metadata_and_align(ptr, size, alignment);
Comment thread
ldorau marked this conversation as resolved.
}

if (!BASE_ALLOC.ac[ac_index]) {
// if creating ac failed, fall back to os allocation
LOG_WARN("base_alloc: allocation class not created. Falling "
"back to OS memory allocation.");
return add_metadata_and_align(ba_os_alloc(size), size, alignment);
void *ptr = ba_os_alloc(size);
if (!ptr) {
return NULL;
}
VALGRIND_DO_MALLOCLIKE_BLOCK(ptr, size, 0, 0);
return add_metadata_and_align(ptr, size, alignment);
Comment thread
ldorau marked this conversation as resolved.
}

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

int ac_index = size_to_idx(total_size);
if (ac_index >= NUM_ALLOCATION_CLASSES) {
VALGRIND_DO_FREELIKE_BLOCK(ptr, 0);
ba_os_free(ptr, total_size);
return;
}

if (!BASE_ALLOC.ac[ac_index]) {
// if creating ac failed, memory must have been allocated by os
VALGRIND_DO_FREELIKE_BLOCK(ptr, 0);
ba_os_free(ptr, total_size);
return;
}
Expand Down
6 changes: 5 additions & 1 deletion src/base_alloc/base_alloc_linear.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2024 Intel Corporation
* Copyright (C) 2024-2025 Intel Corporation
*
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Expand All @@ -13,6 +13,7 @@
#include "utils_common.h"
#include "utils_concurrency.h"
#include "utils_log.h"
#include "utils_sanitizers.h"

#ifndef NDEBUG
#define _DEBUG_EXECUTE(expression) DO_WHILE_EXPRS(expression)
Expand Down Expand Up @@ -184,6 +185,7 @@ void *umf_ba_linear_alloc(umf_ba_linear_pool_t *pool, size_t size) {
}
_DEBUG_EXECUTE(pool->metadata.global_n_allocs++);
_DEBUG_EXECUTE(ba_debug_checks(pool));
VALGRIND_DO_MALLOCLIKE_BLOCK(ptr, aligned_size, 0, 0);
utils_mutex_unlock(&pool->metadata.lock);

return ptr;
Expand All @@ -206,6 +208,7 @@ int umf_ba_linear_free(umf_ba_linear_pool_t *pool, void *ptr) {
if (pool_contains_ptr(pool, pool->metadata.pool_size, pool->data, ptr)) {
pool->metadata.pool_n_allocs--;
_DEBUG_EXECUTE(pool->metadata.global_n_allocs--);
VALGRIND_DO_FREELIKE_BLOCK(ptr, 0);
Comment thread
bratpiorka marked this conversation as resolved.
size_t page_size = ba_os_get_page_size();
if ((pool->metadata.pool_n_allocs == 0) && pool->next_pool &&
(pool->metadata.pool_size > page_size)) {
Expand Down Expand Up @@ -241,6 +244,7 @@ int umf_ba_linear_free(umf_ba_linear_pool_t *pool, void *ptr) {
}
_DEBUG_EXECUTE(ba_debug_checks(pool));
utils_mutex_unlock(&pool->metadata.lock);
VALGRIND_DO_FREELIKE_BLOCK(ptr, 0);
Comment thread
bratpiorka marked this conversation as resolved.
return 0;
}
prev_pool = next_pool;
Expand Down
1 change: 1 addition & 0 deletions src/pool/pool_disjoint.c
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,7 @@ umf_result_t disjoint_pool_finalize(void *pool) {
destroy_bucket(hPool->buckets[i]);
}

umf_ba_global_free(hPool->buckets);
VALGRIND_DO_DESTROY_MEMPOOL(hPool);
ret = umfDisjointPoolSharedLimitsDestroy(hPool->default_shared_limits);
if (ret != UMF_RESULT_SUCCESS) {
Expand Down
35 changes: 0 additions & 35 deletions test/coarse_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,6 @@ static umf_result_t alloc_cb_fails(void *provider, size_t size,
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
}

static umf_result_t free_cb_fails(void *provider, void *ptr, size_t size) {
(void)provider; //unused
(void)ptr; //unused
(void)size; //unused
return UMF_RESULT_ERROR_USER_SPECIFIC;
}

static umf_result_t split_cb_fails(void *provider, void *ptr, size_t totalSize,
size_t firstSize) {
(void)provider; //unused
Expand Down Expand Up @@ -615,34 +608,6 @@ TEST_P(CoarseWithMemoryStrategyTest, coarseTest_basic_alloc_cb_fails) {
umfMemoryProviderDestroy(malloc_memory_provider);
}

TEST_P(CoarseWithMemoryStrategyTest, coarseTest_basic_free_cb_fails) {
umf_memory_provider_handle_t malloc_memory_provider;
umf_result = umfMemoryProviderCreate(&UMF_MALLOC_MEMORY_PROVIDER_OPS, NULL,
&malloc_memory_provider);
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
ASSERT_NE(malloc_memory_provider, nullptr);

coarse_params.provider = malloc_memory_provider;
coarse_params.cb.free = free_cb_fails;

umf_result = coarse_new(&coarse_params, &coarse_handle);
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
ASSERT_NE(coarse_handle, nullptr);

coarse_t *ch = coarse_handle;
const size_t alloc_size = 20 * MB;

umf_result = coarse_add_memory_from_provider(ch, alloc_size);
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);

ASSERT_EQ(coarse_get_stats(ch).used_size, 0 * MB);
ASSERT_EQ(coarse_get_stats(ch).alloc_size, alloc_size);
ASSERT_EQ(coarse_get_stats(ch).num_all_blocks, (size_t)1);

coarse_delete(ch);
umfMemoryProviderDestroy(malloc_memory_provider);
}

TEST_P(CoarseWithMemoryStrategyTest, coarseTest_split_cb_fails) {
if (coarse_params.allocation_strategy ==
UMF_COARSE_MEMORY_STRATEGY_FASTEST) {
Expand Down
Loading