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.
17 * @file include/gnunet_common.h
18 * @brief commonly used definitions; globals in this file
19 * are exempt from the rule that the module name ("common")
20 * must be part of the symbol name.
22 * @author Christian Grothoff
25 * @defgroup logging Logging
26 * @see [Documentation](https://gnunet.org/logging)
28 * @defgroup memory Memory management
30 #ifndef GNUNET_COMMON_H
31 #define GNUNET_COMMON_H
34 #include <sys/socket.h>
37 #include <netinet/in.h>
49 #ifdef HAVE_BYTESWAP_H
56 #if 0 /* keep Emacsens' auto-indent happy */
62 * Version of the API (for entire gnunetutil.so library).
64 #define GNUNET_UTIL_VERSION 0x000A0102
68 * Named constants for return values. The following invariants hold:
69 * `GNUNET_NO == 0` (to allow `if (GNUNET_NO)`) `GNUNET_OK !=
70 * GNUNET_SYSERR`, `GNUNET_OK != GNUNET_NO`, `GNUNET_NO !=
71 * GNUNET_SYSERR` and finally `GNUNET_YES != GNUNET_NO`.
74 #define GNUNET_SYSERR -1
78 #define GNUNET_MIN(a,b) (((a) < (b)) ? (a) : (b))
80 #define GNUNET_MAX(a,b) (((a) > (b)) ? (a) : (b))
82 /* some systems use one underscore only, and mingw uses no underscore... */
85 #define __BYTE_ORDER _BYTE_ORDER
88 #define __BYTE_ORDER BYTE_ORDER
94 #define __BIG_ENDIAN _BIG_ENDIAN
97 #define __BIG_ENDIAN BIG_ENDIAN
101 #ifndef __LITTLE_ENDIAN
102 #ifdef _LITTLE_ENDIAN
103 #define __LITTLE_ENDIAN _LITTLE_ENDIAN
106 #define __LITTLE_ENDIAN LITTLE_ENDIAN
113 * define #GNUNET_EXTRA_LOGGING if using this header outside the GNUnet source
114 * tree where gnunet_config.h is unavailable
116 #ifndef GNUNET_EXTRA_LOGGING
117 #define GNUNET_EXTRA_LOGGING 0
124 # if __BYTE_ORDER == __LITTLE_ENDIAN
125 # define GNUNET_htobe16(x) __bswap_16 (x)
126 # define GNUNET_htole16(x) (x)
127 # define GNUNET_be16toh(x) __bswap_16 (x)
128 # define GNUNET_le16toh(x) (x)
130 # define GNUNET_htobe32(x) __bswap_32 (x)
131 # define GNUNET_htole32(x) (x)
132 # define GNUNET_be32toh(x) __bswap_32 (x)
133 # define GNUNET_le32toh(x) (x)
135 # define GNUNET_htobe64(x) __bswap_64 (x)
136 # define GNUNET_htole64(x) (x)
137 # define GNUNET_be64toh(x) __bswap_64 (x)
138 # define GNUNET_le64toh(x) (x)
140 # if __BYTE_ORDER == __BIG_ENDIAN
141 # define GNUNET_htobe16(x) (x)
142 # define GNUNET_htole16(x) __bswap_16 (x)
143 # define GNUNET_be16toh(x) (x)
144 # define GNUNET_le16toh(x) __bswap_16 (x)
146 # define GNUNET_htobe32(x) (x)
147 # define GNUNET_htole32(x) __bswap_32 (x)
148 # define GNUNET_be32toh(x) (x)
149 # define GNUNET_le32toh(x) __bswap_32 (x)
151 # define GNUNET_htobe64(x) (x)
152 # define GNUNET_htole64(x) __bswap_64 (x)
153 # define GNUNET_be64toh(x) (x)
154 # define GNUNET_le64toh(x) __bswap_64 (x)
159 * Macro used to avoid using 0 for the length of a variable-size
160 * array (Non-Zero-Length).
162 * Basically, C standard says that "int[n] x;" is undefined if n=0.
163 * This was supposed to prevent issues with pointer aliasing.
164 * However, C compilers may conclude that n!=0 as n=0 would be
165 * undefined, and then optimize under the assumption n!=0, which
166 * could cause actual issues. Hence, when initializing an array
167 * on the stack with a variable-length that might be zero, write
168 * "int[GNUNET_NZL(n)] x;" instead of "int[n] x".
170 #define GNUNET_NZL(l) GNUNET_MAX(1,l)
174 * gcc-ism to get packed structs.
176 #define GNUNET_PACKED __attribute__((packed))
179 * gcc-ism to get gcc bitfield layout when compiling with -mms-bitfields
182 #define GNUNET_GCC_STRUCT_LAYOUT __attribute__((gcc_struct))
184 #define GNUNET_GCC_STRUCT_LAYOUT
188 * gcc-ism to force alignment; we use this to align char-arrays
189 * that may then be cast to 'struct's. See also gcc
192 #ifdef __BIGGEST_ALIGNMENT__
193 #define GNUNET_ALIGN __attribute__((aligned (__BIGGEST_ALIGNMENT__)))
195 #define GNUNET_ALIGN __attribute__((aligned (8)))
199 * gcc-ism to document unused arguments
201 #define GNUNET_UNUSED __attribute__((unused))
204 * gcc-ism to document functions that don't return
206 #define GNUNET_NORETURN __attribute__((noreturn))
211 * gcc 4.x-ism to pack structures even on W32 (to be used before structs);
212 * Using this would cause structs to be unaligned on the stack on Sparc,
213 * so we *only* use this on W32 (see #670578 from Debian); fortunately,
214 * W32 doesn't run on sparc anyway.
216 #define GNUNET_NETWORK_STRUCT_BEGIN \
217 _Pragma("pack(push)") \
221 * gcc 4.x-ism to pack structures even on W32 (to be used after structs)
222 * Using this would cause structs to be unaligned on the stack on Sparc,
223 * so we *only* use this on W32 (see #670578 from Debian); fortunately,
224 * W32 doesn't run on sparc anyway.
226 #define GNUNET_NETWORK_STRUCT_END _Pragma("pack(pop)")
229 #error gcc 4.x or higher required on W32 systems
233 * Define as empty, GNUNET_PACKED should suffice, but this won't work on W32
235 #define GNUNET_NETWORK_STRUCT_BEGIN
238 * Define as empty, GNUNET_PACKED should suffice, but this won't work on W32;
240 #define GNUNET_NETWORK_STRUCT_END
243 /* ************************ super-general types *********************** */
245 GNUNET_NETWORK_STRUCT_BEGIN
248 * Header for all communications.
250 struct GNUNET_MessageHeader
254 * The length of the struct (in bytes, including the length field itself),
255 * in big-endian format.
257 uint16_t size GNUNET_PACKED;
260 * The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
262 uint16_t type GNUNET_PACKED;
268 * Answer from service to client about last operation.
270 struct GNUNET_OperationResultMessage
272 struct GNUNET_MessageHeader header;
274 uint32_t reserved GNUNET_PACKED;
279 uint64_t op_id GNUNET_PACKED;
282 * Status code for the operation.
284 uint64_t result_code GNUNET_PACKED;
286 /* Followed by data. */
289 GNUNET_NETWORK_STRUCT_END
292 * Function called with a filename.
295 * @param filename complete filename (absolute path)
296 * @return #GNUNET_OK to continue to iterate,
297 * #GNUNET_NO to stop iteration with no error,
298 * #GNUNET_SYSERR to abort iteration with error!
301 (*GNUNET_FileNameCallback) (void *cls,
302 const char *filename);
306 * Generic continuation callback.
308 * @param cls Closure.
311 (*GNUNET_ContinuationCallback) (void *cls);
315 * Function called with the result of an asynchronous operation.
320 * Result code for the operation.
322 * Data result for the operation.
327 (*GNUNET_ResultCallback) (void *cls, int64_t result_code,
328 const void *data, uint16_t data_size);
331 /* ****************************** logging ***************************** */
337 enum GNUNET_ErrorType
339 GNUNET_ERROR_TYPE_UNSPECIFIED = -1,
340 GNUNET_ERROR_TYPE_NONE = 0,
341 GNUNET_ERROR_TYPE_ERROR = 1,
342 GNUNET_ERROR_TYPE_WARNING = 2,
343 /* UX: We need a message type that is output by
344 * default without looking like there is a problem.
346 GNUNET_ERROR_TYPE_MESSAGE = 4,
347 GNUNET_ERROR_TYPE_INFO = 8,
348 GNUNET_ERROR_TYPE_DEBUG = 16,
349 GNUNET_ERROR_TYPE_INVALID = 32,
350 GNUNET_ERROR_TYPE_BULK = 64
356 * User-defined handler for log messages.
359 * @param kind severeity
360 * @param component what component is issuing the message?
361 * @param date when was the message logged?
362 * @param message what is the message
365 (*GNUNET_Logger) (void *cls,
366 enum GNUNET_ErrorType kind,
367 const char *component,
369 const char *message);
374 * Get the number of log calls that are going to be skipped
376 * @return number of log calls to be ignored
379 GNUNET_get_log_skip (void);
382 #if !defined(GNUNET_CULL_LOGGING)
384 GNUNET_get_log_call_status (int caller_level,
387 const char *function,
396 * @param kind how serious is the error?
397 * @param message what is the message (format string)
398 * @param ... arguments for format string
401 GNUNET_log_nocheck (enum GNUNET_ErrorType kind, const char *message, ...)
402 __attribute__ ((format (gnu_printf, 2, 3)));
405 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
406 #define _GNUNET_BOOLEAN_EXPR(expr) \
408 int _gnunet_boolean_var_; \
410 _gnunet_boolean_var_ = 1; \
412 _gnunet_boolean_var_ = 0; \
413 _gnunet_boolean_var_; \
415 #define GN_LIKELY(expr) (__builtin_expect (_GNUNET_BOOLEAN_EXPR(expr), 1))
416 #define GN_UNLIKELY(expr) (__builtin_expect (_GNUNET_BOOLEAN_EXPR(expr), 0))
418 #define GN_LIKELY(expr) (expr)
419 #define GN_UNLIKELY(expr) (expr)
422 #if !defined(GNUNET_LOG_CALL_STATUS)
423 #define GNUNET_LOG_CALL_STATUS -1
429 * Log function that specifies an alternative component.
430 * This function should be used by plugins.
432 * @param kind how serious is the error?
433 * @param comp component responsible for generating the message
434 * @param message what is the message (format string)
435 * @param ... arguments for format string
438 GNUNET_log_from_nocheck (enum GNUNET_ErrorType kind, const char *comp,
439 const char *message, ...);
441 #if !defined(GNUNET_CULL_LOGGING)
442 #define GNUNET_log_from(kind,comp,...) do { int log_line = __LINE__;\
443 static int log_call_enabled = GNUNET_LOG_CALL_STATUS;\
444 if ((GNUNET_EXTRA_LOGGING > 0) || ((GNUNET_ERROR_TYPE_DEBUG & (kind)) == 0)) { \
445 if (GN_UNLIKELY(log_call_enabled == -1))\
446 log_call_enabled = GNUNET_get_log_call_status ((kind) & (~GNUNET_ERROR_TYPE_BULK), (comp), __FILE__, __FUNCTION__, log_line); \
447 if (GN_UNLIKELY(GNUNET_get_log_skip () > 0)) { GNUNET_log_skip (-1, GNUNET_NO); }\
449 if (GN_UNLIKELY(log_call_enabled))\
450 GNUNET_log_from_nocheck ((kind), comp, __VA_ARGS__); \
455 #define GNUNET_log(kind,...) do { int log_line = __LINE__;\
456 static int log_call_enabled = GNUNET_LOG_CALL_STATUS;\
457 if ((GNUNET_EXTRA_LOGGING > 0) || ((GNUNET_ERROR_TYPE_DEBUG & (kind)) == 0)) { \
458 if (GN_UNLIKELY(log_call_enabled == -1))\
459 log_call_enabled = GNUNET_get_log_call_status ((kind) & (~GNUNET_ERROR_TYPE_BULK), NULL, __FILE__, __FUNCTION__, log_line);\
460 if (GN_UNLIKELY(GNUNET_get_log_skip () > 0)) { GNUNET_log_skip (-1, GNUNET_NO); }\
462 if (GN_UNLIKELY(log_call_enabled))\
463 GNUNET_log_nocheck ((kind), __VA_ARGS__); \
468 #define GNUNET_log(...)
469 #define GNUNET_log_from(...)
475 * Log error message about missing configuration option.
477 * @param kind log level
478 * @param section section with missing option
479 * @param option name of missing option
482 GNUNET_log_config_missing (enum GNUNET_ErrorType kind,
489 * Log error message about invalid configuration option value.
491 * @param kind log level
492 * @param section section with invalid option
493 * @param option name of invalid option
494 * @param required what is required that is invalid about the option
497 GNUNET_log_config_invalid (enum GNUNET_ErrorType kind,
500 const char *required);
505 * Abort the process, generate a core dump if possible.
506 * Most code should use `GNUNET_assert (0)` instead to
507 * first log the location of the failure.
510 GNUNET_abort_ (void) GNUNET_NORETURN;
515 * Ignore the next @a n calls to the log function.
517 * @param n number of log calls to ignore (could be negative)
518 * @param check_reset #GNUNET_YES to assert that the log skip counter is currently zero
521 GNUNET_log_skip (int n,
529 * @param comp default component to use
530 * @param loglevel what types of messages should be logged
531 * @param logfile change logging to logfile (use NULL to keep stderr)
532 * @return #GNUNET_OK on success, #GNUNET_SYSERR if logfile could not be opened
535 GNUNET_log_setup (const char *comp,
536 const char *loglevel,
537 const char *logfile);
542 * Add a custom logger. Note that installing any custom logger
543 * will disable the standard logger. When multiple custom loggers
544 * are installed, all will be called. The standard logger will
545 * only be used if no custom loggers are present.
547 * @param logger log function
548 * @param logger_cls closure for @a logger
551 GNUNET_logger_add (GNUNET_Logger logger,
557 * Remove a custom logger.
559 * @param logger log function
560 * @param logger_cls closure for @a logger
563 GNUNET_logger_remove (GNUNET_Logger logger,
569 * Convert a short hash value to a string (for printing debug messages).
570 * This is one of the very few calls in the entire API that is
573 * @param shc the hash code
577 GNUNET_sh2s (const struct GNUNET_ShortHashCode *shc);
582 * Convert a hash value to a string (for printing debug messages).
583 * This is one of the very few calls in the entire API that is
586 * @param hc the hash code
590 GNUNET_h2s (const struct GNUNET_HashCode *hc);
595 * Convert a hash value to a string (for printing debug messages).
596 * This is one of the very few calls in the entire API that is
597 * NOT reentrant! Identical to #GNUNET_h2s(), except that another
598 * buffer is used so both #GNUNET_h2s() and #GNUNET_h2s2() can be
599 * used within the same log statement.
601 * @param hc the hash code
605 GNUNET_h2s2 (const struct GNUNET_HashCode *hc);
610 * Convert a hash value to a string (for printing debug messages).
611 * This prints all 104 characters of a hashcode!
612 * This is one of the very few calls in the entire API that is
615 * @param hc the hash code
619 GNUNET_h2s_full (const struct GNUNET_HashCode *hc);
623 * Public key. Details in gnunet_util_crypto.h.
625 struct GNUNET_CRYPTO_EddsaPublicKey;
629 * Public key. Details in gnunet_util_crypto.h.
631 struct GNUNET_CRYPTO_EcdhePublicKey;
636 * Convert a public key value to a string (for printing debug messages).
637 * This is one of the very few calls in the entire API that is
640 * @param hc the hash code
644 GNUNET_p2s (const struct GNUNET_CRYPTO_EddsaPublicKey *p);
649 * Convert a public key value to a string (for printing debug messages).
650 * This is one of the very few calls in the entire API that is
653 * @param hc the hash code
657 GNUNET_p2s2 (const struct GNUNET_CRYPTO_EddsaPublicKey *p);
662 * Convert a public key value to a string (for printing debug messages).
663 * This is one of the very few calls in the entire API that is
666 * @param hc the hash code
670 GNUNET_e2s (const struct GNUNET_CRYPTO_EcdhePublicKey *p);
675 * Convert a public key value to a string (for printing debug messages).
676 * This is one of the very few calls in the entire API that is
679 * @param hc the hash code
683 GNUNET_e2s2 (const struct GNUNET_CRYPTO_EcdhePublicKey *p);
688 * Convert a peer identity to a string (for printing debug messages).
689 * This is one of the very few calls in the entire API that is
692 * @param pid the peer identity
693 * @return string form of the pid; will be overwritten by next
694 * call to #GNUNET_i2s().
697 GNUNET_i2s (const struct GNUNET_PeerIdentity *pid);
702 * Convert a peer identity to a string (for printing debug messages).
703 * This is one of the very few calls in the entire API that is
704 * NOT reentrant! Identical to #GNUNET_i2s(), except that another
705 * buffer is used so both #GNUNET_i2s() and #GNUNET_i2s2() can be
706 * used within the same log statement.
708 * @param pid the peer identity
709 * @return string form of the pid; will be overwritten by next
710 * call to #GNUNET_i2s().
713 GNUNET_i2s2 (const struct GNUNET_PeerIdentity *pid);
718 * Convert a peer identity to a string (for printing debug messages).
719 * This is one of the very few calls in the entire API that is
722 * @param pid the peer identity
723 * @return string form of the pid; will be overwritten by next
724 * call to #GNUNET_i2s_full().
727 GNUNET_i2s_full (const struct GNUNET_PeerIdentity *pid);
732 * Convert a "struct sockaddr*" (IPv4 or IPv6 address) to a string
733 * (for printing debug messages). This is one of the very few calls
734 * in the entire API that is NOT reentrant!
736 * @param addr the address
737 * @param addrlen the length of the @a addr
738 * @return nicely formatted string for the address
739 * will be overwritten by next call to #GNUNET_a2s().
742 GNUNET_a2s (const struct sockaddr *addr,
748 * Convert error type to string.
750 * @param kind type to convert
751 * @return string corresponding to the type
754 GNUNET_error_type_to_string (enum GNUNET_ErrorType kind);
759 * Use this for fatal errors that cannot be handled
761 #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)
766 * Use this for fatal errors that cannot be handled
768 #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)
773 * Use this for fatal errors that cannot be handled
775 * @param cond Condition to evaluate
776 * @param comp Component string to use for logging
778 #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)
783 * Use this for internal assertion violations that are
784 * not fatal (can be handled) but should not occur.
786 #define GNUNET_break(cond) do { if (! (cond)) { GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Assertion failed at %s:%d.\n"), __FILE__, __LINE__); } } while(0)
791 * Use this for assertion violations caused by other
792 * peers (i.e. protocol violations). We do not want to
793 * confuse end-users (say, some other peer runs an
794 * older, broken or incompatible GNUnet version), but
795 * we still want to see these problems during
796 * development and testing. "OP == other peer".
798 #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)
803 * Log an error message at log-level 'level' that indicates
804 * a failure of the command 'cmd' with the message given
805 * by strerror(errno).
807 #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)
812 * Log an error message at log-level 'level' that indicates
813 * a failure of the command 'cmd' with the message given
814 * by strerror(errno).
816 #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)
821 * Log an error message at log-level 'level' that indicates
822 * a failure of the command 'cmd' with the message given
823 * by strerror(errno).
825 #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)
830 * Log an error message at log-level 'level' that indicates
831 * a failure of the command 'cmd' with the message given
832 * by strerror(errno).
834 #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)
836 /* ************************* endianess conversion ****************** */
839 * Convert unsigned 64-bit integer to network byte order.
842 * The value in host byte order.
844 * @return The same value in network byte order.
847 GNUNET_htonll (uint64_t n);
851 * Convert unsigned 64-bit integer to host byte order.
854 * The value in network byte order.
856 * @return The same value in host byte order.
859 GNUNET_ntohll (uint64_t n);
863 * Convert double to network byte order.
866 * The value in host byte order.
868 * @return The same value in network byte order.
871 GNUNET_hton_double (double d);
875 * Convert double to host byte order
878 * The value in network byte order.
880 * @return The same value in host byte order.
883 GNUNET_ntoh_double (double d);
886 /* ************************* allocation functions ****************** */
890 * Maximum allocation with GNUNET_malloc macro.
892 #define GNUNET_MAX_MALLOC_CHECKED (1024 * 1024 * 40)
896 * Allocate a struct or union of the given @a type.
897 * Wrapper around #GNUNET_malloc that returns a pointer
898 * to the newly created object of the correct type.
900 * @param type name of the struct or union, i.e. pass 'struct Foo'.
902 #define GNUNET_new(type) (type *) GNUNET_malloc (sizeof (type))
905 * Call memcpy() but check for @a n being 0 first. In the latter
906 * case, it is now safe to pass NULL for @a src or @a dst.
907 * Unlike traditional memcpy(), returns nothing.
909 * @param dst destination of the copy, may be NULL if @a n is zero
910 * @param src source of the copy, may be NULL if @a n is zero
911 * @param n number of bytes to copy
913 #define GNUNET_memcpy(dst,src,n) do { if (0 != n) { (void) memcpy (dst,src,n); } } while (0)
918 * Allocate a size @a n array with structs or unions of the given @a type.
919 * Wrapper around #GNUNET_malloc that returns a pointer
920 * to the newly created objects of the correct type.
922 * @param n number of elements in the array
923 * @param type name of the struct or union, i.e. pass 'struct Foo'.
925 #define GNUNET_new_array(n, type) (type *) GNUNET_malloc ((n) * sizeof (type))
929 * Allocate a size @a n times @a m array
930 * with structs or unions of the given @a type.
932 * @param n size of the first dimension
933 * @param m size of the second dimension
934 * @param type name of the struct or union, i.e. pass 'struct Foo'.
936 #define GNUNET_new_array_2d(n, m, type) (type **) GNUNET_xnew_array_2d_ (n, m, sizeof (type), __FILE__, __LINE__)
940 * Allocate a size @a n times @a m times @a o array
941 * with structs or unions of the given @a type.
943 * @param n size of the first dimension
944 * @param m size of the second dimension
945 * @param o size of the third dimension
946 * @param type name of the struct or union, i.e. pass 'struct Foo'.
948 #define GNUNET_new_array_3d(n, m, o, type) (type ***) GNUNET_xnew_array_3d_ (n, m, o, sizeof (type), __FILE__, __LINE__)
952 * Wrapper around malloc. Allocates size bytes of memory.
953 * The memory will be zero'ed out.
955 * @param size the number of bytes to allocate, must be
956 * smaller than 40 MB.
957 * @return pointer to size bytes of memory, never NULL (!)
959 #define GNUNET_malloc(size) GNUNET_xmalloc_(size, __FILE__, __LINE__)
963 * Allocate and initialize a block of memory.
965 * @param buf data to initalize the block with
966 * @param size the number of bytes in buf (and size of the allocation)
967 * @return pointer to size bytes of memory, never NULL (!)
969 #define GNUNET_memdup(buf,size) GNUNET_xmemdup_(buf, size, __FILE__, __LINE__)
973 * Wrapper around malloc. Allocates size bytes of memory.
974 * The memory will be zero'ed out.
976 * @param size the number of bytes to allocate
977 * @return pointer to size bytes of memory, NULL if we do not have enough memory
979 #define GNUNET_malloc_large(size) GNUNET_xmalloc_unchecked_(size, __FILE__, __LINE__)
983 * Wrapper around realloc. Reallocates size bytes of memory.
984 * The content of the intersection of the new and old size will be unchanged.
986 * @param ptr the pointer to reallocate
987 * @param size the number of bytes to reallocate
988 * @return pointer to size bytes of memory
990 #define GNUNET_realloc(ptr, size) GNUNET_xrealloc_(ptr, size, __FILE__, __LINE__)
994 * Wrapper around free. Frees the memory referred to by ptr.
995 * Note that it is generally better to free memory that was
996 * allocated with #GNUNET_array_grow using #GNUNET_array_grow(mem, size, 0) instead of #GNUNET_free.
998 * @param ptr location where to free the memory. ptr must have
999 * been returned by #GNUNET_strdup, #GNUNET_strndup, #GNUNET_malloc or #GNUNET_array_grow earlier.
1001 #define GNUNET_free(ptr) GNUNET_xfree_(ptr, __FILE__, __LINE__)
1005 * Free the memory pointed to by ptr if ptr is not NULL.
1006 * Equivalent to `if (NULL != ptr) GNUNET_free(ptr)`.
1008 * @param ptr the location in memory to free
1010 #define GNUNET_free_non_null(ptr) do { void * __x__ = ptr; if (__x__ != NULL) { GNUNET_free(__x__); } } while(0)
1014 * Wrapper around #GNUNET_xstrdup_. Makes a copy of the zero-terminated string
1017 * @param a pointer to a zero-terminated string
1018 * @return a copy of the string including zero-termination
1020 #define GNUNET_strdup(a) GNUNET_xstrdup_(a,__FILE__,__LINE__)
1024 * Wrapper around #GNUNET_xstrndup_. Makes a partial copy of the string
1027 * @param a pointer to a string
1028 * @param length of the string to duplicate
1029 * @return a partial copy of the string including zero-termination
1031 #define GNUNET_strndup(a,length) GNUNET_xstrndup_(a,length,__FILE__,__LINE__)
1035 * Grow a well-typed (!) array. This is a convenience
1036 * method to grow a vector @a arr of size @a size
1037 * to the new (target) size @a tsize.
1040 * Example (simple, well-typed stack):
1043 * static struct foo * myVector = NULL;
1044 * static int myVecLen = 0;
1046 * static void push(struct foo * elem) {
1047 * GNUNET_array_grow(myVector, myVecLen, myVecLen+1);
1048 * GNUNET_memcpy(&myVector[myVecLen-1], elem, sizeof(struct foo));
1051 * static void pop(struct foo * elem) {
1052 * if (myVecLen == 0) die();
1053 * GNUNET_memcpy(elem, myVector[myVecLen-1], sizeof(struct foo));
1054 * GNUNET_array_grow(myVector, myVecLen, myVecLen-1);
1058 * @param arr base-pointer of the vector, may be NULL if size is 0;
1059 * will be updated to reflect the new address. The TYPE of
1060 * arr is important since size is the number of elements and
1061 * not the size in bytes
1062 * @param size the number of elements in the existing vector (number
1063 * of elements to copy over), will be updated with the new
1065 * @param tsize the target size for the resulting vector, use 0 to
1066 * free the vector (then, arr will be NULL afterwards).
1068 #define GNUNET_array_grow(arr,size,tsize) GNUNET_xgrow_((void**)&arr, sizeof(arr[0]), &size, tsize, __FILE__, __LINE__)
1072 * Append an element to a list (growing the list by one).
1074 * @param arr base-pointer of the vector, may be NULL if size is 0;
1075 * will be updated to reflect the new address. The TYPE of
1076 * arr is important since size is the number of elements and
1077 * not the size in bytes
1078 * @param size the number of elements in the existing vector (number
1079 * of elements to copy over), will be updated with the new
1081 * @param element the element that will be appended to the array
1083 #define GNUNET_array_append(arr,size,element) do { GNUNET_array_grow(arr,size,size+1); arr[size-1] = element; } while(0)
1087 * Like snprintf, just aborts if the buffer is of insufficient size.
1089 * @param buf pointer to buffer that is written to
1090 * @param size number of bytes in @a buf
1091 * @param format format strings
1092 * @param ... data for format string
1093 * @return number of bytes written to buf or negative value on error
1096 GNUNET_snprintf (char *buf, size_t size, const char *format, ...);
1101 * Like asprintf, just portable.
1103 * @param buf set to a buffer of sufficient size (allocated, caller must free)
1104 * @param format format string (see printf, fprintf, etc.)
1105 * @param ... data for format string
1106 * @return number of bytes in "*buf" excluding 0-termination
1109 GNUNET_asprintf (char **buf, const char *format, ...);
1112 /* ************** internal implementations, use macros above! ************** */
1115 * Allocate memory. Checks the return value, aborts if no more
1116 * memory is available. Don't use GNUNET_xmalloc_ directly. Use the
1117 * #GNUNET_malloc macro.
1118 * The memory will be zero'ed out.
1120 * @param size number of bytes to allocate
1121 * @param filename where is this call being made (for debugging)
1122 * @param linenumber line where this call is being made (for debugging)
1123 * @return allocated memory, never NULL
1126 GNUNET_xmalloc_ (size_t size, const char *filename, int linenumber);
1130 * Allocate memory for a two dimensional array in one block
1131 * and set up pointers. Aborts if no more memory is available.
1132 * Don't use GNUNET_xnew_array_2d_ directly. Use the
1133 * #GNUNET_new_array_2d macro.
1134 * The memory of the elements will be zero'ed out.
1136 * @param n size of the first dimension
1137 * @param m size of the second dimension
1138 * @param elementSize size of a single element in bytes
1139 * @param filename where is this call being made (for debugging)
1140 * @param linenumber line where this call is being made (for debugging)
1141 * @return allocated memory, never NULL
1144 GNUNET_xnew_array_2d_ (size_t n, size_t m, size_t elementSize,
1145 const char *filename, int linenumber);
1149 * Allocate memory for a three dimensional array in one block
1150 * and set up pointers. Aborts if no more memory is available.
1151 * Don't use GNUNET_xnew_array_3d_ directly. Use the
1152 * #GNUNET_new_array_3d macro.
1153 * The memory of the elements will be zero'ed out.
1155 * @param n size of the first dimension
1156 * @param m size of the second dimension
1157 * @param o size of the third dimension
1158 * @param elementSize size of a single element in bytes
1159 * @param filename where is this call being made (for debugging)
1160 * @param linenumber line where this call is being made (for debugging)
1161 * @return allocated memory, never NULL
1164 GNUNET_xnew_array_3d_ (size_t n, size_t m, size_t o, size_t elementSize,
1165 const char *filename, int linenumber);
1169 * Allocate and initialize memory. Checks the return value, aborts if no more
1170 * memory is available. Don't use GNUNET_xmemdup_ directly. Use the
1171 * #GNUNET_memdup macro.
1173 * @param buf buffer to initialize from (must contain size bytes)
1174 * @param size number of bytes to allocate
1175 * @param filename where is this call being made (for debugging)
1176 * @param linenumber line where this call is being made (for debugging)
1177 * @return allocated memory, never NULL
1180 GNUNET_xmemdup_ (const void *buf, size_t size, const char *filename,
1185 * Allocate memory. This function does not check if the allocation
1186 * request is within reasonable bounds, allowing allocations larger
1187 * than 40 MB. If you don't expect the possibility of very large
1188 * allocations, use #GNUNET_malloc instead. The memory will be zero'ed
1191 * @param size number of bytes to allocate
1192 * @param filename where is this call being made (for debugging)
1193 * @param linenumber line where this call is being made (for debugging)
1194 * @return pointer to size bytes of memory, NULL if we do not have enough memory
1197 GNUNET_xmalloc_unchecked_ (size_t size, const char *filename, int linenumber);
1201 * Reallocate memory. Checks the return value, aborts if no more
1202 * memory is available.
1205 GNUNET_xrealloc_ (void *ptr, size_t n, const char *filename, int linenumber);
1209 * Free memory. Merely a wrapper for the case that we
1210 * want to keep track of allocations. Don't use GNUNET_xfree_
1211 * directly. Use the #GNUNET_free macro.
1213 * @param ptr pointer to memory to free
1214 * @param filename where is this call being made (for debugging)
1215 * @param linenumber line where this call is being made (for debugging)
1218 GNUNET_xfree_ (void *ptr, const char *filename, int linenumber);
1222 * Dup a string. Don't call GNUNET_xstrdup_ directly. Use the #GNUNET_strdup macro.
1223 * @param str string to duplicate
1224 * @param filename where is this call being made (for debugging)
1225 * @param linenumber line where this call is being made (for debugging)
1226 * @return the duplicated string
1229 GNUNET_xstrdup_ (const char *str, const char *filename, int linenumber);
1232 * Dup partially a string. Don't call GNUNET_xstrndup_ directly. Use the #GNUNET_strndup macro.
1234 * @param str string to duplicate
1235 * @param len length of the string to duplicate
1236 * @param filename where is this call being made (for debugging)
1237 * @param linenumber line where this call is being made (for debugging)
1238 * @return the duplicated string
1241 GNUNET_xstrndup_ (const char *str, size_t len, const char *filename,
1245 * Grow an array, the new elements are zeroed out.
1246 * Grows old by (*oldCount-newCount)*elementSize
1247 * bytes and sets *oldCount to newCount.
1249 * Don't call GNUNET_xgrow_ directly. Use the #GNUNET_array_grow macro.
1251 * @param old address of the pointer to the array
1253 * @param elementSize the size of the elements of the array
1254 * @param oldCount address of the number of elements in the *old array
1255 * @param newCount number of elements in the new array, may be 0 (then *old will be NULL afterwards)
1256 * @param filename where is this call being made (for debugging)
1257 * @param linenumber line where this call is being made (for debugging)
1260 GNUNET_xgrow_ (void **old, size_t elementSize, unsigned int *oldCount,
1261 unsigned int newCount, const char *filename, int linenumber);
1266 * Create a copy of the given message.
1268 * @param msg message to copy
1269 * @return duplicate of the message
1271 struct GNUNET_MessageHeader *
1272 GNUNET_copy_message (const struct GNUNET_MessageHeader *msg);
1275 #if __STDC_VERSION__ < 199901L
1277 #define __func__ __FUNCTION__
1279 #define __func__ "<unknown>"
1285 * Valid task priorities. Use these, do not pass random integers!
1286 * For various reasons (#3862 -- building with QT Creator, and
1287 * our restricted cross-compilation with emscripten) this cannot
1288 * be in gnunet_scheduler_lib.h, but it works if we declare it here.
1289 * Naturally, logically this is part of the scheduler.
1291 enum GNUNET_SCHEDULER_Priority
1294 * Run with the same priority as the current job.
1296 GNUNET_SCHEDULER_PRIORITY_KEEP = 0,
1299 * Run when otherwise idle.
1301 GNUNET_SCHEDULER_PRIORITY_IDLE = 1,
1304 * Run as background job (higher than idle,
1305 * lower than default).
1307 GNUNET_SCHEDULER_PRIORITY_BACKGROUND = 2,
1310 * Run with the default priority (normal
1311 * P2P operations). Any task that is scheduled
1312 * without an explicit priority being specified
1313 * will run with this priority.
1315 GNUNET_SCHEDULER_PRIORITY_DEFAULT = 3,
1318 * Run with high priority (important requests).
1319 * Higher than DEFAULT.
1321 GNUNET_SCHEDULER_PRIORITY_HIGH = 4,
1324 * Run with priority for interactive tasks.
1325 * Higher than "HIGH".
1327 GNUNET_SCHEDULER_PRIORITY_UI = 5,
1330 * Run with priority for urgent tasks. Use
1331 * for things like aborts and shutdowns that
1332 * need to preempt "UI"-level tasks.
1335 GNUNET_SCHEDULER_PRIORITY_URGENT = 6,
1338 * This is an internal priority level that is only used for tasks
1339 * that are being triggered due to shutdown (they have automatically
1340 * highest priority). User code must not use this priority level
1341 * directly. Tasks run with this priority level that internally
1342 * schedule other tasks will see their original priority level
1343 * be inherited (unless otherwise specified).
1345 GNUNET_SCHEDULER_PRIORITY_SHUTDOWN = 7,
1348 * Number of priorities (must be the last priority).
1349 * This priority must not be used by clients.
1351 GNUNET_SCHEDULER_PRIORITY_COUNT = 8
1355 #if 0 /* keep Emacsens' auto-indent happy */
1362 #endif /* GNUNET_COMMON_H */