Skip to content

Commit 6f02c80

Browse files
authored
Merge pull request #1424 from bratpiorka/rrudnick_ops_tests
improve pool ops tests
2 parents 145b933 + c788f6d commit 6f02c80

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
lines changed

test/common/pool.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,25 @@ typedef struct pool_base_t {
122122
umf_result_t get_last_allocation_error() noexcept {
123123
return UMF_RESULT_SUCCESS;
124124
}
125+
umf_result_t get_name(const char **name) noexcept {
126+
if (name) {
127+
*name = "pool_base";
128+
}
129+
return UMF_RESULT_SUCCESS;
130+
}
125131
} pool_base_t;
126132

127133
struct malloc_pool : public pool_base_t {
128134
void *malloc(size_t size) noexcept { return ::malloc(size); }
135+
129136
void *calloc(size_t num, size_t size) noexcept {
130137
return ::calloc(num, size);
131138
}
139+
132140
void *realloc(void *ptr, size_t size) noexcept {
133141
return ::realloc(ptr, size);
134142
}
143+
135144
void *aligned_malloc(size_t size, size_t alignment) noexcept {
136145
#ifdef _WIN32
137146
(void)size; // unused
@@ -143,6 +152,7 @@ struct malloc_pool : public pool_base_t {
143152
return ::aligned_alloc(alignment, size);
144153
#endif
145154
}
155+
146156
umf_result_t malloc_usable_size(const void *ptr, size_t *size) noexcept {
147157
if (size) {
148158
#ifdef _WIN32
@@ -155,10 +165,18 @@ struct malloc_pool : public pool_base_t {
155165
}
156166
return UMF_RESULT_SUCCESS;
157167
}
168+
158169
umf_result_t free(void *ptr) noexcept {
159170
::free(ptr);
160171
return UMF_RESULT_SUCCESS;
161172
}
173+
174+
umf_result_t get_name(const char **name) noexcept {
175+
if (name) {
176+
*name = "malloc_pool";
177+
}
178+
return UMF_RESULT_SUCCESS;
179+
}
162180
};
163181

164182
umf_memory_pool_ops_t MALLOC_POOL_OPS =

test/pools/pool_base_alloc.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ struct base_alloc_pool : public umf_test::pool_base_t {
4444
umf_result_t get_last_allocation_error() {
4545
return umf_test::getPoolLastStatusRef<base_alloc_pool>();
4646
}
47+
umf_result_t get_name(const char **name) noexcept {
48+
if (name) {
49+
*name = "base_alloc_pool";
50+
}
51+
return UMF_RESULT_SUCCESS;
52+
}
4753
};
4854

4955
umf_memory_pool_ops_t BA_POOL_OPS =

test/utils/cpp_helpers.hpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ using provider_unique_handle_t =
3434
#define UMF_ASSIGN_OP(ops, type, func, default_return) \
3535
ops.func = [](void *obj, auto... args) { \
3636
try { \
37+
if (!obj) { \
38+
return type().func(args...); \
39+
} \
3740
return reinterpret_cast<type *>(obj)->func(args...); \
3841
} catch (...) { \
3942
return default_return; \
@@ -43,6 +46,9 @@ using provider_unique_handle_t =
4346
#define UMF_ASSIGN_OP_NORETURN(ops, type, func) \
4447
ops.func = [](void *obj, auto... args) { \
4548
try { \
49+
if (!obj) { \
50+
return type().func(args...); \
51+
} \
4652
return reinterpret_cast<type *>(obj)->func(args...); \
4753
} catch (...) { \
4854
} \
@@ -76,15 +82,10 @@ template <typename T> umf_memory_pool_ops_t poolOpsBase() {
7682
UMF_ASSIGN_OP(ops, T, calloc, ((void *)nullptr));
7783
UMF_ASSIGN_OP(ops, T, aligned_malloc, ((void *)nullptr));
7884
UMF_ASSIGN_OP(ops, T, realloc, ((void *)nullptr));
79-
UMF_ASSIGN_OP(ops, T, malloc_usable_size, UMF_RESULT_SUCCESS);
80-
UMF_ASSIGN_OP(ops, T, free, UMF_RESULT_SUCCESS);
85+
UMF_ASSIGN_OP(ops, T, get_name, UMF_RESULT_ERROR_UNKNOWN);
86+
UMF_ASSIGN_OP(ops, T, malloc_usable_size, UMF_RESULT_ERROR_UNKNOWN);
87+
UMF_ASSIGN_OP(ops, T, free, UMF_RESULT_ERROR_UNKNOWN);
8188
UMF_ASSIGN_OP(ops, T, get_last_allocation_error, UMF_RESULT_ERROR_UNKNOWN);
82-
ops.get_name = [](void *, const char **name) {
83-
if (name) {
84-
*name = "test_pool";
85-
}
86-
return UMF_RESULT_SUCCESS;
87-
};
8889
return ops;
8990
}
9091

0 commit comments

Comments
 (0)