tests/vg_regtest: Always evaluate prerequisite expressions with sh
[valgrind.git] / memcheck / tests / malloc2.c
blob496fdff15f81e14525b20e67f8cdbf9e01a35b11
2 #include <stdio.h>
3 #include <stdlib.h>
5 /* The original test driver machinery. */
6 #define N_TEST_TRANSACTIONS 500
7 #define N_TEST_ARR 2000
9 #define M_TEST_MALLOC 1000
11 void* test_arr[N_TEST_ARR];
13 unsigned myrandom(void)
15 /* Simple multiply-with-carry random generator. */
16 static unsigned m_w = 11;
17 static unsigned m_z = 13;
19 m_z = 36969 * (m_z & 65535) + (m_z >> 16);
20 m_w = 18000 * (m_w & 65535) + (m_w >> 16);
21 return (m_z << 16) + m_w;
24 int main ( int argc, char** argv )
26 int i, j, k, nbytes;
27 unsigned char* chp;
29 for (i = 0; i < N_TEST_ARR; i++)
30 test_arr[i] = NULL;
32 for (i = 0; i < N_TEST_TRANSACTIONS; i++) {
33 j = myrandom() % N_TEST_ARR;
34 if (test_arr[j]) {
35 free(test_arr[j]);
36 test_arr[j] = NULL;
37 } else {
38 nbytes = 1 + myrandom() % M_TEST_MALLOC;
39 if (myrandom()%64 == 32)
40 nbytes *= 17;
41 test_arr[j] = malloc( nbytes );
42 chp = test_arr[j];
43 for (k = 1; k < nbytes; k++)
44 chp[k] = (unsigned char)(k + 99);
48 for (i = 0; test_arr[i] == NULL; i++) ;
49 free(test_arr[i]);
50 ((char*)test_arr[i])[0] = 0;
52 for (i = 0; i < N_TEST_ARR; i++) {
53 if (test_arr[i]) {
54 free(test_arr[i]);
55 test_arr[i] = NULL;
59 return 0;