2 * The UCW Library -- Miscellaneous Functions
4 * (c) 1997--2008 Martin Mares <mj@ucw.cz>
5 * (c) 2005 Tomas Valla <tom@ucw.cz>
6 * (c) 2006 Robert Spalek <robert@ucw.cz>
7 * (c) 2007 Pavel Charvat <pchar@ucw.cz>
9 * This software may be freely distributed and used according to the terms
10 * of the GNU Lesser General Public License.
16 #include "ucw/config.h"
19 /* Macros for handling structurues, offsets and alignment */
21 #define CHECK_PTR_TYPE(x, type) ((x)-(type)(x) + (type)(x))
22 #define PTR_TO(s, i) &((s*)0)->i
23 #define OFFSETOF(s, i) ((unsigned int) (uintptr_t) PTR_TO(s, i))
24 #define SKIP_BACK(s, i, p) ((s *)((char *)p - OFFSETOF(s, i)))
25 #define ALIGN_TO(s, a) (((s)+a-1)&~(a-1))
26 #define ALIGN_PTR(p, s) ((uintptr_t)(p) % (s) ? (typeof(p))((uintptr_t)(p) + (s) - (uintptr_t)(p) % (s)) : (p))
27 #define UNALIGNED_PART(ptr, type) (((uintptr_t) (ptr)) % sizeof(type))
29 /* Some other macros */
31 #define MIN(a,b) (((a)<(b))?(a):(b))
32 #define MAX(a,b) (((a)>(b))?(a):(b))
33 #define CLAMP(x,min,max) ({ int _t=x; (_t < min) ? min : (_t > max) ? max : _t; })
34 #define ABS(x) ((x) < 0 ? -(x) : (x))
35 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))
36 #define STRINGIFY(x) #x
37 #define STRINGIFY_EXPANDED(x) STRINGIFY(x)
38 #define GLUE(x,y) x##y
39 #define GLUE_(x,y) x##_##y
41 #define COMPARE(x,y) do { if ((x)<(y)) return -1; if ((x)>(y)) return 1; } while(0)
42 #define REV_COMPARE(x,y) COMPARE(y,x)
43 #define COMPARE_LT(x,y) do { if ((x)<(y)) return 1; if ((x)>(y)) return 0; } while(0)
44 #define COMPARE_GT(x,y) COMPARE_LT(y,x)
46 #define ROL(x, bits) (((x) << (bits)) | ((uns)(x) >> (sizeof(uns)*8 - (bits)))) /* Bitwise rotation of an uns to the left */
47 #define ROR(x, bits) (((uns)(x) >> (bits)) | ((x) << (sizeof(uns)*8 - (bits))))
54 #define NONRET __attribute__((noreturn))
55 #define UNUSED __attribute__((unused))
56 #define CONSTRUCTOR __attribute__((constructor))
57 #define PACKED __attribute__((packed))
58 #define CONST __attribute__((const))
59 #define PURE __attribute__((pure))
60 #define FORMAT_CHECK(x,y,z) __attribute__((format(x,y,z)))
61 #define likely(x) __builtin_expect((x),1)
62 #define unlikely(x) __builtin_expect((x),0)
64 #if __GNUC__ >= 4 || __GNUC__ == 3 && __GNUC_MINOR__ >= 3
65 #define ALWAYS_INLINE inline __attribute__((always_inline))
66 #define NO_INLINE __attribute__((noinline))
68 #define ALWAYS_INLINE inline
72 #define LIKE_MALLOC __attribute__((malloc))
73 #define SENTINEL_CHECK __attribute__((sentinel))
76 #define SENTINEL_CHECK
80 #error This program requires the GNU C compiler.
86 * === Basic logging functions (see <<log:,Logging>> and <ucw/log.h> for more)
89 enum log_levels { /** The available log levels to pass to msg() and friends. **/
90 L_DEBUG=0, // 'D' - Debugging
91 L_INFO, // 'I' - Informational
92 L_WARN, // 'W' - Warning
93 L_ERROR, // 'E' - Error, but non-critical
94 L_INFO_R, // 'i' - An alternative set of levels for messages caused by remote events
95 L_WARN_R, // 'w' (e.g., a packet received via network)
97 L_FATAL, // '!' - Fatal error
100 #define L_SIGHANDLER 0x80000000 /** Avoid operations that are unsafe in signal handlers **/
103 * This is the basic printf-like function for logging a message.
104 * The @flags contain the log level and possibly other flag bits (like @L_SIGHANDLER).
106 void msg(uns flags, const char *fmt, ...) FORMAT_CHECK(printf,2,3);
107 void vmsg(uns flags, const char *fmt, va_list args); /** A vararg version of msg(). **/
108 void die(const char *, ...) NONRET FORMAT_CHECK(printf,1,2); /** Log a fatal error message and exit the program. **/
110 extern char *log_title; /** An optional log message title. Set to program name by log_init(). **/
111 extern int log_pid; /** An optional PID printed in each log message. Set to 0 if it shouldn't be logged. **/
112 extern void (*log_die_hook)(void); /** An optional function called just before die() exists. **/
114 void log_init(const char *argv0); /** Set @log_title to the program name extracted from @argv[0]. **/
115 void log_fork(void); /** Call after fork() to update @log_pid. **/
116 void log_file(const char *name); /** Establish logging to the named file. Also redirect stderr there. **/
118 void assert_failed(const char *assertion, const char *file, int line) NONRET;
119 void assert_failed_noinfo(void) NONRET;
123 * Check an assertion. If the condition @x is false, stop the program with a fatal error.
124 * These checks are compiled only when @DEBUG_ASSERTS is defined.
126 #define ASSERT(x) ({ if (unlikely(!(x))) assert_failed(#x, __FILE__, __LINE__); 1; })
128 #define ASSERT(x) ({ if (__builtin_constant_p(x) && !(x)) assert_failed_noinfo(); 1; })
131 #define COMPILE_ASSERT(name,x) typedef char _COMPILE_ASSERT_##name[!!(x)-1]
134 #define DBG(x,y...) msg(L_DEBUG, x,##y) /** If @LOCAL_DEBUG is defined before including <ucw/lib.h>, log a debug message. Otherwise do nothing. **/
136 #define DBG(x,y...) do { } while(0)
139 /* Memory allocation */
141 #define xmalloc ucw_xmalloc
142 #define xrealloc ucw_xrealloc
143 #define xfree ucw_xfree
146 * Unfortunately, several libraries we might want to link to define
147 * their own xmalloc and we don't want to interfere with them, hence
150 void *xmalloc(uns) LIKE_MALLOC;
151 void *xrealloc(void *, uns);
154 void *xmalloc_zero(uns) LIKE_MALLOC;
155 char *xstrdup(const char *) LIKE_MALLOC;
159 timestamp_t get_timestamp(void);
161 void init_timer(timestamp_t *timer);
162 uns get_timer(timestamp_t *timer);
163 uns switch_timer(timestamp_t *oldt, timestamp_t *newt);
167 uns random_u32(void);
168 uns random_max(uns max);
169 u64 random_u64(void);
170 u64 random_max_u64(u64 max);
174 void *mmap_file(const char *name, unsigned *len, int writeable);
175 void munmap_file(void *start, unsigned len);
179 void setproctitle_init(int argc, char **argv);
180 void setproctitle(const char *msg, ...) FORMAT_CHECK(printf,1,2);
181 char *getproctitle(void);
185 void randomkey(byte *buf, uns size);
189 #define EXIT_STATUS_MSG_SIZE 32
190 int format_exit_status(char *msg, int stat);
194 int run_command(const char *cmd, ...);
195 void NONRET exec_command(const char *cmd, ...);
196 void echo_command(char *buf, int size, const char *cmd, ...);
197 int run_command_v(const char *cmd, va_list args);
198 void NONRET exec_command_v(const char *cmd, va_list args);
199 void echo_command_v(char *buf, int size, const char *cmd, va_list args);
203 int careful_read(int fd, void *buf, int len);
204 int careful_write(int fd, const void *buf, int len);
208 void sync_dir(const char *name);
212 typedef int (*ucw_sighandler_t)(int); // gets signum, returns nonzero if abort() should be called
214 void handle_signal(int signum);
215 void unhandle_signal(int signum);
216 ucw_sighandler_t set_signal_handler(int signum, ucw_sighandler_t newh);
220 void *page_alloc(u64 len) LIKE_MALLOC; // allocates a multiple of CPU_PAGE_SIZE bytes with mmap
221 void *page_alloc_zero(u64 len) LIKE_MALLOC;
222 void page_free(void *start, u64 len);
223 void *page_realloc(void *start, u64 old_len, u64 new_len);
225 void *big_alloc(u64 len) LIKE_MALLOC; // allocate a large memory block in the most efficient way available
226 void *big_alloc_zero(u64 len) LIKE_MALLOC;
227 void big_free(void *start, u64 len);