summaryrefslogtreecommitdiff
path: root/util/goodies.h
blob: 79933a9a2b888c836720ee1f6f357f48acba3e4f (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
// goodies.h
// miscellaneous junk

#pragma once

#include "../stdafx.h"

#if !defined(_WIN32)
#include <pthread.h>
inline pthread_t GetCurrentThreadId() { return pthread_self(); }
#endif

/* set to TRUE if we are exiting */
extern bool goingAway;

bool isPrime(int n); 
int nextPrime(int n);

inline void dumpmemory(const char *data, int len) { 
	if( len > 1024 )
		len = 1024;
	try {
	const char *q = data;
	const char *p = q;
	while( len > 0 ) {
		for( int i = 0; i < 16; i++ ) { 
			if( *p >= 32 && *p <= 126 )
				cout << *p;
			else 
				cout << '.';
			p++;
		}
		cout << "  ";
		p -= 16;
		for( int i = 0; i < 16; i++ )
			cout << (unsigned) ((unsigned char)*p++) << ' ';
		cout << endl;
		len -= 16;
	}
	} catch(...) {
	}
}

#undef yassert
#include <boost/thread/thread.hpp>
#include <boost/thread/xtime.hpp>
#undef assert
#define assert xassert
#define yassert 1

struct WrappingInt {
	WrappingInt() { x = 0; } 
	WrappingInt(unsigned z) : x(z) { }
	unsigned x;
	operator unsigned() const { return x; }
	WrappingInt& operator++() { x++; return *this; }
	static int diff(unsigned a, unsigned b) { return a-b; }
	bool operator<=(WrappingInt r) { 
		// platform dependent
		int df = (r.x - x);
		return df >= 0;
	}
	bool operator>(WrappingInt r) { return !(r<=*this); }
};

#include <ctime>

inline void time_t_to_String(time_t t, char *buf) {
#if defined(_WIN32)
	ctime_s(buf, 64, &t);
#else
	ctime_r(&t, buf);
#endif
	buf[24] = 0; // don't want the \n
}

inline void sleepsecs(int s) { 
	boost::xtime xt;
	boost::xtime_get(&xt, boost::TIME_UTC);
	xt.sec += s;
	boost::thread::sleep(xt);
}
inline void sleepmillis(int s) { 
	boost::xtime xt;
	boost::xtime_get(&xt, boost::TIME_UTC);
	xt.nsec += s * 1000000;
	boost::thread::sleep(xt);
}
// note this wraps
inline int tdiff(unsigned told, unsigned tnew) { 
	return WrappingInt::diff(tnew, told);
}
inline unsigned curTimeMillis() {
	boost::xtime xt;
	boost::xtime_get(&xt, boost::TIME_UTC);
	unsigned t = xt.nsec / 1000000;
	return (xt.sec & 0xfffff) * 1000 + t;
}
inline unsigned long long jsTime() {
	boost::xtime xt;
	boost::xtime_get(&xt, boost::TIME_UTC);
	unsigned long long t = xt.nsec / 1000000;
	return ((unsigned long long) xt.sec * 1000) + t;
}
// measures up to 1024 seconds.  or, 512 seconds with tdiff that is...
inline unsigned curTimeMicros() {
	boost::xtime xt;
	boost::xtime_get(&xt, boost::TIME_UTC);
	unsigned t = xt.nsec / 1000;
	unsigned secs = xt.sec % 1024;
	t = secs*1000000 + t;
	return t;
}
using namespace boost;
typedef boost::mutex::scoped_lock lock;

// simple scoped timer
class Timer { 
public:
	Timer() { old = curTimeMicros(); }
	int millis() { return micros() / 1000; }
	int micros() { 
		unsigned n = curTimeMicros();
		return tdiff(old, n); 
	}
private:
	unsigned old;
};