~ubuntu-branches/ubuntu/wily/aspectc++/wily

« back to all changes in this revision

Viewing changes to Puma/src/win32/ptmalloc/t-test.h

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler
  • Date: 2005-12-23 10:49:40 UTC
  • Revision ID: james.westby@ubuntu.com-20051223104940-ig4klhoi991zs7km
Tags: upstream-0.99+1.0pre2
ImportĀ upstreamĀ versionĀ 0.99+1.0pre2

Show diffs side-by-side

added added

removed removed

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