1 /*
2  * Copyright (c) 2014 Anatol Belski
3  * All rights reserved.
4  *
5  * Author: Anatol Belski <ab@php.net>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *	notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *	notice, this list of conditions and the following disclaimer in the
14  *	documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $Id$
29  */
30 
31 #ifndef PHP_HRTIME_H
32 #define PHP_HRTIME_H
33 
34 #define PHP_HRTIME_VERSION "0.6.0"
35 
36 extern zend_module_entry hrtime_module_entry;
37 #define phpext_hrtime_ptr &hrtime_module_entry
38 
39 #ifdef PHP_WIN32
40 #	define PHP_HRTIME_API __declspec(dllexport)
41 #elif defined(__GNUC__) && __GNUC__ >= 4
42 #	define PHP_HRTIME_API __attribute__ ((visibility("default")))
43 #else
44 #	define PHP_HRTIME_API
45 #endif
46 
47 #ifdef ZTS
48 #include "TSRM.h"
49 #endif
50 
51 #include "timer.h"
52 
53 PHP_MINIT_FUNCTION(hrtime);
54 PHP_MSHUTDOWN_FUNCTION(hrtime);
55 PHP_RINIT_FUNCTION(hrtime);
56 PHP_RSHUTDOWN_FUNCTION(hrtime);
57 PHP_MINFO_FUNCTION(hrtime);
58 
59 PHP_METHOD(PerformanceCounter, getFrequency);
60 PHP_METHOD(PerformanceCounter, getTicks);
61 PHP_METHOD(PerformanceCounter, getTicksSince);
62 
63 PHP_METHOD(StopWatch, start);
64 PHP_METHOD(StopWatch, stop);
65 PHP_METHOD(StopWatch, getElapsedTicks);
66 PHP_METHOD(StopWatch, getLastElapsedTicks);
67 PHP_METHOD(StopWatch, isRunning);
68 PHP_METHOD(StopWatch, getElapsedTime);
69 PHP_METHOD(StopWatch, getLastElapsedTime);
70 
71 
72 /*ZEND_BEGIN_MODULE_GLOBALS(hrtime)
73 ZEND_END_MODULE_GLOBALS(hrtime)*/
74 
75 #ifdef ZTS
76 #define HRTIME_G(v) TSRMG(hrtime_globals_id, zend_hrtime_globals *, v)
77 #else
78 #define HRTIME_G(v) (hrtime_globals.v)
79 #endif
80 
81 #if PHP_MAJOR_VERSION < 7
82 typedef long zend_long;
83 #endif
84 
85 #if PHP_MAJOR_VERSION >= 7
86 struct ze_stop_watch_obj {
87 	tick_t start;
88 	tick_t elapsed;
89 	tick_t elapsed_ref;
90 	zend_bool is_running;
91 	zend_object zo;
92 };
93 
94 static zend_always_inline struct ze_stop_watch_obj *
php_stop_watch_fetch_obj(zend_object * obj)95 php_stop_watch_fetch_obj(zend_object *obj)
96 {/*{{{*/
97 	return (struct ze_stop_watch_obj *)((char *)obj - XtOffsetOf(struct ze_stop_watch_obj, zo));
98 }/*}}}*/
99 #else
100 struct ze_stop_watch_obj {
101 	zend_object zo;
102 	tick_t start;
103 	tick_t elapsed;
104 	tick_t elapsed_ref;
105 	zend_bool is_running;
106 };
107 #endif
108 
109 #if PHP_VERSION_ID >= 50399
110 zend_object_handlers default_hrtime_handlers;
111 #endif
112 
113 #endif	/* PHP_HRTIME_H */
114 
115 /*
116  * Local variables:
117  * tab-width: 4
118  * c-basic-offset: 4
119  * End:
120  * vim600: noet sw=4 ts=4 fdm=marker
121  * vim<600: noet sw=4 ts=4
122  */
123