1 /*
2 Copyright (c) 2005-2021 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 #ifdef __cplusplus
18 #error For testing purpose, this file should be compiled with a C compiler, not C++
19 #endif /*__cplusplus */
20
21 #include "tbb/scalable_allocator.h"
22 #include <stdio.h>
23 #include <assert.h>
24 #include <stdlib.h> /* for atexit */
25
26 /*
27 * The test is to check if the scalable_allocator.h and its functions
28 * can be used from pure C programs; also some regression checks are done
29 */
30
31 #if __unix__
32 /* huge pages supported only under Linux so far */
33 const int ExpectedResultHugePages = TBBMALLOC_OK;
34 #else
35 const int ExpectedResultHugePages = TBBMALLOC_NO_EFFECT;
36 #endif
37
38 /* bool type definition for C */
39 #if (defined(_MSC_VER) && _MSC_VER < 1800) || __sun || __SUNPRO_CC
40 typedef int bool;
41 #define false 0
42 #define true 1
43 #else
44 #include <stdbool.h>
45 #endif
46
47 /* test that it's possible to call allocation function from atexit
48 after mallocProcessShutdownNotification() called */
MyExit(void)49 static void MyExit(void) {
50 void *p = scalable_malloc(32);
51 assert(p);
52 scalable_free(p);
53 }
54
main(void)55 int main(void) {
56 size_t i, j;
57 int curr_mode, res;
58 void *p1, *p2;
59
60 atexit( MyExit );
61 for ( curr_mode = 0; curr_mode<=1; curr_mode++) {
62 assert(ExpectedResultHugePages ==
63 scalable_allocation_mode(TBBMALLOC_USE_HUGE_PAGES, !curr_mode));
64 p1 = scalable_malloc(10*1024*1024);
65 assert(p1);
66 assert(ExpectedResultHugePages ==
67 scalable_allocation_mode(TBBMALLOC_USE_HUGE_PAGES, curr_mode));
68 scalable_free(p1);
69 }
70
71 for( i=0; i<=1<<16; ++i) {
72 p1 = scalable_malloc(i);
73 if( !p1 )
74 printf("Warning: there should be memory but scalable_malloc returned NULL\n");
75 scalable_free(p1);
76 }
77 p1 = p2 = NULL;
78 for( i=1024*1024; ; i/=2 )
79 {
80 scalable_free(p1);
81 p1 = scalable_realloc(p2, i);
82 p2 = scalable_calloc(i, 32);
83 if (p2) {
84 if (i<sizeof(size_t)) {
85 for (j=0; j<i; j++)
86 assert(0==*((char*)p2+j));
87 } else {
88 for (j=0; j<i; j+=sizeof(size_t))
89 assert(0==*((size_t*)p2+j));
90 }
91 }
92 scalable_free(p2);
93 p2 = scalable_malloc(i);
94 if (i==0) break;
95 }
96 for( i=1; i<1024*1024; i*=2 )
97 {
98 scalable_free(p1);
99 p1 = scalable_realloc(p2, i);
100 p2 = scalable_malloc(i);
101 }
102 scalable_free(p1);
103 scalable_free(p2);
104 res = scalable_allocation_command(TBBMALLOC_CLEAN_ALL_BUFFERS, NULL);
105 assert(res == TBBMALLOC_OK);
106 res = scalable_allocation_command(TBBMALLOC_CLEAN_THREAD_BUFFERS, NULL);
107 /* expect all caches cleaned before, so got nothing from CLEAN_THREAD_BUFFERS */
108 assert(res == TBBMALLOC_NO_EFFECT);
109 /* check that invalid param argument give expected result*/
110 res = scalable_allocation_command(TBBMALLOC_CLEAN_THREAD_BUFFERS,
111 (void*)(intptr_t)1);
112 assert(res == TBBMALLOC_INVALID_PARAM);
113 printf("done\n");
114 return 0;
115 }
116