1#include <stdlib.h> 2 3#define nth_bit(x, n) ((x >> n) & 1) 4#define Fn(N, Np1) \ 5 void* a##N(int x) { return ( nth_bit(x, N) ? a##Np1(x) : a##Np1(x) ); } 6 7// This test allocates a lot of heap memory, and every allocation features a 8// different stack trace -- the stack traces are effectively a 9// representation of the number 'i', where each function represents a bit in 10// 'i', and if it's a 1 the first function is called, and if it's a 0 the 11// second function is called. 12 13void* a999(int x) 14{ 15 return malloc(100); 16} 17 18Fn(17, 999) 19Fn(16, 17) 20Fn(15, 16) 21Fn(14, 15) 22Fn(13, 14) 23Fn(12, 13) 24Fn(11, 12) 25Fn(10, 11) 26Fn( 9, 10) 27Fn( 8, 9) 28Fn( 7, 8) 29Fn( 6, 7) 30Fn( 5, 6) 31Fn( 4, 5) 32Fn( 3, 4) 33Fn( 2, 3) 34Fn( 1, 2) 35Fn( 0, 1) 36 37int main(void) 38{ 39 int i; 40 41 // Create a large XTree. 42 for (i = 0; i < (1 << 18); i++) 43 a0(i); 44 45 // Do a lot of allocations so it gets dup'd a lot of times. 46 for (i = 0; i < 100000; i++) { 47 free(a1(234)); 48 free(a2(111)); 49 } 50 51 return 0; 52} 53