2 This file is part of GNUnet.
3 (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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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>
55 #if 0 /* keep Emacsens' auto-indent happy */
61 * Version of the API (for entire gnunetutil.so library).
63 #define GNUNET_UTIL_VERSION 0x000A0100
66 * Named constants for return values. The following invariants hold:
67 * `GNUNET_NO == 0` (to allow `if (GNUNET_NO)`) `GNUNET_OK !=
68 * GNUNET_SYSERR`, `GNUNET_OK != GNUNET_NO`, `GNUNET_NO !=
69 * GNUNET_SYSERR` and finally `GNUNET_YES != GNUNET_NO`.
72 #define GNUNET_SYSERR -1
76 #define GNUNET_MIN(a,b) (((a) < (b)) ? (a) : (b))
78 #define GNUNET_MAX(a,b) (((a) > (b)) ? (a) : (b))
80 /* some systems use one underscore only, and mingw uses no underscore... */
83 #define __BYTE_ORDER _BYTE_ORDER
86 #define __BYTE_ORDER BYTE_ORDER
92 #define __BIG_ENDIAN _BIG_ENDIAN
95 #define __BIG_ENDIAN BIG_ENDIAN
99 #ifndef __LITTLE_ENDIAN
100 #ifdef _LITTLE_ENDIAN
101 #define __LITTLE_ENDIAN _LITTLE_ENDIAN
104 #define __LITTLE_ENDIAN LITTLE_ENDIAN
111 * define #GNUNET_EXTRA_LOGGING if using this header outside the GNUnet source
112 * tree where gnunet_config.h is unavailable
114 #ifndef GNUNET_EXTRA_LOGGING
115 #define GNUNET_EXTRA_LOGGING 0
122 # if __BYTE_ORDER == __LITTLE_ENDIAN
123 # define GNUNET_htobe16(x) __bswap_16 (x)
124 # define GNUNET_htole16(x) (x)
125 # define GNUNET_be16toh(x) __bswap_16 (x)
126 # define GNUNET_le16toh(x) (x)
128 # define GNUNET_htobe32(x) __bswap_32 (x)
129 # define GNUNET_htole32(x) (x)
130 # define GNUNET_be32toh(x) __bswap_32 (x)
131 # define GNUNET_le32toh(x) (x)
133 # define GNUNET_htobe64(x) __bswap_64 (x)
134 # define GNUNET_htole64(x) (x)
135 # define GNUNET_be64toh(x) __bswap_64 (x)
136 # define GNUNET_le64toh(x) (x)
138 # if __BYTE_ORDER == __BIG_ENDIAN
139 # define GNUNET_htobe16(x) (x)
140 # define GNUNET_htole16(x) __bswap_16 (x)
141 # define GNUNET_be16toh(x) (x)
142 # define GNUNET_le16toh(x) __bswap_16 (x)
144 # define GNUNET_htobe32(x) (x)
145 # define GNUNET_htole32(x) __bswap_32 (x)
146 # define GNUNET_be32toh(x) (x)
147 # define GNUNET_le32toh(x) __bswap_32 (x)
149 # define GNUNET_htobe64(x) (x)
150 # define GNUNET_htole64(x) __bswap_64 (x)
151 # define GNUNET_be64toh(x) (x)
152 # define GNUNET_le64toh(x) __bswap_64 (x)
159 * gcc-ism to get packed structs.
161 #define GNUNET_PACKED __attribute__((packed))
164 * gcc-ism to get gcc bitfield layout when compiling with -mms-bitfields
167 #define GNUNET_GCC_STRUCT_LAYOUT __attribute__((gcc_struct))
169 #define GNUNET_GCC_STRUCT_LAYOUT
173 * gcc-ism to force alignment; we use this to align char-arrays
174 * that may then be cast to 'struct's. See also gcc
177 #ifdef __BIGGEST_ALIGNMENT__
178 #define GNUNET_ALIGN __attribute__((aligned (__BIGGEST_ALIGNMENT__)))
180 #define GNUNET_ALIGN __attribute__((aligned (8)))
184 * gcc-ism to document unused arguments
186 #define GNUNET_UNUSED __attribute__((unused))
189 * gcc-ism to document functions that don't return
191 #define GNUNET_NORETURN __attribute__((noreturn))
196 * gcc 4.x-ism to pack structures even on W32 (to be used before structs);
197 * Using this would cause structs to be unaligned on the stack on Sparc,
198 * so we *only* use this on W32 (see #670578 from Debian); fortunately,
199 * W32 doesn't run on sparc anyway.
201 #define GNUNET_NETWORK_STRUCT_BEGIN \
202 _Pragma("pack(push)") \
206 * gcc 4.x-ism to pack structures even on W32 (to be used after structs)
207 * Using this would cause structs to be unaligned on the stack on Sparc,
208 * so we *only* use this on W32 (see #670578 from Debian); fortunately,
209 * W32 doesn't run on sparc anyway.
211 #define GNUNET_NETWORK_STRUCT_END _Pragma("pack(pop)")
214 #error gcc 4.x or higher required on W32 systems
218 * Define as empty, GNUNET_PACKED should suffice, but this won't work on W32
220 #define GNUNET_NETWORK_STRUCT_BEGIN
223 * Define as empty, GNUNET_PACKED should suffice, but this won't work on W32;
225 #define GNUNET_NETWORK_STRUCT_END
228 /* ************************ super-general types *********************** */
230 GNUNET_NETWORK_STRUCT_BEGIN
233 * Header for all communications.
235 struct GNUNET_MessageHeader
239 * The length of the struct (in bytes, including the length field itself),
240 * in big-endian format.
242 uint16_t size GNUNET_PACKED;
245 * The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
247 uint16_t type GNUNET_PACKED;
251 GNUNET_NETWORK_STRUCT_END
254 * Function called with a filename.
257 * @param filename complete filename (absolute path)
258 * @return #GNUNET_OK to continue to iterate,
259 * #GNUNET_NO to stop iteration with no error,
260 * #GNUNET_SYSERR to abort iteration with error!
263 (*GNUNET_FileNameCallback) (void *cls,
264 const char *filename);
267 /* ****************************** logging ***************************** */
273 enum GNUNET_ErrorType
275 GNUNET_ERROR_TYPE_UNSPECIFIED = -1,
276 GNUNET_ERROR_TYPE_NONE = 0,
277 GNUNET_ERROR_TYPE_ERROR = 1,
278 GNUNET_ERROR_TYPE_WARNING = 2,
279 GNUNET_ERROR_TYPE_INFO = 4,
280 GNUNET_ERROR_TYPE_DEBUG = 8,
281 GNUNET_ERROR_TYPE_INVALID = 16,
282 GNUNET_ERROR_TYPE_BULK = 32
288 * User-defined handler for log messages.
291 * @param kind severeity
292 * @param component what component is issuing the message?
293 * @param date when was the message logged?
294 * @param message what is the message
296 typedef void (*GNUNET_Logger) (void *cls,
297 enum GNUNET_ErrorType kind,
298 const char *component,
300 const char *message);
305 * Get the number of log calls that are going to be skipped
307 * @return number of log calls to be ignored
310 GNUNET_get_log_skip ();
312 #if !defined(GNUNET_CULL_LOGGING)
314 GNUNET_get_log_call_status (int caller_level,
317 const char *function,
326 * @param kind how serious is the error?
327 * @param message what is the message (format string)
328 * @param ... arguments for format string
331 GNUNET_log_nocheck (enum GNUNET_ErrorType kind, const char *message, ...);
334 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
335 #define _GNUNET_BOOLEAN_EXPR(expr) \
337 int _gnunet_boolean_var_; \
339 _gnunet_boolean_var_ = 1; \
341 _gnunet_boolean_var_ = 0; \
342 _gnunet_boolean_var_; \
344 #define GN_LIKELY(expr) (__builtin_expect (_GNUNET_BOOLEAN_EXPR(expr), 1))
345 #define GN_UNLIKELY(expr) (__builtin_expect (_GNUNET_BOOLEAN_EXPR(expr), 0))
347 #define GN_LIKELY(expr) (expr)
348 #define GN_UNLIKELY(expr) (expr)
351 #if !defined(GNUNET_LOG_CALL_STATUS)
352 #define GNUNET_LOG_CALL_STATUS -1
358 * Log function that specifies an alternative component.
359 * This function should be used by plugins.
361 * @param kind how serious is the error?
362 * @param comp component responsible for generating the message
363 * @param message what is the message (format string)
364 * @param ... arguments for format string
367 GNUNET_log_from_nocheck (enum GNUNET_ErrorType kind, const char *comp,
368 const char *message, ...);
370 #if !defined(GNUNET_CULL_LOGGING)
371 #define GNUNET_log_from(kind,comp,...) do { int log_line = __LINE__;\
372 static int log_call_enabled = GNUNET_LOG_CALL_STATUS;\
373 if ((GNUNET_EXTRA_LOGGING > 0) || ((GNUNET_ERROR_TYPE_DEBUG & (kind)) == 0)) { \
374 if (GN_UNLIKELY(log_call_enabled == -1))\
375 log_call_enabled = GNUNET_get_log_call_status ((kind) & (~GNUNET_ERROR_TYPE_BULK), (comp), __FILE__, __FUNCTION__, log_line); \
376 if (GN_UNLIKELY(GNUNET_get_log_skip () > 0)) { GNUNET_log_skip (-1, GNUNET_NO); }\
378 if (GN_UNLIKELY(log_call_enabled))\
379 GNUNET_log_from_nocheck ((kind), comp, __VA_ARGS__); \
384 #define GNUNET_log(kind,...) do { int log_line = __LINE__;\
385 static int log_call_enabled = GNUNET_LOG_CALL_STATUS;\
386 if ((GNUNET_EXTRA_LOGGING > 0) || ((GNUNET_ERROR_TYPE_DEBUG & (kind)) == 0)) { \
387 if (GN_UNLIKELY(log_call_enabled == -1))\
388 log_call_enabled = GNUNET_get_log_call_status ((kind) & (~GNUNET_ERROR_TYPE_BULK), NULL, __FILE__, __FUNCTION__, log_line);\
389 if (GN_UNLIKELY(GNUNET_get_log_skip () > 0)) { GNUNET_log_skip (-1, GNUNET_NO); }\
391 if (GN_UNLIKELY(log_call_enabled))\
392 GNUNET_log_nocheck ((kind), __VA_ARGS__); \
397 #define GNUNET_log(...)
398 #define GNUNET_log_from(...)
404 * Log error message about missing configuration option.
406 * @param kind log level
407 * @param section section with missing option
408 * @param option name of missing option
411 GNUNET_log_config_missing (enum GNUNET_ErrorType kind,
418 * Log error message about invalid configuration option value.
420 * @param kind log level
421 * @param section section with invalid option
422 * @param option name of invalid option
423 * @param required what is required that is invalid about the option
426 GNUNET_log_config_invalid (enum GNUNET_ErrorType kind,
429 const char *required);
434 * Abort the process, generate a core dump if possible.
437 GNUNET_abort (void) GNUNET_NORETURN;
442 * Ignore the next @a n calls to the log function.
444 * @param n number of log calls to ignore (could be negative)
445 * @param check_reset #GNUNET_YES to assert that the log skip counter is currently zero
448 GNUNET_log_skip (int n,
456 * @param comp default component to use
457 * @param loglevel what types of messages should be logged
458 * @param logfile change logging to logfile (use NULL to keep stderr)
459 * @return #GNUNET_OK on success, #GNUNET_SYSERR if logfile could not be opened
462 GNUNET_log_setup (const char *comp,
463 const char *loglevel,
464 const char *logfile);
469 * Add a custom logger.
471 * @param logger log function
472 * @param logger_cls closure for @a logger
475 GNUNET_logger_add (GNUNET_Logger logger,
481 * Remove a custom logger.
483 * @param logger log function
484 * @param logger_cls closure for @a logger
487 GNUNET_logger_remove (GNUNET_Logger logger,
493 * Convert a hash value to a string (for printing debug messages).
494 * This is one of the very few calls in the entire API that is
497 * @param hc the hash code
501 GNUNET_h2s (const struct GNUNET_HashCode * hc);
506 * Convert a hash value to a string (for printing debug messages).
507 * This prints all 104 characters of a hashcode!
508 * This is one of the very few calls in the entire API that is
511 * @param hc the hash code
515 GNUNET_h2s_full (const struct GNUNET_HashCode * hc);
520 * Convert a peer identity to a string (for printing debug messages).
521 * This is one of the very few calls in the entire API that is
524 * @param pid the peer identity
525 * @return string form of the pid; will be overwritten by next
526 * call to #GNUNET_i2s().
529 GNUNET_i2s (const struct GNUNET_PeerIdentity *pid);
534 * Convert a peer identity to a string (for printing debug messages).
535 * This is one of the very few calls in the entire API that is
538 * @param pid the peer identity
539 * @return string form of the pid; will be overwritten by next
540 * call to #GNUNET_i2s_full().
543 GNUNET_i2s_full (const struct GNUNET_PeerIdentity *pid);
548 * Convert a "struct sockaddr*" (IPv4 or IPv6 address) to a string
549 * (for printing debug messages). This is one of the very few calls
550 * in the entire API that is NOT reentrant!
552 * @param addr the address
553 * @param addrlen the length of the @a addr
554 * @return nicely formatted string for the address
555 * will be overwritten by next call to #GNUNET_a2s().
558 GNUNET_a2s (const struct sockaddr *addr,
564 * Convert error type to string.
566 * @param kind type to convert
567 * @return string corresponding to the type
570 GNUNET_error_type_to_string (enum GNUNET_ErrorType kind);
575 * Use this for fatal errors that cannot be handled
577 #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)
582 * Use this for fatal errors that cannot be handled
584 #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)
589 * Use this for internal assertion violations that are
590 * not fatal (can be handled) but should not occur.
592 #define GNUNET_break(cond) do { if (! (cond)) { GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Assertion failed at %s:%d.\n"), __FILE__, __LINE__); } } while(0)
597 * Use this for assertion violations caused by other
598 * peers (i.e. protocol violations). We do not want to
599 * confuse end-users (say, some other peer runs an
600 * older, broken or incompatible GNUnet version), but
601 * we still want to see these problems during
602 * development and testing. "OP == other peer".
604 #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)
609 * Log an error message at log-level 'level' that indicates
610 * a failure of the command 'cmd' with the message given
611 * by strerror(errno).
613 #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)
618 * Log an error message at log-level 'level' that indicates
619 * a failure of the command 'cmd' with the message given
620 * by strerror(errno).
622 #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)
627 * Log an error message at log-level 'level' that indicates
628 * a failure of the command 'cmd' with the message given
629 * by strerror(errno).
631 #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)
636 * Log an error message at log-level 'level' that indicates
637 * a failure of the command 'cmd' with the message given
638 * by strerror(errno).
640 #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)
642 /* ************************* endianess conversion ****************** */
645 * Convert unsigned 64-bit integer to host-byte-order.
646 * @param n the value in network byte order
647 * @return the same value in host byte order
650 GNUNET_ntohll (uint64_t n);
653 * Convert unsigned 64-bit integer to network-byte-order.
654 * @param n the value in host byte order
655 * @return the same value in network byte order
658 GNUNET_htonll (uint64_t n);
661 * Convert double to network-byte-order.
662 * @param d the value in network byte order
663 * @return the same value in host byte order
666 GNUNET_hton_double (double d);
669 * Convert double to host-byte-order
670 * @param d the value in network byte order
671 * @return the same value in host byte order
674 GNUNET_ntoh_double (double d);
676 /* ************************* allocation functions ****************** */
680 * Maximum allocation with GNUNET_malloc macro.
682 #define GNUNET_MAX_MALLOC_CHECKED (1024 * 1024 * 40)
686 * Allocate a struct or union of the given @a type.
687 * Wrapper around #GNUNET_malloc that returns a pointer
688 * to the newly created object of the correct type.
690 * @param type name of the struct or union, i.e. pass 'struct Foo'.
692 #define GNUNET_new(type) (type *) GNUNET_malloc (sizeof (type))
696 * Allocate a size @a n array with structs or unions of the given @a type.
697 * Wrapper around #GNUNET_malloc that returns a pointer
698 * to the newly created objects of the correct type.
700 * @param n number of elements in the array
701 * @param type name of the struct or union, i.e. pass 'struct Foo'.
703 #define GNUNET_new_array(n, type) (type *) GNUNET_malloc ((n) * sizeof (type))
707 * Wrapper around malloc. Allocates size bytes of memory.
708 * The memory will be zero'ed out.
710 * @param size the number of bytes to allocate, must be
711 * smaller than 40 MB.
712 * @return pointer to size bytes of memory, never NULL (!)
714 #define GNUNET_malloc(size) GNUNET_xmalloc_(size, __FILE__, __LINE__)
718 * Allocate and initialize a block of memory.
720 * @param buf data to initalize the block with
721 * @param size the number of bytes in buf (and size of the allocation)
722 * @return pointer to size bytes of memory, never NULL (!)
724 #define GNUNET_memdup(buf,size) GNUNET_xmemdup_(buf, size, __FILE__, __LINE__)
728 * Wrapper around malloc. Allocates size bytes of memory.
729 * The memory will be zero'ed out.
731 * @param size the number of bytes to allocate
732 * @return pointer to size bytes of memory, NULL if we do not have enough memory
734 #define GNUNET_malloc_large(size) GNUNET_xmalloc_unchecked_(size, __FILE__, __LINE__)
738 * Wrapper around realloc. Rellocates size bytes of memory.
740 * @param ptr the pointer to reallocate
741 * @param size the number of bytes to reallocate
742 * @return pointer to size bytes of memory
744 #define GNUNET_realloc(ptr, size) GNUNET_xrealloc_(ptr, size, __FILE__, __LINE__)
748 * Wrapper around free. Frees the memory referred to by ptr.
749 * Note that is is generally better to free memory that was
750 * allocated with #GNUNET_array_grow using #GNUNET_array_grow(mem, size, 0) instead of #GNUNET_free.
752 * @param ptr location where to free the memory. ptr must have
753 * been returned by #GNUNET_strdup, #GNUNET_strndup, #GNUNET_malloc or #GNUNET_array_grow earlier.
755 #define GNUNET_free(ptr) GNUNET_xfree_(ptr, __FILE__, __LINE__)
759 * Free the memory pointed to by ptr if ptr is not NULL.
760 * Equivalent to `if (NULL != ptr) GNUNET_free(ptr)`.
762 * @param ptr the location in memory to free
764 #define GNUNET_free_non_null(ptr) do { void * __x__ = ptr; if (__x__ != NULL) { GNUNET_free(__x__); } } while(0)
768 * Wrapper around #GNUNET_xstrdup_. Makes a copy of the zero-terminated string
771 * @param a pointer to a zero-terminated string
772 * @return a copy of the string including zero-termination
774 #define GNUNET_strdup(a) GNUNET_xstrdup_(a,__FILE__,__LINE__)
778 * Wrapper around #GNUNET_xstrndup_. Makes a partial copy of the string
781 * @param a pointer to a string
782 * @param length of the string to duplicate
783 * @return a partial copy of the string including zero-termination
785 #define GNUNET_strndup(a,length) GNUNET_xstrndup_(a,length,__FILE__,__LINE__)
789 * Grow a well-typed (!) array. This is a convenience
790 * method to grow a vector @a arr of size @a size
791 * to the new (target) size @a tsize.
794 * Example (simple, well-typed stack):
797 * static struct foo * myVector = NULL;
798 * static int myVecLen = 0;
800 * static void push(struct foo * elem) {
801 * GNUNET_array_grow(myVector, myVecLen, myVecLen+1);
802 * memcpy(&myVector[myVecLen-1], elem, sizeof(struct foo));
805 * static void pop(struct foo * elem) {
806 * if (myVecLen == 0) die();
807 * memcpy(elem, myVector[myVecLen-1], sizeof(struct foo));
808 * GNUNET_array_grow(myVector, myVecLen, myVecLen-1);
812 * @param arr base-pointer of the vector, may be NULL if size is 0;
813 * will be updated to reflect the new address. The TYPE of
814 * arr is important since size is the number of elements and
815 * not the size in bytes
816 * @param size the number of elements in the existing vector (number
817 * of elements to copy over)
818 * @param tsize the target size for the resulting vector, use 0 to
819 * free the vector (then, arr will be NULL afterwards).
821 #define GNUNET_array_grow(arr,size,tsize) GNUNET_xgrow_((void**)&arr, sizeof(arr[0]), &size, tsize, __FILE__, __LINE__)
825 * Append an element to a list (growing the
828 #define GNUNET_array_append(arr,size,element) do { GNUNET_array_grow(arr,size,size+1); arr[size-1] = element; } while(0)
832 * Like snprintf, just aborts if the buffer is of insufficient size.
834 * @param buf pointer to buffer that is written to
835 * @param size number of bytes in @a buf
836 * @param format format strings
837 * @param ... data for format string
838 * @return number of bytes written to buf or negative value on error
841 GNUNET_snprintf (char *buf, size_t size, const char *format, ...);
846 * Like asprintf, just portable.
848 * @param buf set to a buffer of sufficient size (allocated, caller must free)
849 * @param format format string (see printf, fprintf, etc.)
850 * @param ... data for format string
851 * @return number of bytes in "*buf" excluding 0-termination
854 GNUNET_asprintf (char **buf, const char *format, ...);
857 /* ************** internal implementations, use macros above! ************** */
860 * Allocate memory. Checks the return value, aborts if no more
861 * memory is available. Don't use GNUNET_xmalloc_ directly. Use the
862 * #GNUNET_malloc macro.
863 * The memory will be zero'ed out.
865 * @param size number of bytes to allocate
866 * @param filename where is this call being made (for debugging)
867 * @param linenumber line where this call is being made (for debugging)
868 * @return allocated memory, never NULL
871 GNUNET_xmalloc_ (size_t size, const char *filename, int linenumber);
875 * Allocate and initialize memory. Checks the return value, aborts if no more
876 * memory is available. Don't use GNUNET_xmemdup_ directly. Use the
877 * #GNUNET_memdup macro.
879 * @param buf buffer to initialize from (must contain size bytes)
880 * @param size number of bytes to allocate
881 * @param filename where is this call being made (for debugging)
882 * @param linenumber line where this call is being made (for debugging)
883 * @return allocated memory, never NULL
886 GNUNET_xmemdup_ (const void *buf, size_t size, const char *filename,
891 * Allocate memory. This function does not check if the allocation
892 * request is within reasonable bounds, allowing allocations larger
893 * than 40 MB. If you don't expect the possibility of very large
894 * allocations, use #GNUNET_malloc instead. The memory will be zero'ed
897 * @param size number of bytes to allocate
898 * @param filename where is this call being made (for debugging)
899 * @param linenumber line where this call is being made (for debugging)
900 * @return pointer to size bytes of memory, NULL if we do not have enough memory
903 GNUNET_xmalloc_unchecked_ (size_t size, const char *filename, int linenumber);
907 * Reallocate memory. Checks the return value, aborts if no more
908 * memory is available.
911 GNUNET_xrealloc_ (void *ptr, size_t n, const char *filename, int linenumber);
915 * Free memory. Merely a wrapper for the case that we
916 * want to keep track of allocations. Don't use GNUNET_xfree_
917 * directly. Use the #GNUNET_free macro.
919 * @param ptr pointer to memory to free
920 * @param filename where is this call being made (for debugging)
921 * @param linenumber line where this call is being made (for debugging)
924 GNUNET_xfree_ (void *ptr, const char *filename, int linenumber);
928 * Dup a string. Don't call GNUNET_xstrdup_ directly. Use the #GNUNET_strdup macro.
929 * @param str string to duplicate
930 * @param filename where is this call being made (for debugging)
931 * @param linenumber line where this call is being made (for debugging)
932 * @return the duplicated string
935 GNUNET_xstrdup_ (const char *str, const char *filename, int linenumber);
938 * Dup partially a string. Don't call GNUNET_xstrndup_ directly. Use the #GNUNET_strndup macro.
940 * @param str string to duplicate
941 * @param len length of the string to duplicate
942 * @param filename where is this call being made (for debugging)
943 * @param linenumber line where this call is being made (for debugging)
944 * @return the duplicated string
947 GNUNET_xstrndup_ (const char *str, size_t len, const char *filename,
951 * Grow an array, the new elements are zeroed out.
952 * Grows old by (*oldCount-newCount)*elementSize
953 * bytes and sets *oldCount to newCount.
955 * Don't call GNUNET_xgrow_ directly. Use the #GNUNET_array_grow macro.
957 * @param old address of the pointer to the array
959 * @param elementSize the size of the elements of the array
960 * @param oldCount address of the number of elements in the *old array
961 * @param newCount number of elements in the new array, may be 0 (then *old will be NULL afterwards)
962 * @param filename where is this call being made (for debugging)
963 * @param linenumber line where this call is being made (for debugging)
966 GNUNET_xgrow_ (void **old, size_t elementSize, unsigned int *oldCount,
967 unsigned int newCount, const char *filename, int linenumber);
972 * Create a copy of the given message.
974 * @param msg message to copy
975 * @return duplicate of the message
977 struct GNUNET_MessageHeader *
978 GNUNET_copy_message (const struct GNUNET_MessageHeader *msg);
981 #if __STDC_VERSION__ < 199901L
983 #define __func__ __FUNCTION__
985 #define __func__ "<unknown>"
990 #if 0 /* keep Emacsens' auto-indent happy */
1000 #endif /*GNUNET_COMMON_H_ */