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
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
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
30 * @defgroup logging Logging
31 * @see [Documentation](https://gnunet.org/logging)
33 * @defgroup memory Memory management
35 #ifndef GNUNET_COMMON_H
36 #define GNUNET_COMMON_H
39 #include <sys/socket.h>
42 #include <netinet/in.h>
54 #ifdef HAVE_BYTESWAP_H
61 #if 0 /* keep Emacsens' auto-indent happy */
67 * Version of the API (for entire gnunetutil.so library).
69 #define GNUNET_UTIL_VERSION 0x000A0101
73 * Named constants for return values. The following invariants hold:
74 * `GNUNET_NO == 0` (to allow `if (GNUNET_NO)`) `GNUNET_OK !=
75 * GNUNET_SYSERR`, `GNUNET_OK != GNUNET_NO`, `GNUNET_NO !=
76 * GNUNET_SYSERR` and finally `GNUNET_YES != GNUNET_NO`.
79 #define GNUNET_SYSERR -1
83 #define GNUNET_MIN(a,b) (((a) < (b)) ? (a) : (b))
85 #define GNUNET_MAX(a,b) (((a) > (b)) ? (a) : (b))
87 /* some systems use one underscore only, and mingw uses no underscore... */
90 #define __BYTE_ORDER _BYTE_ORDER
93 #define __BYTE_ORDER BYTE_ORDER
99 #define __BIG_ENDIAN _BIG_ENDIAN
102 #define __BIG_ENDIAN BIG_ENDIAN
106 #ifndef __LITTLE_ENDIAN
107 #ifdef _LITTLE_ENDIAN
108 #define __LITTLE_ENDIAN _LITTLE_ENDIAN
111 #define __LITTLE_ENDIAN LITTLE_ENDIAN
118 * define #GNUNET_EXTRA_LOGGING if using this header outside the GNUnet source
119 * tree where gnunet_config.h is unavailable
121 #ifndef GNUNET_EXTRA_LOGGING
122 #define GNUNET_EXTRA_LOGGING 0
129 # if __BYTE_ORDER == __LITTLE_ENDIAN
130 # define GNUNET_htobe16(x) __bswap_16 (x)
131 # define GNUNET_htole16(x) (x)
132 # define GNUNET_be16toh(x) __bswap_16 (x)
133 # define GNUNET_le16toh(x) (x)
135 # define GNUNET_htobe32(x) __bswap_32 (x)
136 # define GNUNET_htole32(x) (x)
137 # define GNUNET_be32toh(x) __bswap_32 (x)
138 # define GNUNET_le32toh(x) (x)
140 # define GNUNET_htobe64(x) __bswap_64 (x)
141 # define GNUNET_htole64(x) (x)
142 # define GNUNET_be64toh(x) __bswap_64 (x)
143 # define GNUNET_le64toh(x) (x)
145 # if __BYTE_ORDER == __BIG_ENDIAN
146 # define GNUNET_htobe16(x) (x)
147 # define GNUNET_htole16(x) __bswap_16 (x)
148 # define GNUNET_be16toh(x) (x)
149 # define GNUNET_le16toh(x) __bswap_16 (x)
151 # define GNUNET_htobe32(x) (x)
152 # define GNUNET_htole32(x) __bswap_32 (x)
153 # define GNUNET_be32toh(x) (x)
154 # define GNUNET_le32toh(x) __bswap_32 (x)
156 # define GNUNET_htobe64(x) (x)
157 # define GNUNET_htole64(x) __bswap_64 (x)
158 # define GNUNET_be64toh(x) (x)
159 # define GNUNET_le64toh(x) __bswap_64 (x)
166 * gcc-ism to get packed structs.
168 #define GNUNET_PACKED __attribute__((packed))
171 * gcc-ism to get gcc bitfield layout when compiling with -mms-bitfields
174 #define GNUNET_GCC_STRUCT_LAYOUT __attribute__((gcc_struct))
176 #define GNUNET_GCC_STRUCT_LAYOUT
180 * gcc-ism to force alignment; we use this to align char-arrays
181 * that may then be cast to 'struct's. See also gcc
184 #ifdef __BIGGEST_ALIGNMENT__
185 #define GNUNET_ALIGN __attribute__((aligned (__BIGGEST_ALIGNMENT__)))
187 #define GNUNET_ALIGN __attribute__((aligned (8)))
191 * gcc-ism to document unused arguments
193 #define GNUNET_UNUSED __attribute__((unused))
196 * gcc-ism to document functions that don't return
198 #define GNUNET_NORETURN __attribute__((noreturn))
203 * gcc 4.x-ism to pack structures even on W32 (to be used before structs);
204 * Using this would cause structs to be unaligned on the stack on Sparc,
205 * so we *only* use this on W32 (see #670578 from Debian); fortunately,
206 * W32 doesn't run on sparc anyway.
208 #define GNUNET_NETWORK_STRUCT_BEGIN \
209 _Pragma("pack(push)") \
213 * gcc 4.x-ism to pack structures even on W32 (to be used after structs)
214 * Using this would cause structs to be unaligned on the stack on Sparc,
215 * so we *only* use this on W32 (see #670578 from Debian); fortunately,
216 * W32 doesn't run on sparc anyway.
218 #define GNUNET_NETWORK_STRUCT_END _Pragma("pack(pop)")
221 #error gcc 4.x or higher required on W32 systems
225 * Define as empty, GNUNET_PACKED should suffice, but this won't work on W32
227 #define GNUNET_NETWORK_STRUCT_BEGIN
230 * Define as empty, GNUNET_PACKED should suffice, but this won't work on W32;
232 #define GNUNET_NETWORK_STRUCT_END
235 /* ************************ super-general types *********************** */
237 GNUNET_NETWORK_STRUCT_BEGIN
240 * Header for all communications.
242 struct GNUNET_MessageHeader
246 * The length of the struct (in bytes, including the length field itself),
247 * in big-endian format.
249 uint16_t size GNUNET_PACKED;
252 * The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
254 uint16_t type GNUNET_PACKED;
260 * Answer from service to client about last operation.
262 struct GNUNET_OperationResultMessage
264 struct GNUNET_MessageHeader header;
266 uint32_t reserved GNUNET_PACKED;
271 uint64_t op_id GNUNET_PACKED;
274 * Status code for the operation.
276 uint64_t result_code GNUNET_PACKED;
278 /* Followed by data. */
281 GNUNET_NETWORK_STRUCT_END
284 * Function called with a filename.
287 * @param filename complete filename (absolute path)
288 * @return #GNUNET_OK to continue to iterate,
289 * #GNUNET_NO to stop iteration with no error,
290 * #GNUNET_SYSERR to abort iteration with error!
293 (*GNUNET_FileNameCallback) (void *cls,
294 const char *filename);
298 * Generic continuation callback.
300 * @param cls Closure.
303 (*GNUNET_ContinuationCallback) (void *cls);
307 * Function called with the result of an asynchronous operation.
312 * Result code for the operation.
314 * Data result for the operation.
319 (*GNUNET_ResultCallback) (void *cls, int64_t result_code,
320 const void *data, uint16_t data_size);
323 /* ****************************** logging ***************************** */
329 enum GNUNET_ErrorType
331 GNUNET_ERROR_TYPE_UNSPECIFIED = -1,
332 GNUNET_ERROR_TYPE_NONE = 0,
333 GNUNET_ERROR_TYPE_ERROR = 1,
334 GNUNET_ERROR_TYPE_WARNING = 2,
335 GNUNET_ERROR_TYPE_INFO = 4,
336 GNUNET_ERROR_TYPE_DEBUG = 8,
337 GNUNET_ERROR_TYPE_INVALID = 16,
338 GNUNET_ERROR_TYPE_BULK = 32
344 * User-defined handler for log messages.
347 * @param kind severeity
348 * @param component what component is issuing the message?
349 * @param date when was the message logged?
350 * @param message what is the message
353 (*GNUNET_Logger) (void *cls,
354 enum GNUNET_ErrorType kind,
355 const char *component,
357 const char *message);
362 * Get the number of log calls that are going to be skipped
364 * @return number of log calls to be ignored
367 GNUNET_get_log_skip (void);
370 #if !defined(GNUNET_CULL_LOGGING)
372 GNUNET_get_log_call_status (int caller_level,
375 const char *function,
384 * @param kind how serious is the error?
385 * @param message what is the message (format string)
386 * @param ... arguments for format string
389 GNUNET_log_nocheck (enum GNUNET_ErrorType kind, const char *message, ...)
390 __attribute__ ((format (printf, 2, 3)));
393 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
394 #define _GNUNET_BOOLEAN_EXPR(expr) \
396 int _gnunet_boolean_var_; \
398 _gnunet_boolean_var_ = 1; \
400 _gnunet_boolean_var_ = 0; \
401 _gnunet_boolean_var_; \
403 #define GN_LIKELY(expr) (__builtin_expect (_GNUNET_BOOLEAN_EXPR(expr), 1))
404 #define GN_UNLIKELY(expr) (__builtin_expect (_GNUNET_BOOLEAN_EXPR(expr), 0))
406 #define GN_LIKELY(expr) (expr)
407 #define GN_UNLIKELY(expr) (expr)
410 #if !defined(GNUNET_LOG_CALL_STATUS)
411 #define GNUNET_LOG_CALL_STATUS -1
417 * Log function that specifies an alternative component.
418 * This function should be used by plugins.
420 * @param kind how serious is the error?
421 * @param comp component responsible for generating the message
422 * @param message what is the message (format string)
423 * @param ... arguments for format string
426 GNUNET_log_from_nocheck (enum GNUNET_ErrorType kind, const char *comp,
427 const char *message, ...);
429 #if !defined(GNUNET_CULL_LOGGING)
430 #define GNUNET_log_from(kind,comp,...) do { int log_line = __LINE__;\
431 static int log_call_enabled = GNUNET_LOG_CALL_STATUS;\
432 if ((GNUNET_EXTRA_LOGGING > 0) || ((GNUNET_ERROR_TYPE_DEBUG & (kind)) == 0)) { \
433 if (GN_UNLIKELY(log_call_enabled == -1))\
434 log_call_enabled = GNUNET_get_log_call_status ((kind) & (~GNUNET_ERROR_TYPE_BULK), (comp), __FILE__, __FUNCTION__, log_line); \
435 if (GN_UNLIKELY(GNUNET_get_log_skip () > 0)) { GNUNET_log_skip (-1, GNUNET_NO); }\
437 if (GN_UNLIKELY(log_call_enabled))\
438 GNUNET_log_from_nocheck ((kind), comp, __VA_ARGS__); \
443 #define GNUNET_log(kind,...) do { int log_line = __LINE__;\
444 static int log_call_enabled = GNUNET_LOG_CALL_STATUS;\
445 if ((GNUNET_EXTRA_LOGGING > 0) || ((GNUNET_ERROR_TYPE_DEBUG & (kind)) == 0)) { \
446 if (GN_UNLIKELY(log_call_enabled == -1))\
447 log_call_enabled = GNUNET_get_log_call_status ((kind) & (~GNUNET_ERROR_TYPE_BULK), NULL, __FILE__, __FUNCTION__, log_line);\
448 if (GN_UNLIKELY(GNUNET_get_log_skip () > 0)) { GNUNET_log_skip (-1, GNUNET_NO); }\
450 if (GN_UNLIKELY(log_call_enabled))\
451 GNUNET_log_nocheck ((kind), __VA_ARGS__); \
456 #define GNUNET_log(...)
457 #define GNUNET_log_from(...)
463 * Log error message about missing configuration option.
465 * @param kind log level
466 * @param section section with missing option
467 * @param option name of missing option
470 GNUNET_log_config_missing (enum GNUNET_ErrorType kind,
477 * Log error message about invalid configuration option value.
479 * @param kind log level
480 * @param section section with invalid option
481 * @param option name of invalid option
482 * @param required what is required that is invalid about the option
485 GNUNET_log_config_invalid (enum GNUNET_ErrorType kind,
488 const char *required);
493 * Abort the process, generate a core dump if possible.
494 * Most code should use `GNUNET_assert (0)` instead to
495 * first log the location of the failure.
498 GNUNET_abort_ (void) GNUNET_NORETURN;
503 * Ignore the next @a n calls to the log function.
505 * @param n number of log calls to ignore (could be negative)
506 * @param check_reset #GNUNET_YES to assert that the log skip counter is currently zero
509 GNUNET_log_skip (int n,
517 * @param comp default component to use
518 * @param loglevel what types of messages should be logged
519 * @param logfile change logging to logfile (use NULL to keep stderr)
520 * @return #GNUNET_OK on success, #GNUNET_SYSERR if logfile could not be opened
523 GNUNET_log_setup (const char *comp,
524 const char *loglevel,
525 const char *logfile);
530 * Add a custom logger. Note that installing any custom logger
531 * will disable the standard logger. When multiple custom loggers
532 * are installed, all will be called. The standard logger will
533 * only be used if no custom loggers are present.
535 * @param logger log function
536 * @param logger_cls closure for @a logger
539 GNUNET_logger_add (GNUNET_Logger logger,
545 * Remove a custom logger.
547 * @param logger log function
548 * @param logger_cls closure for @a logger
551 GNUNET_logger_remove (GNUNET_Logger logger,
557 * Convert a hash value to a string (for printing debug messages).
558 * This is one of the very few calls in the entire API that is
561 * @param hc the hash code
565 GNUNET_h2s (const struct GNUNET_HashCode * hc);
570 * Convert a hash value to a string (for printing debug messages).
571 * This prints all 104 characters of a hashcode!
572 * This is one of the very few calls in the entire API that is
575 * @param hc the hash code
579 GNUNET_h2s_full (const struct GNUNET_HashCode * hc);
584 * Convert a peer identity to a string (for printing debug messages).
585 * This is one of the very few calls in the entire API that is
588 * @param pid the peer identity
589 * @return string form of the pid; will be overwritten by next
590 * call to #GNUNET_i2s().
593 GNUNET_i2s (const struct GNUNET_PeerIdentity *pid);
598 * Convert a peer identity to a string (for printing debug messages).
599 * This is one of the very few calls in the entire API that is
602 * @param pid the peer identity
603 * @return string form of the pid; will be overwritten by next
604 * call to #GNUNET_i2s_full().
607 GNUNET_i2s_full (const struct GNUNET_PeerIdentity *pid);
612 * Convert a "struct sockaddr*" (IPv4 or IPv6 address) to a string
613 * (for printing debug messages). This is one of the very few calls
614 * in the entire API that is NOT reentrant!
616 * @param addr the address
617 * @param addrlen the length of the @a addr
618 * @return nicely formatted string for the address
619 * will be overwritten by next call to #GNUNET_a2s().
622 GNUNET_a2s (const struct sockaddr *addr,
628 * Convert error type to string.
630 * @param kind type to convert
631 * @return string corresponding to the type
634 GNUNET_error_type_to_string (enum GNUNET_ErrorType kind);
639 * Use this for fatal errors that cannot be handled
641 #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)
646 * Use this for fatal errors that cannot be handled
648 #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)
653 * Use this for internal assertion violations that are
654 * not fatal (can be handled) but should not occur.
656 #define GNUNET_break(cond) do { if (! (cond)) { GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Assertion failed at %s:%d.\n"), __FILE__, __LINE__); } } while(0)
661 * Use this for assertion violations caused by other
662 * peers (i.e. protocol violations). We do not want to
663 * confuse end-users (say, some other peer runs an
664 * older, broken or incompatible GNUnet version), but
665 * we still want to see these problems during
666 * development and testing. "OP == other peer".
668 #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)
673 * Log an error message at log-level 'level' that indicates
674 * a failure of the command 'cmd' with the message given
675 * by strerror(errno).
677 #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)
682 * Log an error message at log-level 'level' that indicates
683 * a failure of the command 'cmd' with the message given
684 * by strerror(errno).
686 #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)
691 * Log an error message at log-level 'level' that indicates
692 * a failure of the command 'cmd' with the message given
693 * by strerror(errno).
695 #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)
700 * Log an error message at log-level 'level' that indicates
701 * a failure of the command 'cmd' with the message given
702 * by strerror(errno).
704 #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)
706 /* ************************* endianess conversion ****************** */
709 * Convert unsigned 64-bit integer to network byte order.
712 * The value in host byte order.
714 * @return The same value in network byte order.
717 GNUNET_htonll (uint64_t n);
721 * Convert unsigned 64-bit integer to host byte order.
724 * The value in network byte order.
726 * @return The same value in host byte order.
729 GNUNET_ntohll (uint64_t n);
733 * Convert double to network byte order.
736 * The value in host byte order.
738 * @return The same value in network byte order.
741 GNUNET_hton_double (double d);
745 * Convert double to host byte order
748 * The value in network byte order.
750 * @return The same value in host byte order.
753 GNUNET_ntoh_double (double d);
756 /* ************************* allocation functions ****************** */
760 * Maximum allocation with GNUNET_malloc macro.
762 #define GNUNET_MAX_MALLOC_CHECKED (1024 * 1024 * 40)
766 * Allocate a struct or union of the given @a type.
767 * Wrapper around #GNUNET_malloc that returns a pointer
768 * to the newly created object of the correct type.
770 * @param type name of the struct or union, i.e. pass 'struct Foo'.
772 #define GNUNET_new(type) (type *) GNUNET_malloc (sizeof (type))
775 * Call memcpy() but check for @a n being 0 first. In the latter
776 * case, it is now safe to pass NULL for @a src or @a dst.
777 * Unlike traditional memcpy(), returns nothing.
779 * @param dst destination of the copy, may be NULL if @a n is zero
780 * @param src source of the copy, may be NULL if @a n is zero
781 * @param n number of bytes to copy
783 #define GNUNET_memcpy(dst,src,n) do { if (0 != n) { (void) memcpy (dst,src,n); } } while (0)
787 * Allocate a size @a n array with structs or unions of the given @a type.
788 * Wrapper around #GNUNET_malloc that returns a pointer
789 * to the newly created objects of the correct type.
791 * @param n number of elements in the array
792 * @param type name of the struct or union, i.e. pass 'struct Foo'.
794 #define GNUNET_new_array(n, type) (type *) GNUNET_malloc ((n) * sizeof (type))
798 * Wrapper around malloc. Allocates size bytes of memory.
799 * The memory will be zero'ed out.
801 * @param size the number of bytes to allocate, must be
802 * smaller than 40 MB.
803 * @return pointer to size bytes of memory, never NULL (!)
805 #define GNUNET_malloc(size) GNUNET_xmalloc_(size, __FILE__, __LINE__)
809 * Allocate and initialize a block of memory.
811 * @param buf data to initalize the block with
812 * @param size the number of bytes in buf (and size of the allocation)
813 * @return pointer to size bytes of memory, never NULL (!)
815 #define GNUNET_memdup(buf,size) GNUNET_xmemdup_(buf, size, __FILE__, __LINE__)
819 * Wrapper around malloc. Allocates size bytes of memory.
820 * The memory will be zero'ed out.
822 * @param size the number of bytes to allocate
823 * @return pointer to size bytes of memory, NULL if we do not have enough memory
825 #define GNUNET_malloc_large(size) GNUNET_xmalloc_unchecked_(size, __FILE__, __LINE__)
829 * Wrapper around realloc. Rellocates size bytes of memory.
831 * @param ptr the pointer to reallocate
832 * @param size the number of bytes to reallocate
833 * @return pointer to size bytes of memory
835 #define GNUNET_realloc(ptr, size) GNUNET_xrealloc_(ptr, size, __FILE__, __LINE__)
839 * Wrapper around free. Frees the memory referred to by ptr.
840 * Note that it is generally better to free memory that was
841 * allocated with #GNUNET_array_grow using #GNUNET_array_grow(mem, size, 0) instead of #GNUNET_free.
843 * @param ptr location where to free the memory. ptr must have
844 * been returned by #GNUNET_strdup, #GNUNET_strndup, #GNUNET_malloc or #GNUNET_array_grow earlier.
846 #define GNUNET_free(ptr) GNUNET_xfree_(ptr, __FILE__, __LINE__)
850 * Free the memory pointed to by ptr if ptr is not NULL.
851 * Equivalent to `if (NULL != ptr) GNUNET_free(ptr)`.
853 * @param ptr the location in memory to free
855 #define GNUNET_free_non_null(ptr) do { void * __x__ = ptr; if (__x__ != NULL) { GNUNET_free(__x__); } } while(0)
859 * Wrapper around #GNUNET_xstrdup_. Makes a copy of the zero-terminated string
862 * @param a pointer to a zero-terminated string
863 * @return a copy of the string including zero-termination
865 #define GNUNET_strdup(a) GNUNET_xstrdup_(a,__FILE__,__LINE__)
869 * Wrapper around #GNUNET_xstrndup_. Makes a partial copy of the string
872 * @param a pointer to a string
873 * @param length of the string to duplicate
874 * @return a partial copy of the string including zero-termination
876 #define GNUNET_strndup(a,length) GNUNET_xstrndup_(a,length,__FILE__,__LINE__)
880 * Grow a well-typed (!) array. This is a convenience
881 * method to grow a vector @a arr of size @a size
882 * to the new (target) size @a tsize.
885 * Example (simple, well-typed stack):
888 * static struct foo * myVector = NULL;
889 * static int myVecLen = 0;
891 * static void push(struct foo * elem) {
892 * GNUNET_array_grow(myVector, myVecLen, myVecLen+1);
893 * GNUNET_memcpy(&myVector[myVecLen-1], elem, sizeof(struct foo));
896 * static void pop(struct foo * elem) {
897 * if (myVecLen == 0) die();
898 * GNUNET_memcpy(elem, myVector[myVecLen-1], sizeof(struct foo));
899 * GNUNET_array_grow(myVector, myVecLen, myVecLen-1);
903 * @param arr base-pointer of the vector, may be NULL if size is 0;
904 * will be updated to reflect the new address. The TYPE of
905 * arr is important since size is the number of elements and
906 * not the size in bytes
907 * @param size the number of elements in the existing vector (number
908 * of elements to copy over)
909 * @param tsize the target size for the resulting vector, use 0 to
910 * free the vector (then, arr will be NULL afterwards).
912 #define GNUNET_array_grow(arr,size,tsize) GNUNET_xgrow_((void**)&arr, sizeof(arr[0]), &size, tsize, __FILE__, __LINE__)
916 * Append an element to a list (growing the
919 #define GNUNET_array_append(arr,size,element) do { GNUNET_array_grow(arr,size,size+1); arr[size-1] = element; } while(0)
923 * Like snprintf, just aborts if the buffer is of insufficient size.
925 * @param buf pointer to buffer that is written to
926 * @param size number of bytes in @a buf
927 * @param format format strings
928 * @param ... data for format string
929 * @return number of bytes written to buf or negative value on error
932 GNUNET_snprintf (char *buf, size_t size, const char *format, ...);
937 * Like asprintf, just portable.
939 * @param buf set to a buffer of sufficient size (allocated, caller must free)
940 * @param format format string (see printf, fprintf, etc.)
941 * @param ... data for format string
942 * @return number of bytes in "*buf" excluding 0-termination
945 GNUNET_asprintf (char **buf, const char *format, ...);
948 /* ************** internal implementations, use macros above! ************** */
951 * Allocate memory. Checks the return value, aborts if no more
952 * memory is available. Don't use GNUNET_xmalloc_ directly. Use the
953 * #GNUNET_malloc macro.
954 * The memory will be zero'ed out.
956 * @param size number of bytes to allocate
957 * @param filename where is this call being made (for debugging)
958 * @param linenumber line where this call is being made (for debugging)
959 * @return allocated memory, never NULL
962 GNUNET_xmalloc_ (size_t size, const char *filename, int linenumber);
966 * Allocate and initialize memory. Checks the return value, aborts if no more
967 * memory is available. Don't use GNUNET_xmemdup_ directly. Use the
968 * #GNUNET_memdup macro.
970 * @param buf buffer to initialize from (must contain size bytes)
971 * @param size number of bytes to allocate
972 * @param filename where is this call being made (for debugging)
973 * @param linenumber line where this call is being made (for debugging)
974 * @return allocated memory, never NULL
977 GNUNET_xmemdup_ (const void *buf, size_t size, const char *filename,
982 * Allocate memory. This function does not check if the allocation
983 * request is within reasonable bounds, allowing allocations larger
984 * than 40 MB. If you don't expect the possibility of very large
985 * allocations, use #GNUNET_malloc instead. The memory will be zero'ed
988 * @param size number of bytes to allocate
989 * @param filename where is this call being made (for debugging)
990 * @param linenumber line where this call is being made (for debugging)
991 * @return pointer to size bytes of memory, NULL if we do not have enough memory
994 GNUNET_xmalloc_unchecked_ (size_t size, const char *filename, int linenumber);
998 * Reallocate memory. Checks the return value, aborts if no more
999 * memory is available.
1002 GNUNET_xrealloc_ (void *ptr, size_t n, const char *filename, int linenumber);
1006 * Free memory. Merely a wrapper for the case that we
1007 * want to keep track of allocations. Don't use GNUNET_xfree_
1008 * directly. Use the #GNUNET_free macro.
1010 * @param ptr pointer to memory to free
1011 * @param filename where is this call being made (for debugging)
1012 * @param linenumber line where this call is being made (for debugging)
1015 GNUNET_xfree_ (void *ptr, const char *filename, int linenumber);
1019 * Dup a string. Don't call GNUNET_xstrdup_ directly. Use the #GNUNET_strdup macro.
1020 * @param str string to duplicate
1021 * @param filename where is this call being made (for debugging)
1022 * @param linenumber line where this call is being made (for debugging)
1023 * @return the duplicated string
1026 GNUNET_xstrdup_ (const char *str, const char *filename, int linenumber);
1029 * Dup partially a string. Don't call GNUNET_xstrndup_ directly. Use the #GNUNET_strndup macro.
1031 * @param str string to duplicate
1032 * @param len length of the string to duplicate
1033 * @param filename where is this call being made (for debugging)
1034 * @param linenumber line where this call is being made (for debugging)
1035 * @return the duplicated string
1038 GNUNET_xstrndup_ (const char *str, size_t len, const char *filename,
1042 * Grow an array, the new elements are zeroed out.
1043 * Grows old by (*oldCount-newCount)*elementSize
1044 * bytes and sets *oldCount to newCount.
1046 * Don't call GNUNET_xgrow_ directly. Use the #GNUNET_array_grow macro.
1048 * @param old address of the pointer to the array
1050 * @param elementSize the size of the elements of the array
1051 * @param oldCount address of the number of elements in the *old array
1052 * @param newCount number of elements in the new array, may be 0 (then *old will be NULL afterwards)
1053 * @param filename where is this call being made (for debugging)
1054 * @param linenumber line where this call is being made (for debugging)
1057 GNUNET_xgrow_ (void **old, size_t elementSize, unsigned int *oldCount,
1058 unsigned int newCount, const char *filename, int linenumber);
1063 * Create a copy of the given message.
1065 * @param msg message to copy
1066 * @return duplicate of the message
1068 struct GNUNET_MessageHeader *
1069 GNUNET_copy_message (const struct GNUNET_MessageHeader *msg);
1072 #if __STDC_VERSION__ < 199901L
1074 #define __func__ __FUNCTION__
1076 #define __func__ "<unknown>"
1082 * Valid task priorities. Use these, do not pass random integers!
1083 * For various reasons (#3862 -- building with QT Creator, and
1084 * our restricted cross-compilation with emscripten) this cannot
1085 * be in gnunet_scheduler_lib.h, but it works if we declare it here.
1086 * Naturally, logically this is part of the scheduler.
1088 enum GNUNET_SCHEDULER_Priority
1091 * Run with the same priority as the current job.
1093 GNUNET_SCHEDULER_PRIORITY_KEEP = 0,
1096 * Run when otherwise idle.
1098 GNUNET_SCHEDULER_PRIORITY_IDLE = 1,
1101 * Run as background job (higher than idle,
1102 * lower than default).
1104 GNUNET_SCHEDULER_PRIORITY_BACKGROUND = 2,
1107 * Run with the default priority (normal
1108 * P2P operations). Any task that is scheduled
1109 * without an explicit priority being specified
1110 * will run with this priority.
1112 GNUNET_SCHEDULER_PRIORITY_DEFAULT = 3,
1115 * Run with high priority (important requests).
1116 * Higher than DEFAULT.
1118 GNUNET_SCHEDULER_PRIORITY_HIGH = 4,
1121 * Run with priority for interactive tasks.
1122 * Higher than "HIGH".
1124 GNUNET_SCHEDULER_PRIORITY_UI = 5,
1127 * Run with priority for urgent tasks. Use
1128 * for things like aborts and shutdowns that
1129 * need to preempt "UI"-level tasks.
1132 GNUNET_SCHEDULER_PRIORITY_URGENT = 6,
1135 * This is an internal priority level that is only used for tasks
1136 * that are being triggered due to shutdown (they have automatically
1137 * highest priority). User code must not use this priority level
1138 * directly. Tasks run with this priority level that internally
1139 * schedule other tasks will see their original priority level
1140 * be inherited (unless otherwise specified).
1142 GNUNET_SCHEDULER_PRIORITY_SHUTDOWN = 7,
1145 * Number of priorities (must be the last priority).
1146 * This priority must not be used by clients.
1148 GNUNET_SCHEDULER_PRIORITY_COUNT = 8
1152 #if 0 /* keep Emacsens' auto-indent happy */
1159 #endif /* GNUNET_COMMON_H */