summaryrefslogtreecommitdiffstats
path: root/tools/perf.c
blob: 7c6c0f142a82c883ba31e9e54cc508123ca4d37d (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
/* iksemel (XML parser for Jabber)
** Copyright (C) 2000-2003 Gurer Ozen
** This code is free software; you can redistribute it and/or
** modify it under the terms of GNU Lesser General Public License.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#include <windows.h>
#include <limits.h>
#else
#include <sys/time.h>
#endif

#include "iksemel.h"

/* timing functions */

#ifdef _WIN32
static DWORD start_tv;

void
t_reset (void)
{
	start_tv = GetTickCount ();
}

unsigned long
t_elapsed (void)
{
	DWORD end_tv;

	end_tv = GetTickCount ();
	if (end_tv < start_tv)
		return UINT_MAX - (start_tv - end_tv - 1);
	else
		return end_tv - start_tv;
}

#else
static struct timeval start_tv;

void
t_reset (void)
{
	gettimeofday (&start_tv, NULL);
}

unsigned long
t_elapsed (void)
{
	unsigned long msec;
	struct timeval cur_tv;

	gettimeofday (&cur_tv, NULL);
	msec = (cur_tv.tv_sec * 1000) + (cur_tv.tv_usec / 1000);
	msec -= (start_tv.tv_sec * 1000) + (start_tv.tv_usec / 1000);
	return msec;
}
#endif

/* memory functions */

static void *
m_malloc (size_t size)
{
	void *ptr = malloc (size);
	printf ("MEM: malloc (%d) => %p\n", size, ptr);
	return ptr;
}

static void
m_free (void *ptr)
{
	printf ("MEM: free (%p)\n", ptr);
}

void
m_trace (void)
{
	iks_set_mem_funcs (m_malloc, m_free);
}