Skip to content

Commit f633a24

Browse files
authored
Merge pull request #342 from ldorau/Move_functions_from_utils_common.h_to_utils_common.c
Move functions from utils_common.h to utils_common.c
2 parents e521e4b + 4e77815 commit f633a24

File tree

10 files changed

+129
-79
lines changed

10 files changed

+129
-79
lines changed

src/base_alloc/base_alloc.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ umf_ba_pool_t *umf_ba_create(size_t size) {
167167
char *data_ptr = (char *)&pool->data;
168168
size_t size_left = pool_size - offsetof(umf_ba_pool_t, data);
169169

170-
align_ptr_size((void **)&data_ptr, &size_left, MEMORY_ALIGNMENT);
170+
util_align_ptr_size((void **)&data_ptr, &size_left, MEMORY_ALIGNMENT);
171171

172172
// init free_lock
173173
os_mutex_t *mutex = util_mutex_init(&pool->metadata.free_lock);
@@ -208,7 +208,7 @@ void *umf_ba_alloc(umf_ba_pool_t *pool) {
208208
size_t size_left =
209209
pool->metadata.pool_size - offsetof(umf_ba_next_pool_t, data);
210210

211-
align_ptr_size((void **)&data_ptr, &size_left, MEMORY_ALIGNMENT);
211+
util_align_ptr_size((void **)&data_ptr, &size_left, MEMORY_ALIGNMENT);
212212
ba_divide_memory_into_chunks(pool, data_ptr, size_left);
213213
}
214214

@@ -281,7 +281,7 @@ void umf_ba_destroy(umf_ba_pool_t *pool) {
281281
// Do not destroy if we are running in the proxy library,
282282
// because it may need those resources till
283283
// the very end of exiting the application.
284-
if (pool->metadata.n_allocs && is_running_in_proxy_lib()) {
284+
if (pool->metadata.n_allocs && util_is_running_in_proxy_lib()) {
285285
return;
286286
}
287287

src/base_alloc/base_alloc_linear.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ umf_ba_linear_pool_t *umf_ba_linear_create(size_t pool_size) {
9797
void *data_ptr = &pool->data;
9898
size_t size_left = pool_size - offsetof(umf_ba_linear_pool_t, data);
9999

100-
align_ptr_size(&data_ptr, &size_left, MEMORY_ALIGNMENT);
100+
util_align_ptr_size(&data_ptr, &size_left, MEMORY_ALIGNMENT);
101101

102102
pool->metadata.pool_size = pool_size;
103103
pool->metadata.data_ptr = data_ptr;
@@ -145,7 +145,7 @@ void *umf_ba_linear_alloc(umf_ba_linear_pool_t *pool, size_t size) {
145145
void *data_ptr = &new_pool->data;
146146
size_t size_left =
147147
new_pool->pool_size - offsetof(umf_ba_next_linear_pool_t, data);
148-
align_ptr_size(&data_ptr, &size_left, MEMORY_ALIGNMENT);
148+
util_align_ptr_size(&data_ptr, &size_left, MEMORY_ALIGNMENT);
149149

150150
pool->metadata.data_ptr = data_ptr;
151151
pool->metadata.size_left = size_left;
@@ -239,7 +239,7 @@ void umf_ba_linear_destroy(umf_ba_linear_pool_t *pool) {
239239
// Do not destroy if we are running in the proxy library,
240240
// because it may need those resources till
241241
// the very end of exiting the application.
242-
if (is_running_in_proxy_lib()) {
242+
if (util_is_running_in_proxy_lib()) {
243243
return;
244244
}
245245

src/critnib/critnib.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
#include "base_alloc.h"
6161
#include "base_alloc_global.h"
6262
#include "critnib.h"
63+
#include "utils_assert.h"
6364
#include "utils_common.h"
6465
#include "utils_concurrency.h"
6566

src/memory_pool.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
#include "libumf.h"
1111
#include "memory_pool_internal.h"
12-
#include "utils_common.h"
12+
#include "utils_assert.h"
1313

1414
#include <umf/memory_pool.h>
1515
#include <umf/memory_pool_ops.h>

src/memory_provider.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include "base_alloc_global.h"
1919
#include "libumf.h"
2020
#include "memory_provider_internal.h"
21-
#include "utils_common.h"
21+
#include "utils_assert.h"
2222

2323
typedef struct umf_memory_provider_t {
2424
umf_memory_provider_ops_t ops;

src/provider/provider_tracking.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ static void check_if_tracker_is_empty(umf_memory_tracker_handle_t hTracker,
361361
// Do not assert if we are running in the proxy library,
362362
// because it may need those resources till
363363
// the very end of exiting the application.
364-
if (!is_running_in_proxy_lib()) {
364+
if (!util_is_running_in_proxy_lib()) {
365365
if (pool) {
366366
fprintf(stderr,
367367
"ASSERT: tracking provider of pool %p is not empty! "
@@ -514,7 +514,7 @@ void umfMemoryTrackerDestroy(umf_memory_tracker_handle_t handle) {
514514
// Do not destroy if we are running in the proxy library,
515515
// because it may need those resources till
516516
// the very end of exiting the application.
517-
if (is_running_in_proxy_lib()) {
517+
if (util_is_running_in_proxy_lib()) {
518518
return;
519519
}
520520

src/utils/CMakeLists.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
include(${UMF_CMAKE_SOURCE_DIR}/cmake/helpers.cmake)
66
include(FindThreads)
77

8+
set(UMF_UTILS_SOURCES_COMMON
9+
utils_common.c
10+
)
11+
812
set(UMF_UTILS_SOURCES_POSIX
913
utils_posix_common.c
1014
utils_posix_concurrency.c
@@ -31,9 +35,9 @@ if(USE_VALGRIND)
3135
endif()
3236

3337
if(LINUX OR MACOSX)
34-
set(UMF_UTILS_SOURCES ${UMF_UTILS_SOURCES_POSIX})
38+
set(UMF_UTILS_SOURCES ${UMF_UTILS_SOURCES_COMMON} ${UMF_UTILS_SOURCES_POSIX})
3539
elseif(WINDOWS)
36-
set(UMF_UTILS_SOURCES ${UMF_UTILS_SOURCES_WINDOWS})
40+
set(UMF_UTILS_SOURCES ${UMF_UTILS_SOURCES_COMMON} ${UMF_UTILS_SOURCES_WINDOWS})
3741
endif()
3842

3943
add_umf_library(NAME umf_utils

src/utils/utils_assert.h

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
*
3+
* Copyright (C) 2023-2024 Intel Corporation
4+
*
5+
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
6+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
*
8+
*/
9+
10+
#ifndef UMF_ASSERT_H
11+
#define UMF_ASSERT_H 1
12+
13+
#include <stdint.h>
14+
#include <stdio.h>
15+
#include <stdlib.h>
16+
17+
#ifdef __cplusplus
18+
extern "C" {
19+
#endif
20+
21+
#define NOFUNCTION \
22+
do { \
23+
} while (0)
24+
25+
#ifdef NDEBUG
26+
#define ASSERT(x) NOFUNCTION
27+
#define ASSERTne(x, y) NOFUNCTION
28+
#else
29+
#define ASSERT(x) \
30+
do { \
31+
if (!(x)) { \
32+
fprintf(stderr, \
33+
"Assertion failed: " #x " at " __FILE__ " line %d.\n", \
34+
__LINE__); \
35+
abort(); \
36+
} \
37+
} while (0)
38+
#define ASSERTne(x, y) \
39+
do { \
40+
long X = (x); \
41+
long Y = (y); \
42+
if (X == Y) { \
43+
fprintf(stderr, \
44+
"Assertion failed: " #x " != " #y \
45+
", both are %ld, at " __FILE__ " line %d.\n", \
46+
X, __LINE__); \
47+
abort(); \
48+
} \
49+
} while (0)
50+
#endif
51+
52+
#define UMF_CHECK(condition, errorStatus) \
53+
do { \
54+
if (!(condition)) { \
55+
fprintf(stderr, "UMF check failed: " #condition " in %s\n", \
56+
__func__); \
57+
return errorStatus; \
58+
} \
59+
} while (0)
60+
61+
#ifdef __cplusplus
62+
}
63+
#endif
64+
65+
#endif /* UMF_ASSERT_H */

src/utils/utils_common.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
*
3+
* Copyright (C) 2024 Intel Corporation
4+
*
5+
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
6+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
*
8+
*/
9+
10+
#include "utils_common.h"
11+
#include "utils_assert.h"
12+
13+
// align a pointer and a size
14+
void util_align_ptr_size(void **ptr, size_t *size, size_t alignment) {
15+
uintptr_t p = (uintptr_t)*ptr;
16+
size_t s = *size;
17+
18+
// align pointer to 'alignment' bytes and adjust the size
19+
size_t rest = p & (alignment - 1);
20+
if (rest) {
21+
p += alignment - rest;
22+
s -= alignment - rest;
23+
}
24+
25+
ASSERT((p & (alignment - 1)) == 0);
26+
ASSERT((s & (alignment - 1)) == 0);
27+
28+
*ptr = (void *)p;
29+
*size = s;
30+
}
31+
32+
// check if we are running in the proxy library
33+
int util_is_running_in_proxy_lib(void) {
34+
return util_env_var_has_str("LD_PRELOAD", "libumf_proxy.so");
35+
}

src/utils/utils_common.h

Lines changed: 12 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
#ifndef UMF_COMMON_H
1111
#define UMF_COMMON_H 1
1212

13+
#include <assert.h>
14+
#include <stddef.h>
1315
#include <stdint.h>
14-
#include <stdio.h>
15-
#include <stdlib.h>
1616

1717
#ifdef __cplusplus
1818
extern "C" {
@@ -21,11 +21,18 @@ extern "C" {
2121
#define DO_WHILE_EMPTY \
2222
do { \
2323
} while (0)
24+
2425
#define DO_WHILE_EXPRS(expression) \
2526
do { \
2627
expression; \
2728
} while (0)
2829

30+
#define ALIGN_UP(value, align) (((value) + (align)-1) & ~((align)-1))
31+
#define ALIGN_DOWN(value, align) ((value) & ~((align)-1))
32+
33+
#define VALGRIND_ANNOTATE_NEW_MEMORY(p, s) DO_WHILE_EMPTY
34+
#define VALGRIND_HG_DRD_DISABLE_CHECKING(p, s) DO_WHILE_EMPTY
35+
2936
#ifdef _WIN32 /* Windows */
3037

3138
#define __TLS __declspec(thread)
@@ -55,76 +62,14 @@ int util_env_var(const char *envvar, char *buffer, size_t buffer_size);
5562
int util_env_var_has_str(const char *envvar, const char *str);
5663

5764
// check if we are running in the proxy library
58-
static inline int is_running_in_proxy_lib(void) {
59-
return util_env_var_has_str("LD_PRELOAD", "libumf_proxy.so");
60-
}
65+
int util_is_running_in_proxy_lib(void);
6166

6267
size_t util_get_page_size(void);
63-
char *util_strncpy(char *dest, size_t destSize, const char *src, size_t n);
64-
65-
#define NOFUNCTION \
66-
do { \
67-
} while (0)
68-
#define VALGRIND_ANNOTATE_NEW_MEMORY(p, s) NOFUNCTION
69-
#define VALGRIND_HG_DRD_DISABLE_CHECKING(p, s) NOFUNCTION
70-
71-
#ifdef NDEBUG
72-
#define ASSERT(x) NOFUNCTION
73-
#define ASSERTne(x, y) ASSERT(x != y)
74-
#else
75-
#define ASSERT(x) \
76-
do { \
77-
if (!(x)) { \
78-
fprintf(stderr, \
79-
"Assertion failed: " #x " at " __FILE__ " line %d.\n", \
80-
__LINE__); \
81-
abort(); \
82-
} \
83-
} while (0)
84-
#define ASSERTne(x, y) \
85-
do { \
86-
long X = (x); \
87-
long Y = (y); \
88-
if (X == Y) { \
89-
fprintf(stderr, \
90-
"Assertion failed: " #x " != " #y \
91-
", both are %ld, at " __FILE__ " line %d.\n", \
92-
X, __LINE__); \
93-
abort(); \
94-
} \
95-
} while (0)
96-
#endif
9768

98-
#define UMF_CHECK(condition, errorStatus) \
99-
do { \
100-
if (!(condition)) { \
101-
fprintf(stderr, "UMF check failed: " #condition " in %s\n", \
102-
__func__); \
103-
return errorStatus; \
104-
} \
105-
} while (0)
69+
char *util_strncpy(char *dest, size_t destSize, const char *src, size_t n);
10670

10771
// align a pointer and a size
108-
static inline void align_ptr_size(void **ptr, size_t *size, size_t alignment) {
109-
uintptr_t p = (uintptr_t)*ptr;
110-
size_t s = *size;
111-
112-
// align pointer to 'alignment' bytes and adjust the size
113-
size_t rest = p & (alignment - 1);
114-
if (rest) {
115-
p += alignment - rest;
116-
s -= alignment - rest;
117-
}
118-
119-
ASSERT((p & (alignment - 1)) == 0);
120-
ASSERT((s & (alignment - 1)) == 0);
121-
122-
*ptr = (void *)p;
123-
*size = s;
124-
}
125-
126-
#define ALIGN_UP(value, align) (((value) + (align)-1) & ~((align)-1))
127-
#define ALIGN_DOWN(value, align) ((value) & ~((align)-1))
72+
void util_align_ptr_size(void **ptr, size_t *size, size_t alignment);
12873

12974
#ifdef __cplusplus
13075
}

0 commit comments

Comments
 (0)