1 | /* |
---|
2 | * zzuf - general purpose fuzzer |
---|
3 | * Copyright (c) 2006 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * $Id: timer.c 1727 2007-02-01 15:57:10Z sam $ |
---|
7 | * |
---|
8 | * This program is free software. It comes without any warranty, to |
---|
9 | * the extent permitted by applicable law. You can redistribute it |
---|
10 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
11 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
12 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
13 | */ |
---|
14 | |
---|
15 | /* |
---|
16 | * timer.c: timing functions |
---|
17 | */ |
---|
18 | |
---|
19 | #include "config.h" |
---|
20 | |
---|
21 | #if defined HAVE_STDINT_H |
---|
22 | # include <stdint.h> |
---|
23 | #elif defined HAVE_INTTYPES_H |
---|
24 | # include <inttypes.h> |
---|
25 | #endif |
---|
26 | #if defined HAVE_WINDOWS_H |
---|
27 | # include <windows.h> |
---|
28 | #endif |
---|
29 | #if defined HAVE_SYS_TIME_H |
---|
30 | # include <sys/time.h> |
---|
31 | #endif |
---|
32 | #include <stdio.h> |
---|
33 | #include <time.h> |
---|
34 | |
---|
35 | #include "timer.h" |
---|
36 | |
---|
37 | int64_t _zz_time(void) |
---|
38 | { |
---|
39 | #if defined HAVE_GETTIMEOFDAY |
---|
40 | struct timeval tv; |
---|
41 | gettimeofday(&tv, NULL); |
---|
42 | return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec; |
---|
43 | #else |
---|
44 | static CRITICAL_SECTION cs; |
---|
45 | static unsigned long int prev; |
---|
46 | static int64_t tv_base = 0; |
---|
47 | unsigned long int tv_msec; |
---|
48 | |
---|
49 | if(tv_base == 0) |
---|
50 | { |
---|
51 | tv_base = 1; |
---|
52 | prev = 0; |
---|
53 | InitializeCriticalSection(&cs); |
---|
54 | } |
---|
55 | |
---|
56 | EnterCriticalSection(&cs); |
---|
57 | tv_msec = GetTickCount(); |
---|
58 | if(tv_msec < prev) |
---|
59 | tv_base += 0x100000000LL; /* We wrapped */ |
---|
60 | prev = tv_msec; |
---|
61 | LeaveCriticalSection(&cs); |
---|
62 | |
---|
63 | return (tv_base + tv_msec) * 1000; |
---|
64 | #endif |
---|
65 | } |
---|
66 | |
---|