2
* by Wolfram Gloger 1996.
3
* Common data structures and functions for testing malloc performance.
11
/* For large allocation sizes, the time required by copying in
12
realloc() can dwarf all other execution times. Avoid this with a
15
#define REALLOC_MAX 2000
26
mem_init(unsigned char *ptr, unsigned long size)
31
for(i=0; i<size; i+=2047) {
32
j = (unsigned long)ptr ^ i;
33
ptr[i] = ((j ^ (j>>8)) & 0xFF);
35
j = (unsigned long)ptr ^ (size-1);
36
ptr[size-1] = ((j ^ (j>>8)) & 0xFF);
40
mem_check(unsigned char *ptr, unsigned long size)
44
if(size == 0) return 0;
45
for(i=0; i<size; i+=2047) {
46
j = (unsigned long)ptr ^ i;
47
if(ptr[i] != ((j ^ (j>>8)) & 0xFF)) return 1;
49
j = (unsigned long)ptr ^ (size-1);
50
if(ptr[size-1] != ((j ^ (j>>8)) & 0xFF)) return 2;
55
zero_check(unsigned* ptr, unsigned long size)
59
while(size >= sizeof(*ptr)) {
64
ptr2 = (unsigned char*)ptr;
75
/* Allocate a bin with malloc(), realloc() or memalign(). r must be a
76
random number >= 1024. */
79
bin_alloc(struct bin *m, unsigned long size, int r)
82
if(mem_check(m->ptr, m->size)) {
83
printf("memory corrupt!\n");
89
if(r < 4) { /* memalign */
90
if(m->size > 0) free(m->ptr);
91
m->ptr = (unsigned char *)memalign(sizeof(int) << r, size);
92
} else if(r < 20) { /* calloc */
93
if(m->size > 0) free(m->ptr);
94
m->ptr = (unsigned char *)calloc(size, 1);
96
if(zero_check((unsigned*)m->ptr, size)) {
101
printf("calloc'ed memory non-zero (ptr=%p, i=%ld)!\n", m->ptr, i);
105
} else if(r < 100 && m->size < REALLOC_MAX) { /* realloc */
106
if(m->size == 0) m->ptr = NULL;
107
m->ptr = realloc(m->ptr, size);
108
} else { /* plain malloc */
109
if(m->size > 0) free(m->ptr);
110
m->ptr = (unsigned char *)malloc(size);
113
printf("out of memory (r=%d, size=%ld)!\n", r, (long)size);
118
mem_init(m->ptr, m->size);
125
bin_free(struct bin *m)
127
if(m->size == 0) return;
129
if(mem_check(m->ptr, m->size)) {
130
printf("memory corrupt!\n");