Skip to content

Commit 8ecbefe

Browse files
authored
Merge pull request #118 from PatKamin/pure-c-umf
Refactor the only .cpp file to pure C
2 parents da4ae27 + 38c1a70 commit 8ecbefe

File tree

6 files changed

+101
-54
lines changed

6 files changed

+101
-54
lines changed

src/CMakeLists.txt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (C) 2023 Intel Corporation
1+
# Copyright (C) 2023-2024 Intel Corporation
22
# Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
33
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
44

@@ -18,8 +18,16 @@ set(UMF_SOURCES
1818
critnib/critnib.c
1919
)
2020

21+
set(UMF_SOURCES_LINUX
22+
provider/provider_tracking_linux.c
23+
)
24+
2125
set(UMF_SOURCES_WINDOWS
22-
provider/provider_tracking_windows.cpp
26+
provider/provider_tracking_windows.c
27+
)
28+
29+
set(UMF_SOURCES_MACOSX
30+
provider/provider_tracking_linux.c
2331
)
2432

2533
if(UMF_BUILD_OS_MEMORY_PROVIDER)
@@ -38,6 +46,8 @@ if(LINUX)
3846
set(UMF_SOURCES ${UMF_SOURCES} ${UMF_SOURCES_LINUX})
3947
elseif(WINDOWS)
4048
set(UMF_SOURCES ${UMF_SOURCES} ${UMF_SOURCES_WINDOWS})
49+
elseif(MACOSX)
50+
set(UMF_SOURCES ${UMF_SOURCES} ${UMF_SOURCES_MACOSX})
4151
endif()
4252

4353
if(UMF_BUILD_SHARED_LIBRARY)

src/provider/provider_tracking.c

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
*
3-
* Copyright (C) 2023 Intel Corporation
3+
* Copyright (C) 2023-2024 Intel Corporation
44
*
55
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
66
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
@@ -18,20 +18,6 @@
1818
#include <errno.h>
1919
#include <stdlib.h>
2020

21-
#if !defined(_WIN32)
22-
critnib *TRACKER = NULL;
23-
void __attribute__((constructor)) createLibTracker(void) {
24-
TRACKER = critnib_new();
25-
}
26-
void __attribute__((destructor)) deleteLibTracker(void) {
27-
critnib_delete(TRACKER);
28-
}
29-
30-
umf_memory_tracker_handle_t umfMemoryTrackerGet(void) {
31-
return (umf_memory_tracker_handle_t)TRACKER;
32-
}
33-
#endif
34-
3521
typedef struct tracker_value_t {
3622
umf_memory_pool_handle_t pool;
3723
size_t size;
@@ -233,9 +219,14 @@ umf_memory_provider_ops_t UMF_TRACKING_MEMORY_PROVIDER_OPS = {
233219
umf_result_t umfTrackingMemoryProviderCreate(
234220
umf_memory_provider_handle_t hUpstream, umf_memory_pool_handle_t hPool,
235221
umf_memory_provider_handle_t *hTrackingProvider) {
222+
umfTrackingMemoryProviderInit();
223+
236224
umf_tracking_memory_provider_t params;
237225
params.hUpstream = hUpstream;
238226
params.hTracker = umfMemoryTrackerGet();
227+
if (!params.hTracker) {
228+
return UMF_RESULT_ERROR_UNKNOWN;
229+
}
239230
params.pool = hPool;
240231

241232
return umfMemoryProviderCreate(&UMF_TRACKING_MEMORY_PROVIDER_OPS, &params,

src/provider/provider_tracking.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
*
3-
* Copyright (C) 2023 Intel Corporation
3+
* Copyright (C) 2023-2024 Intel Corporation
44
*
55
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
66
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
@@ -32,6 +32,9 @@ umf_result_t umfTrackingMemoryProviderCreate(
3232
umf_memory_provider_handle_t hUpstream, umf_memory_pool_handle_t hPool,
3333
umf_memory_provider_handle_t *hTrackingProvider);
3434

35+
// Initialize critnib for a UMF static library build on Windows
36+
void umfTrackingMemoryProviderInit(void);
37+
3538
void umfTrackingMemoryProviderGetUpstreamProvider(
3639
umf_memory_provider_handle_t hTrackingProvider,
3740
umf_memory_provider_handle_t *hUpstream);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 "critnib.h"
11+
#include "provider_tracking.h"
12+
13+
#include <stddef.h>
14+
15+
static critnib *TRACKER = NULL;
16+
17+
void __attribute__((constructor)) createLibTracker(void) {
18+
TRACKER = critnib_new();
19+
}
20+
void __attribute__((destructor)) deleteLibTracker(void) {
21+
critnib_delete(TRACKER);
22+
}
23+
24+
void umfTrackingMemoryProviderInit(void) {
25+
// do nothing, additional initialization not needed
26+
}
27+
28+
umf_memory_tracker_handle_t umfMemoryTrackerGet(void) {
29+
return (umf_memory_tracker_handle_t)TRACKER;
30+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 "critnib.h"
11+
#include "provider_tracking.h"
12+
13+
#include <windows.h>
14+
15+
static critnib *TRACKER = NULL;
16+
17+
#if defined(UMF_SHARED_LIBRARY)
18+
BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
19+
if (fdwReason == DLL_PROCESS_DETACH) {
20+
critnib_delete(TRACKER);
21+
} else if (fdwReason == DLL_PROCESS_ATTACH) {
22+
TRACKER = critnib_new();
23+
}
24+
return TRUE;
25+
}
26+
27+
void umfTrackingMemoryProviderInit(void) {
28+
// do nothing, additional initialization not needed
29+
}
30+
#else
31+
INIT_ONCE init_once_flag = INIT_ONCE_STATIC_INIT;
32+
33+
static void providerFini(void) { critnib_delete(TRACKER); }
34+
35+
BOOL CALLBACK providerInit(PINIT_ONCE InitOnce, PVOID Parameter,
36+
PVOID *lpContext) {
37+
TRACKER = critnib_new();
38+
atexit(providerFini);
39+
return TRACKER ? TRUE : FALSE;
40+
}
41+
42+
void umfTrackingMemoryProviderInit(void) {
43+
InitOnceExecuteOnce(&init_once_flag, providerInit, NULL, NULL);
44+
}
45+
#endif
46+
47+
umf_memory_tracker_handle_t umfMemoryTrackerGet(void) {
48+
return (umf_memory_tracker_handle_t)TRACKER;
49+
}

src/provider/provider_tracking_windows.cpp

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)