summaryrefslogtreecommitdiff
path: root/tests/grab.c
blob: b65fa28042054849d3a7d56c740952c168b7e395 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

#include <stdio.h>
#include <malloc.h>

struct s
{
   struct s * n;
   char v[1];
};

#define M  ((unsigned)-1>>1)
#define V  (M^(M>>1))

main (argc,argv)
int argc;
char ** argv;
{
	struct s * ptr1 = 0;
	struct s * ptr2;
	struct s * ptr3;
	int i,sz;
	unsigned long total = 0;

	for(i=0, sz=256 ; i<32; i++, sz = ((sz << 1) | (sz & V)) & M)
	{
		ptr2 = (struct s *) malloc(sz-sizeof(int));
		printf("%2d(%8u)..%08lx..%ld\n",i,sz,(long)ptr2,(long)ptr2);
		if(ptr2==0) break;
		total+=sz;
		if(ptr1==0)
		{
			ptr1 = ptr3 = ptr2;
			ptr3->n = 0;
		}
		else
		{
			ptr3->n = ptr2;
			ptr3 = ptr2;
			ptr3->n = 0;
		}
	}
	for(sz>>=1; sz>3; )
	{
		ptr2 = (struct s *) malloc(sz-sizeof(int));
		if(ptr2==0) { sz >>=1; continue; }
		printf("%2d(%8u)..%08lx..%ld\n",i++,sz,(long)ptr2,(long)ptr2);
		total+=sz;
		if(ptr1==0)
		{
			ptr1 = ptr3 = ptr2;
			ptr3->n = 0;
		}
		else
		{
			ptr3->n = ptr2;
			ptr3 = ptr2;
			ptr3->n = 0;
		}
	}
	printf("Free all - total was %ldK bytes\n", total/1024);
	while( ptr1 )
	{
		ptr3 = ptr1->n;
		free(ptr1);
		ptr1 = ptr3;
	}
	ptr2 = (struct s *) malloc(200);
	printf("%2d(%8u)..%08lx..%ld\n",i++,200,(long)ptr2,(long)ptr2);
	ptr2 = (struct s *) malloc(30000);
	printf("%2d(%8u)..%08lx..%ld\n",i++,30000,(long)ptr2,(long)ptr2);
	ptr2 = (struct s *) malloc(20000);
	printf("%2d(%8u)..%08lx..%ld\n",i++,20000,(long)ptr2,(long)ptr2);
	sz = (256<<sizeof(int));
        do
        {
	   ptr2 = (struct s *) malloc(sz-sizeof(int));
	   printf("%2d(%8u)..%08lx..%ld\n",i++,sz,(long)ptr2,(long)ptr2);
        }
	while(ptr2 && i < 100);
	exit(0);
}