1 /*
2     Copyright (c) 2005-2020 Intel Corporation
3 
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7 
8         http://www.apache.org/licenses/LICENSE-2.0
9 
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16 
17 #define __TBB_NO_IMPLICIT_LINKAGE 1
18 
19 #if _USRDLL
20 
21 #include <stdlib.h>
22 #include "common/config.h"
23 #include "common/utils_assert.h"
24 #include "tbb/scalable_allocator.h"
25 
26 #if _WIN32||_WIN64
27 extern "C" {
28     extern __declspec(dllexport) void callDll();
29 }
30 #endif
31 
32 extern "C" void callDll()
33 {
34     static const int NUM = 20;
35     void *ptrs[NUM];
36 
37     for (int i=0; i<NUM; i++) {
38         ptrs[i] = scalable_malloc(i*1024);
39         ASSERT(ptrs[i], NULL);
40     }
41     for (int i=0; i<NUM; i++)
42         scalable_free(ptrs[i]);
43 }
44 
45 int main() {}
46 
47 
48 #else // _USRDLL
49 // FIXME: fix the test to support Windows* 8 Store Apps mode.
50 #if !__TBB_WIN8UI_SUPPORT
51 
52 #define __TBB_NO_IMPLICIT_LINKAGE 1
53 #include "common/test.h"
54 #include "common/utils.h"
55 #include "common/utils_dynamic_libs.h"
56 #include "common/utils_report.h"
57 #include "common/memory_usage.h"
58 #include "common/spin_barrier.h"
59 
60 class UseDll {
61     utils::FunctionAddress run;
62 public:
63     UseDll(utils::FunctionAddress runPtr) : run(runPtr) { }
64     void operator()( std::size_t /*id*/ ) const {
65         (*run)();
66     }
67 };
68 
69 void LoadThreadsUnload()
70 {
71     utils::LIBRARY_HANDLE lib =
72         utils::OpenLibrary(TEST_LIBRARY_NAME("_test_malloc_used_by_lib"));
73     ASSERT(lib, "Can't load " TEST_LIBRARY_NAME("_test_malloc_used_by_lib"));
74     utils::NativeParallelFor(std::size_t(4), UseDll(utils::GetAddress(lib, "callDll")));
75     utils::CloseLibrary(lib);
76 }
77 
78 struct UnloadCallback {
79     utils::LIBRARY_HANDLE lib;
80 
81     void operator() () const {
82         utils::CloseLibrary(lib);
83     }
84 };
85 
86 struct RunWithLoad {
87     static utils::SpinBarrier startBarr, endBarr;
88     static UnloadCallback unloadCallback;
89     static utils::FunctionAddress runPtr;
90 
91     void operator()(std::size_t id) const {
92         if (!id) {
93             utils::LIBRARY_HANDLE lib =
94                 utils::OpenLibrary(TEST_LIBRARY_NAME("_test_malloc_used_by_lib"));
95             ASSERT(lib, "Can't load " TEST_LIBRARY_NAME("_test_malloc_used_by_lib"));
96             runPtr = utils::GetAddress(lib, "callDll");
97             unloadCallback.lib = lib;
98         }
99         startBarr.wait();
100         (*runPtr)();
101         endBarr.wait(unloadCallback);
102     }
103 };
104 
105 utils::SpinBarrier RunWithLoad::startBarr{}, RunWithLoad::endBarr{};
106 UnloadCallback RunWithLoad::unloadCallback;
107 utils::FunctionAddress RunWithLoad::runPtr;
108 
109 void ThreadsLoadUnload() {
110     constexpr std::size_t threads = 4;
111 
112     RunWithLoad::startBarr.initialize(threads);
113     RunWithLoad::endBarr.initialize(threads);
114     RunWithLoad body{};
115     utils::NativeParallelFor(threads, body);
116 }
117 
118 //! \brief \ref error_guessing
119 TEST_CASE("use test as lib") {
120     const int ITERS = 20;
121     int i;
122     std::ptrdiff_t memory_leak = 0;
123 
124     utils::GetMemoryUsage();
125 
126     for (int run = 0; run < 2; run++) {
127         // expect that memory consumption stabilized after several runs
128         for (i = 0; i < ITERS; i++) {
129             std::size_t memory_in_use = utils::GetMemoryUsage();
130             if (run) {
131                 LoadThreadsUnload();
132             } else {
133                 ThreadsLoadUnload();
134             }
135             memory_leak = utils::GetMemoryUsage() - memory_in_use;
136             if (memory_leak == 0)  // possibly too strong?
137                 break;
138         }
139         if(i==ITERS) {
140             // not stabilized, could be leak
141             REPORT( "Error: memory leak of up to %ld bytes\n", static_cast<long>(memory_leak));
142             WARN(false);
143         }
144     }
145 }
146 
147 #endif /* __TBB_WIN8UI_SUPPORT */
148 #endif // _USRDLL
149