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))
124 * define #GNUNET_EXTRA_LOGGING if using this header outside the GNUnet source
125 * tree where gnunet_config.h is unavailable
127 #ifndef GNUNET_EXTRA_LOGGING
128 #define GNUNET_EXTRA_LOGGING 0
135 # if __BYTE_ORDER == __LITTLE_ENDIAN
136 # define GNUNET_htobe16(x) __bswap_16 (x)
137 # define GNUNET_htole16(x) (x)
138 # define GNUNET_be16toh(x) __bswap_16 (x)
139 # define GNUNET_le16toh(x) (x)
141 # define GNUNET_htobe32(x) __bswap_32 (x)
142 # define GNUNET_htole32(x) (x)
143 # define GNUNET_be32toh(x) __bswap_32 (x)
144 # define GNUNET_le32toh(x) (x)
146 # define GNUNET_htobe64(x) __bswap_64 (x)
147 # define GNUNET_htole64(x) (x)
148 # define GNUNET_be64toh(x) __bswap_64 (x)
149 # define GNUNET_le64toh(x) (x)
151 # if __BYTE_ORDER == __BIG_ENDIAN
152 # define GNUNET_htobe16(x) (x)
153 # define GNUNET_htole16(x) __bswap_16 (x)
154 # define GNUNET_be16toh(x) (x)
155 # define GNUNET_le16toh(x) __bswap_16 (x)
157 # define GNUNET_htobe32(x) (x)
158 # define GNUNET_htole32(x) __bswap_32 (x)
159 # define GNUNET_be32toh(x) (x)
160 # define GNUNET_le32toh(x) __bswap_32 (x)
162 # define GNUNET_htobe64(x) (x)
163 # define GNUNET_htole64(x) __bswap_64 (x)
164 # define GNUNET_be64toh(x) (x)
165 # define GNUNET_le64toh(x) __bswap_64 (x)
170 * Macro used to avoid using 0 for the length of a variable-size
171 * array (Non-Zero-Length).
173 * Basically, C standard says that "int[n] x;" is undefined if n=0.
174 * This was supposed to prevent issues with pointer aliasing.
175 * However, C compilers may conclude that n!=0 as n=0 would be
176 * undefined, and then optimize under the assumption n!=0, which
177 * could cause actual issues. Hence, when initializing an array
178 * on the stack with a variable-length that might be zero, write
179 * "int[GNUNET_NZL(n)] x;" instead of "int[n] x".
181 #define GNUNET_NZL(l) GNUNET_MAX(1,l)
185 * gcc-ism to get packed structs.
187 #define GNUNET_PACKED __attribute__((packed))
190 * gcc-ism to get gcc bitfield layout when compiling with -mms-bitfields
193 #define GNUNET_GCC_STRUCT_LAYOUT __attribute__((gcc_struct))
195 #define GNUNET_GCC_STRUCT_LAYOUT
199 * gcc-ism to force alignment; we use this to align char-arrays
200 * that may then be cast to 'struct's. See also gcc
203 #ifdef __BIGGEST_ALIGNMENT__
204 #define GNUNET_ALIGN __attribute__((aligned (__BIGGEST_ALIGNMENT__)))
206 #define GNUNET_ALIGN __attribute__((aligned (8)))
210 * gcc-ism to document unused arguments
212 #define GNUNET_UNUSED __attribute__((unused))
215 * gcc-ism to document functions that don't return
217 #define GNUNET_NORETURN __attribute__((noreturn))
222 * gcc 4.x-ism to pack structures even on W32 (to be used before structs);
223 * Using this would cause structs to be unaligned on the stack on Sparc,
224 * so we *only* use this on W32 (see #670578 from Debian); fortunately,
225 * W32 doesn't run on sparc anyway.
227 #define GNUNET_NETWORK_STRUCT_BEGIN \
228 _Pragma("pack(push)") \
232 * gcc 4.x-ism to pack structures even on W32 (to be used after structs)
233 * Using this would cause structs to be unaligned on the stack on Sparc,
234 * so we *only* use this on W32 (see #670578 from Debian); fortunately,
235 * W32 doesn't run on sparc anyway.
237 #define GNUNET_NETWORK_STRUCT_END _Pragma("pack(pop)")
240 #error gcc 4.x or higher required on W32 systems
244 * Define as empty, GNUNET_PACKED should suffice, but this won't work on W32
246 #define GNUNET_NETWORK_STRUCT_BEGIN
249 * Define as empty, GNUNET_PACKED should suffice, but this won't work on W32;
251 #define GNUNET_NETWORK_STRUCT_END
254 /* ************************ super-general types *********************** */
256 GNUNET_NETWORK_STRUCT_BEGIN
259 * Header for all communications.
261 struct GNUNET_MessageHeader
265 * The length of the struct (in bytes, including the length field itself),
266 * in big-endian format.
268 uint16_t size GNUNET_PACKED;
271 * The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
273 uint16_t type GNUNET_PACKED;
279 * Answer from service to client about last operation.
281 struct GNUNET_OperationResultMessage
283 struct GNUNET_MessageHeader header;
285 uint32_t reserved GNUNET_PACKED;
290 uint64_t op_id GNUNET_PACKED;
293 * Status code for the operation.
295 uint64_t result_code GNUNET_PACKED;
297 /* Followed by data. */
300 GNUNET_NETWORK_STRUCT_END
303 * Function called with a filename.
306 * @param filename complete filename (absolute path)
307 * @return #GNUNET_OK to continue to iterate,
308 * #GNUNET_NO to stop iteration with no error,
309 * #GNUNET_SYSERR to abort iteration with error!
312 (*GNUNET_FileNameCallback) (void *cls,
313 const char *filename);
317 * Generic continuation callback.
319 * @param cls Closure.
322 (*GNUNET_ContinuationCallback) (void *cls);
326 * Function called with the result of an asynchronous operation.
331 * Result code for the operation.
333 * Data result for the operation.
338 (*GNUNET_ResultCallback) (void *cls, int64_t result_code,
339 const void *data, uint16_t data_size);
342 /* ****************************** logging ***************************** */
348 enum GNUNET_ErrorType
350 GNUNET_ERROR_TYPE_UNSPECIFIED = -1,
351 GNUNET_ERROR_TYPE_NONE = 0,
352 GNUNET_ERROR_TYPE_ERROR = 1,
353 GNUNET_ERROR_TYPE_WARNING = 2,
354 /* UX: We need a message type that is output by
355 * default without looking like there is a problem.
357 GNUNET_ERROR_TYPE_MESSAGE = 4,
358 GNUNET_ERROR_TYPE_INFO = 8,
359 GNUNET_ERROR_TYPE_DEBUG = 16,
360 GNUNET_ERROR_TYPE_INVALID = 32,
361 GNUNET_ERROR_TYPE_BULK = 64
367 * User-defined handler for log messages.
370 * @param kind severeity
371 * @param component what component is issuing the message?
372 * @param date when was the message logged?
373 * @param message what is the message
376 (*GNUNET_Logger) (void *cls,
377 enum GNUNET_ErrorType kind,
378 const char *component,
380 const char *message);
385 * Get the number of log calls that are going to be skipped
387 * @return number of log calls to be ignored
390 GNUNET_get_log_skip (void);
393 #if !defined(GNUNET_CULL_LOGGING)
395 GNUNET_get_log_call_status (int caller_level,
398 const char *function,
407 * @param kind how serious is the error?
408 * @param message what is the message (format string)
409 * @param ... arguments for format string
412 GNUNET_log_nocheck (enum GNUNET_ErrorType kind, const char *message, ...)
413 __attribute__ ((format (gnu_printf, 2, 3)));
416 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
417 #define _GNUNET_BOOLEAN_EXPR(expr) \
419 int _gnunet_boolean_var_; \
421 _gnunet_boolean_var_ = 1; \
423 _gnunet_boolean_var_ = 0; \
424 _gnunet_boolean_var_; \
426 #define GN_LIKELY(expr) (__builtin_expect (_GNUNET_BOOLEAN_EXPR(expr), 1))
427 #define GN_UNLIKELY(expr) (__builtin_expect (_GNUNET_BOOLEAN_EXPR(expr), 0))
429 #define GN_LIKELY(expr) (expr)
430 #define GN_UNLIKELY(expr) (expr)
433 #if !defined(GNUNET_LOG_CALL_STATUS)
434 #define GNUNET_LOG_CALL_STATUS -1
440 * Log function that specifies an alternative component.
441 * This function should be used by plugins.
443 * @param kind how serious is the error?
444 * @param comp component responsible for generating the message
445 * @param message what is the message (format string)
446 * @param ... arguments for format string
449 GNUNET_log_from_nocheck (enum GNUNET_ErrorType kind, const char *comp,
450 const char *message, ...);
452 #if !defined(GNUNET_CULL_LOGGING)
453 #define GNUNET_log_from(kind,comp,...) do { int log_line = __LINE__;\
454 static int log_call_enabled = GNUNET_LOG_CALL_STATUS;\
455 if ((GNUNET_EXTRA_LOGGING > 0) || ((GNUNET_ERROR_TYPE_DEBUG & (kind)) == 0)) { \
456 if (GN_UNLIKELY(log_call_enabled == -1))\
457 log_call_enabled = GNUNET_get_log_call_status ((kind) & (~GNUNET_ERROR_TYPE_BULK), (comp), __FILE__, __FUNCTION__, log_line); \
458 if (GN_UNLIKELY(GNUNET_get_log_skip () > 0)) { GNUNET_log_skip (-1, GNUNET_NO); }\
460 if (GN_UNLIKELY(log_call_enabled))\
461 GNUNET_log_from_nocheck ((kind), comp, __VA_ARGS__); \
466 #define GNUNET_log(kind,...) do { int log_line = __LINE__;\
467 static int log_call_enabled = GNUNET_LOG_CALL_STATUS;\
468 if ((GNUNET_EXTRA_LOGGING > 0) || ((GNUNET_ERROR_TYPE_DEBUG & (kind)) == 0)) { \
469 if (GN_UNLIKELY(log_call_enabled == -1))\
470 log_call_enabled = GNUNET_get_log_call_status ((kind) & (~GNUNET_ERROR_TYPE_BULK), NULL, __FILE__, __FUNCTION__, log_line);\
471 if (GN_UNLIKELY(GNUNET_get_log_skip () > 0)) { GNUNET_log_skip (-1, GNUNET_NO); }\
473 if (GN_UNLIKELY(log_call_enabled))\
474 GNUNET_log_nocheck ((kind), __VA_ARGS__); \
479 #define GNUNET_log(...)
480 #define GNUNET_log_from(...)
486 * Log error message about missing configuration option.
488 * @param kind log level
489 * @param section section with missing option
490 * @param option name of missing option
493 GNUNET_log_config_missing (enum GNUNET_ErrorType kind,
500 * Log error message about invalid configuration option value.
502 * @param kind log level
503 * @param section section with invalid option
504 * @param option name of invalid option
505 * @param required what is required that is invalid about the option
508 GNUNET_log_config_invalid (enum GNUNET_ErrorType kind,
511 const char *required);
516 * Abort the process, generate a core dump if possible.
517 * Most code should use `GNUNET_assert (0)` instead to
518 * first log the location of the failure.
521 GNUNET_abort_ (void) GNUNET_NORETURN;
526 * Ignore the next @a n calls to the log function.
528 * @param n number of log calls to ignore (could be negative)
529 * @param check_reset #GNUNET_YES to assert that the log skip counter is currently zero
532 GNUNET_log_skip (int n,
540 * @param comp default component to use
541 * @param loglevel what types of messages should be logged
542 * @param logfile change logging to logfile (use NULL to keep stderr)
543 * @return #GNUNET_OK on success, #GNUNET_SYSERR if logfile could not be opened
546 GNUNET_log_setup (const char *comp,
547 const char *loglevel,
548 const char *logfile);
553 * Add a custom logger. Note that installing any custom logger
554 * will disable the standard logger. When multiple custom loggers
555 * are installed, all will be called. The standard logger will
556 * only be used if no custom loggers are present.
558 * @param logger log function
559 * @param logger_cls closure for @a logger
562 GNUNET_logger_add (GNUNET_Logger logger,
568 * Remove a custom logger.
570 * @param logger log function
571 * @param logger_cls closure for @a logger
574 GNUNET_logger_remove (GNUNET_Logger logger,
580 * Convert a short hash value to a string (for printing debug messages).
581 * This is one of the very few calls in the entire API that is
584 * @param shc the hash code
588 GNUNET_sh2s (const struct GNUNET_ShortHashCode *shc);
593 * Convert a hash value to a string (for printing debug messages).
594 * This is one of the very few calls in the entire API that is
597 * @param hc the hash code
601 GNUNET_h2s (const struct GNUNET_HashCode *hc);
606 * Convert a hash value to a string (for printing debug messages).
607 * This is one of the very few calls in the entire API that is
608 * NOT reentrant! Identical to #GNUNET_h2s(), except that another
609 * buffer is used so both #GNUNET_h2s() and #GNUNET_h2s2() can be
610 * used within the same log statement.
612 * @param hc the hash code
616 GNUNET_h2s2 (const struct GNUNET_HashCode *hc);
621 * Convert a hash value to a string (for printing debug messages).
622 * This prints all 104 characters of a hashcode!
623 * This is one of the very few calls in the entire API that is
626 * @param hc the hash code
630 GNUNET_h2s_full (const struct GNUNET_HashCode *hc);
634 * Public key. Details in gnunet_util_crypto.h.
636 struct GNUNET_CRYPTO_EddsaPublicKey;
640 * Public key. Details in gnunet_util_crypto.h.
642 struct GNUNET_CRYPTO_EcdhePublicKey;
647 * Convert a public key value to a string (for printing debug messages).
648 * This is one of the very few calls in the entire API that is
651 * @param hc the hash code
655 GNUNET_p2s (const struct GNUNET_CRYPTO_EddsaPublicKey *p);
660 * Convert a public key value to a string (for printing debug messages).
661 * This is one of the very few calls in the entire API that is
664 * @param hc the hash code
668 GNUNET_p2s2 (const struct GNUNET_CRYPTO_EddsaPublicKey *p);
673 * Convert a public key value to a string (for printing debug messages).
674 * This is one of the very few calls in the entire API that is
677 * @param hc the hash code
681 GNUNET_e2s (const struct GNUNET_CRYPTO_EcdhePublicKey *p);
686 * Convert a public key value to a string (for printing debug messages).
687 * This is one of the very few calls in the entire API that is
690 * @param hc the hash code
694 GNUNET_e2s2 (const struct GNUNET_CRYPTO_EcdhePublicKey *p);
699 * Convert a peer identity to a string (for printing debug messages).
700 * This is one of the very few calls in the entire API that is
703 * @param pid the peer identity
704 * @return string form of the pid; will be overwritten by next
705 * call to #GNUNET_i2s().
708 GNUNET_i2s (const struct GNUNET_PeerIdentity *pid);
713 * Convert a peer identity to a string (for printing debug messages).
714 * This is one of the very few calls in the entire API that is
715 * NOT reentrant! Identical to #GNUNET_i2s(), except that another
716 * buffer is used so both #GNUNET_i2s() and #GNUNET_i2s2() can be
717 * used within the same log statement.
719 * @param pid the peer identity
720 * @return string form of the pid; will be overwritten by next
721 * call to #GNUNET_i2s().
724 GNUNET_i2s2 (const struct GNUNET_PeerIdentity *pid);
729 * Convert a peer identity to a string (for printing debug messages).
730 * This is one of the very few calls in the entire API that is
733 * @param pid the peer identity
734 * @return string form of the pid; will be overwritten by next
735 * call to #GNUNET_i2s_full().
738 GNUNET_i2s_full (const struct GNUNET_PeerIdentity *pid);
743 * Convert a "struct sockaddr*" (IPv4 or IPv6 address) to a string
744 * (for printing debug messages). This is one of the very few calls
745 * in the entire API that is NOT reentrant!
747 * @param addr the address
748 * @param addrlen the length of the @a addr
749 * @return nicely formatted string for the address
750 * will be overwritten by next call to #GNUNET_a2s().
753 GNUNET_a2s (const struct sockaddr *addr,
759 * Convert error type to string.
761 * @param kind type to convert
762 * @return string corresponding to the type
765 GNUNET_error_type_to_string (enum GNUNET_ErrorType kind);
770 * Use this for fatal errors that cannot be handled
772 #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)
777 * Use this for fatal errors that cannot be handled
779 #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)
784 * Use this for fatal errors that cannot be handled
786 * @param cond Condition to evaluate
787 * @param comp Component string to use for logging
789 #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)
794 * Use this for internal assertion violations that are
795 * not fatal (can be handled) but should not occur.
797 #define GNUNET_break(cond) do { if (! (cond)) { GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Assertion failed at %s:%d.\n"), __FILE__, __LINE__); } } while(0)
802 * Use this for assertion violations caused by other
803 * peers (i.e. protocol violations). We do not want to
804 * confuse end-users (say, some other peer runs an
805 * older, broken or incompatible GNUnet version), but
806 * we still want to see these problems during
807 * development and testing. "OP == other peer".
809 #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)
814 * Log an error message at log-level 'level' that indicates
815 * a failure of the command 'cmd' with the message given
816 * by strerror(errno).
818 #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)
823 * Log an error message at log-level 'level' that indicates
824 * a failure of the command 'cmd' with the message given
825 * by strerror(errno).
827 #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)
832 * Log an error message at log-level 'level' that indicates
833 * a failure of the command 'cmd' with the message given
834 * by strerror(errno).
836 #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)
841 * Log an error message at log-level 'level' that indicates
842 * a failure of the command 'cmd' with the message given
843 * by strerror(errno).
845 #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)
847 /* ************************* endianess conversion ****************** */
850 * Convert unsigned 64-bit integer to network byte order.
853 * The value in host byte order.
855 * @return The same value in network byte order.
858 GNUNET_htonll (uint64_t n);
862 * Convert unsigned 64-bit integer to host byte order.
865 * The value in network byte order.
867 * @return The same value in host byte order.
870 GNUNET_ntohll (uint64_t n);
874 * Convert double to network byte order.
877 * The value in host byte order.
879 * @return The same value in network byte order.
882 GNUNET_hton_double (double d);
886 * Convert double to host byte order
889 * The value in network byte order.
891 * @return The same value in host byte order.
894 GNUNET_ntoh_double (double d);
897 /* ************************* allocation functions ****************** */
901 * Maximum allocation with GNUNET_malloc macro.
903 #define GNUNET_MAX_MALLOC_CHECKED (1024 * 1024 * 40)
907 * Allocate a struct or union of the given @a type.
908 * Wrapper around #GNUNET_malloc that returns a pointer
909 * to the newly created object of the correct type.
911 * @param type name of the struct or union, i.e. pass 'struct Foo'.
913 #define GNUNET_new(type) (type *) GNUNET_malloc (sizeof (type))
916 * Call memcpy() but check for @a n being 0 first. In the latter
917 * case, it is now safe to pass NULL for @a src or @a dst.
918 * Unlike traditional memcpy(), returns nothing.
920 * @param dst destination of the copy, may be NULL if @a n is zero
921 * @param src source of the copy, may be NULL if @a n is zero
922 * @param n number of bytes to copy
924 #define GNUNET_memcpy(dst,src,n) do { if (0 != n) { (void) memcpy (dst,src,n); } } while (0)
929 * Allocate a size @a n array with structs or unions of the given @a type.
930 * Wrapper around #GNUNET_malloc that returns a pointer
931 * to the newly created objects of the correct type.
933 * @param n number of elements in the array
934 * @param type name of the struct or union, i.e. pass 'struct Foo'.
936 #define GNUNET_new_array(n, type) (type *) GNUNET_malloc ((n) * sizeof (type))
940 * Allocate a size @a n times @a m 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 type name of the struct or union, i.e. pass 'struct Foo'.
947 #define GNUNET_new_array_2d(n, m, type) (type **) GNUNET_xnew_array_2d_ (n, m, sizeof (type), __FILE__, __LINE__)
951 * Allocate a size @a n times @a m times @a o array
952 * with structs or unions of the given @a type.
954 * @param n size of the first dimension
955 * @param m size of the second dimension
956 * @param o size of the third dimension
957 * @param type name of the struct or union, i.e. pass 'struct Foo'.
959 #define GNUNET_new_array_3d(n, m, o, type) (type ***) GNUNET_xnew_array_3d_ (n, m, o, sizeof (type), __FILE__, __LINE__)
963 * Wrapper around malloc. Allocates size bytes of memory.
964 * The memory will be zero'ed out.
966 * @param size the number of bytes to allocate, must be
967 * smaller than 40 MB.
968 * @return pointer to size bytes of memory, never NULL (!)
970 #define GNUNET_malloc(size) GNUNET_xmalloc_(size, __FILE__, __LINE__)
974 * Allocate and initialize a block of memory.
976 * @param buf data to initalize the block with
977 * @param size the number of bytes in buf (and size of the allocation)
978 * @return pointer to size bytes of memory, never NULL (!)
980 #define GNUNET_memdup(buf,size) GNUNET_xmemdup_(buf, size, __FILE__, __LINE__)
984 * Wrapper around malloc. Allocates size bytes of memory.
985 * The memory will be zero'ed out.
987 * @param size the number of bytes to allocate
988 * @return pointer to size bytes of memory, NULL if we do not have enough memory
990 #define GNUNET_malloc_large(size) GNUNET_xmalloc_unchecked_(size, __FILE__, __LINE__)
994 * Wrapper around realloc. Reallocates size bytes of memory.
995 * The content of the intersection of the new and old size will be unchanged.
997 * @param ptr the pointer to reallocate
998 * @param size the number of bytes to reallocate
999 * @return pointer to size bytes of memory
1001 #define GNUNET_realloc(ptr, size) GNUNET_xrealloc_(ptr, size, __FILE__, __LINE__)
1005 * Wrapper around free. Frees the memory referred to by ptr.
1006 * Note that it is generally better to free memory that was
1007 * allocated with #GNUNET_array_grow using #GNUNET_array_grow(mem, size, 0) instead of #GNUNET_free.
1009 * @param ptr location where to free the memory. ptr must have
1010 * been returned by #GNUNET_strdup, #GNUNET_strndup, #GNUNET_malloc or #GNUNET_array_grow earlier.
1012 #define GNUNET_free(ptr) GNUNET_xfree_(ptr, __FILE__, __LINE__)
1016 * Free the memory pointed to by ptr if ptr is not NULL.
1017 * Equivalent to `if (NULL != ptr) GNUNET_free(ptr)`.
1019 * @param ptr the location in memory to free
1021 #define GNUNET_free_non_null(ptr) do { void * __x__ = ptr; if (__x__ != NULL) { GNUNET_free(__x__); } } while(0)
1025 * Wrapper around #GNUNET_xstrdup_. Makes a copy of the zero-terminated string
1028 * @param a pointer to a zero-terminated string
1029 * @return a copy of the string including zero-termination
1031 #define GNUNET_strdup(a) GNUNET_xstrdup_(a,__FILE__,__LINE__)
1035 * Wrapper around #GNUNET_xstrndup_. Makes a partial copy of the string
1038 * @param a pointer to a string
1039 * @param length of the string to duplicate
1040 * @return a partial copy of the string including zero-termination
1042 #define GNUNET_strndup(a,length) GNUNET_xstrndup_(a,length,__FILE__,__LINE__)
1046 * Grow a well-typed (!) array. This is a convenience
1047 * method to grow a vector @a arr of size @a size
1048 * to the new (target) size @a tsize.
1051 * Example (simple, well-typed stack):
1054 * static struct foo * myVector = NULL;
1055 * static int myVecLen = 0;
1057 * static void push(struct foo * elem) {
1058 * GNUNET_array_grow(myVector, myVecLen, myVecLen+1);
1059 * GNUNET_memcpy(&myVector[myVecLen-1], elem, sizeof(struct foo));
1062 * static void pop(struct foo * elem) {
1063 * if (myVecLen == 0) die();
1064 * GNUNET_memcpy(elem, myVector[myVecLen-1], sizeof(struct foo));
1065 * GNUNET_array_grow(myVector, myVecLen, myVecLen-1);
1069 * @param arr base-pointer of the vector, may be NULL if size is 0;
1070 * will be updated to reflect the new address. The TYPE of
1071 * arr is important since size is the number of elements and
1072 * not the size in bytes
1073 * @param size the number of elements in the existing vector (number
1074 * of elements to copy over), will be updated with the new
1076 * @param tsize the target size for the resulting vector, use 0 to
1077 * free the vector (then, arr will be NULL afterwards).
1079 #define GNUNET_array_grow(arr,size,tsize) GNUNET_xgrow_((void**)&(arr), sizeof((arr)[0]), &size, tsize, __FILE__, __LINE__)
1083 * Append an element to a list (growing the list by one).
1085 * @param arr base-pointer of the vector, may be NULL if size is 0;
1086 * will be updated to reflect the new address. The TYPE of
1087 * arr is important since size is the number of elements and
1088 * not the size in bytes
1089 * @param size the number of elements in the existing vector (number
1090 * of elements to copy over), will be updated with the new
1092 * @param element the element that will be appended to the array
1094 #define GNUNET_array_append(arr,size,element) do { GNUNET_array_grow(arr,size,size+1); (arr)[size-1] = element; } while(0)
1098 * Like snprintf, just aborts if the buffer is of insufficient size.
1100 * @param buf pointer to buffer that is written to
1101 * @param size number of bytes in @a buf
1102 * @param format format strings
1103 * @param ... data for format string
1104 * @return number of bytes written to buf or negative value on error
1107 GNUNET_snprintf (char *buf, size_t size, const char *format, ...);
1112 * Like asprintf, just portable.
1114 * @param buf set to a buffer of sufficient size (allocated, caller must free)
1115 * @param format format string (see printf, fprintf, etc.)
1116 * @param ... data for format string
1117 * @return number of bytes in "*buf" excluding 0-termination
1120 GNUNET_asprintf (char **buf, const char *format, ...);
1123 /* ************** internal implementations, use macros above! ************** */
1126 * Allocate memory. Checks the return value, aborts if no more
1127 * memory is available. Don't use GNUNET_xmalloc_ directly. Use the
1128 * #GNUNET_malloc macro.
1129 * The memory will be zero'ed out.
1131 * @param size number of bytes to allocate
1132 * @param filename where is this call being made (for debugging)
1133 * @param linenumber line where this call is being made (for debugging)
1134 * @return allocated memory, never NULL
1137 GNUNET_xmalloc_ (size_t size, const char *filename, int linenumber);
1141 * Allocate memory for a two dimensional array in one block
1142 * and set up pointers. Aborts if no more memory is available.
1143 * Don't use GNUNET_xnew_array_2d_ directly. Use the
1144 * #GNUNET_new_array_2d macro.
1145 * The memory of the elements will be zero'ed out.
1147 * @param n size of the first dimension
1148 * @param m size of the second dimension
1149 * @param elementSize size of a single element in bytes
1150 * @param filename where is this call being made (for debugging)
1151 * @param linenumber line where this call is being made (for debugging)
1152 * @return allocated memory, never NULL
1155 GNUNET_xnew_array_2d_ (size_t n, size_t m, size_t elementSize,
1156 const char *filename, int linenumber);
1160 * Allocate memory for a three dimensional array in one block
1161 * and set up pointers. Aborts if no more memory is available.
1162 * Don't use GNUNET_xnew_array_3d_ directly. Use the
1163 * #GNUNET_new_array_3d macro.
1164 * The memory of the elements will be zero'ed out.
1166 * @param n size of the first dimension
1167 * @param m size of the second dimension
1168 * @param o size of the third dimension
1169 * @param elementSize size of a single element in bytes
1170 * @param filename where is this call being made (for debugging)
1171 * @param linenumber line where this call is being made (for debugging)
1172 * @return allocated memory, never NULL
1175 GNUNET_xnew_array_3d_ (size_t n, size_t m, size_t o, size_t elementSize,
1176 const char *filename, int linenumber);
1180 * Allocate and initialize memory. Checks the return value, aborts if no more
1181 * memory is available. Don't use GNUNET_xmemdup_ directly. Use the
1182 * #GNUNET_memdup macro.
1184 * @param buf buffer to initialize from (must contain size bytes)
1185 * @param size number of bytes to allocate
1186 * @param filename where is this call being made (for debugging)
1187 * @param linenumber line where this call is being made (for debugging)
1188 * @return allocated memory, never NULL
1191 GNUNET_xmemdup_ (const void *buf, size_t size, const char *filename,
1196 * Allocate memory. This function does not check if the allocation
1197 * request is within reasonable bounds, allowing allocations larger
1198 * than 40 MB. If you don't expect the possibility of very large
1199 * allocations, use #GNUNET_malloc instead. The memory will be zero'ed
1202 * @param size number of bytes to allocate
1203 * @param filename where is this call being made (for debugging)
1204 * @param linenumber line where this call is being made (for debugging)
1205 * @return pointer to size bytes of memory, NULL if we do not have enough memory
1208 GNUNET_xmalloc_unchecked_ (size_t size, const char *filename, int linenumber);
1212 * Reallocate memory. Checks the return value, aborts if no more
1213 * memory is available.
1216 GNUNET_xrealloc_ (void *ptr, size_t n, const char *filename, int linenumber);
1220 * Free memory. Merely a wrapper for the case that we
1221 * want to keep track of allocations. Don't use GNUNET_xfree_
1222 * directly. Use the #GNUNET_free macro.
1224 * @param ptr pointer to memory to free
1225 * @param filename where is this call being made (for debugging)
1226 * @param linenumber line where this call is being made (for debugging)
1229 GNUNET_xfree_ (void *ptr, const char *filename, int linenumber);
1233 * Dup a string. Don't call GNUNET_xstrdup_ directly. Use the #GNUNET_strdup macro.
1234 * @param str string to duplicate
1235 * @param filename where is this call being made (for debugging)
1236 * @param linenumber line where this call is being made (for debugging)
1237 * @return the duplicated string
1240 GNUNET_xstrdup_ (const char *str, const char *filename, int linenumber);
1243 * Dup partially a string. Don't call GNUNET_xstrndup_ directly. Use the #GNUNET_strndup macro.
1245 * @param str string to duplicate
1246 * @param len length of the string to duplicate
1247 * @param filename where is this call being made (for debugging)
1248 * @param linenumber line where this call is being made (for debugging)
1249 * @return the duplicated string
1252 GNUNET_xstrndup_ (const char *str, size_t len, const char *filename,
1256 * Grow an array, the new elements are zeroed out.
1257 * Grows old by (*oldCount-newCount)*elementSize
1258 * bytes and sets *oldCount to newCount.
1260 * Don't call GNUNET_xgrow_ directly. Use the #GNUNET_array_grow macro.
1262 * @param old address of the pointer to the array
1264 * @param elementSize the size of the elements of the array
1265 * @param oldCount address of the number of elements in the *old array
1266 * @param newCount number of elements in the new array, may be 0 (then *old will be NULL afterwards)
1267 * @param filename where is this call being made (for debugging)
1268 * @param linenumber line where this call is being made (for debugging)
1271 GNUNET_xgrow_ (void **old, size_t elementSize, unsigned int *oldCount,
1272 unsigned int newCount, const char *filename, int linenumber);
1277 * Create a copy of the given message.
1279 * @param msg message to copy
1280 * @return duplicate of the message
1282 struct GNUNET_MessageHeader *
1283 GNUNET_copy_message (const struct GNUNET_MessageHeader *msg);
1286 #if __STDC_VERSION__ < 199901L
1288 #define __func__ __FUNCTION__
1290 #define __func__ "<unknown>"
1296 * Valid task priorities. Use these, do not pass random integers!
1297 * For various reasons (#3862 -- building with QT Creator, and
1298 * our restricted cross-compilation with emscripten) this cannot
1299 * be in gnunet_scheduler_lib.h, but it works if we declare it here.
1300 * Naturally, logically this is part of the scheduler.
1302 enum GNUNET_SCHEDULER_Priority
1305 * Run with the same priority as the current job.
1307 GNUNET_SCHEDULER_PRIORITY_KEEP = 0,
1310 * Run when otherwise idle.
1312 GNUNET_SCHEDULER_PRIORITY_IDLE = 1,
1315 * Run as background job (higher than idle,
1316 * lower than default).
1318 GNUNET_SCHEDULER_PRIORITY_BACKGROUND = 2,
1321 * Run with the default priority (normal
1322 * P2P operations). Any task that is scheduled
1323 * without an explicit priority being specified
1324 * will run with this priority.
1326 GNUNET_SCHEDULER_PRIORITY_DEFAULT = 3,
1329 * Run with high priority (important requests).
1330 * Higher than DEFAULT.
1332 GNUNET_SCHEDULER_PRIORITY_HIGH = 4,
1335 * Run with priority for interactive tasks.
1336 * Higher than "HIGH".
1338 GNUNET_SCHEDULER_PRIORITY_UI = 5,
1341 * Run with priority for urgent tasks. Use
1342 * for things like aborts and shutdowns that
1343 * need to preempt "UI"-level tasks.
1346 GNUNET_SCHEDULER_PRIORITY_URGENT = 6,
1349 * This is an internal priority level that is only used for tasks
1350 * that are being triggered due to shutdown (they have automatically
1351 * highest priority). User code must not use this priority level
1352 * directly. Tasks run with this priority level that internally
1353 * schedule other tasks will see their original priority level
1354 * be inherited (unless otherwise specified).
1356 GNUNET_SCHEDULER_PRIORITY_SHUTDOWN = 7,
1359 * Number of priorities (must be the last priority).
1360 * This priority must not be used by clients.
1362 GNUNET_SCHEDULER_PRIORITY_COUNT = 8
1366 #if 0 /* keep Emacsens' auto-indent happy */
1373 #endif /* GNUNET_COMMON_H */