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
71 /* support old MHD versions */
73 #define MHD_USE_TLS MHD_USE_SSL
78 * Named constants for return values. The following invariants hold:
79 * `GNUNET_NO == 0` (to allow `if (GNUNET_NO)`) `GNUNET_OK !=
80 * GNUNET_SYSERR`, `GNUNET_OK != GNUNET_NO`, `GNUNET_NO !=
81 * GNUNET_SYSERR` and finally `GNUNET_YES != GNUNET_NO`.
84 #define GNUNET_SYSERR -1
88 #define GNUNET_MIN(a,b) (((a) < (b)) ? (a) : (b))
90 #define GNUNET_MAX(a,b) (((a) > (b)) ? (a) : (b))
92 /* some systems use one underscore only, and mingw uses no underscore... */
95 #define __BYTE_ORDER _BYTE_ORDER
98 #define __BYTE_ORDER BYTE_ORDER
104 #define __BIG_ENDIAN _BIG_ENDIAN
107 #define __BIG_ENDIAN BIG_ENDIAN
111 #ifndef __LITTLE_ENDIAN
112 #ifdef _LITTLE_ENDIAN
113 #define __LITTLE_ENDIAN _LITTLE_ENDIAN
116 #define __LITTLE_ENDIAN LITTLE_ENDIAN
123 * define #GNUNET_EXTRA_LOGGING if using this header outside the GNUnet source
124 * tree where gnunet_config.h is unavailable
126 #ifndef GNUNET_EXTRA_LOGGING
127 #define GNUNET_EXTRA_LOGGING 0
134 # if __BYTE_ORDER == __LITTLE_ENDIAN
135 # define GNUNET_htobe16(x) __bswap_16 (x)
136 # define GNUNET_htole16(x) (x)
137 # define GNUNET_be16toh(x) __bswap_16 (x)
138 # define GNUNET_le16toh(x) (x)
140 # define GNUNET_htobe32(x) __bswap_32 (x)
141 # define GNUNET_htole32(x) (x)
142 # define GNUNET_be32toh(x) __bswap_32 (x)
143 # define GNUNET_le32toh(x) (x)
145 # define GNUNET_htobe64(x) __bswap_64 (x)
146 # define GNUNET_htole64(x) (x)
147 # define GNUNET_be64toh(x) __bswap_64 (x)
148 # define GNUNET_le64toh(x) (x)
150 # if __BYTE_ORDER == __BIG_ENDIAN
151 # define GNUNET_htobe16(x) (x)
152 # define GNUNET_htole16(x) __bswap_16 (x)
153 # define GNUNET_be16toh(x) (x)
154 # define GNUNET_le16toh(x) __bswap_16 (x)
156 # define GNUNET_htobe32(x) (x)
157 # define GNUNET_htole32(x) __bswap_32 (x)
158 # define GNUNET_be32toh(x) (x)
159 # define GNUNET_le32toh(x) __bswap_32 (x)
161 # define GNUNET_htobe64(x) (x)
162 # define GNUNET_htole64(x) __bswap_64 (x)
163 # define GNUNET_be64toh(x) (x)
164 # define GNUNET_le64toh(x) __bswap_64 (x)
171 * gcc-ism to get packed structs.
173 #define GNUNET_PACKED __attribute__((packed))
176 * gcc-ism to get gcc bitfield layout when compiling with -mms-bitfields
179 #define GNUNET_GCC_STRUCT_LAYOUT __attribute__((gcc_struct))
181 #define GNUNET_GCC_STRUCT_LAYOUT
185 * gcc-ism to force alignment; we use this to align char-arrays
186 * that may then be cast to 'struct's. See also gcc
189 #ifdef __BIGGEST_ALIGNMENT__
190 #define GNUNET_ALIGN __attribute__((aligned (__BIGGEST_ALIGNMENT__)))
192 #define GNUNET_ALIGN __attribute__((aligned (8)))
196 * gcc-ism to document unused arguments
198 #define GNUNET_UNUSED __attribute__((unused))
201 * gcc-ism to document functions that don't return
203 #define GNUNET_NORETURN __attribute__((noreturn))
208 * gcc 4.x-ism to pack structures even on W32 (to be used before structs);
209 * Using this would cause structs to be unaligned on the stack on Sparc,
210 * so we *only* use this on W32 (see #670578 from Debian); fortunately,
211 * W32 doesn't run on sparc anyway.
213 #define GNUNET_NETWORK_STRUCT_BEGIN \
214 _Pragma("pack(push)") \
218 * gcc 4.x-ism to pack structures even on W32 (to be used after structs)
219 * Using this would cause structs to be unaligned on the stack on Sparc,
220 * so we *only* use this on W32 (see #670578 from Debian); fortunately,
221 * W32 doesn't run on sparc anyway.
223 #define GNUNET_NETWORK_STRUCT_END _Pragma("pack(pop)")
226 #error gcc 4.x or higher required on W32 systems
230 * Define as empty, GNUNET_PACKED should suffice, but this won't work on W32
232 #define GNUNET_NETWORK_STRUCT_BEGIN
235 * Define as empty, GNUNET_PACKED should suffice, but this won't work on W32;
237 #define GNUNET_NETWORK_STRUCT_END
240 /* ************************ super-general types *********************** */
242 GNUNET_NETWORK_STRUCT_BEGIN
245 * Header for all communications.
247 struct GNUNET_MessageHeader
251 * The length of the struct (in bytes, including the length field itself),
252 * in big-endian format.
254 uint16_t size GNUNET_PACKED;
257 * The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
259 uint16_t type GNUNET_PACKED;
265 * Answer from service to client about last operation.
267 struct GNUNET_OperationResultMessage
269 struct GNUNET_MessageHeader header;
271 uint32_t reserved GNUNET_PACKED;
276 uint64_t op_id GNUNET_PACKED;
279 * Status code for the operation.
281 uint64_t result_code GNUNET_PACKED;
283 /* Followed by data. */
286 GNUNET_NETWORK_STRUCT_END
289 * Function called with a filename.
292 * @param filename complete filename (absolute path)
293 * @return #GNUNET_OK to continue to iterate,
294 * #GNUNET_NO to stop iteration with no error,
295 * #GNUNET_SYSERR to abort iteration with error!
298 (*GNUNET_FileNameCallback) (void *cls,
299 const char *filename);
303 * Generic continuation callback.
305 * @param cls Closure.
308 (*GNUNET_ContinuationCallback) (void *cls);
312 * Function called with the result of an asynchronous operation.
317 * Result code for the operation.
319 * Data result for the operation.
324 (*GNUNET_ResultCallback) (void *cls, int64_t result_code,
325 const void *data, uint16_t data_size);
328 /* ****************************** logging ***************************** */
334 enum GNUNET_ErrorType
336 GNUNET_ERROR_TYPE_UNSPECIFIED = -1,
337 GNUNET_ERROR_TYPE_NONE = 0,
338 GNUNET_ERROR_TYPE_ERROR = 1,
339 GNUNET_ERROR_TYPE_WARNING = 2,
340 /* UX: We need a message type that is output by
341 * default without looking like there is a problem.
343 GNUNET_ERROR_TYPE_MESSAGE = 4,
344 GNUNET_ERROR_TYPE_INFO = 8,
345 GNUNET_ERROR_TYPE_DEBUG = 16,
346 GNUNET_ERROR_TYPE_INVALID = 32,
347 GNUNET_ERROR_TYPE_BULK = 64
353 * User-defined handler for log messages.
356 * @param kind severeity
357 * @param component what component is issuing the message?
358 * @param date when was the message logged?
359 * @param message what is the message
362 (*GNUNET_Logger) (void *cls,
363 enum GNUNET_ErrorType kind,
364 const char *component,
366 const char *message);
371 * Get the number of log calls that are going to be skipped
373 * @return number of log calls to be ignored
376 GNUNET_get_log_skip (void);
379 #if !defined(GNUNET_CULL_LOGGING)
381 GNUNET_get_log_call_status (int caller_level,
384 const char *function,
393 * @param kind how serious is the error?
394 * @param message what is the message (format string)
395 * @param ... arguments for format string
398 GNUNET_log_nocheck (enum GNUNET_ErrorType kind, const char *message, ...)
399 __attribute__ ((format (printf, 2, 3)));
402 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
403 #define _GNUNET_BOOLEAN_EXPR(expr) \
405 int _gnunet_boolean_var_; \
407 _gnunet_boolean_var_ = 1; \
409 _gnunet_boolean_var_ = 0; \
410 _gnunet_boolean_var_; \
412 #define GN_LIKELY(expr) (__builtin_expect (_GNUNET_BOOLEAN_EXPR(expr), 1))
413 #define GN_UNLIKELY(expr) (__builtin_expect (_GNUNET_BOOLEAN_EXPR(expr), 0))
415 #define GN_LIKELY(expr) (expr)
416 #define GN_UNLIKELY(expr) (expr)
419 #if !defined(GNUNET_LOG_CALL_STATUS)
420 #define GNUNET_LOG_CALL_STATUS -1
426 * Log function that specifies an alternative component.
427 * This function should be used by plugins.
429 * @param kind how serious is the error?
430 * @param comp component responsible for generating the message
431 * @param message what is the message (format string)
432 * @param ... arguments for format string
435 GNUNET_log_from_nocheck (enum GNUNET_ErrorType kind, const char *comp,
436 const char *message, ...);
438 #if !defined(GNUNET_CULL_LOGGING)
439 #define GNUNET_log_from(kind,comp,...) do { int log_line = __LINE__;\
440 static int log_call_enabled = GNUNET_LOG_CALL_STATUS;\
441 if ((GNUNET_EXTRA_LOGGING > 0) || ((GNUNET_ERROR_TYPE_DEBUG & (kind)) == 0)) { \
442 if (GN_UNLIKELY(log_call_enabled == -1))\
443 log_call_enabled = GNUNET_get_log_call_status ((kind) & (~GNUNET_ERROR_TYPE_BULK), (comp), __FILE__, __FUNCTION__, log_line); \
444 if (GN_UNLIKELY(GNUNET_get_log_skip () > 0)) { GNUNET_log_skip (-1, GNUNET_NO); }\
446 if (GN_UNLIKELY(log_call_enabled))\
447 GNUNET_log_from_nocheck ((kind), comp, __VA_ARGS__); \
452 #define GNUNET_log(kind,...) do { int log_line = __LINE__;\
453 static int log_call_enabled = GNUNET_LOG_CALL_STATUS;\
454 if ((GNUNET_EXTRA_LOGGING > 0) || ((GNUNET_ERROR_TYPE_DEBUG & (kind)) == 0)) { \
455 if (GN_UNLIKELY(log_call_enabled == -1))\
456 log_call_enabled = GNUNET_get_log_call_status ((kind) & (~GNUNET_ERROR_TYPE_BULK), NULL, __FILE__, __FUNCTION__, log_line);\
457 if (GN_UNLIKELY(GNUNET_get_log_skip () > 0)) { GNUNET_log_skip (-1, GNUNET_NO); }\
459 if (GN_UNLIKELY(log_call_enabled))\
460 GNUNET_log_nocheck ((kind), __VA_ARGS__); \
465 #define GNUNET_log(...)
466 #define GNUNET_log_from(...)
472 * Log error message about missing configuration option.
474 * @param kind log level
475 * @param section section with missing option
476 * @param option name of missing option
479 GNUNET_log_config_missing (enum GNUNET_ErrorType kind,
486 * Log error message about invalid configuration option value.
488 * @param kind log level
489 * @param section section with invalid option
490 * @param option name of invalid option
491 * @param required what is required that is invalid about the option
494 GNUNET_log_config_invalid (enum GNUNET_ErrorType kind,
497 const char *required);
502 * Abort the process, generate a core dump if possible.
503 * Most code should use `GNUNET_assert (0)` instead to
504 * first log the location of the failure.
507 GNUNET_abort_ (void) GNUNET_NORETURN;
512 * Ignore the next @a n calls to the log function.
514 * @param n number of log calls to ignore (could be negative)
515 * @param check_reset #GNUNET_YES to assert that the log skip counter is currently zero
518 GNUNET_log_skip (int n,
526 * @param comp default component to use
527 * @param loglevel what types of messages should be logged
528 * @param logfile change logging to logfile (use NULL to keep stderr)
529 * @return #GNUNET_OK on success, #GNUNET_SYSERR if logfile could not be opened
532 GNUNET_log_setup (const char *comp,
533 const char *loglevel,
534 const char *logfile);
539 * Add a custom logger. Note that installing any custom logger
540 * will disable the standard logger. When multiple custom loggers
541 * are installed, all will be called. The standard logger will
542 * only be used if no custom loggers are present.
544 * @param logger log function
545 * @param logger_cls closure for @a logger
548 GNUNET_logger_add (GNUNET_Logger logger,
554 * Remove a custom logger.
556 * @param logger log function
557 * @param logger_cls closure for @a logger
560 GNUNET_logger_remove (GNUNET_Logger logger,
566 * Convert a hash value to a string (for printing debug messages).
567 * This is one of the very few calls in the entire API that is
570 * @param hc the hash code
574 GNUNET_h2s (const struct GNUNET_HashCode * hc);
579 * Convert a hash value to a string (for printing debug messages).
580 * This prints all 104 characters of a hashcode!
581 * This is one of the very few calls in the entire API that is
584 * @param hc the hash code
588 GNUNET_h2s_full (const struct GNUNET_HashCode * hc);
593 * Convert a peer identity to a string (for printing debug messages).
594 * This is one of the very few calls in the entire API that is
597 * @param pid the peer identity
598 * @return string form of the pid; will be overwritten by next
599 * call to #GNUNET_i2s().
602 GNUNET_i2s (const struct GNUNET_PeerIdentity *pid);
607 * Convert a peer identity to a string (for printing debug messages).
608 * This is one of the very few calls in the entire API that is
611 * @param pid the peer identity
612 * @return string form of the pid; will be overwritten by next
613 * call to #GNUNET_i2s_full().
616 GNUNET_i2s_full (const struct GNUNET_PeerIdentity *pid);
621 * Convert a "struct sockaddr*" (IPv4 or IPv6 address) to a string
622 * (for printing debug messages). This is one of the very few calls
623 * in the entire API that is NOT reentrant!
625 * @param addr the address
626 * @param addrlen the length of the @a addr
627 * @return nicely formatted string for the address
628 * will be overwritten by next call to #GNUNET_a2s().
631 GNUNET_a2s (const struct sockaddr *addr,
637 * Convert error type to string.
639 * @param kind type to convert
640 * @return string corresponding to the type
643 GNUNET_error_type_to_string (enum GNUNET_ErrorType kind);
648 * Use this for fatal errors that cannot be handled
650 #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)
655 * Use this for fatal errors that cannot be handled
657 #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)
662 * Use this for internal assertion violations that are
663 * not fatal (can be handled) but should not occur.
665 #define GNUNET_break(cond) do { if (! (cond)) { GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Assertion failed at %s:%d.\n"), __FILE__, __LINE__); } } while(0)
670 * Use this for assertion violations caused by other
671 * peers (i.e. protocol violations). We do not want to
672 * confuse end-users (say, some other peer runs an
673 * older, broken or incompatible GNUnet version), but
674 * we still want to see these problems during
675 * development and testing. "OP == other peer".
677 #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)
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_strerror(level, cmd) do { GNUNET_log(level, _("`%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_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)
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_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)
709 * Log an error message at log-level 'level' that indicates
710 * a failure of the command 'cmd' with the message given
711 * by strerror(errno).
713 #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)
715 /* ************************* endianess conversion ****************** */
718 * Convert unsigned 64-bit integer to network byte order.
721 * The value in host byte order.
723 * @return The same value in network byte order.
726 GNUNET_htonll (uint64_t n);
730 * Convert unsigned 64-bit integer to host byte order.
733 * The value in network byte order.
735 * @return The same value in host byte order.
738 GNUNET_ntohll (uint64_t n);
742 * Convert double to network byte order.
745 * The value in host byte order.
747 * @return The same value in network byte order.
750 GNUNET_hton_double (double d);
754 * Convert double to host byte order
757 * The value in network byte order.
759 * @return The same value in host byte order.
762 GNUNET_ntoh_double (double d);
765 /* ************************* allocation functions ****************** */
769 * Maximum allocation with GNUNET_malloc macro.
771 #define GNUNET_MAX_MALLOC_CHECKED (1024 * 1024 * 40)
775 * Allocate a struct or union of the given @a type.
776 * Wrapper around #GNUNET_malloc that returns a pointer
777 * to the newly created object of the correct type.
779 * @param type name of the struct or union, i.e. pass 'struct Foo'.
781 #define GNUNET_new(type) (type *) GNUNET_malloc (sizeof (type))
784 * Call memcpy() but check for @a n being 0 first. In the latter
785 * case, it is now safe to pass NULL for @a src or @a dst.
786 * Unlike traditional memcpy(), returns nothing.
788 * @param dst destination of the copy, may be NULL if @a n is zero
789 * @param src source of the copy, may be NULL if @a n is zero
790 * @param n number of bytes to copy
792 #define GNUNET_memcpy(dst,src,n) do { if (0 != n) { (void) memcpy (dst,src,n); } } while (0)
797 * Allocate a size @a n array with structs or unions of the given @a type.
798 * Wrapper around #GNUNET_malloc that returns a pointer
799 * to the newly created objects of the correct type.
801 * @param n number of elements in the array
802 * @param type name of the struct or union, i.e. pass 'struct Foo'.
804 #define GNUNET_new_array(n, type) (type *) GNUNET_malloc ((n) * sizeof (type))
808 * Allocate a size @a n times @a m array
809 * with structs or unions of the given @a type.
811 * @param n size of the first dimension
812 * @param m size of the second dimension
813 * @param type name of the struct or union, i.e. pass 'struct Foo'.
815 #define GNUNET_new_array_2d(n, m, type) (type **) GNUNET_xnew_array_2d_ (n, m, sizeof (type), __FILE__, __LINE__)
819 * Allocate a size @a n times @a m times @a o array
820 * with structs or unions of the given @a type.
822 * @param n size of the first dimension
823 * @param m size of the second dimension
824 * @param o size of the third dimension
825 * @param type name of the struct or union, i.e. pass 'struct Foo'.
827 #define GNUNET_new_array_3d(n, m, o, type) (type ***) GNUNET_xnew_array_3d_ (n, m, o, sizeof (type), __FILE__, __LINE__)
831 * Wrapper around malloc. Allocates size bytes of memory.
832 * The memory will be zero'ed out.
834 * @param size the number of bytes to allocate, must be
835 * smaller than 40 MB.
836 * @return pointer to size bytes of memory, never NULL (!)
838 #define GNUNET_malloc(size) GNUNET_xmalloc_(size, __FILE__, __LINE__)
842 * Allocate and initialize a block of memory.
844 * @param buf data to initalize the block with
845 * @param size the number of bytes in buf (and size of the allocation)
846 * @return pointer to size bytes of memory, never NULL (!)
848 #define GNUNET_memdup(buf,size) GNUNET_xmemdup_(buf, size, __FILE__, __LINE__)
852 * Wrapper around malloc. Allocates size bytes of memory.
853 * The memory will be zero'ed out.
855 * @param size the number of bytes to allocate
856 * @return pointer to size bytes of memory, NULL if we do not have enough memory
858 #define GNUNET_malloc_large(size) GNUNET_xmalloc_unchecked_(size, __FILE__, __LINE__)
862 * Wrapper around realloc. Reallocates size bytes of memory.
863 * The content of the intersection of the new and old size will be unchanged.
865 * @param ptr the pointer to reallocate
866 * @param size the number of bytes to reallocate
867 * @return pointer to size bytes of memory
869 #define GNUNET_realloc(ptr, size) GNUNET_xrealloc_(ptr, size, __FILE__, __LINE__)
873 * Wrapper around free. Frees the memory referred to by ptr.
874 * Note that it is generally better to free memory that was
875 * allocated with #GNUNET_array_grow using #GNUNET_array_grow(mem, size, 0) instead of #GNUNET_free.
877 * @param ptr location where to free the memory. ptr must have
878 * been returned by #GNUNET_strdup, #GNUNET_strndup, #GNUNET_malloc or #GNUNET_array_grow earlier.
880 #define GNUNET_free(ptr) GNUNET_xfree_(ptr, __FILE__, __LINE__)
884 * Free the memory pointed to by ptr if ptr is not NULL.
885 * Equivalent to `if (NULL != ptr) GNUNET_free(ptr)`.
887 * @param ptr the location in memory to free
889 #define GNUNET_free_non_null(ptr) do { void * __x__ = ptr; if (__x__ != NULL) { GNUNET_free(__x__); } } while(0)
893 * Wrapper around #GNUNET_xstrdup_. Makes a copy of the zero-terminated string
896 * @param a pointer to a zero-terminated string
897 * @return a copy of the string including zero-termination
899 #define GNUNET_strdup(a) GNUNET_xstrdup_(a,__FILE__,__LINE__)
903 * Wrapper around #GNUNET_xstrndup_. Makes a partial copy of the string
906 * @param a pointer to a string
907 * @param length of the string to duplicate
908 * @return a partial copy of the string including zero-termination
910 #define GNUNET_strndup(a,length) GNUNET_xstrndup_(a,length,__FILE__,__LINE__)
914 * Grow a well-typed (!) array. This is a convenience
915 * method to grow a vector @a arr of size @a size
916 * to the new (target) size @a tsize.
919 * Example (simple, well-typed stack):
922 * static struct foo * myVector = NULL;
923 * static int myVecLen = 0;
925 * static void push(struct foo * elem) {
926 * GNUNET_array_grow(myVector, myVecLen, myVecLen+1);
927 * GNUNET_memcpy(&myVector[myVecLen-1], elem, sizeof(struct foo));
930 * static void pop(struct foo * elem) {
931 * if (myVecLen == 0) die();
932 * GNUNET_memcpy(elem, myVector[myVecLen-1], sizeof(struct foo));
933 * GNUNET_array_grow(myVector, myVecLen, myVecLen-1);
937 * @param arr base-pointer of the vector, may be NULL if size is 0;
938 * will be updated to reflect the new address. The TYPE of
939 * arr is important since size is the number of elements and
940 * not the size in bytes
941 * @param size the number of elements in the existing vector (number
942 * of elements to copy over)
943 * @param tsize the target size for the resulting vector, use 0 to
944 * free the vector (then, arr will be NULL afterwards).
946 #define GNUNET_array_grow(arr,size,tsize) GNUNET_xgrow_((void**)&arr, sizeof(arr[0]), &size, tsize, __FILE__, __LINE__)
950 * Append an element to a list (growing the
953 #define GNUNET_array_append(arr,size,element) do { GNUNET_array_grow(arr,size,size+1); arr[size-1] = element; } while(0)
957 * Like snprintf, just aborts if the buffer is of insufficient size.
959 * @param buf pointer to buffer that is written to
960 * @param size number of bytes in @a buf
961 * @param format format strings
962 * @param ... data for format string
963 * @return number of bytes written to buf or negative value on error
966 GNUNET_snprintf (char *buf, size_t size, const char *format, ...);
971 * Like asprintf, just portable.
973 * @param buf set to a buffer of sufficient size (allocated, caller must free)
974 * @param format format string (see printf, fprintf, etc.)
975 * @param ... data for format string
976 * @return number of bytes in "*buf" excluding 0-termination
979 GNUNET_asprintf (char **buf, const char *format, ...);
982 /* ************** internal implementations, use macros above! ************** */
985 * Allocate memory. Checks the return value, aborts if no more
986 * memory is available. Don't use GNUNET_xmalloc_ directly. Use the
987 * #GNUNET_malloc macro.
988 * The memory will be zero'ed out.
990 * @param size number of bytes to allocate
991 * @param filename where is this call being made (for debugging)
992 * @param linenumber line where this call is being made (for debugging)
993 * @return allocated memory, never NULL
996 GNUNET_xmalloc_ (size_t size, const char *filename, int linenumber);
1000 * Allocate memory for a two dimensional array in one block
1001 * and set up pointers. Aborts if no more memory is available.
1002 * Don't use GNUNET_xnew_array_2d_ directly. Use the
1003 * #GNUNET_new_array_2d macro.
1004 * The memory of the elements will be zero'ed out.
1006 * @param n size of the first dimension
1007 * @param m size of the second dimension
1008 * @param elementSize size of a single element in bytes
1009 * @param filename where is this call being made (for debugging)
1010 * @param linenumber line where this call is being made (for debugging)
1011 * @return allocated memory, never NULL
1014 GNUNET_xnew_array_2d_ (size_t n, size_t m, size_t elementSize,
1015 const char *filename, int linenumber);
1019 * Allocate memory for a three dimensional array in one block
1020 * and set up pointers. Aborts if no more memory is available.
1021 * Don't use GNUNET_xnew_array_3d_ directly. Use the
1022 * #GNUNET_new_array_3d macro.
1023 * The memory of the elements will be zero'ed out.
1025 * @param n size of the first dimension
1026 * @param m size of the second dimension
1027 * @param o size of the third dimension
1028 * @param elementSize size of a single element in bytes
1029 * @param filename where is this call being made (for debugging)
1030 * @param linenumber line where this call is being made (for debugging)
1031 * @return allocated memory, never NULL
1034 GNUNET_xnew_array_3d_ (size_t n, size_t m, size_t o, size_t elementSize,
1035 const char *filename, int linenumber);
1039 * Allocate and initialize memory. Checks the return value, aborts if no more
1040 * memory is available. Don't use GNUNET_xmemdup_ directly. Use the
1041 * #GNUNET_memdup macro.
1043 * @param buf buffer to initialize from (must contain size bytes)
1044 * @param size number of bytes to allocate
1045 * @param filename where is this call being made (for debugging)
1046 * @param linenumber line where this call is being made (for debugging)
1047 * @return allocated memory, never NULL
1050 GNUNET_xmemdup_ (const void *buf, size_t size, const char *filename,
1055 * Allocate memory. This function does not check if the allocation
1056 * request is within reasonable bounds, allowing allocations larger
1057 * than 40 MB. If you don't expect the possibility of very large
1058 * allocations, use #GNUNET_malloc instead. The memory will be zero'ed
1061 * @param size number of bytes to allocate
1062 * @param filename where is this call being made (for debugging)
1063 * @param linenumber line where this call is being made (for debugging)
1064 * @return pointer to size bytes of memory, NULL if we do not have enough memory
1067 GNUNET_xmalloc_unchecked_ (size_t size, const char *filename, int linenumber);
1071 * Reallocate memory. Checks the return value, aborts if no more
1072 * memory is available.
1075 GNUNET_xrealloc_ (void *ptr, size_t n, const char *filename, int linenumber);
1079 * Free memory. Merely a wrapper for the case that we
1080 * want to keep track of allocations. Don't use GNUNET_xfree_
1081 * directly. Use the #GNUNET_free macro.
1083 * @param ptr pointer to memory to free
1084 * @param filename where is this call being made (for debugging)
1085 * @param linenumber line where this call is being made (for debugging)
1088 GNUNET_xfree_ (void *ptr, const char *filename, int linenumber);
1092 * Dup a string. Don't call GNUNET_xstrdup_ directly. Use the #GNUNET_strdup macro.
1093 * @param str string to duplicate
1094 * @param filename where is this call being made (for debugging)
1095 * @param linenumber line where this call is being made (for debugging)
1096 * @return the duplicated string
1099 GNUNET_xstrdup_ (const char *str, const char *filename, int linenumber);
1102 * Dup partially a string. Don't call GNUNET_xstrndup_ directly. Use the #GNUNET_strndup macro.
1104 * @param str string to duplicate
1105 * @param len length of the string to duplicate
1106 * @param filename where is this call being made (for debugging)
1107 * @param linenumber line where this call is being made (for debugging)
1108 * @return the duplicated string
1111 GNUNET_xstrndup_ (const char *str, size_t len, const char *filename,
1115 * Grow an array, the new elements are zeroed out.
1116 * Grows old by (*oldCount-newCount)*elementSize
1117 * bytes and sets *oldCount to newCount.
1119 * Don't call GNUNET_xgrow_ directly. Use the #GNUNET_array_grow macro.
1121 * @param old address of the pointer to the array
1123 * @param elementSize the size of the elements of the array
1124 * @param oldCount address of the number of elements in the *old array
1125 * @param newCount number of elements in the new array, may be 0 (then *old will be NULL afterwards)
1126 * @param filename where is this call being made (for debugging)
1127 * @param linenumber line where this call is being made (for debugging)
1130 GNUNET_xgrow_ (void **old, size_t elementSize, unsigned int *oldCount,
1131 unsigned int newCount, const char *filename, int linenumber);
1136 * Create a copy of the given message.
1138 * @param msg message to copy
1139 * @return duplicate of the message
1141 struct GNUNET_MessageHeader *
1142 GNUNET_copy_message (const struct GNUNET_MessageHeader *msg);
1145 #if __STDC_VERSION__ < 199901L
1147 #define __func__ __FUNCTION__
1149 #define __func__ "<unknown>"
1155 * Valid task priorities. Use these, do not pass random integers!
1156 * For various reasons (#3862 -- building with QT Creator, and
1157 * our restricted cross-compilation with emscripten) this cannot
1158 * be in gnunet_scheduler_lib.h, but it works if we declare it here.
1159 * Naturally, logically this is part of the scheduler.
1161 enum GNUNET_SCHEDULER_Priority
1164 * Run with the same priority as the current job.
1166 GNUNET_SCHEDULER_PRIORITY_KEEP = 0,
1169 * Run when otherwise idle.
1171 GNUNET_SCHEDULER_PRIORITY_IDLE = 1,
1174 * Run as background job (higher than idle,
1175 * lower than default).
1177 GNUNET_SCHEDULER_PRIORITY_BACKGROUND = 2,
1180 * Run with the default priority (normal
1181 * P2P operations). Any task that is scheduled
1182 * without an explicit priority being specified
1183 * will run with this priority.
1185 GNUNET_SCHEDULER_PRIORITY_DEFAULT = 3,
1188 * Run with high priority (important requests).
1189 * Higher than DEFAULT.
1191 GNUNET_SCHEDULER_PRIORITY_HIGH = 4,
1194 * Run with priority for interactive tasks.
1195 * Higher than "HIGH".
1197 GNUNET_SCHEDULER_PRIORITY_UI = 5,
1200 * Run with priority for urgent tasks. Use
1201 * for things like aborts and shutdowns that
1202 * need to preempt "UI"-level tasks.
1205 GNUNET_SCHEDULER_PRIORITY_URGENT = 6,
1208 * This is an internal priority level that is only used for tasks
1209 * that are being triggered due to shutdown (they have automatically
1210 * highest priority). User code must not use this priority level
1211 * directly. Tasks run with this priority level that internally
1212 * schedule other tasks will see their original priority level
1213 * be inherited (unless otherwise specified).
1215 GNUNET_SCHEDULER_PRIORITY_SHUTDOWN = 7,
1218 * Number of priorities (must be the last priority).
1219 * This priority must not be used by clients.
1221 GNUNET_SCHEDULER_PRIORITY_COUNT = 8
1225 #if 0 /* keep Emacsens' auto-indent happy */
1232 #endif /* GNUNET_COMMON_H */