common.h 2.55 KB
Newer Older
1 2 3
#ifndef _COMMON_H_
#define _COMMON_H_

4 5
#define _POSIX_C_SOURCE 200809L

6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <stdio.h>
#include <stdbool.h>
#include <errno.h>

#define __STDC_FORMAT_MACROS
#include <inttypes.h>

#ifdef MINGW
#include <winsock2.h>
#include <windows.h>
#endif


/**
20 21 22 23
 * Compiler attributes.
 */
#define PACKED __attribute__ ((__packed__))

24
#if defined(WHOLE) && __has_attribute(externally_visible)
25 26 27 28 29 30 31 32 33
#define VISIBLE __attribute__ ((externally_visible))
#else
#define VISIBLE
#endif



/**
 * Returns the size of an array in array elements.
34 35 36 37 38
 */
#define sizeof_array(array) \
	(sizeof (array) / sizeof ((array) [0]))


39 40 41 42 43 44 45
/**
 * Returns the size of a structure member.
 */
#define sizeof_member(type, member) \
	(sizeof (((type *) 0)->member))


46 47 48 49 50 51 52 53
/**
 * Prints a debug message to stdout, unless NDEBUG is defined.
 *
 * @format	format string for printf
 * @args	arguments associated with the format string
 *
 */
#ifdef NDEBUG
54
#  define debug(args...) do {} while (0)
55
#else
56
#  define debug(args...) fprintf (stdout, args); fflush (stdout)
57 58 59
#endif


60 61 62
#define ldebug(args...) { \
	debug ("%s:%d: ", __FUNCTION__, __LINE__); \
	debug (args); \
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
}

//

#define warn(args...) { \
	fprintf (stderr, "warning: "); \
	fprintf (stderr, args); \
}

//

int find_value_index (const char * strval, const char * values [], int nvals);

//

#define ERROR_GENERIC 10000
#define ERROR_SERVER 10003
#define ERROR_STD 10002
#define ERROR_JVMTI 10003

83
#define ERROR_PREFIX "disl-agent: error: "
84 85 86 87 88 89 90 91 92 93 94

//

void die_with_error (const char * message);
void die_with_std_error (const char * message, int errnum);


/**
 * Reports a general error and terminates the program if the provided
 * error condition is true.
 */
95
static inline void
96 97 98 99 100 101 102 103 104 105 106
check_error (bool error, const char * message) {
	if (error) {
		die_with_error (message);
	}
}


/**
 * Reports a standard library error and terminates the program if the provided
 * error condition is true.
 */
107
static inline void
108 109 110 111 112 113
check_std_error (bool error, const char * message) {
	if (error) {
		die_with_std_error (message, errno);
	}
}

114 115 116 117 118 119 120 121 122 123 124 125

/**
 * Warns about a standard library error if the provided
 * error condition is true.
 */
static inline void
warn_std_error (bool error, const char * message) {
	if (error) {
		warn (message, errno);
	}
}

126 127 128 129 130 131 132 133 134 135 136
//

#ifdef MINGW

void die_with_win_error (const char * message, DWORD errnum);


/**
 * Reports a windows error and terminates the program if the provided
 * error condition is true.
 */
137
static inline void
138 139 140 141 142 143 144 145 146 147
check_win_error (bool error, const char * message) {
	if (error) {
		die_with_win_error (message, GetLastError ());
	}
}

#endif /* MINGW */


#endif /* _COMMON_H_ */