2 This file is part of GNUnet.
3 Copyright (C) 2006-2013 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file include/gnunet_common.h
21 * @brief commonly used definitions; globals in this file
22 * are exempt from the rule that the module name ("common")
23 * must be part of the symbol name.
25 * @author Christian Grothoff
28 * @defgroup logging Logging
29 * @see [Documentation](https://gnunet.org/logging)
31 * @defgroup memory Memory management
33 #ifndef GNUNET_COMMON_H
34 #define GNUNET_COMMON_H
37 #include <sys/socket.h>
40 #include <netinet/in.h>
52 #ifdef HAVE_BYTESWAP_H
59 #if 0 /* keep Emacsens' auto-indent happy */
65 * Version of the API (for entire gnunetutil.so library).
67 #define GNUNET_UTIL_VERSION 0x000A0102
71 * Named constants for return values. The following invariants hold:
72 * `GNUNET_NO == 0` (to allow `if (GNUNET_NO)`) `GNUNET_OK !=
73 * GNUNET_SYSERR`, `GNUNET_OK != GNUNET_NO`, `GNUNET_NO !=
74 * GNUNET_SYSERR` and finally `GNUNET_YES != GNUNET_NO`.
77 #define GNUNET_SYSERR -1
81 #define GNUNET_MIN(a,b) (((a) < (b)) ? (a) : (b))
83 #define GNUNET_MAX(a,b) (((a) > (b)) ? (a) : (b))
85 /* some systems use one underscore only, and mingw uses no underscore... */
88 #define __BYTE_ORDER _BYTE_ORDER
91 #define __BYTE_ORDER BYTE_ORDER
97 #define __BIG_ENDIAN _BIG_ENDIAN
100 #define __BIG_ENDIAN BIG_ENDIAN
104 #ifndef __LITTLE_ENDIAN
105 #ifdef _LITTLE_ENDIAN
106 #define __LITTLE_ENDIAN _LITTLE_ENDIAN
109 #define __LITTLE_ENDIAN LITTLE_ENDIAN
116 * define #GNUNET_EXTRA_LOGGING if using this header outside the GNUnet source
117 * tree where gnunet_config.h is unavailable
119 #ifndef GNUNET_EXTRA_LOGGING
120 #define GNUNET_EXTRA_LOGGING 0
127 # if __BYTE_ORDER == __LITTLE_ENDIAN
128 # define GNUNET_htobe16(x) __bswap_16 (x)
129 # define GNUNET_htole16(x) (x)
130 # define GNUNET_be16toh(x) __bswap_16 (x)
131 # define GNUNET_le16toh(x) (x)
133 # define GNUNET_htobe32(x) __bswap_32 (x)
134 # define GNUNET_htole32(x) (x)
135 # define GNUNET_be32toh(x) __bswap_32 (x)
136 # define GNUNET_le32toh(x) (x)
138 # define GNUNET_htobe64(x) __bswap_64 (x)
139 # define GNUNET_htole64(x) (x)
140 # define GNUNET_be64toh(x) __bswap_64 (x)
141 # define GNUNET_le64toh(x) (x)
143 # if __BYTE_ORDER == __BIG_ENDIAN
144 # define GNUNET_htobe16(x) (x)
145 # define GNUNET_htole16(x) __bswap_16 (x)
146 # define GNUNET_be16toh(x) (x)
147 # define GNUNET_le16toh(x) __bswap_16 (x)
149 # define GNUNET_htobe32(x) (x)
150 # define GNUNET_htole32(x) __bswap_32 (x)
151 # define GNUNET_be32toh(x) (x)
152 # define GNUNET_le32toh(x) __bswap_32 (x)
154 # define GNUNET_htobe64(x) (x)
155 # define GNUNET_htole64(x) __bswap_64 (x)
156 # define GNUNET_be64toh(x) (x)
157 # define GNUNET_le64toh(x) __bswap_64 (x)
162 * Macro used to avoid using 0 for the length of a variable-size
163 * array (Non-Zero-Length).
165 * Basically, C standard says that "int[n] x;" is undefined if n=0.
166 * This was supposed to prevent issues with pointer aliasing.
167 * However, C compilers may conclude that n!=0 as n=0 would be
168 * undefined, and then optimize under the assumption n!=0, which
169 * could cause actual issues. Hence, when initializing an array
170 * on the stack with a variable-length that might be zero, write
171 * "int[GNUNET_NZL(n)] x;" instead of "int[n] x".
173 #define GNUNET_NZL(l) GNUNET_MAX(1,l)
177 * gcc-ism to get packed structs.
179 #define GNUNET_PACKED __attribute__((packed))
182 * gcc-ism to get gcc bitfield layout when compiling with -mms-bitfields
185 #define GNUNET_GCC_STRUCT_LAYOUT __attribute__((gcc_struct))
187 #define GNUNET_GCC_STRUCT_LAYOUT
191 * gcc-ism to force alignment; we use this to align char-arrays
192 * that may then be cast to 'struct's. See also gcc
195 #ifdef __BIGGEST_ALIGNMENT__
196 #define GNUNET_ALIGN __attribute__((aligned (__BIGGEST_ALIGNMENT__)))
198 #define GNUNET_ALIGN __attribute__((aligned (8)))
202 * gcc-ism to document unused arguments
204 #define GNUNET_UNUSED __attribute__((unused))
207 * gcc-ism to document functions that don't return
209 #define GNUNET_NORETURN __attribute__((noreturn))
214 * gcc 4.x-ism to pack structures even on W32 (to be used before structs);
215 * Using this would cause structs to be unaligned on the stack on Sparc,
216 * so we *only* use this on W32 (see #670578 from Debian); fortunately,
217 * W32 doesn't run on sparc anyway.
219 #define GNUNET_NETWORK_STRUCT_BEGIN \
220 _Pragma("pack(push)") \
224 * gcc 4.x-ism to pack structures even on W32 (to be used after structs)
225 * Using this would cause structs to be unaligned on the stack on Sparc,
226 * so we *only* use this on W32 (see #670578 from Debian); fortunately,
227 * W32 doesn't run on sparc anyway.
229 #define GNUNET_NETWORK_STRUCT_END _Pragma("pack(pop)")
232 #error gcc 4.x or higher required on W32 systems
236 * Define as empty, GNUNET_PACKED should suffice, but this won't work on W32
238 #define GNUNET_NETWORK_STRUCT_BEGIN
241 * Define as empty, GNUNET_PACKED should suffice, but this won't work on W32;
243 #define GNUNET_NETWORK_STRUCT_END
246 /* ************************ super-general types *********************** */
248 GNUNET_NETWORK_STRUCT_BEGIN
251 * Header for all communications.
253 struct GNUNET_MessageHeader
257 * The length of the struct (in bytes, including the length field itself),
258 * in big-endian format.
260 uint16_t size GNUNET_PACKED;
263 * The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
265 uint16_t type GNUNET_PACKED;
271 * Answer from service to client about last operation.
273 struct GNUNET_OperationResultMessage
275 struct GNUNET_MessageHeader header;
277 uint32_t reserved GNUNET_PACKED;
282 uint64_t op_id GNUNET_PACKED;
285 * Status code for the operation.
287 uint64_t result_code GNUNET_PACKED;
289 /* Followed by data. */
292 GNUNET_NETWORK_STRUCT_END
295 * Function called with a filename.
298 * @param filename complete filename (absolute path)
299 * @return #GNUNET_OK to continue to iterate,
300 * #GNUNET_NO to stop iteration with no error,
301 * #GNUNET_SYSERR to abort iteration with error!
304 (*GNUNET_FileNameCallback) (void *cls,
305 const char *filename);
309 * Generic continuation callback.
311 * @param cls Closure.
314 (*GNUNET_ContinuationCallback) (void *cls);
318 * Function called with the result of an asynchronous operation.
323 * Result code for the operation.
325 * Data result for the operation.
330 (*GNUNET_ResultCallback) (void *cls, int64_t result_code,
331 const void *data, uint16_t data_size);
334 /* ****************************** logging ***************************** */
340 enum GNUNET_ErrorType
342 GNUNET_ERROR_TYPE_UNSPECIFIED = -1,
343 GNUNET_ERROR_TYPE_NONE = 0,
344 GNUNET_ERROR_TYPE_ERROR = 1,
345 GNUNET_ERROR_TYPE_WARNING = 2,
346 /* UX: We need a message type that is output by
347 * default without looking like there is a problem.
349 GNUNET_ERROR_TYPE_MESSAGE = 4,
350 GNUNET_ERROR_TYPE_INFO = 8,
351 GNUNET_ERROR_TYPE_DEBUG = 16,
352 GNUNET_ERROR_TYPE_INVALID = 32,
353 GNUNET_ERROR_TYPE_BULK = 64
359 * User-defined handler for log messages.
362 * @param kind severeity
363 * @param component what component is issuing the message?
364 * @param date when was the message logged?
365 * @param message what is the message
368 (*GNUNET_Logger) (void *cls,
369 enum GNUNET_ErrorType kind,
370 const char *component,
372 const char *message);
377 * Get the number of log calls that are going to be skipped
379 * @return number of log calls to be ignored
382 GNUNET_get_log_skip (void);
385 #if !defined(GNUNET_CULL_LOGGING)
387 GNUNET_get_log_call_status (int caller_level,
390 const char *function,
399 * @param kind how serious is the error?
400 * @param message what is the message (format string)
401 * @param ... arguments for format string
404 GNUNET_log_nocheck (enum GNUNET_ErrorType kind, const char *message, ...)
405 __attribute__ ((format (gnu_printf, 2, 3)));
408 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
409 #define _GNUNET_BOOLEAN_EXPR(expr) \
411 int _gnunet_boolean_var_; \
413 _gnunet_boolean_var_ = 1; \
415 _gnunet_boolean_var_ = 0; \
416 _gnunet_boolean_var_; \
418 #define GN_LIKELY(expr) (__builtin_expect (_GNUNET_BOOLEAN_EXPR(expr), 1))
419 #define GN_UNLIKELY(expr) (__builtin_expect (_GNUNET_BOOLEAN_EXPR(expr), 0))
421 #define GN_LIKELY(expr) (expr)
422 #define GN_UNLIKELY(expr) (expr)
425 #if !defined(GNUNET_LOG_CALL_STATUS)
426 #define GNUNET_LOG_CALL_STATUS -1
432 * Log function that specifies an alternative component.
433 * This function should be used by plugins.
435 * @param kind how serious is the error?
436 * @param comp component responsible for generating the message
437 * @param message what is the message (format string)
438 * @param ... arguments for format string
441 GNUNET_log_from_nocheck (enum GNUNET_ErrorType kind, const char *comp,
442 const char *message, ...);
444 #if !defined(GNUNET_CULL_LOGGING)
445 #define GNUNET_log_from(kind,comp,...) do { int log_line = __LINE__;\
446 static int log_call_enabled = GNUNET_LOG_CALL_STATUS;\
447 if ((GNUNET_EXTRA_LOGGING > 0) || ((GNUNET_ERROR_TYPE_DEBUG & (kind)) == 0)) { \
448 if (GN_UNLIKELY(log_call_enabled == -1))\
449 log_call_enabled = GNUNET_get_log_call_status ((kind) & (~GNUNET_ERROR_TYPE_BULK), (comp), __FILE__, __FUNCTION__, log_line); \
450 if (GN_UNLIKELY(GNUNET_get_log_skip () > 0)) { GNUNET_log_skip (-1, GNUNET_NO); }\
452 if (GN_UNLIKELY(log_call_enabled))\
453 GNUNET_log_from_nocheck ((kind), comp, __VA_ARGS__); \
458 #define GNUNET_log(kind,...) do { int log_line = __LINE__;\
459 static int log_call_enabled = GNUNET_LOG_CALL_STATUS;\
460 if ((GNUNET_EXTRA_LOGGING > 0) || ((GNUNET_ERROR_TYPE_DEBUG & (kind)) == 0)) { \
461 if (GN_UNLIKELY(log_call_enabled == -1))\
462 log_call_enabled = GNUNET_get_log_call_status ((kind) & (~GNUNET_ERROR_TYPE_BULK), NULL, __FILE__, __FUNCTION__, log_line);\
463 if (GN_UNLIKELY(GNUNET_get_log_skip () > 0)) { GNUNET_log_skip (-1, GNUNET_NO); }\
465 if (GN_UNLIKELY(log_call_enabled))\
466 GNUNET_log_nocheck ((kind), __VA_ARGS__); \
471 #define GNUNET_log(...)
472 #define GNUNET_log_from(...)
478 * Log error message about missing configuration option.
480 * @param kind log level
481 * @param section section with missing option
482 * @param option name of missing option
485 GNUNET_log_config_missing (enum GNUNET_ErrorType kind,
492 * Log error message about invalid configuration option value.
494 * @param kind log level
495 * @param section section with invalid option
496 * @param option name of invalid option
497 * @param required what is required that is invalid about the option
500 GNUNET_log_config_invalid (enum GNUNET_ErrorType kind,
503 const char *required);
508 * Abort the process, generate a core dump if possible.
509 * Most code should use `GNUNET_assert (0)` instead to
510 * first log the location of the failure.
513 GNUNET_abort_ (void) GNUNET_NORETURN;
518 * Ignore the next @a n calls to the log function.
520 * @param n number of log calls to ignore (could be negative)
521 * @param check_reset #GNUNET_YES to assert that the log skip counter is currently zero
524 GNUNET_log_skip (int n,
532 * @param comp default component to use
533 * @param loglevel what types of messages should be logged
534 * @param logfile change logging to logfile (use NULL to keep stderr)
535 * @return #GNUNET_OK on success, #GNUNET_SYSERR if logfile could not be opened
538 GNUNET_log_setup (const char *comp,
539 const char *loglevel,
540 const char *logfile);
545 * Add a custom logger. Note that installing any custom logger
546 * will disable the standard logger. When multiple custom loggers
547 * are installed, all will be called. The standard logger will
548 * only be used if no custom loggers are present.
550 * @param logger log function
551 * @param logger_cls closure for @a logger
554 GNUNET_logger_add (GNUNET_Logger logger,
560 * Remove a custom logger.
562 * @param logger log function
563 * @param logger_cls closure for @a logger
566 GNUNET_logger_remove (GNUNET_Logger logger,
572 * Convert a short hash value to a string (for printing debug messages).
573 * This is one of the very few calls in the entire API that is
576 * @param shc the hash code
580 GNUNET_sh2s (const struct GNUNET_ShortHashCode *shc);
585 * Convert a hash value to a string (for printing debug messages).
586 * This is one of the very few calls in the entire API that is
589 * @param hc the hash code
593 GNUNET_h2s (const struct GNUNET_HashCode *hc);
598 * Convert a hash value to a string (for printing debug messages).
599 * This is one of the very few calls in the entire API that is
600 * NOT reentrant! Identical to #GNUNET_h2s(), except that another
601 * buffer is used so both #GNUNET_h2s() and #GNUNET_h2s2() can be
602 * used within the same log statement.
604 * @param hc the hash code
608 GNUNET_h2s2 (const struct GNUNET_HashCode *hc);
613 * Convert a hash value to a string (for printing debug messages).
614 * This prints all 104 characters of a hashcode!
615 * This is one of the very few calls in the entire API that is
618 * @param hc the hash code
622 GNUNET_h2s_full (const struct GNUNET_HashCode *hc);
626 * Public key. Details in gnunet_util_crypto.h.
628 struct GNUNET_CRYPTO_EddsaPublicKey;
632 * Public key. Details in gnunet_util_crypto.h.
634 struct GNUNET_CRYPTO_EcdhePublicKey;
639 * Convert a public key value to a string (for printing debug messages).
640 * This is one of the very few calls in the entire API that is
643 * @param hc the hash code
647 GNUNET_p2s (const struct GNUNET_CRYPTO_EddsaPublicKey *p);
652 * Convert a public key value to a string (for printing debug messages).
653 * This is one of the very few calls in the entire API that is
656 * @param hc the hash code
660 GNUNET_p2s2 (const struct GNUNET_CRYPTO_EddsaPublicKey *p);
665 * Convert a public key value to a string (for printing debug messages).
666 * This is one of the very few calls in the entire API that is
669 * @param hc the hash code
673 GNUNET_e2s (const struct GNUNET_CRYPTO_EcdhePublicKey *p);
678 * Convert a public key value to a string (for printing debug messages).
679 * This is one of the very few calls in the entire API that is
682 * @param hc the hash code
686 GNUNET_e2s2 (const struct GNUNET_CRYPTO_EcdhePublicKey *p);
691 * Convert a peer identity to a string (for printing debug messages).
692 * This is one of the very few calls in the entire API that is
695 * @param pid the peer identity
696 * @return string form of the pid; will be overwritten by next
697 * call to #GNUNET_i2s().
700 GNUNET_i2s (const struct GNUNET_PeerIdentity *pid);
705 * Convert a peer identity to a string (for printing debug messages).
706 * This is one of the very few calls in the entire API that is
707 * NOT reentrant! Identical to #GNUNET_i2s(), except that another
708 * buffer is used so both #GNUNET_i2s() and #GNUNET_i2s2() can be
709 * used within the same log statement.
711 * @param pid the peer identity
712 * @return string form of the pid; will be overwritten by next
713 * call to #GNUNET_i2s().
716 GNUNET_i2s2 (const struct GNUNET_PeerIdentity *pid);
721 * Convert a peer identity to a string (for printing debug messages).
722 * This is one of the very few calls in the entire API that is
725 * @param pid the peer identity
726 * @return string form of the pid; will be overwritten by next
727 * call to #GNUNET_i2s_full().
730 GNUNET_i2s_full (const struct GNUNET_PeerIdentity *pid);
735 * Convert a "struct sockaddr*" (IPv4 or IPv6 address) to a string
736 * (for printing debug messages). This is one of the very few calls
737 * in the entire API that is NOT reentrant!
739 * @param addr the address
740 * @param addrlen the length of the @a addr
741 * @return nicely formatted string for the address
742 * will be overwritten by next call to #GNUNET_a2s().
745 GNUNET_a2s (const struct sockaddr *addr,
751 * Convert error type to string.
753 * @param kind type to convert
754 * @return string corresponding to the type
757 GNUNET_error_type_to_string (enum GNUNET_ErrorType kind);
762 * Use this for fatal errors that cannot be handled
764 #define GNUNET_assert(cond) do { if (! (cond)) { GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Assertion failed at %s:%d.\n"), __FILE__, __LINE__); GNUNET_abort_(); } } while(0)
769 * Use this for fatal errors that cannot be handled
771 #define GNUNET_assert_at(cond, f, l) do { if (! (cond)) { GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Assertion failed at %s:%d.\n"), f, l); GNUNET_abort_(); } } while(0)
776 * Use this for fatal errors that cannot be handled
778 * @param cond Condition to evaluate
779 * @param comp Component string to use for logging
781 #define GNUNET_assert_from(cond, comp) do { if (! (cond)) { GNUNET_log_from(GNUNET_ERROR_TYPE_ERROR, comp, _("Assertion failed at %s:%d.\n"), __FILE__, __LINE__); GNUNET_abort_(); } } while(0)
786 * Use this for internal assertion violations that are
787 * not fatal (can be handled) but should not occur.
789 #define GNUNET_break(cond) do { if (! (cond)) { GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Assertion failed at %s:%d.\n"), __FILE__, __LINE__); } } while(0)
794 * Use this for assertion violations caused by other
795 * peers (i.e. protocol violations). We do not want to
796 * confuse end-users (say, some other peer runs an
797 * older, broken or incompatible GNUnet version), but
798 * we still want to see these problems during
799 * development and testing. "OP == other peer".
801 #define GNUNET_break_op(cond) do { if (! (cond)) { GNUNET_log(GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, _("External protocol violation detected at %s:%d.\n"), __FILE__, __LINE__); } } while(0)
806 * Log an error message at log-level 'level' that indicates
807 * a failure of the command 'cmd' with the message given
808 * by strerror(errno).
810 #define GNUNET_log_strerror(level, cmd) do { GNUNET_log(level, _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, STRERROR(errno)); } while(0)
815 * Log an error message at log-level 'level' that indicates
816 * a failure of the command 'cmd' with the message given
817 * by strerror(errno).
819 #define GNUNET_log_from_strerror(level, component, cmd) do { GNUNET_log_from (level, component, _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, STRERROR(errno)); } while(0)
824 * Log an error message at log-level 'level' that indicates
825 * a failure of the command 'cmd' with the message given
826 * by strerror(errno).
828 #define GNUNET_log_strerror_file(level, cmd, filename) do { GNUNET_log(level, _("`%s' failed on file `%s' at %s:%d with error: %s\n"), cmd, filename, __FILE__, __LINE__, STRERROR(errno)); } while(0)
833 * Log an error message at log-level 'level' that indicates
834 * a failure of the command 'cmd' with the message given
835 * by strerror(errno).
837 #define GNUNET_log_from_strerror_file(level, component, cmd, filename) do { GNUNET_log_from (level, component, _("`%s' failed on file `%s' at %s:%d with error: %s\n"), cmd, filename,__FILE__, __LINE__, STRERROR(errno)); } while(0)
839 /* ************************* endianess conversion ****************** */
842 * Convert unsigned 64-bit integer to network byte order.
845 * The value in host byte order.
847 * @return The same value in network byte order.
850 GNUNET_htonll (uint64_t n);
854 * Convert unsigned 64-bit integer to host byte order.
857 * The value in network byte order.
859 * @return The same value in host byte order.
862 GNUNET_ntohll (uint64_t n);
866 * Convert double to network byte order.
869 * The value in host byte order.
871 * @return The same value in network byte order.
874 GNUNET_hton_double (double d);
878 * Convert double to host byte order
881 * The value in network byte order.
883 * @return The same value in host byte order.
886 GNUNET_ntoh_double (double d);
889 /* ************************* allocation functions ****************** */
893 * Maximum allocation with GNUNET_malloc macro.
895 #define GNUNET_MAX_MALLOC_CHECKED (1024 * 1024 * 40)
899 * Allocate a struct or union of the given @a type.
900 * Wrapper around #GNUNET_malloc that returns a pointer
901 * to the newly created object of the correct type.
903 * @param type name of the struct or union, i.e. pass 'struct Foo'.
905 #define GNUNET_new(type) (type *) GNUNET_malloc (sizeof (type))
908 * Call memcpy() but check for @a n being 0 first. In the latter
909 * case, it is now safe to pass NULL for @a src or @a dst.
910 * Unlike traditional memcpy(), returns nothing.
912 * @param dst destination of the copy, may be NULL if @a n is zero
913 * @param src source of the copy, may be NULL if @a n is zero
914 * @param n number of bytes to copy
916 #define GNUNET_memcpy(dst,src,n) do { if (0 != n) { (void) memcpy (dst,src,n); } } while (0)
921 * Allocate a size @a n array with structs or unions of the given @a type.
922 * Wrapper around #GNUNET_malloc that returns a pointer
923 * to the newly created objects of the correct type.
925 * @param n number of elements in the array
926 * @param type name of the struct or union, i.e. pass 'struct Foo'.
928 #define GNUNET_new_array(n, type) (type *) GNUNET_malloc ((n) * sizeof (type))
932 * Allocate a size @a n times @a m array
933 * with structs or unions of the given @a type.
935 * @param n size of the first dimension
936 * @param m size of the second dimension
937 * @param type name of the struct or union, i.e. pass 'struct Foo'.
939 #define GNUNET_new_array_2d(n, m, type) (type **) GNUNET_xnew_array_2d_ (n, m, sizeof (type), __FILE__, __LINE__)
943 * Allocate a size @a n times @a m times @a o array
944 * with structs or unions of the given @a type.
946 * @param n size of the first dimension
947 * @param m size of the second dimension
948 * @param o size of the third dimension
949 * @param type name of the struct or union, i.e. pass 'struct Foo'.
951 #define GNUNET_new_array_3d(n, m, o, type) (type ***) GNUNET_xnew_array_3d_ (n, m, o, sizeof (type), __FILE__, __LINE__)
955 * Wrapper around malloc. Allocates size bytes of memory.
956 * The memory will be zero'ed out.
958 * @param size the number of bytes to allocate, must be
959 * smaller than 40 MB.
960 * @return pointer to size bytes of memory, never NULL (!)
962 #define GNUNET_malloc(size) GNUNET_xmalloc_(size, __FILE__, __LINE__)
966 * Allocate and initialize a block of memory.
968 * @param buf data to initalize the block with
969 * @param size the number of bytes in buf (and size of the allocation)
970 * @return pointer to size bytes of memory, never NULL (!)
972 #define GNUNET_memdup(buf,size) GNUNET_xmemdup_(buf, size, __FILE__, __LINE__)
976 * Wrapper around malloc. Allocates size bytes of memory.
977 * The memory will be zero'ed out.
979 * @param size the number of bytes to allocate
980 * @return pointer to size bytes of memory, NULL if we do not have enough memory
982 #define GNUNET_malloc_large(size) GNUNET_xmalloc_unchecked_(size, __FILE__, __LINE__)
986 * Wrapper around realloc. Reallocates size bytes of memory.
987 * The content of the intersection of the new and old size will be unchanged.
989 * @param ptr the pointer to reallocate
990 * @param size the number of bytes to reallocate
991 * @return pointer to size bytes of memory
993 #define GNUNET_realloc(ptr, size) GNUNET_xrealloc_(ptr, size, __FILE__, __LINE__)
997 * Wrapper around free. Frees the memory referred to by ptr.
998 * Note that it is generally better to free memory that was
999 * allocated with #GNUNET_array_grow using #GNUNET_array_grow(mem, size, 0) instead of #GNUNET_free.
1001 * @param ptr location where to free the memory. ptr must have
1002 * been returned by #GNUNET_strdup, #GNUNET_strndup, #GNUNET_malloc or #GNUNET_array_grow earlier.
1004 #define GNUNET_free(ptr) GNUNET_xfree_(ptr, __FILE__, __LINE__)
1008 * Free the memory pointed to by ptr if ptr is not NULL.
1009 * Equivalent to `if (NULL != ptr) GNUNET_free(ptr)`.
1011 * @param ptr the location in memory to free
1013 #define GNUNET_free_non_null(ptr) do { void * __x__ = ptr; if (__x__ != NULL) { GNUNET_free(__x__); } } while(0)
1017 * Wrapper around #GNUNET_xstrdup_. Makes a copy of the zero-terminated string
1020 * @param a pointer to a zero-terminated string
1021 * @return a copy of the string including zero-termination
1023 #define GNUNET_strdup(a) GNUNET_xstrdup_(a,__FILE__,__LINE__)
1027 * Wrapper around #GNUNET_xstrndup_. Makes a partial copy of the string
1030 * @param a pointer to a string
1031 * @param length of the string to duplicate
1032 * @return a partial copy of the string including zero-termination
1034 #define GNUNET_strndup(a,length) GNUNET_xstrndup_(a,length,__FILE__,__LINE__)
1038 * Grow a well-typed (!) array. This is a convenience
1039 * method to grow a vector @a arr of size @a size
1040 * to the new (target) size @a tsize.
1043 * Example (simple, well-typed stack):
1046 * static struct foo * myVector = NULL;
1047 * static int myVecLen = 0;
1049 * static void push(struct foo * elem) {
1050 * GNUNET_array_grow(myVector, myVecLen, myVecLen+1);
1051 * GNUNET_memcpy(&myVector[myVecLen-1], elem, sizeof(struct foo));
1054 * static void pop(struct foo * elem) {
1055 * if (myVecLen == 0) die();
1056 * GNUNET_memcpy(elem, myVector[myVecLen-1], sizeof(struct foo));
1057 * GNUNET_array_grow(myVector, myVecLen, myVecLen-1);
1061 * @param arr base-pointer of the vector, may be NULL if size is 0;
1062 * will be updated to reflect the new address. The TYPE of
1063 * arr is important since size is the number of elements and
1064 * not the size in bytes
1065 * @param size the number of elements in the existing vector (number
1066 * of elements to copy over), will be updated with the new
1068 * @param tsize the target size for the resulting vector, use 0 to
1069 * free the vector (then, arr will be NULL afterwards).
1071 #define GNUNET_array_grow(arr,size,tsize) GNUNET_xgrow_((void**)&arr, sizeof(arr[0]), &size, tsize, __FILE__, __LINE__)
1075 * Append an element to a list (growing the list by one).
1077 * @param arr base-pointer of the vector, may be NULL if size is 0;
1078 * will be updated to reflect the new address. The TYPE of
1079 * arr is important since size is the number of elements and
1080 * not the size in bytes
1081 * @param size the number of elements in the existing vector (number
1082 * of elements to copy over), will be updated with the new
1084 * @param element the element that will be appended to the array
1086 #define GNUNET_array_append(arr,size,element) do { GNUNET_array_grow(arr,size,size+1); arr[size-1] = element; } while(0)
1090 * Like snprintf, just aborts if the buffer is of insufficient size.
1092 * @param buf pointer to buffer that is written to
1093 * @param size number of bytes in @a buf
1094 * @param format format strings
1095 * @param ... data for format string
1096 * @return number of bytes written to buf or negative value on error
1099 GNUNET_snprintf (char *buf, size_t size, const char *format, ...);
1104 * Like asprintf, just portable.
1106 * @param buf set to a buffer of sufficient size (allocated, caller must free)
1107 * @param format format string (see printf, fprintf, etc.)
1108 * @param ... data for format string
1109 * @return number of bytes in "*buf" excluding 0-termination
1112 GNUNET_asprintf (char **buf, const char *format, ...);
1115 /* ************** internal implementations, use macros above! ************** */
1118 * Allocate memory. Checks the return value, aborts if no more
1119 * memory is available. Don't use GNUNET_xmalloc_ directly. Use the
1120 * #GNUNET_malloc macro.
1121 * The memory will be zero'ed out.
1123 * @param size number of bytes to allocate
1124 * @param filename where is this call being made (for debugging)
1125 * @param linenumber line where this call is being made (for debugging)
1126 * @return allocated memory, never NULL
1129 GNUNET_xmalloc_ (size_t size, const char *filename, int linenumber);
1133 * Allocate memory for a two dimensional array in one block
1134 * and set up pointers. Aborts if no more memory is available.
1135 * Don't use GNUNET_xnew_array_2d_ directly. Use the
1136 * #GNUNET_new_array_2d macro.
1137 * The memory of the elements will be zero'ed out.
1139 * @param n size of the first dimension
1140 * @param m size of the second dimension
1141 * @param elementSize size of a single element in bytes
1142 * @param filename where is this call being made (for debugging)
1143 * @param linenumber line where this call is being made (for debugging)
1144 * @return allocated memory, never NULL
1147 GNUNET_xnew_array_2d_ (size_t n, size_t m, size_t elementSize,
1148 const char *filename, int linenumber);
1152 * Allocate memory for a three dimensional array in one block
1153 * and set up pointers. Aborts if no more memory is available.
1154 * Don't use GNUNET_xnew_array_3d_ directly. Use the
1155 * #GNUNET_new_array_3d macro.
1156 * The memory of the elements will be zero'ed out.
1158 * @param n size of the first dimension
1159 * @param m size of the second dimension
1160 * @param o size of the third dimension
1161 * @param elementSize size of a single element in bytes
1162 * @param filename where is this call being made (for debugging)
1163 * @param linenumber line where this call is being made (for debugging)
1164 * @return allocated memory, never NULL
1167 GNUNET_xnew_array_3d_ (size_t n, size_t m, size_t o, size_t elementSize,
1168 const char *filename, int linenumber);
1172 * Allocate and initialize memory. Checks the return value, aborts if no more
1173 * memory is available. Don't use GNUNET_xmemdup_ directly. Use the
1174 * #GNUNET_memdup macro.
1176 * @param buf buffer to initialize from (must contain size bytes)
1177 * @param size number of bytes to allocate
1178 * @param filename where is this call being made (for debugging)
1179 * @param linenumber line where this call is being made (for debugging)
1180 * @return allocated memory, never NULL
1183 GNUNET_xmemdup_ (const void *buf, size_t size, const char *filename,
1188 * Allocate memory. This function does not check if the allocation
1189 * request is within reasonable bounds, allowing allocations larger
1190 * than 40 MB. If you don't expect the possibility of very large
1191 * allocations, use #GNUNET_malloc instead. The memory will be zero'ed
1194 * @param size number of bytes to allocate
1195 * @param filename where is this call being made (for debugging)
1196 * @param linenumber line where this call is being made (for debugging)
1197 * @return pointer to size bytes of memory, NULL if we do not have enough memory
1200 GNUNET_xmalloc_unchecked_ (size_t size, const char *filename, int linenumber);
1204 * Reallocate memory. Checks the return value, aborts if no more
1205 * memory is available.
1208 GNUNET_xrealloc_ (void *ptr, size_t n, const char *filename, int linenumber);
1212 * Free memory. Merely a wrapper for the case that we
1213 * want to keep track of allocations. Don't use GNUNET_xfree_
1214 * directly. Use the #GNUNET_free macro.
1216 * @param ptr pointer to memory to free
1217 * @param filename where is this call being made (for debugging)
1218 * @param linenumber line where this call is being made (for debugging)
1221 GNUNET_xfree_ (void *ptr, const char *filename, int linenumber);
1225 * Dup a string. Don't call GNUNET_xstrdup_ directly. Use the #GNUNET_strdup macro.
1226 * @param str string to duplicate
1227 * @param filename where is this call being made (for debugging)
1228 * @param linenumber line where this call is being made (for debugging)
1229 * @return the duplicated string
1232 GNUNET_xstrdup_ (const char *str, const char *filename, int linenumber);
1235 * Dup partially a string. Don't call GNUNET_xstrndup_ directly. Use the #GNUNET_strndup macro.
1237 * @param str string to duplicate
1238 * @param len length of the string to duplicate
1239 * @param filename where is this call being made (for debugging)
1240 * @param linenumber line where this call is being made (for debugging)
1241 * @return the duplicated string
1244 GNUNET_xstrndup_ (const char *str, size_t len, const char *filename,
1248 * Grow an array, the new elements are zeroed out.
1249 * Grows old by (*oldCount-newCount)*elementSize
1250 * bytes and sets *oldCount to newCount.
1252 * Don't call GNUNET_xgrow_ directly. Use the #GNUNET_array_grow macro.
1254 * @param old address of the pointer to the array
1256 * @param elementSize the size of the elements of the array
1257 * @param oldCount address of the number of elements in the *old array
1258 * @param newCount number of elements in the new array, may be 0 (then *old will be NULL afterwards)
1259 * @param filename where is this call being made (for debugging)
1260 * @param linenumber line where this call is being made (for debugging)
1263 GNUNET_xgrow_ (void **old, size_t elementSize, unsigned int *oldCount,
1264 unsigned int newCount, const char *filename, int linenumber);
1269 * Create a copy of the given message.
1271 * @param msg message to copy
1272 * @return duplicate of the message
1274 struct GNUNET_MessageHeader *
1275 GNUNET_copy_message (const struct GNUNET_MessageHeader *msg);
1278 #if __STDC_VERSION__ < 199901L
1280 #define __func__ __FUNCTION__
1282 #define __func__ "<unknown>"
1288 * Valid task priorities. Use these, do not pass random integers!
1289 * For various reasons (#3862 -- building with QT Creator, and
1290 * our restricted cross-compilation with emscripten) this cannot
1291 * be in gnunet_scheduler_lib.h, but it works if we declare it here.
1292 * Naturally, logically this is part of the scheduler.
1294 enum GNUNET_SCHEDULER_Priority
1297 * Run with the same priority as the current job.
1299 GNUNET_SCHEDULER_PRIORITY_KEEP = 0,
1302 * Run when otherwise idle.
1304 GNUNET_SCHEDULER_PRIORITY_IDLE = 1,
1307 * Run as background job (higher than idle,
1308 * lower than default).
1310 GNUNET_SCHEDULER_PRIORITY_BACKGROUND = 2,
1313 * Run with the default priority (normal
1314 * P2P operations). Any task that is scheduled
1315 * without an explicit priority being specified
1316 * will run with this priority.
1318 GNUNET_SCHEDULER_PRIORITY_DEFAULT = 3,
1321 * Run with high priority (important requests).
1322 * Higher than DEFAULT.
1324 GNUNET_SCHEDULER_PRIORITY_HIGH = 4,
1327 * Run with priority for interactive tasks.
1328 * Higher than "HIGH".
1330 GNUNET_SCHEDULER_PRIORITY_UI = 5,
1333 * Run with priority for urgent tasks. Use
1334 * for things like aborts and shutdowns that
1335 * need to preempt "UI"-level tasks.
1338 GNUNET_SCHEDULER_PRIORITY_URGENT = 6,
1341 * This is an internal priority level that is only used for tasks
1342 * that are being triggered due to shutdown (they have automatically
1343 * highest priority). User code must not use this priority level
1344 * directly. Tasks run with this priority level that internally
1345 * schedule other tasks will see their original priority level
1346 * be inherited (unless otherwise specified).
1348 GNUNET_SCHEDULER_PRIORITY_SHUTDOWN = 7,
1351 * Number of priorities (must be the last priority).
1352 * This priority must not be used by clients.
1354 GNUNET_SCHEDULER_PRIORITY_COUNT = 8
1358 #if 0 /* keep Emacsens' auto-indent happy */
1365 #endif /* GNUNET_COMMON_H */