2 This file is part of GNUnet.
3 Copyright (C) 2006-2013 Christian Grothoff (and other contributing authors)
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 * @defgroup memory Memory management
33 #ifndef GNUNET_COMMON_H
34 #define GNUNET_COMMON_H
37 #include <sys/socket.h>
40 #include <netinet/in.h>
52 #ifdef HAVE_BYTESWAP_H
59 #if 0 /* keep Emacsens' auto-indent happy */
65 * Version of the API (for entire gnunetutil.so library).
67 #define GNUNET_UTIL_VERSION 0x000A0100
71 * Named constants for return values. The following invariants hold:
72 * `GNUNET_NO == 0` (to allow `if (GNUNET_NO)`) `GNUNET_OK !=
73 * GNUNET_SYSERR`, `GNUNET_OK != GNUNET_NO`, `GNUNET_NO !=
74 * GNUNET_SYSERR` and finally `GNUNET_YES != GNUNET_NO`.
77 #define GNUNET_SYSERR -1
81 #define GNUNET_MIN(a,b) (((a) < (b)) ? (a) : (b))
83 #define GNUNET_MAX(a,b) (((a) > (b)) ? (a) : (b))
85 /* some systems use one underscore only, and mingw uses no underscore... */
88 #define __BYTE_ORDER _BYTE_ORDER
91 #define __BYTE_ORDER BYTE_ORDER
97 #define __BIG_ENDIAN _BIG_ENDIAN
100 #define __BIG_ENDIAN BIG_ENDIAN
104 #ifndef __LITTLE_ENDIAN
105 #ifdef _LITTLE_ENDIAN
106 #define __LITTLE_ENDIAN _LITTLE_ENDIAN
109 #define __LITTLE_ENDIAN LITTLE_ENDIAN
116 * define #GNUNET_EXTRA_LOGGING if using this header outside the GNUnet source
117 * tree where gnunet_config.h is unavailable
119 #ifndef GNUNET_EXTRA_LOGGING
120 #define GNUNET_EXTRA_LOGGING 0
127 # if __BYTE_ORDER == __LITTLE_ENDIAN
128 # define GNUNET_htobe16(x) __bswap_16 (x)
129 # define GNUNET_htole16(x) (x)
130 # define GNUNET_be16toh(x) __bswap_16 (x)
131 # define GNUNET_le16toh(x) (x)
133 # define GNUNET_htobe32(x) __bswap_32 (x)
134 # define GNUNET_htole32(x) (x)
135 # define GNUNET_be32toh(x) __bswap_32 (x)
136 # define GNUNET_le32toh(x) (x)
138 # define GNUNET_htobe64(x) __bswap_64 (x)
139 # define GNUNET_htole64(x) (x)
140 # define GNUNET_be64toh(x) __bswap_64 (x)
141 # define GNUNET_le64toh(x) (x)
143 # if __BYTE_ORDER == __BIG_ENDIAN
144 # define GNUNET_htobe16(x) (x)
145 # define GNUNET_htole16(x) __bswap_16 (x)
146 # define GNUNET_be16toh(x) (x)
147 # define GNUNET_le16toh(x) __bswap_16 (x)
149 # define GNUNET_htobe32(x) (x)
150 # define GNUNET_htole32(x) __bswap_32 (x)
151 # define GNUNET_be32toh(x) (x)
152 # define GNUNET_le32toh(x) __bswap_32 (x)
154 # define GNUNET_htobe64(x) (x)
155 # define GNUNET_htole64(x) __bswap_64 (x)
156 # define GNUNET_be64toh(x) (x)
157 # define GNUNET_le64toh(x) __bswap_64 (x)
164 * gcc-ism to get packed structs.
166 #define GNUNET_PACKED __attribute__((packed))
169 * gcc-ism to get gcc bitfield layout when compiling with -mms-bitfields
172 #define GNUNET_GCC_STRUCT_LAYOUT __attribute__((gcc_struct))
174 #define GNUNET_GCC_STRUCT_LAYOUT
178 * gcc-ism to force alignment; we use this to align char-arrays
179 * that may then be cast to 'struct's. See also gcc
182 #ifdef __BIGGEST_ALIGNMENT__
183 #define GNUNET_ALIGN __attribute__((aligned (__BIGGEST_ALIGNMENT__)))
185 #define GNUNET_ALIGN __attribute__((aligned (8)))
189 * gcc-ism to document unused arguments
191 #define GNUNET_UNUSED __attribute__((unused))
194 * gcc-ism to document functions that don't return
196 #define GNUNET_NORETURN __attribute__((noreturn))
201 * gcc 4.x-ism to pack structures even on W32 (to be used before structs);
202 * Using this would cause structs to be unaligned on the stack on Sparc,
203 * so we *only* use this on W32 (see #670578 from Debian); fortunately,
204 * W32 doesn't run on sparc anyway.
206 #define GNUNET_NETWORK_STRUCT_BEGIN \
207 _Pragma("pack(push)") \
211 * gcc 4.x-ism to pack structures even on W32 (to be used after structs)
212 * Using this would cause structs to be unaligned on the stack on Sparc,
213 * so we *only* use this on W32 (see #670578 from Debian); fortunately,
214 * W32 doesn't run on sparc anyway.
216 #define GNUNET_NETWORK_STRUCT_END _Pragma("pack(pop)")
219 #error gcc 4.x or higher required on W32 systems
223 * Define as empty, GNUNET_PACKED should suffice, but this won't work on W32
225 #define GNUNET_NETWORK_STRUCT_BEGIN
228 * Define as empty, GNUNET_PACKED should suffice, but this won't work on W32;
230 #define GNUNET_NETWORK_STRUCT_END
233 /* ************************ super-general types *********************** */
235 GNUNET_NETWORK_STRUCT_BEGIN
238 * Header for all communications.
240 struct GNUNET_MessageHeader
244 * The length of the struct (in bytes, including the length field itself),
245 * in big-endian format.
247 uint16_t size GNUNET_PACKED;
250 * The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
252 uint16_t type GNUNET_PACKED;
258 * Answer from service to client about last operation.
260 struct GNUNET_OperationResultMessage
262 struct GNUNET_MessageHeader header;
264 uint32_t reserved GNUNET_PACKED;
269 uint64_t op_id GNUNET_PACKED;
272 * Status code for the operation.
274 uint64_t result_code GNUNET_PACKED;
276 /* Followed by data. */
279 GNUNET_NETWORK_STRUCT_END
282 * Function called with a filename.
285 * @param filename complete filename (absolute path)
286 * @return #GNUNET_OK to continue to iterate,
287 * #GNUNET_NO to stop iteration with no error,
288 * #GNUNET_SYSERR to abort iteration with error!
291 (*GNUNET_FileNameCallback) (void *cls,
292 const char *filename);
296 * Generic continuation callback.
298 * @param cls Closure.
301 (*GNUNET_ContinuationCallback) (void *cls);
305 * Function called with the result of an asynchronous operation.
310 * Result code for the operation.
312 * Data result for the operation.
317 (*GNUNET_ResultCallback) (void *cls, int64_t result_code,
318 const void *data, uint16_t data_size);
321 /* ****************************** logging ***************************** */
327 enum GNUNET_ErrorType
329 GNUNET_ERROR_TYPE_UNSPECIFIED = -1,
330 GNUNET_ERROR_TYPE_NONE = 0,
331 GNUNET_ERROR_TYPE_ERROR = 1,
332 GNUNET_ERROR_TYPE_WARNING = 2,
333 GNUNET_ERROR_TYPE_INFO = 4,
334 GNUNET_ERROR_TYPE_DEBUG = 8,
335 GNUNET_ERROR_TYPE_INVALID = 16,
336 GNUNET_ERROR_TYPE_BULK = 32
342 * User-defined handler for log messages.
345 * @param kind severeity
346 * @param component what component is issuing the message?
347 * @param date when was the message logged?
348 * @param message what is the message
351 (*GNUNET_Logger) (void *cls,
352 enum GNUNET_ErrorType kind,
353 const char *component,
355 const char *message);
360 * Get the number of log calls that are going to be skipped
362 * @return number of log calls to be ignored
365 GNUNET_get_log_skip (void);
368 #if !defined(GNUNET_CULL_LOGGING)
370 GNUNET_get_log_call_status (int caller_level,
373 const char *function,
382 * @param kind how serious is the error?
383 * @param message what is the message (format string)
384 * @param ... arguments for format string
387 GNUNET_log_nocheck (enum GNUNET_ErrorType kind, const char *message, ...);
390 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
391 #define _GNUNET_BOOLEAN_EXPR(expr) \
393 int _gnunet_boolean_var_; \
395 _gnunet_boolean_var_ = 1; \
397 _gnunet_boolean_var_ = 0; \
398 _gnunet_boolean_var_; \
400 #define GN_LIKELY(expr) (__builtin_expect (_GNUNET_BOOLEAN_EXPR(expr), 1))
401 #define GN_UNLIKELY(expr) (__builtin_expect (_GNUNET_BOOLEAN_EXPR(expr), 0))
403 #define GN_LIKELY(expr) (expr)
404 #define GN_UNLIKELY(expr) (expr)
407 #if !defined(GNUNET_LOG_CALL_STATUS)
408 #define GNUNET_LOG_CALL_STATUS -1
414 * Log function that specifies an alternative component.
415 * This function should be used by plugins.
417 * @param kind how serious is the error?
418 * @param comp component responsible for generating the message
419 * @param message what is the message (format string)
420 * @param ... arguments for format string
423 GNUNET_log_from_nocheck (enum GNUNET_ErrorType kind, const char *comp,
424 const char *message, ...);
426 #if !defined(GNUNET_CULL_LOGGING)
427 #define GNUNET_log_from(kind,comp,...) do { int log_line = __LINE__;\
428 static int log_call_enabled = GNUNET_LOG_CALL_STATUS;\
429 if ((GNUNET_EXTRA_LOGGING > 0) || ((GNUNET_ERROR_TYPE_DEBUG & (kind)) == 0)) { \
430 if (GN_UNLIKELY(log_call_enabled == -1))\
431 log_call_enabled = GNUNET_get_log_call_status ((kind) & (~GNUNET_ERROR_TYPE_BULK), (comp), __FILE__, __FUNCTION__, log_line); \
432 if (GN_UNLIKELY(GNUNET_get_log_skip () > 0)) { GNUNET_log_skip (-1, GNUNET_NO); }\
434 if (GN_UNLIKELY(log_call_enabled))\
435 GNUNET_log_from_nocheck ((kind), comp, __VA_ARGS__); \
440 #define GNUNET_log(kind,...) do { int log_line = __LINE__;\
441 static int log_call_enabled = GNUNET_LOG_CALL_STATUS;\
442 if ((GNUNET_EXTRA_LOGGING > 0) || ((GNUNET_ERROR_TYPE_DEBUG & (kind)) == 0)) { \
443 if (GN_UNLIKELY(log_call_enabled == -1))\
444 log_call_enabled = GNUNET_get_log_call_status ((kind) & (~GNUNET_ERROR_TYPE_BULK), NULL, __FILE__, __FUNCTION__, log_line);\
445 if (GN_UNLIKELY(GNUNET_get_log_skip () > 0)) { GNUNET_log_skip (-1, GNUNET_NO); }\
447 if (GN_UNLIKELY(log_call_enabled))\
448 GNUNET_log_nocheck ((kind), __VA_ARGS__); \
453 #define GNUNET_log(...)
454 #define GNUNET_log_from(...)
460 * Log error message about missing configuration option.
462 * @param kind log level
463 * @param section section with missing option
464 * @param option name of missing option
467 GNUNET_log_config_missing (enum GNUNET_ErrorType kind,
474 * Log error message about invalid configuration option value.
476 * @param kind log level
477 * @param section section with invalid option
478 * @param option name of invalid option
479 * @param required what is required that is invalid about the option
482 GNUNET_log_config_invalid (enum GNUNET_ErrorType kind,
485 const char *required);
490 * Abort the process, generate a core dump if possible.
491 * Most code should use `GNUNET_assert (0)` instead to
492 * first log the location of the failure.
495 GNUNET_abort_ (void) GNUNET_NORETURN;
500 * Ignore the next @a n calls to the log function.
502 * @param n number of log calls to ignore (could be negative)
503 * @param check_reset #GNUNET_YES to assert that the log skip counter is currently zero
506 GNUNET_log_skip (int n,
514 * @param comp default component to use
515 * @param loglevel what types of messages should be logged
516 * @param logfile change logging to logfile (use NULL to keep stderr)
517 * @return #GNUNET_OK on success, #GNUNET_SYSERR if logfile could not be opened
520 GNUNET_log_setup (const char *comp,
521 const char *loglevel,
522 const char *logfile);
527 * Add a custom logger. Note that installing any custom logger
528 * will disable the standard logger. When multiple custom loggers
529 * are installed, all will be called. The standard logger will
530 * only be used if no custom loggers are present.
532 * @param logger log function
533 * @param logger_cls closure for @a logger
536 GNUNET_logger_add (GNUNET_Logger logger,
542 * Remove a custom logger.
544 * @param logger log function
545 * @param logger_cls closure for @a logger
548 GNUNET_logger_remove (GNUNET_Logger logger,
554 * Convert a hash value to a string (for printing debug messages).
555 * This is one of the very few calls in the entire API that is
558 * @param hc the hash code
562 GNUNET_h2s (const struct GNUNET_HashCode * hc);
567 * Convert a hash value to a string (for printing debug messages).
568 * This prints all 104 characters of a hashcode!
569 * This is one of the very few calls in the entire API that is
572 * @param hc the hash code
576 GNUNET_h2s_full (const struct GNUNET_HashCode * hc);
581 * Convert a peer identity to a string (for printing debug messages).
582 * This is one of the very few calls in the entire API that is
585 * @param pid the peer identity
586 * @return string form of the pid; will be overwritten by next
587 * call to #GNUNET_i2s().
590 GNUNET_i2s (const struct GNUNET_PeerIdentity *pid);
595 * Convert a peer identity to a string (for printing debug messages).
596 * This is one of the very few calls in the entire API that is
599 * @param pid the peer identity
600 * @return string form of the pid; will be overwritten by next
601 * call to #GNUNET_i2s_full().
604 GNUNET_i2s_full (const struct GNUNET_PeerIdentity *pid);
609 * Convert a "struct sockaddr*" (IPv4 or IPv6 address) to a string
610 * (for printing debug messages). This is one of the very few calls
611 * in the entire API that is NOT reentrant!
613 * @param addr the address
614 * @param addrlen the length of the @a addr
615 * @return nicely formatted string for the address
616 * will be overwritten by next call to #GNUNET_a2s().
619 GNUNET_a2s (const struct sockaddr *addr,
625 * Convert error type to string.
627 * @param kind type to convert
628 * @return string corresponding to the type
631 GNUNET_error_type_to_string (enum GNUNET_ErrorType kind);
636 * Use this for fatal errors that cannot be handled
638 #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)
643 * Use this for fatal errors that cannot be handled
645 #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)
650 * Use this for internal assertion violations that are
651 * not fatal (can be handled) but should not occur.
653 #define GNUNET_break(cond) do { if (! (cond)) { GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Assertion failed at %s:%d.\n"), __FILE__, __LINE__); } } while(0)
658 * Use this for assertion violations caused by other
659 * peers (i.e. protocol violations). We do not want to
660 * confuse end-users (say, some other peer runs an
661 * older, broken or incompatible GNUnet version), but
662 * we still want to see these problems during
663 * development and testing. "OP == other peer".
665 #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)
670 * Log an error message at log-level 'level' that indicates
671 * a failure of the command 'cmd' with the message given
672 * by strerror(errno).
674 #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)
679 * Log an error message at log-level 'level' that indicates
680 * a failure of the command 'cmd' with the message given
681 * by strerror(errno).
683 #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)
688 * Log an error message at log-level 'level' that indicates
689 * a failure of the command 'cmd' with the message given
690 * by strerror(errno).
692 #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)
697 * Log an error message at log-level 'level' that indicates
698 * a failure of the command 'cmd' with the message given
699 * by strerror(errno).
701 #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)
703 /* ************************* endianess conversion ****************** */
706 * Convert unsigned 64-bit integer to network byte order.
709 * The value in host byte order.
711 * @return The same value in network byte order.
714 GNUNET_htonll (uint64_t n);
718 * Convert unsigned 64-bit integer to host byte order.
721 * The value in network byte order.
723 * @return The same value in host byte order.
726 GNUNET_ntohll (uint64_t n);
730 * Convert double to network byte order.
733 * The value in host byte order.
735 * @return The same value in network byte order.
738 GNUNET_hton_double (double d);
742 * Convert double to host byte order
745 * The value in network byte order.
747 * @return The same value in host byte order.
750 GNUNET_ntoh_double (double d);
753 /* ************************* allocation functions ****************** */
757 * Maximum allocation with GNUNET_malloc macro.
759 #define GNUNET_MAX_MALLOC_CHECKED (1024 * 1024 * 40)
763 * Allocate a struct or union of the given @a type.
764 * Wrapper around #GNUNET_malloc that returns a pointer
765 * to the newly created object of the correct type.
767 * @param type name of the struct or union, i.e. pass 'struct Foo'.
769 #define GNUNET_new(type) (type *) GNUNET_malloc (sizeof (type))
773 * Allocate a size @a n array with structs or unions of the given @a type.
774 * Wrapper around #GNUNET_malloc that returns a pointer
775 * to the newly created objects of the correct type.
777 * @param n number of elements in the array
778 * @param type name of the struct or union, i.e. pass 'struct Foo'.
780 #define GNUNET_new_array(n, type) (type *) GNUNET_malloc ((n) * sizeof (type))
784 * Wrapper around malloc. Allocates size bytes of memory.
785 * The memory will be zero'ed out.
787 * @param size the number of bytes to allocate, must be
788 * smaller than 40 MB.
789 * @return pointer to size bytes of memory, never NULL (!)
791 #define GNUNET_malloc(size) GNUNET_xmalloc_(size, __FILE__, __LINE__)
795 * Allocate and initialize a block of memory.
797 * @param buf data to initalize the block with
798 * @param size the number of bytes in buf (and size of the allocation)
799 * @return pointer to size bytes of memory, never NULL (!)
801 #define GNUNET_memdup(buf,size) GNUNET_xmemdup_(buf, size, __FILE__, __LINE__)
805 * Wrapper around malloc. Allocates size bytes of memory.
806 * The memory will be zero'ed out.
808 * @param size the number of bytes to allocate
809 * @return pointer to size bytes of memory, NULL if we do not have enough memory
811 #define GNUNET_malloc_large(size) GNUNET_xmalloc_unchecked_(size, __FILE__, __LINE__)
815 * Wrapper around realloc. Rellocates size bytes of memory.
817 * @param ptr the pointer to reallocate
818 * @param size the number of bytes to reallocate
819 * @return pointer to size bytes of memory
821 #define GNUNET_realloc(ptr, size) GNUNET_xrealloc_(ptr, size, __FILE__, __LINE__)
825 * Wrapper around free. Frees the memory referred to by ptr.
826 * Note that is is generally better to free memory that was
827 * allocated with #GNUNET_array_grow using #GNUNET_array_grow(mem, size, 0) instead of #GNUNET_free.
829 * @param ptr location where to free the memory. ptr must have
830 * been returned by #GNUNET_strdup, #GNUNET_strndup, #GNUNET_malloc or #GNUNET_array_grow earlier.
832 #define GNUNET_free(ptr) GNUNET_xfree_(ptr, __FILE__, __LINE__)
836 * Free the memory pointed to by ptr if ptr is not NULL.
837 * Equivalent to `if (NULL != ptr) GNUNET_free(ptr)`.
839 * @param ptr the location in memory to free
841 #define GNUNET_free_non_null(ptr) do { void * __x__ = ptr; if (__x__ != NULL) { GNUNET_free(__x__); } } while(0)
845 * Wrapper around #GNUNET_xstrdup_. Makes a copy of the zero-terminated string
848 * @param a pointer to a zero-terminated string
849 * @return a copy of the string including zero-termination
851 #define GNUNET_strdup(a) GNUNET_xstrdup_(a,__FILE__,__LINE__)
855 * Wrapper around #GNUNET_xstrndup_. Makes a partial copy of the string
858 * @param a pointer to a string
859 * @param length of the string to duplicate
860 * @return a partial copy of the string including zero-termination
862 #define GNUNET_strndup(a,length) GNUNET_xstrndup_(a,length,__FILE__,__LINE__)
866 * Grow a well-typed (!) array. This is a convenience
867 * method to grow a vector @a arr of size @a size
868 * to the new (target) size @a tsize.
871 * Example (simple, well-typed stack):
874 * static struct foo * myVector = NULL;
875 * static int myVecLen = 0;
877 * static void push(struct foo * elem) {
878 * GNUNET_array_grow(myVector, myVecLen, myVecLen+1);
879 * memcpy(&myVector[myVecLen-1], elem, sizeof(struct foo));
882 * static void pop(struct foo * elem) {
883 * if (myVecLen == 0) die();
884 * memcpy(elem, myVector[myVecLen-1], sizeof(struct foo));
885 * GNUNET_array_grow(myVector, myVecLen, myVecLen-1);
889 * @param arr base-pointer of the vector, may be NULL if size is 0;
890 * will be updated to reflect the new address. The TYPE of
891 * arr is important since size is the number of elements and
892 * not the size in bytes
893 * @param size the number of elements in the existing vector (number
894 * of elements to copy over)
895 * @param tsize the target size for the resulting vector, use 0 to
896 * free the vector (then, arr will be NULL afterwards).
898 #define GNUNET_array_grow(arr,size,tsize) GNUNET_xgrow_((void**)&arr, sizeof(arr[0]), &size, tsize, __FILE__, __LINE__)
902 * Append an element to a list (growing the
905 #define GNUNET_array_append(arr,size,element) do { GNUNET_array_grow(arr,size,size+1); arr[size-1] = element; } while(0)
909 * Like snprintf, just aborts if the buffer is of insufficient size.
911 * @param buf pointer to buffer that is written to
912 * @param size number of bytes in @a buf
913 * @param format format strings
914 * @param ... data for format string
915 * @return number of bytes written to buf or negative value on error
918 GNUNET_snprintf (char *buf, size_t size, const char *format, ...);
923 * Like asprintf, just portable.
925 * @param buf set to a buffer of sufficient size (allocated, caller must free)
926 * @param format format string (see printf, fprintf, etc.)
927 * @param ... data for format string
928 * @return number of bytes in "*buf" excluding 0-termination
931 GNUNET_asprintf (char **buf, const char *format, ...);
934 /* ************** internal implementations, use macros above! ************** */
937 * Allocate memory. Checks the return value, aborts if no more
938 * memory is available. Don't use GNUNET_xmalloc_ directly. Use the
939 * #GNUNET_malloc macro.
940 * The memory will be zero'ed out.
942 * @param size number of bytes to allocate
943 * @param filename where is this call being made (for debugging)
944 * @param linenumber line where this call is being made (for debugging)
945 * @return allocated memory, never NULL
948 GNUNET_xmalloc_ (size_t size, const char *filename, int linenumber);
952 * Allocate and initialize memory. Checks the return value, aborts if no more
953 * memory is available. Don't use GNUNET_xmemdup_ directly. Use the
954 * #GNUNET_memdup macro.
956 * @param buf buffer to initialize from (must contain size bytes)
957 * @param size number of bytes to allocate
958 * @param filename where is this call being made (for debugging)
959 * @param linenumber line where this call is being made (for debugging)
960 * @return allocated memory, never NULL
963 GNUNET_xmemdup_ (const void *buf, size_t size, const char *filename,
968 * Allocate memory. This function does not check if the allocation
969 * request is within reasonable bounds, allowing allocations larger
970 * than 40 MB. If you don't expect the possibility of very large
971 * allocations, use #GNUNET_malloc instead. The memory will be zero'ed
974 * @param size number of bytes to allocate
975 * @param filename where is this call being made (for debugging)
976 * @param linenumber line where this call is being made (for debugging)
977 * @return pointer to size bytes of memory, NULL if we do not have enough memory
980 GNUNET_xmalloc_unchecked_ (size_t size, const char *filename, int linenumber);
984 * Reallocate memory. Checks the return value, aborts if no more
985 * memory is available.
988 GNUNET_xrealloc_ (void *ptr, size_t n, const char *filename, int linenumber);
992 * Free memory. Merely a wrapper for the case that we
993 * want to keep track of allocations. Don't use GNUNET_xfree_
994 * directly. Use the #GNUNET_free macro.
996 * @param ptr pointer to memory to free
997 * @param filename where is this call being made (for debugging)
998 * @param linenumber line where this call is being made (for debugging)
1001 GNUNET_xfree_ (void *ptr, const char *filename, int linenumber);
1005 * Dup a string. Don't call GNUNET_xstrdup_ directly. Use the #GNUNET_strdup macro.
1006 * @param str string to duplicate
1007 * @param filename where is this call being made (for debugging)
1008 * @param linenumber line where this call is being made (for debugging)
1009 * @return the duplicated string
1012 GNUNET_xstrdup_ (const char *str, const char *filename, int linenumber);
1015 * Dup partially a string. Don't call GNUNET_xstrndup_ directly. Use the #GNUNET_strndup macro.
1017 * @param str string to duplicate
1018 * @param len length of the string to duplicate
1019 * @param filename where is this call being made (for debugging)
1020 * @param linenumber line where this call is being made (for debugging)
1021 * @return the duplicated string
1024 GNUNET_xstrndup_ (const char *str, size_t len, const char *filename,
1028 * Grow an array, the new elements are zeroed out.
1029 * Grows old by (*oldCount-newCount)*elementSize
1030 * bytes and sets *oldCount to newCount.
1032 * Don't call GNUNET_xgrow_ directly. Use the #GNUNET_array_grow macro.
1034 * @param old address of the pointer to the array
1036 * @param elementSize the size of the elements of the array
1037 * @param oldCount address of the number of elements in the *old array
1038 * @param newCount number of elements in the new array, may be 0 (then *old will be NULL afterwards)
1039 * @param filename where is this call being made (for debugging)
1040 * @param linenumber line where this call is being made (for debugging)
1043 GNUNET_xgrow_ (void **old, size_t elementSize, unsigned int *oldCount,
1044 unsigned int newCount, const char *filename, int linenumber);
1049 * Create a copy of the given message.
1051 * @param msg message to copy
1052 * @return duplicate of the message
1054 struct GNUNET_MessageHeader *
1055 GNUNET_copy_message (const struct GNUNET_MessageHeader *msg);
1058 #if __STDC_VERSION__ < 199901L
1060 #define __func__ __FUNCTION__
1062 #define __func__ "<unknown>"
1068 * Valid task priorities. Use these, do not pass random integers!
1069 * For various reasons (#3862 -- building with QT Creator, and
1070 * our restricted cross-compilation with emscripten) this cannot
1071 * be in gnunet_scheduler_lib.h, but it works if we declare it here.
1072 * Naturally, logically this is part of the scheduler.
1074 enum GNUNET_SCHEDULER_Priority
1077 * Run with the same priority as the current job.
1079 GNUNET_SCHEDULER_PRIORITY_KEEP = 0,
1082 * Run when otherwise idle.
1084 GNUNET_SCHEDULER_PRIORITY_IDLE = 1,
1087 * Run as background job (higher than idle,
1088 * lower than default).
1090 GNUNET_SCHEDULER_PRIORITY_BACKGROUND = 2,
1093 * Run with the default priority (normal
1094 * P2P operations). Any task that is scheduled
1095 * without an explicit priority being specified
1096 * will run with this priority.
1098 GNUNET_SCHEDULER_PRIORITY_DEFAULT = 3,
1101 * Run with high priority (important requests).
1102 * Higher than DEFAULT.
1104 GNUNET_SCHEDULER_PRIORITY_HIGH = 4,
1107 * Run with priority for interactive tasks.
1108 * Higher than "HIGH".
1110 GNUNET_SCHEDULER_PRIORITY_UI = 5,
1113 * Run with priority for urgent tasks. Use
1114 * for things like aborts and shutdowns that
1115 * need to preempt "UI"-level tasks.
1118 GNUNET_SCHEDULER_PRIORITY_URGENT = 6,
1121 * This is an internal priority level that is only used for tasks
1122 * that are being triggered due to shutdown (they have automatically
1123 * highest priority). User code must not use this priority level
1124 * directly. Tasks run with this priority level that internally
1125 * schedule other tasks will see their original priority level
1126 * be inherited (unless otherwise specified).
1128 GNUNET_SCHEDULER_PRIORITY_SHUTDOWN = 7,
1131 * Number of priorities (must be the last priority).
1132 * This priority must not be used by clients.
1134 GNUNET_SCHEDULER_PRIORITY_COUNT = 8
1138 #if 0 /* keep Emacsens' auto-indent happy */
1145 #endif /* GNUNET_COMMON_H */