~ubuntu-branches/ubuntu/edgy/ncbi-tools6/edgy

« back to all changes in this revision

Viewing changes to connect/test/test_ncbi_heapmgr.c

  • Committer: Bazaar Package Importer
  • Author(s): Barry deFreese
  • Date: 2006-07-19 23:28:07 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20060719232807-et3cdmcjgmnyleyx
Tags: 6.1.20060507-3ubuntu1
Re-merge with Debian

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  $Id: test_ncbi_heapmgr.c,v 6.14 2005/07/11 18:24:46 lavr Exp $
 
1
/*  $Id: test_ncbi_heapmgr.c,v 6.15 2006/03/05 17:42:10 lavr Exp $
2
2
 * ===========================================================================
3
3
 *
4
4
 *                            PUBLIC DOMAIN NOTICE
67
67
int main(void)
68
68
{
69
69
    SHEAP_Block* blk;
70
 
    int r, j, i;
 
70
    int r, j, i, n;
71
71
    HEAP heap;
72
72
    char* c;
73
73
 
74
74
    CORE_SetLOGFILE(stderr, 0/*false*/);
75
75
    for (j = 1; j <= 3; j++) {
76
 
        g_NCBI_ConnectRandomSeed = (int) time(0) + NCBI_CONNECT_SRAND_ADDEND;
 
76
        g_NCBI_ConnectRandomSeed = (int) time(0) ^ NCBI_CONNECT_SRAND_ADDEND;
77
77
        srand(g_NCBI_ConnectRandomSeed);
78
78
        CORE_LOGF(eLOG_Note, ("Creating heap %d", j));
79
79
        if (!(heap = HEAP_Create(0, 0, 4096, s_Expand, 0)))
80
80
            CORE_LOG(eLOG_Error, "Cannot create heap");
81
 
        while (rand() != 12345) {
 
81
        for (n = 0; n < 1000 && (rand() & 0xFFFF) != 0x1234; n++) {
82
82
            r = rand() & 7;
83
83
            if (r == 1 || r == 3) {
84
84
                i = rand() & 0xFF;
85
85
                if (i) {
86
 
                    CORE_LOGF(eLOG_Note, ("Allocating %d bytes", i));
 
86
                    CORE_LOGF(eLOG_Note, ("Allocating %d data byte(s)", i));
87
87
                    if (!(blk = HEAP_Alloc(heap, i)))
88
88
                        CORE_LOG(eLOG_Error, "Allocation failed");
89
89
                    else
102
102
                    i++;
103
103
                } while (rand() & 0x7);
104
104
                if (blk && (short) blk->flag) {
 
105
                    unsigned data_size = blk->size - sizeof(*blk);
105
106
                    CORE_LOGF(eLOG_Note,
106
 
                              ("Deleting block #%d, size %u", i,
107
 
                               (unsigned) (blk->size - sizeof(*blk))));
 
107
                              ("Deleting block #%d, data size %u", i,
 
108
                               data_size));
 
109
                    assert(data_size);
108
110
                    HEAP_Free(heap, blk);
109
111
                    CORE_LOG(eLOG_Note, "Done");
110
112
                }
112
114
                blk = 0;
113
115
                i = 0;
114
116
                CORE_LOG(eLOG_Note, "Walking the heap");
115
 
                do {
116
 
                    blk = HEAP_Walk(heap, blk);
117
 
                    if (blk)
118
 
                        CORE_LOGF(eLOG_Note,
119
 
                                  ("Block #%d (%s), size %u", ++i,
120
 
                                   (short) blk->flag ? "used" : "free",
121
 
                                   (unsigned) (blk->size - sizeof(*blk))));
122
 
                } while (blk);
 
117
                while ((blk = HEAP_Walk(heap, blk)) != 0) {
 
118
                    unsigned data_size = blk->size - sizeof(*blk);
 
119
                    CORE_LOGF(eLOG_Note,
 
120
                              ("Block #%d (%s), data size %u", ++i,
 
121
                               (short) blk->flag ? "used" : "free",
 
122
                               data_size));
 
123
                    assert(data_size);
 
124
                }
123
125
                CORE_LOGF(eLOG_Note,
124
126
                          ("Total of %d block%s", i, i == 1 ? "" : "s"));
125
127
            } else if (r == 6 || r == 7) {
126
128
                HEAP newheap;
127
129
 
128
130
                if (r == 6)
129
 
                    newheap = HEAP_Attach(/* HACK! */*((char**)heap));
 
131
                    newheap = HEAP_Attach(HEAP_Base(heap), 0);
130
132
                else
131
 
                    newheap = HEAP_Copy(heap);
 
133
                    newheap = HEAP_Copy(heap, 0, 0);
132
134
 
133
 
                if (!newheap)
 
135
                if (!newheap) {
134
136
                    CORE_LOGF(eLOG_Error, ("%s failed",
135
137
                                           r == 6 ? "Attach" : "Copy"));
 
138
                }
 
139
                i = 0;
136
140
                blk = 0;
137
 
                i = 0;
138
141
                CORE_LOGF(eLOG_Note, ("Walking %s heap",
139
142
                                      r == 6 ? "attached" : "copied"));
140
 
                do {
141
 
                    blk = HEAP_Walk(newheap, blk);
142
 
                    if (blk)
143
 
                        CORE_LOGF(eLOG_Note,
144
 
                                  ("Block #%d (%s), size %u", ++i,
145
 
                                   (short) blk->flag ? "used" : "free",
146
 
                                   (unsigned) (blk->size - sizeof(*blk))));
147
 
                } while (blk);
 
143
                while ((blk = HEAP_Walk(newheap, blk)) != 0) {
 
144
                    unsigned data_size = blk->size - sizeof(*blk);
 
145
                    CORE_LOGF(eLOG_Note,
 
146
                              ("Block #%d (%s), data size %u", ++i,
 
147
                               (short) blk->flag ? "used" : "free",
 
148
                               data_size));
 
149
                    assert(data_size);
 
150
                }
148
151
                CORE_LOGF(eLOG_Note,
149
152
                          ("Total of %d block%s", i, i == 1 ? "" : "s"));
150
153
                HEAP_Detach(newheap);
151
154
            } else {
152
155
                TNCBI_Size size = HEAP_Size(heap);
153
 
                CORE_LOGF(eLOG_Note, ("Heap trimmed: %u -> %u",
 
156
                HEAP    newheap = HEAP_Trim(heap);
 
157
                CORE_LOGF(eLOG_Note, ("Heap %strimmed: %u -> %u",
 
158
                                      newheap ? "" : "NOT ",
154
159
                                      (unsigned) size,
155
 
                                      (unsigned) HEAP_Trim(heap)));
 
160
                                      (unsigned) HEAP_Size(newheap)));
 
161
                if (newheap)
 
162
                    heap = newheap;
156
163
            }
157
164
        }
158
165
        HEAP_Destroy(heap);
159
166
        CORE_LOGF(eLOG_Note, ("Heap %d done", j));
160
167
    }
161
168
    CORE_LOG(eLOG_Note, "Test completed");
 
169
    CORE_SetLOG(0);
162
170
    return 0;
163
171
}
164
172
 
166
174
/*
167
175
 * --------------------------------------------------------------------------
168
176
 * $Log: test_ncbi_heapmgr.c,v $
 
177
 * Revision 6.15  2006/03/05 17:42:10  lavr
 
178
 * Adjust for revised API
 
179
 *
169
180
 * Revision 6.14  2005/07/11 18:24:46  lavr
170
181
 * Spell ADDEND
171
182
 *