summaryrefslogtreecommitdiff
path: root/source4/torture/local/talloc.c
blob: 4c353ecbbdf32010454deee329715574534950ea (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* 
   Unix SMB/CIFS implementation.

   local testing of talloc routines.

   Copyright (C) Andrew Tridgell 2004
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"

static struct timeval tp1,tp2;

static void start_timer(void)
{
	gettimeofday(&tp1,NULL);
}

static double end_timer(void)
{
	gettimeofday(&tp2,NULL);
	return((tp2.tv_sec - tp1.tv_sec) + 
	       (tp2.tv_usec - tp1.tv_usec)*1.0e-6);
}

/*
  test references 
*/
static BOOL test_ref1(void)
{
	void *p1, *p2, *ref, *r1;

	printf("TESTING SINGLE REFERENCE FREE\n");

	p1 = talloc_named_const(NULL, 1, "p1");
	p2 = talloc_named_const(p1, 1, "p2");
	talloc_named_const(p1, 1, "x1");
	talloc_named_const(p1, 1, "x2");
	talloc_named_const(p1, 1, "x3");

	r1 = talloc_named_const(NULL, 1, "r1");	
	ref = talloc_reference(r1, p2);
	talloc_report_full(NULL, stdout);

	printf("Freeing p2\n");
	talloc_free(p2);
	talloc_report_full(NULL, stdout);

	printf("Freeing p1\n");
	talloc_free(p1);
	talloc_report_full(NULL, stdout);

	printf("Freeing r1\n");
	talloc_free(r1);
	talloc_report_full(NULL, stdout);

	if (talloc_total_size(NULL) != 0) {
		printf("non-zero total size\n");
		return False;
	}

	return True;
}

/*
  test references 
*/
static BOOL test_ref2(void)
{
	void *p1, *p2, *ref, *r1;

	printf("TESTING DOUBLE REFERENCE FREE\n");

	p1 = talloc_named_const(NULL, 1, "p1");
	talloc_named_const(p1, 1, "x1");
	talloc_named_const(p1, 1, "x2");
	talloc_named_const(p1, 1, "x3");
	p2 = talloc_named_const(p1, 1, "p2");

	r1 = talloc_named_const(NULL, 1, "r1");	
	ref = talloc_reference(r1, p2);
	talloc_report_full(NULL, stdout);
	printf("Freeing ref\n");
	talloc_free(ref);
	talloc_report_full(NULL, stdout);
	printf("Freeing p2\n");
	talloc_free(p2);
	talloc_report_full(NULL, stdout);
	printf("Freeing p1\n");
	talloc_free(p1);
	talloc_report_full(NULL, stdout);
	printf("Freeing r1\n");
	talloc_free(r1);
	talloc_report_full(NULL, stdout);

	if (talloc_total_size(NULL) != 0) {
		printf("non-zero total size\n");
		return False;
	}

	return True;
}

/*
  measure the speed of talloc versus malloc
*/
static BOOL test_speed(void)
{
	void *ctx = talloc(NULL, 0);
	uint_t count;

	printf("MEASURING TALLOC VS MALLOC SPEED\n");

	start_timer();
	count = 0;
	do {
		void *p1, *p2, *p3;
		p1 = talloc(ctx, count);
		p2 = talloc_strdup(p1, "foo bar");
		p3 = talloc(p1, 300);
		talloc_free(p1);
		count += 3;
	} while (end_timer() < 5.0);

	printf("talloc: %.0f ops/sec\n", count/end_timer());

	start_timer();
	count = 0;
	do {
		void *p1, *p2, *p3;
		p1 = malloc(count);
		p2 = strdup("foo bar");
		p3 = malloc(300);
		free(p1);
		free(p2);
		free(p3);
		count += 3;
	} while (end_timer() < 5.0);

	printf("malloc: %.0f ops/sec\n", count/end_timer());

	return True;	
}


BOOL torture_local_talloc(int dummy) 
{
	BOOL ret = True;

	init_iconv();

	ret &= test_ref1();
	ret &= test_ref2();
	ret &= test_speed();

	return True;
}