OpenMPI 0.1.1

- Main Page
- Related Pages
- Modules
- Data Structures
- Files

- File List
- Globals

t-test.h
1 /*
2 * $Id: t-test.h,v 1.1 2004/11/04 14:32:21 wg Exp $
3 * by Wolfram Gloger 1996.
4 * Common data structures and functions for testing malloc performance.
5 */
6
7 /* Testing level */
8 #ifndef TEST
9 #define TEST 0
10 #endif
11
12 /* For large allocation sizes, the time required by copying in
13 realloc() can dwarf all other execution times. Avoid this with a
14 size threshold. */
15 #ifndef REALLOC_MAX
16 #define REALLOC_MAX 2000
17 #endif
18
19 struct bin {
20 unsigned char *ptr;
21 unsigned long size;
22 };
23
24 #if TEST > 0
25
26 static void
27 mem_init(unsigned char *ptr, unsigned long size)
28 {
29 unsigned long i, j;
30
31 if(size == 0) return;
32 for(i=0; i<size; i+=2047) {
33 j = (unsigned long)ptr ^ i;
34 ptr[i] = ((j ^ (j>>8)) & 0xFF);
35 }
36 j = (unsigned long)ptr ^ (size-1);
37 ptr[size-1] = ((j ^ (j>>8)) & 0xFF);
38 }
39
40 static int
41 mem_check(unsigned char *ptr, unsigned long size)
42 {
43 unsigned long i, j;
44
45 if(size == 0) return 0;
46 for(i=0; i<size; i+=2047) {
47 j = (unsigned long)ptr ^ i;
48 if(ptr[i] != ((j ^ (j>>8)) & 0xFF)) return 1;
49 }
50 j = (unsigned long)ptr ^ (size-1);
51 if(ptr[size-1] != ((j ^ (j>>8)) & 0xFF)) return 2;
52 return 0;
53 }
54
55 static int
56 zero_check(unsigned* ptr, unsigned long size)
57 {
58 unsigned char* ptr2;
59
60 while(size >= sizeof(*ptr)) {
61 if(*ptr++ != 0)
62 return -1;
63 size -= sizeof(*ptr);
64 }
65 ptr2 = (unsigned char*)ptr;
66 while(size > 0) {
67 if(*ptr2++ != 0)
68 return -1;
69 --size;
70 }
71 return 0;
72 }
73
74 #endif /* TEST > 0 */
75
76 /* Allocate a bin with malloc(), realloc() or memalign(). r must be a
77 random number >= 1024. */
78
79 static void
80 bin_alloc(struct bin *m, unsigned long size, int r)
81 {
82 #if TEST > 0
83 if(mem_check(m->ptr, m->size)) {
84 printf("memory corrupt!\n");
85 exit(1);
86 }
87 #endif
88 r %= 1024;
89 /*printf("%d ", r);*/
90 if(r < 4) { /* memalign */
91 if(m->size > 0) free(m->ptr);
92 m->ptr = (unsigned char *)memalign(sizeof(int) << r, size);
93 } else if(r < 20) { /* calloc */
94 if(m->size > 0) free(m->ptr);
95 m->ptr = (unsigned char *)calloc(size, 1);
96 #if TEST > 0
97 if(zero_check((unsigned*)m->ptr, size)) {
98 long i;
99 for(i=0; i<size; i++)
100 if(m->ptr[i] != 0)
101 break;
102 printf("calloc'ed memory non-zero (ptr=%p, i=%ld)!\n", m->ptr, i);
103 exit(1);
104 }
105 #endif
106 } else if(r < 100 && m->size < REALLOC_MAX) { /* realloc */
107 if(m->size == 0) m->ptr = NULL;
108 m->ptr = realloc(m->ptr, size);
109 } else { /* plain malloc */
110 if(m->size > 0) free(m->ptr);
111 m->ptr = (unsigned char *)malloc(size);
112 }
113 if(!m->ptr) {
114 printf("out of memory (r=%d, size=%ld)!\n", r, (long)size);
115 exit(1);
116 }
117 m->size = size;
118 #if TEST > 0
119 mem_init(m->ptr, m->size);
120 #endif
121 }
122
123 /* Free a bin. */
124
125 static void
126 bin_free(struct bin *m)
127 {
128 if(m->size == 0) return;
129 #if TEST > 0
130 if(mem_check(m->ptr, m->size)) {
131 printf("memory corrupt!\n");
132 exit(1);
133 }
134 #endif
135 free(m->ptr);
136 m->size = 0;
137 }
138
139 /*
140 * Local variables:
141 * tab-width: 4
142 * End:
143 */
bin
Definition: t-test.h:19