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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file include/gnunet_common.h
23 * @brief commonly used definitions; globals in this file
24 * are exempt from the rule that the module name ("common")
25 * must be part of the symbol name.
27 * @author Christian Grothoff
29 * @author Martin Schanzenbach
31 * @defgroup logging Logging
32 * @see [Documentation](https://gnunet.org/logging)
34 * @defgroup memory Memory management
36 #ifndef GNUNET_COMMON_H
37 #define GNUNET_COMMON_H
40 #include <sys/socket.h>
43 #include <netinet/in.h>
55 #ifdef HAVE_BYTESWAP_H
62 #if 0 /* keep Emacsens' auto-indent happy */
68 * Version of the API (for entire gnunetutil.so library).
70 #define GNUNET_UTIL_VERSION 0x000A0102
74 * Named constants for return values. The following invariants hold:
75 * `GNUNET_NO == 0` (to allow `if (GNUNET_NO)`) `GNUNET_OK !=
76 * GNUNET_SYSERR`, `GNUNET_OK != GNUNET_NO`, `GNUNET_NO !=
77 * GNUNET_SYSERR` and finally `GNUNET_YES != GNUNET_NO`.
80 #define GNUNET_SYSERR -1
84 #define GNUNET_MIN(a,b) (((a) < (b)) ? (a) : (b))
86 #define GNUNET_MAX(a,b) (((a) > (b)) ? (a) : (b))
88 /* some systems use one underscore only, and mingw uses no underscore... */
91 #define __BYTE_ORDER _BYTE_ORDER
94 #define __BYTE_ORDER BYTE_ORDER
100 #define __BIG_ENDIAN _BIG_ENDIAN
103 #define __BIG_ENDIAN BIG_ENDIAN
107 #ifndef __LITTLE_ENDIAN
108 #ifdef _LITTLE_ENDIAN
109 #define __LITTLE_ENDIAN _LITTLE_ENDIAN
112 #define __LITTLE_ENDIAN LITTLE_ENDIAN
118 * wrap va_arg for enums
120 #define GNUNET_VA_ARG_ENUM(va,X) ((enum X) va_arg (va, int))
125 * define #GNUNET_EXTRA_LOGGING if using this header outside the GNUnet source
126 * tree where gnunet_config.h is unavailable
128 #ifndef GNUNET_EXTRA_LOGGING
129 #define GNUNET_EXTRA_LOGGING 0
136 # if __BYTE_ORDER == __LITTLE_ENDIAN
137 # define GNUNET_htobe16(x) __bswap_16 (x)
138 # define GNUNET_htole16(x) (x)
139 # define GNUNET_be16toh(x) __bswap_16 (x)
140 # define GNUNET_le16toh(x) (x)
142 # define GNUNET_htobe32(x) __bswap_32 (x)
143 # define GNUNET_htole32(x) (x)
144 # define GNUNET_be32toh(x) __bswap_32 (x)
145 # define GNUNET_le32toh(x) (x)
147 # define GNUNET_htobe64(x) __bswap_64 (x)
148 # define GNUNET_htole64(x) (x)
149 # define GNUNET_be64toh(x) __bswap_64 (x)
150 # define GNUNET_le64toh(x) (x)
152 # if __BYTE_ORDER == __BIG_ENDIAN
153 # define GNUNET_htobe16(x) (x)
154 # define GNUNET_htole16(x) __bswap_16 (x)
155 # define GNUNET_be16toh(x) (x)
156 # define GNUNET_le16toh(x) __bswap_16 (x)
158 # define GNUNET_htobe32(x) (x)
159 # define GNUNET_htole32(x) __bswap_32 (x)
160 # define GNUNET_be32toh(x) (x)
161 # define GNUNET_le32toh(x) __bswap_32 (x)
163 # define GNUNET_htobe64(x) (x)
164 # define GNUNET_htole64(x) __bswap_64 (x)
165 # define GNUNET_be64toh(x) (x)
166 # define GNUNET_le64toh(x) __bswap_64 (x)
171 * Macro used to avoid using 0 for the length of a variable-size
172 * array (Non-Zero-Length).
174 * Basically, C standard says that "int[n] x;" is undefined if n=0.
175 * This was supposed to prevent issues with pointer aliasing.
176 * However, C compilers may conclude that n!=0 as n=0 would be
177 * undefined, and then optimize under the assumption n!=0, which
178 * could cause actual issues. Hence, when initializing an array
179 * on the stack with a variable-length that might be zero, write
180 * "int[GNUNET_NZL(n)] x;" instead of "int[n] x".
182 #define GNUNET_NZL(l) GNUNET_MAX(1,l)
186 * gcc-ism to get packed structs.
188 #define GNUNET_PACKED __attribute__((packed))
191 * gcc-ism to get gcc bitfield layout when compiling with -mms-bitfields
194 #define GNUNET_GCC_STRUCT_LAYOUT __attribute__((gcc_struct))
196 #define GNUNET_GCC_STRUCT_LAYOUT
200 * gcc-ism to force alignment; we use this to align char-arrays
201 * that may then be cast to 'struct's. See also gcc
204 #ifdef __BIGGEST_ALIGNMENT__
205 #define GNUNET_ALIGN __attribute__((aligned (__BIGGEST_ALIGNMENT__)))
207 #define GNUNET_ALIGN __attribute__((aligned (8)))
211 * gcc-ism to document unused arguments
213 #define GNUNET_UNUSED __attribute__((unused))
216 * gcc-ism to document functions that don't return
218 #define GNUNET_NORETURN __attribute__((noreturn))
223 * gcc 4.x-ism to pack structures even on W32 (to be used before structs);
224 * Using this would cause structs to be unaligned on the stack on Sparc,
225 * so we *only* use this on W32 (see #670578 from Debian); fortunately,
226 * W32 doesn't run on sparc anyway.
228 #define GNUNET_NETWORK_STRUCT_BEGIN \
229 _Pragma("pack(push)") \
233 * gcc 4.x-ism to pack structures even on W32 (to be used after structs)
234 * Using this would cause structs to be unaligned on the stack on Sparc,
235 * so we *only* use this on W32 (see #670578 from Debian); fortunately,
236 * W32 doesn't run on sparc anyway.
238 #define GNUNET_NETWORK_STRUCT_END _Pragma("pack(pop)")
241 #error gcc 4.x or higher required on W32 systems
245 * Define as empty, GNUNET_PACKED should suffice, but this won't work on W32
247 #define GNUNET_NETWORK_STRUCT_BEGIN
250 * Define as empty, GNUNET_PACKED should suffice, but this won't work on W32;
252 #define GNUNET_NETWORK_STRUCT_END
255 /* ************************ super-general types *********************** */
257 GNUNET_NETWORK_STRUCT_BEGIN
260 * Header for all communications.
262 struct GNUNET_MessageHeader
266 * The length of the struct (in bytes, including the length field itself),
267 * in big-endian format.
269 uint16_t size GNUNET_PACKED;
272 * The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
274 uint16_t type GNUNET_PACKED;
280 * Answer from service to client about last operation.
282 struct GNUNET_OperationResultMessage
284 struct GNUNET_MessageHeader header;
286 uint32_t reserved GNUNET_PACKED;
291 uint64_t op_id GNUNET_PACKED;
294 * Status code for the operation.
296 uint64_t result_code GNUNET_PACKED;
298 /* Followed by data. */
301 GNUNET_NETWORK_STRUCT_END
304 * Function called with a filename.
307 * @param filename complete filename (absolute path)
308 * @return #GNUNET_OK to continue to iterate,
309 * #GNUNET_NO to stop iteration with no error,
310 * #GNUNET_SYSERR to abort iteration with error!
313 (*GNUNET_FileNameCallback) (void *cls,
314 const char *filename);
318 * Generic continuation callback.
320 * @param cls Closure.
323 (*GNUNET_ContinuationCallback) (void *cls);
327 * Function called with the result of an asynchronous operation.
332 * Result code for the operation.
334 * Data result for the operation.
339 (*GNUNET_ResultCallback) (void *cls, int64_t result_code,
340 const void *data, uint16_t data_size);
343 /* ****************************** logging ***************************** */
349 enum GNUNET_ErrorType
351 GNUNET_ERROR_TYPE_UNSPECIFIED = -1,
352 GNUNET_ERROR_TYPE_NONE = 0,
353 GNUNET_ERROR_TYPE_ERROR = 1,
354 GNUNET_ERROR_TYPE_WARNING = 2,
355 /* UX: We need a message type that is output by
356 * default without looking like there is a problem.
358 GNUNET_ERROR_TYPE_MESSAGE = 4,
359 GNUNET_ERROR_TYPE_INFO = 8,
360 GNUNET_ERROR_TYPE_DEBUG = 16,
361 GNUNET_ERROR_TYPE_INVALID = 32,
362 GNUNET_ERROR_TYPE_BULK = 64
368 * User-defined handler for log messages.
371 * @param kind severeity
372 * @param component what component is issuing the message?
373 * @param date when was the message logged?
374 * @param message what is the message
377 (*GNUNET_Logger) (void *cls,
378 enum GNUNET_ErrorType kind,
379 const char *component,
381 const char *message);
386 * Get the number of log calls that are going to be skipped
388 * @return number of log calls to be ignored
391 GNUNET_get_log_skip (void);
394 #if !defined(GNUNET_CULL_LOGGING)
396 GNUNET_get_log_call_status (int caller_level,
399 const char *function,
408 * @param kind how serious is the error?
409 * @param message what is the message (format string)
410 * @param ... arguments for format string
413 GNUNET_log_nocheck (enum GNUNET_ErrorType kind, const char *message, ...)
414 __attribute__ ((format (printf, 2, 3)));
417 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
418 #define _GNUNET_BOOLEAN_EXPR(expr) \
420 int _gnunet_boolean_var_; \
422 _gnunet_boolean_var_ = 1; \
424 _gnunet_boolean_var_ = 0; \
425 _gnunet_boolean_var_; \
427 #define GN_LIKELY(expr) (__builtin_expect (_GNUNET_BOOLEAN_EXPR(expr), 1))
428 #define GN_UNLIKELY(expr) (__builtin_expect (_GNUNET_BOOLEAN_EXPR(expr), 0))
430 #define GN_LIKELY(expr) (expr)
431 #define GN_UNLIKELY(expr) (expr)
434 #if !defined(GNUNET_LOG_CALL_STATUS)
435 #define GNUNET_LOG_CALL_STATUS -1
441 * Log function that specifies an alternative component.
442 * This function should be used by plugins.
444 * @param kind how serious is the error?
445 * @param comp component responsible for generating the message
446 * @param message what is the message (format string)
447 * @param ... arguments for format string
450 GNUNET_log_from_nocheck (enum GNUNET_ErrorType kind, const char *comp,
451 const char *message, ...);
453 #if !defined(GNUNET_CULL_LOGGING)
454 #define GNUNET_log_from(kind,comp,...) do { int log_line = __LINE__;\
455 static int log_call_enabled = GNUNET_LOG_CALL_STATUS;\
456 if ((GNUNET_EXTRA_LOGGING > 0) || ((GNUNET_ERROR_TYPE_DEBUG & (kind)) == 0)) { \
457 if (GN_UNLIKELY(log_call_enabled == -1))\
458 log_call_enabled = GNUNET_get_log_call_status ((kind) & (~GNUNET_ERROR_TYPE_BULK), (comp), __FILE__, __FUNCTION__, log_line); \
459 if (GN_UNLIKELY(GNUNET_get_log_skip () > 0)) { GNUNET_log_skip (-1, GNUNET_NO); }\
461 if (GN_UNLIKELY(log_call_enabled))\
462 GNUNET_log_from_nocheck ((kind), comp, __VA_ARGS__); \
467 #define GNUNET_log(kind,...) do { int log_line = __LINE__;\
468 static int log_call_enabled = GNUNET_LOG_CALL_STATUS;\
469 if ((GNUNET_EXTRA_LOGGING > 0) || ((GNUNET_ERROR_TYPE_DEBUG & (kind)) == 0)) { \
470 if (GN_UNLIKELY(log_call_enabled == -1))\
471 log_call_enabled = GNUNET_get_log_call_status ((kind) & (~GNUNET_ERROR_TYPE_BULK), NULL, __FILE__, __FUNCTION__, log_line);\
472 if (GN_UNLIKELY(GNUNET_get_log_skip () > 0)) { GNUNET_log_skip (-1, GNUNET_NO); }\
474 if (GN_UNLIKELY(log_call_enabled))\
475 GNUNET_log_nocheck ((kind), __VA_ARGS__); \
480 #define GNUNET_log(...)
481 #define GNUNET_log_from(...)
487 * Log error message about missing configuration option.
489 * @param kind log level
490 * @param section section with missing option
491 * @param option name of missing option
494 GNUNET_log_config_missing (enum GNUNET_ErrorType kind,
501 * Log error message about invalid configuration option value.
503 * @param kind log level
504 * @param section section with invalid option
505 * @param option name of invalid option
506 * @param required what is required that is invalid about the option
509 GNUNET_log_config_invalid (enum GNUNET_ErrorType kind,
512 const char *required);
517 * Abort the process, generate a core dump if possible.
518 * Most code should use `GNUNET_assert (0)` instead to
519 * first log the location of the failure.
522 GNUNET_abort_ (void) GNUNET_NORETURN;
527 * Ignore the next @a n calls to the log function.
529 * @param n number of log calls to ignore (could be negative)
530 * @param check_reset #GNUNET_YES to assert that the log skip counter is currently zero
533 GNUNET_log_skip (int n,
541 * @param comp default component to use
542 * @param loglevel what types of messages should be logged
543 * @param logfile change logging to logfile (use NULL to keep stderr)
544 * @return #GNUNET_OK on success, #GNUNET_SYSERR if logfile could not be opened
547 GNUNET_log_setup (const char *comp,
548 const char *loglevel,
549 const char *logfile);
554 * Add a custom logger. Note that installing any custom logger
555 * will disable the standard logger. When multiple custom loggers
556 * are installed, all will be called. The standard logger will
557 * only be used if no custom loggers are present.
559 * @param logger log function
560 * @param logger_cls closure for @a logger
563 GNUNET_logger_add (GNUNET_Logger logger,
569 * Remove a custom logger.
571 * @param logger log function
572 * @param logger_cls closure for @a logger
575 GNUNET_logger_remove (GNUNET_Logger logger,
581 * Convert a short hash value to a string (for printing debug messages).
582 * This is one of the very few calls in the entire API that is
585 * @param shc the hash code
589 GNUNET_sh2s (const struct GNUNET_ShortHashCode *shc);
594 * Convert a hash value to a string (for printing debug messages).
595 * This is one of the very few calls in the entire API that is
598 * @param hc the hash code
602 GNUNET_h2s (const struct GNUNET_HashCode *hc);
607 * Convert a hash value to a string (for printing debug messages).
608 * This is one of the very few calls in the entire API that is
609 * NOT reentrant! Identical to #GNUNET_h2s(), except that another
610 * buffer is used so both #GNUNET_h2s() and #GNUNET_h2s2() can be
611 * used within the same log statement.
613 * @param hc the hash code
617 GNUNET_h2s2 (const struct GNUNET_HashCode *hc);
622 * Convert a hash value to a string (for printing debug messages).
623 * This prints all 104 characters of a hashcode!
624 * This is one of the very few calls in the entire API that is
627 * @param hc the hash code
631 GNUNET_h2s_full (const struct GNUNET_HashCode *hc);
635 * Public key. Details in gnunet_util_crypto.h.
637 struct GNUNET_CRYPTO_EddsaPublicKey;
641 * Public key. Details in gnunet_util_crypto.h.
643 struct GNUNET_CRYPTO_EcdhePublicKey;
648 * Convert a public key value to a string (for printing debug messages).
649 * This is one of the very few calls in the entire API that is
652 * @param hc the hash code
656 GNUNET_p2s (const struct GNUNET_CRYPTO_EddsaPublicKey *p);
661 * Convert a public key value to a string (for printing debug messages).
662 * This is one of the very few calls in the entire API that is
665 * @param hc the hash code
669 GNUNET_p2s2 (const struct GNUNET_CRYPTO_EddsaPublicKey *p);
674 * Convert a public key value to a string (for printing debug messages).
675 * This is one of the very few calls in the entire API that is
678 * @param hc the hash code
682 GNUNET_e2s (const struct GNUNET_CRYPTO_EcdhePublicKey *p);
687 * Convert a public key value to a string (for printing debug messages).
688 * This is one of the very few calls in the entire API that is
691 * @param hc the hash code
695 GNUNET_e2s2 (const struct GNUNET_CRYPTO_EcdhePublicKey *p);
700 * Convert a peer identity to a string (for printing debug messages).
701 * This is one of the very few calls in the entire API that is
704 * @param pid the peer identity
705 * @return string form of the pid; will be overwritten by next
706 * call to #GNUNET_i2s().
709 GNUNET_i2s (const struct GNUNET_PeerIdentity *pid);
714 * Convert a peer identity to a string (for printing debug messages).
715 * This is one of the very few calls in the entire API that is
716 * NOT reentrant! Identical to #GNUNET_i2s(), except that another
717 * buffer is used so both #GNUNET_i2s() and #GNUNET_i2s2() can be
718 * used within the same log statement.
720 * @param pid the peer identity
721 * @return string form of the pid; will be overwritten by next
722 * call to #GNUNET_i2s().
725 GNUNET_i2s2 (const struct GNUNET_PeerIdentity *pid);
730 * Convert a peer identity to a string (for printing debug messages).
731 * This is one of the very few calls in the entire API that is
734 * @param pid the peer identity
735 * @return string form of the pid; will be overwritten by next
736 * call to #GNUNET_i2s_full().
739 GNUNET_i2s_full (const struct GNUNET_PeerIdentity *pid);
744 * Convert a "struct sockaddr*" (IPv4 or IPv6 address) to a string
745 * (for printing debug messages). This is one of the very few calls
746 * in the entire API that is NOT reentrant!
748 * @param addr the address
749 * @param addrlen the length of the @a addr
750 * @return nicely formatted string for the address
751 * will be overwritten by next call to #GNUNET_a2s().
754 GNUNET_a2s (const struct sockaddr *addr,
760 * Convert error type to string.
762 * @param kind type to convert
763 * @return string corresponding to the type
766 GNUNET_error_type_to_string (enum GNUNET_ErrorType kind);
771 * Use this for fatal errors that cannot be handled
773 #define GNUNET_assert(cond) do { if (! (cond)) { GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Assertion failed at %s:%d. Aborting.\n"), __FILE__, __LINE__); GNUNET_abort_(); } } while(0)
778 * Use this for fatal errors that cannot be handled
780 #define GNUNET_assert_at(cond, f, l) do { if (! (cond)) { GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Assertion failed at %s:%d. Aborting.\n"), f, l); GNUNET_abort_(); } } while(0)
785 * Use this for fatal errors that cannot be handled
787 * @param cond Condition to evaluate
788 * @param comp Component string to use for logging
790 #define GNUNET_assert_from(cond, comp) do { if (! (cond)) { GNUNET_log_from(GNUNET_ERROR_TYPE_ERROR, comp, _("Assertion failed at %s:%d. Aborting.\n"), __FILE__, __LINE__); GNUNET_abort_(); } } while(0)
795 * Use this for internal assertion violations that are
796 * not fatal (can be handled) but should not occur.
798 #define GNUNET_break(cond) do { if (! (cond)) { GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Assertion failed at %s:%d.\n"), __FILE__, __LINE__); } } while(0)
803 * Use this for assertion violations caused by other
804 * peers (i.e. protocol violations). We do not want to
805 * confuse end-users (say, some other peer runs an
806 * older, broken or incompatible GNUnet version), but
807 * we still want to see these problems during
808 * development and testing. "OP == other peer".
810 #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)
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_strerror(level, cmd) do { GNUNET_log(level, _("`%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_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)
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_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)
842 * Log an error message at log-level 'level' that indicates
843 * a failure of the command 'cmd' with the message given
844 * by strerror(errno).
846 #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)
848 /* ************************* endianess conversion ****************** */
851 * Convert unsigned 64-bit integer to network byte order.
854 * The value in host byte order.
856 * @return The same value in network byte order.
859 GNUNET_htonll (uint64_t n);
863 * Convert unsigned 64-bit integer to host byte order.
866 * The value in network byte order.
868 * @return The same value in host byte order.
871 GNUNET_ntohll (uint64_t n);
875 * Convert double to network byte order.
878 * The value in host byte order.
880 * @return The same value in network byte order.
883 GNUNET_hton_double (double d);
887 * Convert double to host byte order
890 * The value in network byte order.
892 * @return The same value in host byte order.
895 GNUNET_ntoh_double (double d);
898 /* ************************* allocation functions ****************** */
902 * Maximum allocation with GNUNET_malloc macro.
904 #define GNUNET_MAX_MALLOC_CHECKED (1024 * 1024 * 40)
908 * Allocate a struct or union of the given @a type.
909 * Wrapper around #GNUNET_malloc that returns a pointer
910 * to the newly created object of the correct type.
912 * @param type name of the struct or union, i.e. pass 'struct Foo'.
914 #define GNUNET_new(type) (type *) GNUNET_malloc (sizeof (type))
917 * Call memcpy() but check for @a n being 0 first. In the latter
918 * case, it is now safe to pass NULL for @a src or @a dst.
919 * Unlike traditional memcpy(), returns nothing.
921 * @param dst destination of the copy, may be NULL if @a n is zero
922 * @param src source of the copy, may be NULL if @a n is zero
923 * @param n number of bytes to copy
925 #define GNUNET_memcpy(dst,src,n) do { if (0 != n) { (void) memcpy (dst,src,n); } } while (0)
930 * Allocate a size @a n array with structs or unions of the given @a type.
931 * Wrapper around #GNUNET_malloc that returns a pointer
932 * to the newly created objects of the correct type.
934 * @param n number of elements in the array
935 * @param type name of the struct or union, i.e. pass 'struct Foo'.
937 #define GNUNET_new_array(n, type) (type *) GNUNET_malloc ((n) * sizeof (type))
941 * Allocate a size @a n times @a m array
942 * with structs or unions of the given @a type.
944 * @param n size of the first dimension
945 * @param m size of the second dimension
946 * @param type name of the struct or union, i.e. pass 'struct Foo'.
948 #define GNUNET_new_array_2d(n, m, type) (type **) GNUNET_xnew_array_2d_ (n, m, sizeof (type), __FILE__, __LINE__)
952 * Allocate a size @a n times @a m times @a o array
953 * with structs or unions of the given @a type.
955 * @param n size of the first dimension
956 * @param m size of the second dimension
957 * @param o size of the third dimension
958 * @param type name of the struct or union, i.e. pass 'struct Foo'.
960 #define GNUNET_new_array_3d(n, m, o, type) (type ***) GNUNET_xnew_array_3d_ (n, m, o, sizeof (type), __FILE__, __LINE__)
964 * Wrapper around malloc. Allocates size bytes of memory.
965 * The memory will be zero'ed out.
967 * @param size the number of bytes to allocate, must be
968 * smaller than 40 MB.
969 * @return pointer to size bytes of memory, never NULL (!)
971 #define GNUNET_malloc(size) GNUNET_xmalloc_(size, __FILE__, __LINE__)
975 * Allocate and initialize a block of memory.
977 * @param buf data to initalize the block with
978 * @param size the number of bytes in buf (and size of the allocation)
979 * @return pointer to size bytes of memory, never NULL (!)
981 #define GNUNET_memdup(buf,size) GNUNET_xmemdup_(buf, size, __FILE__, __LINE__)
985 * Wrapper around malloc. Allocates size bytes of memory.
986 * The memory will be zero'ed out.
988 * @param size the number of bytes to allocate
989 * @return pointer to size bytes of memory, NULL if we do not have enough memory
991 #define GNUNET_malloc_large(size) GNUNET_xmalloc_unchecked_(size, __FILE__, __LINE__)
995 * Wrapper around realloc. Reallocates size bytes of memory.
996 * The content of the intersection of the new and old size will be unchanged.
998 * @param ptr the pointer to reallocate
999 * @param size the number of bytes to reallocate
1000 * @return pointer to size bytes of memory
1002 #define GNUNET_realloc(ptr, size) GNUNET_xrealloc_(ptr, size, __FILE__, __LINE__)
1006 * Wrapper around free. Frees the memory referred to by ptr.
1007 * Note that it is generally better to free memory that was
1008 * allocated with #GNUNET_array_grow using #GNUNET_array_grow(mem, size, 0) instead of #GNUNET_free.
1010 * @param ptr location where to free the memory. ptr must have
1011 * been returned by #GNUNET_strdup, #GNUNET_strndup, #GNUNET_malloc or #GNUNET_array_grow earlier.
1013 #define GNUNET_free(ptr) GNUNET_xfree_(ptr, __FILE__, __LINE__)
1017 * Free the memory pointed to by ptr if ptr is not NULL.
1018 * Equivalent to `if (NULL != ptr) GNUNET_free(ptr)`.
1020 * @param ptr the location in memory to free
1022 #define GNUNET_free_non_null(ptr) do { void * __x__ = ptr; if (__x__ != NULL) { GNUNET_free(__x__); } } while(0)
1026 * Wrapper around #GNUNET_xstrdup_. Makes a copy of the zero-terminated string
1029 * @param a pointer to a zero-terminated string
1030 * @return a copy of the string including zero-termination
1032 #define GNUNET_strdup(a) GNUNET_xstrdup_(a,__FILE__,__LINE__)
1036 * Wrapper around #GNUNET_xstrndup_. Makes a partial copy of the string
1039 * @param a pointer to a string
1040 * @param length of the string to duplicate
1041 * @return a partial copy of the string including zero-termination
1043 #define GNUNET_strndup(a,length) GNUNET_xstrndup_(a,length,__FILE__,__LINE__)
1047 * Grow a well-typed (!) array. This is a convenience
1048 * method to grow a vector @a arr of size @a size
1049 * to the new (target) size @a tsize.
1052 * Example (simple, well-typed stack):
1055 * static struct foo * myVector = NULL;
1056 * static int myVecLen = 0;
1058 * static void push(struct foo * elem) {
1059 * GNUNET_array_grow(myVector, myVecLen, myVecLen+1);
1060 * GNUNET_memcpy(&myVector[myVecLen-1], elem, sizeof(struct foo));
1063 * static void pop(struct foo * elem) {
1064 * if (myVecLen == 0) die();
1065 * GNUNET_memcpy(elem, myVector[myVecLen-1], sizeof(struct foo));
1066 * GNUNET_array_grow(myVector, myVecLen, myVecLen-1);
1070 * @param arr base-pointer of the vector, may be NULL if size is 0;
1071 * will be updated to reflect the new address. The TYPE of
1072 * arr is important since size is the number of elements and
1073 * not the size in bytes
1074 * @param size the number of elements in the existing vector (number
1075 * of elements to copy over), will be updated with the new
1077 * @param tsize the target size for the resulting vector, use 0 to
1078 * free the vector (then, arr will be NULL afterwards).
1080 #define GNUNET_array_grow(arr,size,tsize) GNUNET_xgrow_((void**)&(arr), sizeof((arr)[0]), &size, tsize, __FILE__, __LINE__)
1084 * Append an element to a list (growing the list by one).
1086 * @param arr base-pointer of the vector, may be NULL if size is 0;
1087 * will be updated to reflect the new address. The TYPE of
1088 * arr is important since size is the number of elements and
1089 * not the size in bytes
1090 * @param size the number of elements in the existing vector (number
1091 * of elements to copy over), will be updated with the new
1093 * @param element the element that will be appended to the array
1095 #define GNUNET_array_append(arr,size,element) do { GNUNET_array_grow(arr,size,size+1); (arr)[size-1] = element; } while(0)
1099 * Like snprintf, just aborts if the buffer is of insufficient size.
1101 * @param buf pointer to buffer that is written to
1102 * @param size number of bytes in @a buf
1103 * @param format format strings
1104 * @param ... data for format string
1105 * @return number of bytes written to buf or negative value on error
1108 GNUNET_snprintf (char *buf, size_t size, const char *format, ...);
1113 * Like asprintf, just portable.
1115 * @param buf set to a buffer of sufficient size (allocated, caller must free)
1116 * @param format format string (see printf, fprintf, etc.)
1117 * @param ... data for format string
1118 * @return number of bytes in "*buf" excluding 0-termination
1121 GNUNET_asprintf (char **buf, const char *format, ...);
1124 /* ************** internal implementations, use macros above! ************** */
1127 * Allocate memory. Checks the return value, aborts if no more
1128 * memory is available. Don't use GNUNET_xmalloc_ directly. Use the
1129 * #GNUNET_malloc macro.
1130 * The memory will be zero'ed out.
1132 * @param size number of bytes to allocate
1133 * @param filename where is this call being made (for debugging)
1134 * @param linenumber line where this call is being made (for debugging)
1135 * @return allocated memory, never NULL
1138 GNUNET_xmalloc_ (size_t size, const char *filename, int linenumber);
1142 * Allocate memory for a two dimensional array in one block
1143 * and set up pointers. Aborts if no more memory is available.
1144 * Don't use GNUNET_xnew_array_2d_ directly. Use the
1145 * #GNUNET_new_array_2d macro.
1146 * The memory of the elements will be zero'ed out.
1148 * @param n size of the first dimension
1149 * @param m size of the second dimension
1150 * @param elementSize size of a single element in bytes
1151 * @param filename where is this call being made (for debugging)
1152 * @param linenumber line where this call is being made (for debugging)
1153 * @return allocated memory, never NULL
1156 GNUNET_xnew_array_2d_ (size_t n, size_t m, size_t elementSize,
1157 const char *filename, int linenumber);
1161 * Allocate memory for a three dimensional array in one block
1162 * and set up pointers. Aborts if no more memory is available.
1163 * Don't use GNUNET_xnew_array_3d_ directly. Use the
1164 * #GNUNET_new_array_3d macro.
1165 * The memory of the elements will be zero'ed out.
1167 * @param n size of the first dimension
1168 * @param m size of the second dimension
1169 * @param o size of the third dimension
1170 * @param elementSize size of a single element in bytes
1171 * @param filename where is this call being made (for debugging)
1172 * @param linenumber line where this call is being made (for debugging)
1173 * @return allocated memory, never NULL
1176 GNUNET_xnew_array_3d_ (size_t n, size_t m, size_t o, size_t elementSize,
1177 const char *filename, int linenumber);
1181 * Allocate and initialize memory. Checks the return value, aborts if no more
1182 * memory is available. Don't use GNUNET_xmemdup_ directly. Use the
1183 * #GNUNET_memdup macro.
1185 * @param buf buffer to initialize from (must contain size bytes)
1186 * @param size number of bytes to allocate
1187 * @param filename where is this call being made (for debugging)
1188 * @param linenumber line where this call is being made (for debugging)
1189 * @return allocated memory, never NULL
1192 GNUNET_xmemdup_ (const void *buf, size_t size, const char *filename,
1197 * Allocate memory. This function does not check if the allocation
1198 * request is within reasonable bounds, allowing allocations larger
1199 * than 40 MB. If you don't expect the possibility of very large
1200 * allocations, use #GNUNET_malloc instead. The memory will be zero'ed
1203 * @param size number of bytes to allocate
1204 * @param filename where is this call being made (for debugging)
1205 * @param linenumber line where this call is being made (for debugging)
1206 * @return pointer to size bytes of memory, NULL if we do not have enough memory
1209 GNUNET_xmalloc_unchecked_ (size_t size, const char *filename, int linenumber);
1213 * Reallocate memory. Checks the return value, aborts if no more
1214 * memory is available.
1217 GNUNET_xrealloc_ (void *ptr, size_t n, const char *filename, int linenumber);
1221 * Free memory. Merely a wrapper for the case that we
1222 * want to keep track of allocations. Don't use GNUNET_xfree_
1223 * directly. Use the #GNUNET_free macro.
1225 * @param ptr pointer to memory to free
1226 * @param filename where is this call being made (for debugging)
1227 * @param linenumber line where this call is being made (for debugging)
1230 GNUNET_xfree_ (void *ptr, const char *filename, int linenumber);
1234 * Dup a string. Don't call GNUNET_xstrdup_ directly. Use the #GNUNET_strdup macro.
1235 * @param str 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_xstrdup_ (const char *str, const char *filename, int linenumber);
1244 * Dup partially a string. Don't call GNUNET_xstrndup_ directly. Use the #GNUNET_strndup macro.
1246 * @param str string to duplicate
1247 * @param len length of the string to duplicate
1248 * @param filename where is this call being made (for debugging)
1249 * @param linenumber line where this call is being made (for debugging)
1250 * @return the duplicated string
1253 GNUNET_xstrndup_ (const char *str, size_t len, const char *filename,
1257 * Grow an array, the new elements are zeroed out.
1258 * Grows old by (*oldCount-newCount)*elementSize
1259 * bytes and sets *oldCount to newCount.
1261 * Don't call GNUNET_xgrow_ directly. Use the #GNUNET_array_grow macro.
1263 * @param old address of the pointer to the array
1265 * @param elementSize the size of the elements of the array
1266 * @param oldCount address of the number of elements in the *old array
1267 * @param newCount number of elements in the new array, may be 0 (then *old will be NULL afterwards)
1268 * @param filename where is this call being made (for debugging)
1269 * @param linenumber line where this call is being made (for debugging)
1272 GNUNET_xgrow_ (void **old, size_t elementSize, unsigned int *oldCount,
1273 unsigned int newCount, const char *filename, int linenumber);
1278 * Create a copy of the given message.
1280 * @param msg message to copy
1281 * @return duplicate of the message
1283 struct GNUNET_MessageHeader *
1284 GNUNET_copy_message (const struct GNUNET_MessageHeader *msg);
1287 #if __STDC_VERSION__ < 199901L
1289 #define __func__ __FUNCTION__
1291 #define __func__ "<unknown>"
1297 * Valid task priorities. Use these, do not pass random integers!
1298 * For various reasons (#3862 -- building with QT Creator, and
1299 * our restricted cross-compilation with emscripten) this cannot
1300 * be in gnunet_scheduler_lib.h, but it works if we declare it here.
1301 * Naturally, logically this is part of the scheduler.
1303 enum GNUNET_SCHEDULER_Priority
1306 * Run with the same priority as the current job.
1308 GNUNET_SCHEDULER_PRIORITY_KEEP = 0,
1311 * Run when otherwise idle.
1313 GNUNET_SCHEDULER_PRIORITY_IDLE = 1,
1316 * Run as background job (higher than idle,
1317 * lower than default).
1319 GNUNET_SCHEDULER_PRIORITY_BACKGROUND = 2,
1322 * Run with the default priority (normal
1323 * P2P operations). Any task that is scheduled
1324 * without an explicit priority being specified
1325 * will run with this priority.
1327 GNUNET_SCHEDULER_PRIORITY_DEFAULT = 3,
1330 * Run with high priority (important requests).
1331 * Higher than DEFAULT.
1333 GNUNET_SCHEDULER_PRIORITY_HIGH = 4,
1336 * Run with priority for interactive tasks.
1337 * Higher than "HIGH".
1339 GNUNET_SCHEDULER_PRIORITY_UI = 5,
1342 * Run with priority for urgent tasks. Use
1343 * for things like aborts and shutdowns that
1344 * need to preempt "UI"-level tasks.
1347 GNUNET_SCHEDULER_PRIORITY_URGENT = 6,
1350 * This is an internal priority level that is only used for tasks
1351 * that are being triggered due to shutdown (they have automatically
1352 * highest priority). User code must not use this priority level
1353 * directly. Tasks run with this priority level that internally
1354 * schedule other tasks will see their original priority level
1355 * be inherited (unless otherwise specified).
1357 GNUNET_SCHEDULER_PRIORITY_SHUTDOWN = 7,
1360 * Number of priorities (must be the last priority).
1361 * This priority must not be used by clients.
1363 GNUNET_SCHEDULER_PRIORITY_COUNT = 8
1367 #if 0 /* keep Emacsens' auto-indent happy */
1374 #endif /* GNUNET_COMMON_H */