1 /* ``Licensed under the Apache License, Version 2.0 (the "License");
2  * you may not use this file except in compliance with the License.
3  * You may obtain a copy of the License at
4  *
5  *     http://www.apache.org/licenses/LICENSE-2.0
6  *
7  * Unless required by applicable law or agreed to in writing, software
8  * distributed under the License is distributed on an "AS IS" BASIS,
9  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10  * See the License for the specific language governing permissions and
11  * limitations under the License.
12  *
13  * The Initial Developer of the Original Code is Ericsson Utvecklings AB.
14  * Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
15  * AB. All Rights Reserved.''
16  *
17  *     $Id$
18  */
19 
20 #include "testcase_driver.h"
21 #include "allocator_test.h"
22 
23 #define MAX_SEGS 10
24 
25 typedef struct {
26     void *ptr;
27     Ulong size;
28 } seg_t;
29 
30 char *
testcase_name(void)31 testcase_name(void)
32 {
33     return "mseg_clear_cache";
34 }
35 
36 void
testcase_run(TestCaseState_t * tcs)37 testcase_run(TestCaseState_t *tcs)
38 {
39     int i;
40     Ulong n;
41     seg_t *seg;
42 
43     if (!HAVE_MSEG())
44 	testcase_skipped(tcs, "No mseg_alloc; nothing to test");
45 
46     seg = (seg_t *) testcase_alloc(sizeof(seg_t)*(MAX_SEGS+1));
47 
48     ASSERT(tcs, seg);
49 
50     for (i = 0; i <= MAX_SEGS; i++)
51 	seg[i].ptr = NULL;
52 
53     tcs->extra = &seg[0];
54 
55     for (i = 0; i < MAX_SEGS; i++) {
56 	seg[i].size = 1 << 18;
57 	seg[i].ptr = MSEG_ALLOC(&seg[i].size);
58 	ASSERT(tcs, seg[i].ptr);
59 	ASSERT(tcs, seg[i].size >= (1 << 18));
60     }
61 
62     n = MSEG_NO();
63     testcase_printf(tcs, "MSEG_NO() = %lu\n", n);
64 
65     ASSERT(tcs, n >= MAX_SEGS);
66 
67     testcase_printf(tcs, "Deallocating half of the segments\n");
68     for (i = MAX_SEGS-1; i >= MAX_SEGS/2; i--) {
69 	MSEG_DEALLOC(seg[i].ptr, seg[i].size);
70 	seg[i].ptr = NULL;
71     }
72 
73     n = MSEG_NO();
74     testcase_printf(tcs, "MSEG_NO() = %lu\n", n);
75 
76     ASSERT(tcs, n >= MAX_SEGS/2);
77 
78     n = MSEG_CACHE_SIZE();
79     testcase_printf(tcs, "MSEG_CACHE_SIZE() = %lu\n", n);
80     ASSERT(tcs, n > 0);
81 
82     testcase_printf(tcs, "MSEG_CLEAR_CACHE()\n");
83     MSEG_CLEAR_CACHE();
84 
85     n = MSEG_CACHE_SIZE();
86     testcase_printf(tcs, "MSEG_CACHE_SIZE() = %lu\n", n);
87 
88     ASSERT(tcs, n == 0);
89 
90 }
91 
92 void
testcase_cleanup(TestCaseState_t * tcs)93 testcase_cleanup(TestCaseState_t *tcs)
94 {
95     if (tcs->extra) {
96 	seg_t *seg = (seg_t *) tcs->extra;
97 	int i;
98 	for (i = 0; seg[i].ptr; i++)
99 	    MSEG_DEALLOC(seg[i].ptr, seg[i].size);
100 	testcase_free((void *) seg);
101 	tcs->extra = NULL;
102     }
103 }
104 
105 ERL_NIF_INIT(mseg_clear_cache, testcase_nif_funcs, testcase_nif_init,
106 	     NULL, NULL, NULL);
107