4 * @brief Helper functions and definitions.
9 * Copyright (C) 2005-2006 Martin Willi
10 * Copyright (C) 2005 Jan Hutter
11 * Hochschule fuer Technik Rapperswil
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
32 #include <printf_hook.h>
34 ENUM(status_names
, SUCCESS
, DESTROY_ME
,
50 * Described in header.
52 void *clalloc(void * pointer
, size_t size
)
57 memcpy(data
, pointer
,size
);
63 * Described in header.
65 void memxor(u_int8_t dest
[], u_int8_t src
[], size_t n
)
68 for (i
= 0; i
< n
; i
++)
75 * We use a single mutex for all refcount variables. This
76 * is not optimal for performance, but the critical section
78 * TODO: Consider to include a mutex in each refcount_t variable.
80 static pthread_mutex_t ref_mutex
= PTHREAD_MUTEX_INITIALIZER
;
83 * Described in header.
85 * TODO: May be implemented with atomic CPU instructions
88 void ref_get(refcount_t
*ref
)
90 pthread_mutex_lock(&ref_mutex
);
92 pthread_mutex_unlock(&ref_mutex
);
96 * Described in header.
98 * TODO: May be implemented with atomic CPU instructions
101 bool ref_put(refcount_t
*ref
)
105 pthread_mutex_lock(&ref_mutex
);
106 more_refs
= --(*ref
);
107 pthread_mutex_unlock(&ref_mutex
);
112 * output handler in printf() for time_t
114 static int print_time(FILE *stream
, const struct printf_info
*info
,
115 const void *const *args
)
117 static const char* months
[] = {
118 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
119 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
121 time_t *time
= *((time_t**)(args
[0]));
127 utc
= *((bool*)(args
[1]));
129 if (time
== UNDEFINED_TIME
)
131 return fprintf(stream
, "--- -- --:--:--%s----",
132 info
->alt ?
" UTC " : " ");
140 localtime_r(time
, &t
);
142 return fprintf(stream
, "%s %02d %02d:%02d:%02d%s%04d",
143 months
[t
.tm_mon
], t
.tm_mday
, t
.tm_hour
, t
.tm_min
,
144 t
.tm_sec
, utc ?
" UTC " : " ", t
.tm_year
+ 1900);
148 * output handler in printf() for time deltas
150 static int print_time_delta(FILE *stream
, const struct printf_info
*info
,
151 const void *const *args
)
153 time_t *start
= *((time_t**)(args
[0]));
154 time_t *end
= *((time_t**)(args
[1]));
155 u_int delta
= abs(*end
- *start
);
157 char* unit
= "second";
159 if (delta
> 2 * 60 * 60 * 24)
161 delta
/= 60 * 60 * 24;
164 else if (delta
> 2 * 60 * 60)
169 else if (delta
> 2 * 60)
174 return fprintf(stream
, "%d %s%s", delta
, unit
, (delta
== 1)?
"":"s");
178 * register printf() handlers for time_t
180 static void __attribute__ ((constructor
))print_register()
182 register_printf_function(PRINTF_TIME
, print_time
, arginfo_ptr_alt_ptr_int
);
183 register_printf_function(PRINTF_TIME_DELTA
, print_time_delta
, arginfo_ptr_ptr
);