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 /* UX: We need a message type that is output by
336 * default without looking like there is a problem.
338 GNUNET_ERROR_TYPE_MESSAGE = 4,
339 GNUNET_ERROR_TYPE_INFO = 8,
340 GNUNET_ERROR_TYPE_DEBUG = 16,
341 GNUNET_ERROR_TYPE_INVALID = 32,
342 GNUNET_ERROR_TYPE_BULK = 64
348 * User-defined handler for log messages.
351 * @param kind severeity
352 * @param component what component is issuing the message?
353 * @param date when was the message logged?
354 * @param message what is the message
357 (*GNUNET_Logger) (void *cls,
358 enum GNUNET_ErrorType kind,
359 const char *component,
361 const char *message);
366 * Get the number of log calls that are going to be skipped
368 * @return number of log calls to be ignored
371 GNUNET_get_log_skip (void);
374 #if !defined(GNUNET_CULL_LOGGING)
376 GNUNET_get_log_call_status (int caller_level,
379 const char *function,
388 * @param kind how serious is the error?
389 * @param message what is the message (format string)
390 * @param ... arguments for format string
393 GNUNET_log_nocheck (enum GNUNET_ErrorType kind, const char *message, ...)
394 __attribute__ ((format (printf, 2, 3)));
397 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
398 #define _GNUNET_BOOLEAN_EXPR(expr) \
400 int _gnunet_boolean_var_; \
402 _gnunet_boolean_var_ = 1; \
404 _gnunet_boolean_var_ = 0; \
405 _gnunet_boolean_var_; \
407 #define GN_LIKELY(expr) (__builtin_expect (_GNUNET_BOOLEAN_EXPR(expr), 1))
408 #define GN_UNLIKELY(expr) (__builtin_expect (_GNUNET_BOOLEAN_EXPR(expr), 0))
410 #define GN_LIKELY(expr) (expr)
411 #define GN_UNLIKELY(expr) (expr)
414 #if !defined(GNUNET_LOG_CALL_STATUS)
415 #define GNUNET_LOG_CALL_STATUS -1
421 * Log function that specifies an alternative component.
422 * This function should be used by plugins.
424 * @param kind how serious is the error?
425 * @param comp component responsible for generating the message
426 * @param message what is the message (format string)
427 * @param ... arguments for format string
430 GNUNET_log_from_nocheck (enum GNUNET_ErrorType kind, const char *comp,
431 const char *message, ...);
433 #if !defined(GNUNET_CULL_LOGGING)
434 #define GNUNET_log_from(kind,comp,...) do { int log_line = __LINE__;\
435 static int log_call_enabled = GNUNET_LOG_CALL_STATUS;\
436 if ((GNUNET_EXTRA_LOGGING > 0) || ((GNUNET_ERROR_TYPE_DEBUG & (kind)) == 0)) { \
437 if (GN_UNLIKELY(log_call_enabled == -1))\
438 log_call_enabled = GNUNET_get_log_call_status ((kind) & (~GNUNET_ERROR_TYPE_BULK), (comp), __FILE__, __FUNCTION__, log_line); \
439 if (GN_UNLIKELY(GNUNET_get_log_skip () > 0)) { GNUNET_log_skip (-1, GNUNET_NO); }\
441 if (GN_UNLIKELY(log_call_enabled))\
442 GNUNET_log_from_nocheck ((kind), comp, __VA_ARGS__); \
447 #define GNUNET_log(kind,...) do { int log_line = __LINE__;\
448 static int log_call_enabled = GNUNET_LOG_CALL_STATUS;\
449 if ((GNUNET_EXTRA_LOGGING > 0) || ((GNUNET_ERROR_TYPE_DEBUG & (kind)) == 0)) { \
450 if (GN_UNLIKELY(log_call_enabled == -1))\
451 log_call_enabled = GNUNET_get_log_call_status ((kind) & (~GNUNET_ERROR_TYPE_BULK), NULL, __FILE__, __FUNCTION__, log_line);\
452 if (GN_UNLIKELY(GNUNET_get_log_skip () > 0)) { GNUNET_log_skip (-1, GNUNET_NO); }\
454 if (GN_UNLIKELY(log_call_enabled))\
455 GNUNET_log_nocheck ((kind), __VA_ARGS__); \
460 #define GNUNET_log(...)
461 #define GNUNET_log_from(...)
467 * Log error message about missing configuration option.
469 * @param kind log level
470 * @param section section with missing option
471 * @param option name of missing option
474 GNUNET_log_config_missing (enum GNUNET_ErrorType kind,
481 * Log error message about invalid configuration option value.
483 * @param kind log level
484 * @param section section with invalid option
485 * @param option name of invalid option
486 * @param required what is required that is invalid about the option
489 GNUNET_log_config_invalid (enum GNUNET_ErrorType kind,
492 const char *required);
497 * Abort the process, generate a core dump if possible.
498 * Most code should use `GNUNET_assert (0)` instead to
499 * first log the location of the failure.
502 GNUNET_abort_ (void) GNUNET_NORETURN;
507 * Ignore the next @a n calls to the log function.
509 * @param n number of log calls to ignore (could be negative)
510 * @param check_reset #GNUNET_YES to assert that the log skip counter is currently zero
513 GNUNET_log_skip (int n,
521 * @param comp default component to use
522 * @param loglevel what types of messages should be logged
523 * @param logfile change logging to logfile (use NULL to keep stderr)
524 * @return #GNUNET_OK on success, #GNUNET_SYSERR if logfile could not be opened
527 GNUNET_log_setup (const char *comp,
528 const char *loglevel,
529 const char *logfile);
534 * Add a custom logger. Note that installing any custom logger
535 * will disable the standard logger. When multiple custom loggers
536 * are installed, all will be called. The standard logger will
537 * only be used if no custom loggers are present.
539 * @param logger log function
540 * @param logger_cls closure for @a logger
543 GNUNET_logger_add (GNUNET_Logger logger,
549 * Remove a custom logger.
551 * @param logger log function
552 * @param logger_cls closure for @a logger
555 GNUNET_logger_remove (GNUNET_Logger logger,
561 * Convert a hash value to a string (for printing debug messages).
562 * This is one of the very few calls in the entire API that is
565 * @param hc the hash code
569 GNUNET_h2s (const struct GNUNET_HashCode * hc);
574 * Convert a hash value to a string (for printing debug messages).
575 * This prints all 104 characters of a hashcode!
576 * This is one of the very few calls in the entire API that is
579 * @param hc the hash code
583 GNUNET_h2s_full (const struct GNUNET_HashCode * hc);
588 * Convert a peer identity to a string (for printing debug messages).
589 * This is one of the very few calls in the entire API that is
592 * @param pid the peer identity
593 * @return string form of the pid; will be overwritten by next
594 * call to #GNUNET_i2s().
597 GNUNET_i2s (const struct GNUNET_PeerIdentity *pid);
602 * Convert a peer identity to a string (for printing debug messages).
603 * This is one of the very few calls in the entire API that is
606 * @param pid the peer identity
607 * @return string form of the pid; will be overwritten by next
608 * call to #GNUNET_i2s_full().
611 GNUNET_i2s_full (const struct GNUNET_PeerIdentity *pid);
616 * Convert a "struct sockaddr*" (IPv4 or IPv6 address) to a string
617 * (for printing debug messages). This is one of the very few calls
618 * in the entire API that is NOT reentrant!
620 * @param addr the address
621 * @param addrlen the length of the @a addr
622 * @return nicely formatted string for the address
623 * will be overwritten by next call to #GNUNET_a2s().
626 GNUNET_a2s (const struct sockaddr *addr,
632 * Convert error type to string.
634 * @param kind type to convert
635 * @return string corresponding to the type
638 GNUNET_error_type_to_string (enum GNUNET_ErrorType kind);
643 * Use this for fatal errors that cannot be handled
645 #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)
650 * Use this for fatal errors that cannot be handled
652 #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)
657 * Use this for internal assertion violations that are
658 * not fatal (can be handled) but should not occur.
660 #define GNUNET_break(cond) do { if (! (cond)) { GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Assertion failed at %s:%d.\n"), __FILE__, __LINE__); } } while(0)
665 * Use this for assertion violations caused by other
666 * peers (i.e. protocol violations). We do not want to
667 * confuse end-users (say, some other peer runs an
668 * older, broken or incompatible GNUnet version), but
669 * we still want to see these problems during
670 * development and testing. "OP == other peer".
672 #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)
677 * Log an error message at log-level 'level' that indicates
678 * a failure of the command 'cmd' with the message given
679 * by strerror(errno).
681 #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)
686 * Log an error message at log-level 'level' that indicates
687 * a failure of the command 'cmd' with the message given
688 * by strerror(errno).
690 #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)
695 * Log an error message at log-level 'level' that indicates
696 * a failure of the command 'cmd' with the message given
697 * by strerror(errno).
699 #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)
704 * Log an error message at log-level 'level' that indicates
705 * a failure of the command 'cmd' with the message given
706 * by strerror(errno).
708 #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)
710 /* ************************* endianess conversion ****************** */
713 * Convert unsigned 64-bit integer to network byte order.
716 * The value in host byte order.
718 * @return The same value in network byte order.
721 GNUNET_htonll (uint64_t n);
725 * Convert unsigned 64-bit integer to host byte order.
728 * The value in network byte order.
730 * @return The same value in host byte order.
733 GNUNET_ntohll (uint64_t n);
737 * Convert double to network byte order.
740 * The value in host byte order.
742 * @return The same value in network byte order.
745 GNUNET_hton_double (double d);
749 * Convert double to host byte order
752 * The value in network byte order.
754 * @return The same value in host byte order.
757 GNUNET_ntoh_double (double d);
760 /* ************************* allocation functions ****************** */
764 * Maximum allocation with GNUNET_malloc macro.
766 #define GNUNET_MAX_MALLOC_CHECKED (1024 * 1024 * 40)
770 * Allocate a struct or union of the given @a type.
771 * Wrapper around #GNUNET_malloc that returns a pointer
772 * to the newly created object of the correct type.
774 * @param type name of the struct or union, i.e. pass 'struct Foo'.
776 #define GNUNET_new(type) (type *) GNUNET_malloc (sizeof (type))
779 * Call memcpy() but check for @a n being 0 first. In the latter
780 * case, it is now safe to pass NULL for @a src or @a dst.
781 * Unlike traditional memcpy(), returns nothing.
783 * @param dst destination of the copy, may be NULL if @a n is zero
784 * @param src source of the copy, may be NULL if @a n is zero
785 * @param n number of bytes to copy
787 #define GNUNET_memcpy(dst,src,n) do { if (0 != n) { (void) memcpy (dst,src,n); } } while (0)
792 * Allocate a size @a n array with structs or unions of the given @a type.
793 * Wrapper around #GNUNET_malloc that returns a pointer
794 * to the newly created objects of the correct type.
796 * @param n number of elements in the array
797 * @param type name of the struct or union, i.e. pass 'struct Foo'.
799 #define GNUNET_new_array(n, type) (type *) GNUNET_malloc ((n) * sizeof (type))
803 * Allocate a size @a n times @a m array
804 * with structs or unions of the given @a type.
806 * @param n size of the first dimension
807 * @param m size of the second dimension
808 * @param type name of the struct or union, i.e. pass 'struct Foo'.
810 #define GNUNET_new_array_2d(n, m, type) (type **) GNUNET_xnew_array_2d_ (n, m, sizeof (type), __FILE__, __LINE__)
814 * Allocate a size @a n times @a m times @a o array
815 * with structs or unions of the given @a type.
817 * @param n size of the first dimension
818 * @param m size of the second dimension
819 * @param o size of the third dimension
820 * @param type name of the struct or union, i.e. pass 'struct Foo'.
822 #define GNUNET_new_array_3d(n, m, o, type) (type ***) GNUNET_xnew_array_3d_ (n, m, o, sizeof (type), __FILE__, __LINE__)
826 * Wrapper around malloc. Allocates size bytes of memory.
827 * The memory will be zero'ed out.
829 * @param size the number of bytes to allocate, must be
830 * smaller than 40 MB.
831 * @return pointer to size bytes of memory, never NULL (!)
833 #define GNUNET_malloc(size) GNUNET_xmalloc_(size, __FILE__, __LINE__)
837 * Allocate and initialize a block of memory.
839 * @param buf data to initalize the block with
840 * @param size the number of bytes in buf (and size of the allocation)
841 * @return pointer to size bytes of memory, never NULL (!)
843 #define GNUNET_memdup(buf,size) GNUNET_xmemdup_(buf, size, __FILE__, __LINE__)
847 * Wrapper around malloc. Allocates size bytes of memory.
848 * The memory will be zero'ed out.
850 * @param size the number of bytes to allocate
851 * @return pointer to size bytes of memory, NULL if we do not have enough memory
853 #define GNUNET_malloc_large(size) GNUNET_xmalloc_unchecked_(size, __FILE__, __LINE__)
857 * Wrapper around realloc. Reallocates size bytes of memory.
858 * The content of the intersection of the new and old size will be unchanged.
860 * @param ptr the pointer to reallocate
861 * @param size the number of bytes to reallocate
862 * @return pointer to size bytes of memory
864 #define GNUNET_realloc(ptr, size) GNUNET_xrealloc_(ptr, size, __FILE__, __LINE__)
868 * Wrapper around free. Frees the memory referred to by ptr.
869 * Note that it is generally better to free memory that was
870 * allocated with #GNUNET_array_grow using #GNUNET_array_grow(mem, size, 0) instead of #GNUNET_free.
872 * @param ptr location where to free the memory. ptr must have
873 * been returned by #GNUNET_strdup, #GNUNET_strndup, #GNUNET_malloc or #GNUNET_array_grow earlier.
875 #define GNUNET_free(ptr) GNUNET_xfree_(ptr, __FILE__, __LINE__)
879 * Free the memory pointed to by ptr if ptr is not NULL.
880 * Equivalent to `if (NULL != ptr) GNUNET_free(ptr)`.
882 * @param ptr the location in memory to free
884 #define GNUNET_free_non_null(ptr) do { void * __x__ = ptr; if (__x__ != NULL) { GNUNET_free(__x__); } } while(0)
888 * Wrapper around #GNUNET_xstrdup_. Makes a copy of the zero-terminated string
891 * @param a pointer to a zero-terminated string
892 * @return a copy of the string including zero-termination
894 #define GNUNET_strdup(a) GNUNET_xstrdup_(a,__FILE__,__LINE__)
898 * Wrapper around #GNUNET_xstrndup_. Makes a partial copy of the string
901 * @param a pointer to a string
902 * @param length of the string to duplicate
903 * @return a partial copy of the string including zero-termination
905 #define GNUNET_strndup(a,length) GNUNET_xstrndup_(a,length,__FILE__,__LINE__)
909 * Grow a well-typed (!) array. This is a convenience
910 * method to grow a vector @a arr of size @a size
911 * to the new (target) size @a tsize.
914 * Example (simple, well-typed stack):
917 * static struct foo * myVector = NULL;
918 * static int myVecLen = 0;
920 * static void push(struct foo * elem) {
921 * GNUNET_array_grow(myVector, myVecLen, myVecLen+1);
922 * GNUNET_memcpy(&myVector[myVecLen-1], elem, sizeof(struct foo));
925 * static void pop(struct foo * elem) {
926 * if (myVecLen == 0) die();
927 * GNUNET_memcpy(elem, myVector[myVecLen-1], sizeof(struct foo));
928 * GNUNET_array_grow(myVector, myVecLen, myVecLen-1);
932 * @param arr base-pointer of the vector, may be NULL if size is 0;
933 * will be updated to reflect the new address. The TYPE of
934 * arr is important since size is the number of elements and
935 * not the size in bytes
936 * @param size the number of elements in the existing vector (number
937 * of elements to copy over)
938 * @param tsize the target size for the resulting vector, use 0 to
939 * free the vector (then, arr will be NULL afterwards).
941 #define GNUNET_array_grow(arr,size,tsize) GNUNET_xgrow_((void**)&arr, sizeof(arr[0]), &size, tsize, __FILE__, __LINE__)
945 * Append an element to a list (growing the
948 #define GNUNET_array_append(arr,size,element) do { GNUNET_array_grow(arr,size,size+1); arr[size-1] = element; } while(0)
952 * Like snprintf, just aborts if the buffer is of insufficient size.
954 * @param buf pointer to buffer that is written to
955 * @param size number of bytes in @a buf
956 * @param format format strings
957 * @param ... data for format string
958 * @return number of bytes written to buf or negative value on error
961 GNUNET_snprintf (char *buf, size_t size, const char *format, ...);
966 * Like asprintf, just portable.
968 * @param buf set to a buffer of sufficient size (allocated, caller must free)
969 * @param format format string (see printf, fprintf, etc.)
970 * @param ... data for format string
971 * @return number of bytes in "*buf" excluding 0-termination
974 GNUNET_asprintf (char **buf, const char *format, ...);
977 /* ************** internal implementations, use macros above! ************** */
980 * Allocate memory. Checks the return value, aborts if no more
981 * memory is available. Don't use GNUNET_xmalloc_ directly. Use the
982 * #GNUNET_malloc macro.
983 * The memory will be zero'ed out.
985 * @param size number of bytes to allocate
986 * @param filename where is this call being made (for debugging)
987 * @param linenumber line where this call is being made (for debugging)
988 * @return allocated memory, never NULL
991 GNUNET_xmalloc_ (size_t size, const char *filename, int linenumber);
995 * Allocate memory for a two dimensional array in one block
996 * and set up pointers. Aborts if no more memory is available.
997 * Don't use GNUNET_xnew_array_2d_ directly. Use the
998 * #GNUNET_new_array_2d macro.
999 * The memory of the elements will be zero'ed out.
1001 * @param n size of the first dimension
1002 * @param m size of the second dimension
1003 * @param elementSize size of a single element in bytes
1004 * @param filename where is this call being made (for debugging)
1005 * @param linenumber line where this call is being made (for debugging)
1006 * @return allocated memory, never NULL
1009 GNUNET_xnew_array_2d_ (size_t n, size_t m, size_t elementSize,
1010 const char *filename, int linenumber);
1014 * Allocate memory for a three dimensional array in one block
1015 * and set up pointers. Aborts if no more memory is available.
1016 * Don't use GNUNET_xnew_array_3d_ directly. Use the
1017 * #GNUNET_new_array_3d macro.
1018 * The memory of the elements will be zero'ed out.
1020 * @param n size of the first dimension
1021 * @param m size of the second dimension
1022 * @param o size of the third dimension
1023 * @param elementSize size of a single element in bytes
1024 * @param filename where is this call being made (for debugging)
1025 * @param linenumber line where this call is being made (for debugging)
1026 * @return allocated memory, never NULL
1029 GNUNET_xnew_array_3d_ (size_t n, size_t m, size_t o, size_t elementSize,
1030 const char *filename, int linenumber);
1034 * Allocate and initialize memory. Checks the return value, aborts if no more
1035 * memory is available. Don't use GNUNET_xmemdup_ directly. Use the
1036 * #GNUNET_memdup macro.
1038 * @param buf buffer to initialize from (must contain size bytes)
1039 * @param size number of bytes to allocate
1040 * @param filename where is this call being made (for debugging)
1041 * @param linenumber line where this call is being made (for debugging)
1042 * @return allocated memory, never NULL
1045 GNUNET_xmemdup_ (const void *buf, size_t size, const char *filename,
1050 * Allocate memory. This function does not check if the allocation
1051 * request is within reasonable bounds, allowing allocations larger
1052 * than 40 MB. If you don't expect the possibility of very large
1053 * allocations, use #GNUNET_malloc instead. The memory will be zero'ed
1056 * @param size number of bytes to allocate
1057 * @param filename where is this call being made (for debugging)
1058 * @param linenumber line where this call is being made (for debugging)
1059 * @return pointer to size bytes of memory, NULL if we do not have enough memory
1062 GNUNET_xmalloc_unchecked_ (size_t size, const char *filename, int linenumber);
1066 * Reallocate memory. Checks the return value, aborts if no more
1067 * memory is available.
1070 GNUNET_xrealloc_ (void *ptr, size_t n, const char *filename, int linenumber);
1074 * Free memory. Merely a wrapper for the case that we
1075 * want to keep track of allocations. Don't use GNUNET_xfree_
1076 * directly. Use the #GNUNET_free macro.
1078 * @param ptr pointer to memory to free
1079 * @param filename where is this call being made (for debugging)
1080 * @param linenumber line where this call is being made (for debugging)
1083 GNUNET_xfree_ (void *ptr, const char *filename, int linenumber);
1087 * Dup a string. Don't call GNUNET_xstrdup_ directly. Use the #GNUNET_strdup macro.
1088 * @param str string to duplicate
1089 * @param filename where is this call being made (for debugging)
1090 * @param linenumber line where this call is being made (for debugging)
1091 * @return the duplicated string
1094 GNUNET_xstrdup_ (const char *str, const char *filename, int linenumber);
1097 * Dup partially a string. Don't call GNUNET_xstrndup_ directly. Use the #GNUNET_strndup macro.
1099 * @param str string to duplicate
1100 * @param len length of the string to duplicate
1101 * @param filename where is this call being made (for debugging)
1102 * @param linenumber line where this call is being made (for debugging)
1103 * @return the duplicated string
1106 GNUNET_xstrndup_ (const char *str, size_t len, const char *filename,
1110 * Grow an array, the new elements are zeroed out.
1111 * Grows old by (*oldCount-newCount)*elementSize
1112 * bytes and sets *oldCount to newCount.
1114 * Don't call GNUNET_xgrow_ directly. Use the #GNUNET_array_grow macro.
1116 * @param old address of the pointer to the array
1118 * @param elementSize the size of the elements of the array
1119 * @param oldCount address of the number of elements in the *old array
1120 * @param newCount number of elements in the new array, may be 0 (then *old will be NULL afterwards)
1121 * @param filename where is this call being made (for debugging)
1122 * @param linenumber line where this call is being made (for debugging)
1125 GNUNET_xgrow_ (void **old, size_t elementSize, unsigned int *oldCount,
1126 unsigned int newCount, const char *filename, int linenumber);
1131 * Create a copy of the given message.
1133 * @param msg message to copy
1134 * @return duplicate of the message
1136 struct GNUNET_MessageHeader *
1137 GNUNET_copy_message (const struct GNUNET_MessageHeader *msg);
1140 #if __STDC_VERSION__ < 199901L
1142 #define __func__ __FUNCTION__
1144 #define __func__ "<unknown>"
1150 * Valid task priorities. Use these, do not pass random integers!
1151 * For various reasons (#3862 -- building with QT Creator, and
1152 * our restricted cross-compilation with emscripten) this cannot
1153 * be in gnunet_scheduler_lib.h, but it works if we declare it here.
1154 * Naturally, logically this is part of the scheduler.
1156 enum GNUNET_SCHEDULER_Priority
1159 * Run with the same priority as the current job.
1161 GNUNET_SCHEDULER_PRIORITY_KEEP = 0,
1164 * Run when otherwise idle.
1166 GNUNET_SCHEDULER_PRIORITY_IDLE = 1,
1169 * Run as background job (higher than idle,
1170 * lower than default).
1172 GNUNET_SCHEDULER_PRIORITY_BACKGROUND = 2,
1175 * Run with the default priority (normal
1176 * P2P operations). Any task that is scheduled
1177 * without an explicit priority being specified
1178 * will run with this priority.
1180 GNUNET_SCHEDULER_PRIORITY_DEFAULT = 3,
1183 * Run with high priority (important requests).
1184 * Higher than DEFAULT.
1186 GNUNET_SCHEDULER_PRIORITY_HIGH = 4,
1189 * Run with priority for interactive tasks.
1190 * Higher than "HIGH".
1192 GNUNET_SCHEDULER_PRIORITY_UI = 5,
1195 * Run with priority for urgent tasks. Use
1196 * for things like aborts and shutdowns that
1197 * need to preempt "UI"-level tasks.
1200 GNUNET_SCHEDULER_PRIORITY_URGENT = 6,
1203 * This is an internal priority level that is only used for tasks
1204 * that are being triggered due to shutdown (they have automatically
1205 * highest priority). User code must not use this priority level
1206 * directly. Tasks run with this priority level that internally
1207 * schedule other tasks will see their original priority level
1208 * be inherited (unless otherwise specified).
1210 GNUNET_SCHEDULER_PRIORITY_SHUTDOWN = 7,
1213 * Number of priorities (must be the last priority).
1214 * This priority must not be used by clients.
1216 GNUNET_SCHEDULER_PRIORITY_COUNT = 8
1220 #if 0 /* keep Emacsens' auto-indent happy */
1227 #endif /* GNUNET_COMMON_H */