2 This file is part of GNUnet.
3 (C) 2006, 2009 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 2, 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 #ifndef GNUNET_COMMON_H
31 #define GNUNET_COMMON_H
34 #include <sys/socket.h>
37 #include <netinet/in.h>
52 #if 0 /* keep Emacsens' auto-indent happy */
58 * Version of the API (for entire gnunetutil.so library).
60 #define GNUNET_UTIL_VERSION 0x00090500
63 * Named constants for return values. The following
64 * invariants hold: "GNUNET_NO == 0" (to allow "if (GNUNET_NO)")
65 * "GNUNET_OK != GNUNET_SYSERR", "GNUNET_OK != GNUNET_NO", "GNUNET_NO != GNUNET_SYSERR"
66 * and finally "GNUNET_YES != GNUNET_NO".
69 #define GNUNET_SYSERR -1
73 #define GNUNET_MIN(a,b) (((a) < (b)) ? (a) : (b))
75 #define GNUNET_MAX(a,b) (((a) > (b)) ? (a) : (b))
77 /* some systems use one underscore only, and mingw uses no underscore... */
80 #define __BYTE_ORDER _BYTE_ORDER
83 #define __BYTE_ORDER BYTE_ORDER
89 #define __BIG_ENDIAN _BIG_ENDIAN
92 #define __BIG_ENDIAN BIG_ENDIAN
96 #ifndef __LITTLE_ENDIAN
98 #define __LITTLE_ENDIAN _LITTLE_ENDIAN
101 #define __LITTLE_ENDIAN LITTLE_ENDIAN
110 # if __BYTE_ORDER == __LITTLE_ENDIAN
111 # define GNUNET_htobe16(x) __bswap_16 (x)
112 # define GNUNET_htole16(x) (x)
113 # define GNUNET_be16toh(x) __bswap_16 (x)
114 # define GNUNET_le16toh(x) (x)
116 # define GNUNET_htobe32(x) __bswap_32 (x)
117 # define GNUNET_htole32(x) (x)
118 # define GNUNET_be32toh(x) __bswap_32 (x)
119 # define GNUNET_le32toh(x) (x)
121 # define GNUNET_htobe64(x) __bswap_64 (x)
122 # define GNUNET_htole64(x) (x)
123 # define GNUNET_be64toh(x) __bswap_64 (x)
124 # define GNUNET_le64toh(x) (x)
126 # if __BYTE_ORDER == __BIG_ENDIAN
127 # define GNUNET_htobe16(x) (x)
128 # define GNUNET_htole16(x) __bswap_16 (x)
129 # define GNUNET_be16toh(x) (x)
130 # define GNUNET_le16toh(x) __bswap_16 (x)
132 # define GNUNET_htobe32(x) (x)
133 # define GNUNET_htole32(x) __bswap_32 (x)
134 # define GNUNET_be32toh(x) (x)
135 # define GNUNET_le32toh(x) __bswap_32 (x)
137 # define GNUNET_htobe64(x) (x)
138 # define GNUNET_htole64(x) __bswap_64 (x)
139 # define GNUNET_be64toh(x) (x)
140 # define GNUNET_le64toh(x) __bswap_64 (x)
147 * gcc-ism to get packed structs.
149 #define GNUNET_PACKED __attribute__((packed))
152 * gcc-ism to get gcc bitfield layout when compiling with -mms-bitfields
155 #define GNUNET_GCC_STRUCT_LAYOUT __attribute__((gcc_struct))
157 #define GNUNET_GCC_STRUCT_LAYOUT
161 * gcc-ism to force alignment; we use this to align char-arrays
162 * that may then be cast to 'struct's. See also gcc
165 #ifdef __BIGGEST_ALIGNMENT__
166 #define GNUNET_ALIGN __attribute__((aligned (__BIGGEST_ALIGNMENT__)))
168 #define GNUNET_ALIGN __attribute__((aligned (8)))
172 * gcc-ism to document unused arguments
174 #define GNUNET_UNUSED __attribute__((unused))
177 * gcc-ism to document functions that don't return
179 #define GNUNET_NORETURN __attribute__((noreturn))
184 * gcc 4.x-ism to pack structures even on W32 (to be used before structs);
185 * Using this would cause structs to be unaligned on the stack on Sparc,
186 * so we *only* use this on W32 (see #670578 from Debian); fortunately,
187 * W32 doesn't run on sparc anyway.
189 #define GNUNET_NETWORK_STRUCT_BEGIN \
190 _Pragma("pack(push)") \
194 * gcc 4.x-ism to pack structures even on W32 (to be used after structs)
195 * Using this would cause structs to be unaligned on the stack on Sparc,
196 * so we *only* use this on W32 (see #670578 from Debian); fortunately,
197 * W32 doesn't run on sparc anyway.
199 #define GNUNET_NETWORK_STRUCT_END _Pragma("pack(pop)")
202 #error gcc 4.x or higher required on W32 systems
206 * Define as empty, GNUNET_PACKED should suffice, but this won't work on W32
208 #define GNUNET_NETWORK_STRUCT_BEGIN
211 * Define as empty, GNUNET_PACKED should suffice, but this won't work on W32;
213 #define GNUNET_NETWORK_STRUCT_END
216 /* ************************ super-general types *********************** */
218 GNUNET_NETWORK_STRUCT_BEGIN
221 * Header for all communications.
223 struct GNUNET_MessageHeader
227 * The length of the struct (in bytes, including the length field itself),
228 * in big-endian format.
230 uint16_t size GNUNET_PACKED;
233 * The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
235 uint16_t type GNUNET_PACKED;
241 * @brief A SHA-512 hashcode
243 struct GNUNET_HashCode
245 uint32_t bits[512 / 8 / sizeof (uint32_t)]; /* = 16 */
250 * @brief A SHA-256 hashcode
252 struct GNUNET_CRYPTO_ShortHashCode
254 uint32_t bits[256 / 8 / sizeof (uint32_t)]; /* = 8 */
259 * The identity of the host (basically the SHA-512 hashcode of
262 struct GNUNET_PeerIdentity
264 struct GNUNET_HashCode hashPubKey;
266 GNUNET_NETWORK_STRUCT_END
269 * Function called with a filename.
272 * @param filename complete filename (absolute path)
273 * @return GNUNET_OK to continue to iterate,
274 * GNUNET_NO to stop iteration with no error,
275 * GNUNET_SYSERR to abort iteration with error!
277 typedef int (*GNUNET_FileNameCallback) (void *cls, const char *filename);
280 /* ****************************** logging ***************************** */
285 enum GNUNET_ErrorType
287 GNUNET_ERROR_TYPE_UNSPECIFIED = -1,
288 GNUNET_ERROR_TYPE_NONE = 0,
289 GNUNET_ERROR_TYPE_ERROR = 1,
290 GNUNET_ERROR_TYPE_WARNING = 2,
291 GNUNET_ERROR_TYPE_INFO = 4,
292 GNUNET_ERROR_TYPE_DEBUG = 8,
293 GNUNET_ERROR_TYPE_INVALID = 16,
294 GNUNET_ERROR_TYPE_BULK = 32
299 * User-defined handler for log messages.
302 * @param kind severeity
303 * @param component what component is issuing the message?
304 * @param date when was the message logged?
305 * @param message what is the message
307 typedef void (*GNUNET_Logger) (void *cls, enum GNUNET_ErrorType kind,
308 const char *component, const char *date,
309 const char *message);
313 * Get the number of log calls that are going to be skipped
315 * @return number of log calls to be ignored
318 GNUNET_get_log_skip ();
320 #if !defined(GNUNET_CULL_LOGGING)
322 GNUNET_get_log_call_status (int caller_level, const char *comp,
323 const char *file, const char *function, int line);
330 * @param kind how serious is the error?
331 * @param message what is the message (format string)
332 * @param ... arguments for format string
335 GNUNET_log_nocheck (enum GNUNET_ErrorType kind, const char *message, ...);
338 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
339 #define _GNUNET_BOOLEAN_EXPR(expr) \
341 int _gnunet_boolean_var_; \
343 _gnunet_boolean_var_ = 1; \
345 _gnunet_boolean_var_ = 0; \
346 _gnunet_boolean_var_; \
348 #define GN_LIKELY(expr) (__builtin_expect (_GNUNET_BOOLEAN_EXPR(expr), 1))
349 #define GN_UNLIKELY(expr) (__builtin_expect (_GNUNET_BOOLEAN_EXPR(expr), 0))
351 #define GN_LIKELY(expr) (expr)
352 #define GN_UNLIKELY(expr) (expr)
355 #if !defined(GNUNET_LOG_CALL_STATUS)
356 #define GNUNET_LOG_CALL_STATUS -1
360 * Log function that specifies an alternative component.
361 * This function should be used by plugins.
363 * @param kind how serious is the error?
364 * @param comp component responsible for generating the message
365 * @param message what is the message (format string)
366 * @param ... arguments for format string
369 GNUNET_log_from_nocheck (enum GNUNET_ErrorType kind, const char *comp,
370 const char *message, ...);
372 #if !defined(GNUNET_CULL_LOGGING)
373 #define GNUNET_log_from(kind,comp,...) do { int log_line = __LINE__;\
374 static int log_call_enabled = GNUNET_LOG_CALL_STATUS;\
375 if ((GNUNET_EXTRA_LOGGING > 0) || ((GNUNET_ERROR_TYPE_DEBUG & (kind)) == 0)) { \
376 if (GN_UNLIKELY(log_call_enabled == -1))\
377 log_call_enabled = GNUNET_get_log_call_status ((kind) & (~GNUNET_ERROR_TYPE_BULK), (comp), __FILE__, __FUNCTION__, log_line); \
378 if (GN_UNLIKELY(GNUNET_get_log_skip () > 0)) { GNUNET_log_skip (-1, GNUNET_NO); }\
380 if (GN_UNLIKELY(log_call_enabled))\
381 GNUNET_log_from_nocheck ((kind), comp, __VA_ARGS__); \
386 #define GNUNET_log(kind,...) do { int log_line = __LINE__;\
387 static int log_call_enabled = GNUNET_LOG_CALL_STATUS;\
388 if ((GNUNET_EXTRA_LOGGING > 0) || ((GNUNET_ERROR_TYPE_DEBUG & (kind)) == 0)) { \
389 if (GN_UNLIKELY(log_call_enabled == -1))\
390 log_call_enabled = GNUNET_get_log_call_status ((kind) & (~GNUNET_ERROR_TYPE_BULK), NULL, __FILE__, __FUNCTION__, log_line);\
391 if (GN_UNLIKELY(GNUNET_get_log_skip () > 0)) { GNUNET_log_skip (-1, GNUNET_NO); }\
393 if (GN_UNLIKELY(log_call_enabled))\
394 GNUNET_log_nocheck ((kind), __VA_ARGS__); \
399 #define GNUNET_log(...)
400 #define GNUNET_log_from(...)
405 * Log error message about missing configuration option.
407 * @param kind log level
408 * @param section section with missing option
409 * @param option name of missing option
412 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);
433 * Abort the process, generate a core dump if possible.
436 GNUNET_abort (void) GNUNET_NORETURN;
439 * Ignore the next n calls to the log function.
441 * @param n number of log calls to ignore (could be negative)
442 * @param check_reset GNUNET_YES to assert that the log skip counter is currently zero
445 GNUNET_log_skip (int n, int check_reset);
451 * @param comp default component to use
452 * @param loglevel what types of messages should be logged
453 * @param logfile change logging to logfile (use NULL to keep stderr)
454 * @return GNUNET_OK on success, GNUNET_SYSERR if logfile could not be opened
457 GNUNET_log_setup (const char *comp, const char *loglevel, const char *logfile);
461 * Add a custom logger.
463 * @param logger log function
464 * @param logger_cls closure for logger
467 GNUNET_logger_add (GNUNET_Logger logger, void *logger_cls);
471 * Remove a custom logger.
473 * @param logger log function
474 * @param logger_cls closure for logger
477 GNUNET_logger_remove (GNUNET_Logger logger, void *logger_cls);
481 * Convert a short hash value to a string (for printing debug messages).
482 * This is one of the very few calls in the entire API that is
485 * @param hc the short hash code
489 GNUNET_short_h2s (const struct GNUNET_CRYPTO_ShortHashCode * hc);
493 * Convert a short hash value to a string (for printing debug messages).
494 * This prints all 104 characters of a hashcode!
495 * This is one of the very few calls in the entire API that is
498 * @param hc the short hash code
502 GNUNET_short_h2s_full (const struct GNUNET_CRYPTO_ShortHashCode * hc);
506 * Convert a hash value to a string (for printing debug messages).
507 * This is one of the very few calls in the entire API that is
510 * @param hc the hash code
514 GNUNET_h2s (const struct GNUNET_HashCode * hc);
518 * Convert a hash value to a string (for printing debug messages).
519 * This prints all 104 characters of a hashcode!
520 * This is one of the very few calls in the entire API that is
523 * @param hc the hash code
527 GNUNET_h2s_full (const struct GNUNET_HashCode * hc);
531 * Convert a peer identity to a string (for printing debug messages).
532 * This is one of the very few calls in the entire API that is
535 * @param pid the peer identity
536 * @return string form of the pid; will be overwritten by next
537 * call to GNUNET_i2s.
540 GNUNET_i2s (const struct GNUNET_PeerIdentity *pid);
543 * Convert a peer identity to a string (for printing debug messages).
544 * This is one of the very few calls in the entire API that is
547 * @param pid the peer identity
548 * @return string form of the pid; will be overwritten by next
549 * call to GNUNET_i2s.
552 GNUNET_i2s_full (const struct GNUNET_PeerIdentity *pid);
555 * Convert a "struct sockaddr*" (IPv4 or IPv6 address) to a string
556 * (for printing debug messages). This is one of the very few calls
557 * in the entire API that is NOT reentrant!
559 * @param addr the address
560 * @param addrlen the length of the address
561 * @return nicely formatted string for the address
562 * will be overwritten by next call to GNUNET_a2s.
565 GNUNET_a2s (const struct sockaddr *addr, socklen_t addrlen);
568 * Convert error type to string.
570 * @param kind type to convert
571 * @return string corresponding to the type
574 GNUNET_error_type_to_string (enum GNUNET_ErrorType kind);
578 * Use this for fatal errors that cannot be handled
580 #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)
583 * Use this for fatal errors that cannot be handled
585 #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)
588 * Use this for internal assertion violations that are
589 * not fatal (can be handled) but should not occur.
591 #define GNUNET_break(cond) do { if (! (cond)) { GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Assertion failed at %s:%d.\n"), __FILE__, __LINE__); } } while(0)
594 * Use this for assertion violations caused by other
595 * peers (i.e. protocol violations). We do not want to
596 * confuse end-users (say, some other peer runs an
597 * older, broken or incompatible GNUnet version), but
598 * we still want to see these problems during
599 * development and testing. "OP == other peer".
601 #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)
604 * Log an error message at log-level 'level' that indicates
605 * a failure of the command 'cmd' with the message given
606 * by strerror(errno).
608 #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)
611 * Log an error message at log-level 'level' that indicates
612 * a failure of the command 'cmd' with the message given
613 * by strerror(errno).
615 #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)
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_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)
625 * Log an error message at log-level 'level' that indicates
626 * a failure of the command 'cmd' with the message given
627 * by strerror(errno).
629 #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)
631 /* ************************* endianess conversion ****************** */
634 * Convert unsigned 64-bit integer to host-byte-order.
635 * @param n the value in network byte order
636 * @return the same value in host byte order
639 GNUNET_ntohll (uint64_t n);
642 * Convert unsigned 64-bit integer to network-byte-order.
643 * @param n the value in host byte order
644 * @return the same value in network byte order
647 GNUNET_htonll (uint64_t n);
650 * Convert double to network-byte-order.
651 * @param d the value in network byte order
652 * @return the same value in host byte order
655 GNUNET_hton_double (double d);
658 * Convert double to host-byte-order
659 * @param d the value in network byte order
660 * @return the same value in host byte order
663 GNUNET_ntoh_double (double d);
665 /* ************************* allocation functions ****************** */
668 * Maximum allocation with GNUNET_malloc macro.
670 #define GNUNET_MAX_MALLOC_CHECKED (1024 * 1024 * 40)
673 * Allocate a struct or union of the given 'type'.
674 * Wrapper around GNUNET_malloc that returns a pointer
675 * to the newly created object of the correct type.
677 * @param type name of the struct or union, i.e. pass 'struct Foo'.
679 #define GNUNET_new(type) (type *) GNUNET_malloc (sizeof (type))
682 * Wrapper around malloc. Allocates size bytes of memory.
683 * The memory will be zero'ed out.
685 * @param size the number of bytes to allocate, must be
686 * smaller than 40 MB.
687 * @return pointer to size bytes of memory, never NULL (!)
689 #define GNUNET_malloc(size) GNUNET_xmalloc_(size, __FILE__, __LINE__)
692 * Allocate and initialize a block of memory.
694 * @param buf data to initalize the block with
695 * @param size the number of bytes in buf (and size of the allocation)
696 * @return pointer to size bytes of memory, never NULL (!)
698 #define GNUNET_memdup(buf,size) GNUNET_xmemdup_(buf, size, __FILE__, __LINE__)
701 * Wrapper around malloc. Allocates size bytes of memory.
702 * The memory will be zero'ed out.
704 * @param size the number of bytes to allocate
705 * @return pointer to size bytes of memory, NULL if we do not have enough memory
707 #define GNUNET_malloc_large(size) GNUNET_xmalloc_unchecked_(size, __FILE__, __LINE__)
710 * Wrapper around realloc. Rellocates size bytes of memory.
712 * @param ptr the pointer to reallocate
713 * @param size the number of bytes to reallocate
714 * @return pointer to size bytes of memory
716 #define GNUNET_realloc(ptr, size) GNUNET_xrealloc_(ptr, size, __FILE__, __LINE__)
719 * Wrapper around free. Frees the memory referred to by ptr.
720 * Note that is is generally better to free memory that was
721 * allocated with GNUNET_array_grow using GNUNET_array_grow(mem, size, 0) instead of GNUNET_free.
723 * @param ptr location where to free the memory. ptr must have
724 * been returned by GNUNET_strdup, GNUNET_strndup, GNUNET_malloc or GNUNET_array_grow earlier.
726 #define GNUNET_free(ptr) GNUNET_xfree_(ptr, __FILE__, __LINE__)
729 * Free the memory pointed to by ptr if ptr is not NULL.
730 * Equivalent to if (ptr!=null)GNUNET_free(ptr).
732 * @param ptr the location in memory to free
734 #define GNUNET_free_non_null(ptr) do { void * __x__ = ptr; if (__x__ != NULL) { GNUNET_free(__x__); } } while(0)
737 * Wrapper around GNUNET_strdup. Makes a copy of the zero-terminated string
740 * @param a pointer to a zero-terminated string
741 * @return a copy of the string including zero-termination
743 #define GNUNET_strdup(a) GNUNET_xstrdup_(a,__FILE__,__LINE__)
746 * Wrapper around GNUNET_strndup. Makes a partial copy of the string
749 * @param a pointer to a string
750 * @param length of the string to duplicate
751 * @return a partial copy of the string including zero-termination
753 #define GNUNET_strndup(a,length) GNUNET_xstrndup_(a,length,__FILE__,__LINE__)
756 * Grow a well-typed (!) array. This is a convenience
757 * method to grow a vector <tt>arr</tt> of size <tt>size</tt>
758 * to the new (target) size <tt>tsize</tt>.
761 * Example (simple, well-typed stack):
764 * static struct foo * myVector = NULL;
765 * static int myVecLen = 0;
767 * static void push(struct foo * elem) {
768 * GNUNET_array_grow(myVector, myVecLen, myVecLen+1);
769 * memcpy(&myVector[myVecLen-1], elem, sizeof(struct foo));
772 * static void pop(struct foo * elem) {
773 * if (myVecLen == 0) die();
774 * memcpy(elem, myVector[myVecLen-1], sizeof(struct foo));
775 * GNUNET_array_grow(myVector, myVecLen, myVecLen-1);
779 * @param arr base-pointer of the vector, may be NULL if size is 0;
780 * will be updated to reflect the new address. The TYPE of
781 * arr is important since size is the number of elements and
782 * not the size in bytes
783 * @param size the number of elements in the existing vector (number
784 * of elements to copy over)
785 * @param tsize the target size for the resulting vector, use 0 to
786 * free the vector (then, arr will be NULL afterwards).
788 #define GNUNET_array_grow(arr,size,tsize) GNUNET_xgrow_((void**)&arr, sizeof(arr[0]), &size, tsize, __FILE__, __LINE__)
791 * Append an element to a list (growing the
794 #define GNUNET_array_append(arr,size,element) do { GNUNET_array_grow(arr,size,size+1); arr[size-1] = element; } while(0)
797 * Like snprintf, just aborts if the buffer is of insufficient size.
799 * @param buf pointer to buffer that is written to
800 * @param size number of bytes in buf
801 * @param format format strings
802 * @param ... data for format string
803 * @return number of bytes written to buf or negative value on error
806 GNUNET_snprintf (char *buf, size_t size, const char *format, ...);
810 * Like asprintf, just portable.
812 * @param buf set to a buffer of sufficient size (allocated, caller must free)
813 * @param format format string (see printf, fprintf, etc.)
814 * @param ... data for format string
815 * @return number of bytes in "*buf" excluding 0-termination
818 GNUNET_asprintf (char **buf, const char *format, ...);
821 /* ************** internal implementations, use macros above! ************** */
824 * Allocate memory. Checks the return value, aborts if no more
825 * memory is available. Don't use GNUNET_xmalloc_ directly. Use the
826 * GNUNET_malloc macro.
827 * The memory will be zero'ed out.
829 * @param size number of bytes to allocate
830 * @param filename where is this call being made (for debugging)
831 * @param linenumber line where this call is being made (for debugging)
832 * @return allocated memory, never NULL
835 GNUNET_xmalloc_ (size_t size, const char *filename, int linenumber);
839 * Allocate and initialize memory. Checks the return value, aborts if no more
840 * memory is available. Don't use GNUNET_xmemdup_ directly. Use the
841 * GNUNET_memdup macro.
843 * @param buf buffer to initialize from (must contain size bytes)
844 * @param size number of bytes to allocate
845 * @param filename where is this call being made (for debugging)
846 * @param linenumber line where this call is being made (for debugging)
847 * @return allocated memory, never NULL
850 GNUNET_xmemdup_ (const void *buf, size_t size, const char *filename,
855 * Allocate memory. This function does not check if the allocation
856 * request is within reasonable bounds, allowing allocations larger
857 * than 40 MB. If you don't expect the possibility of very large
858 * allocations, use GNUNET_malloc instead. The memory will be zero'ed
861 * @param size number of bytes to allocate
862 * @param filename where is this call being made (for debugging)
863 * @param linenumber line where this call is being made (for debugging)
864 * @return pointer to size bytes of memory, NULL if we do not have enough memory
867 GNUNET_xmalloc_unchecked_ (size_t size, const char *filename, int linenumber);
870 * Reallocate memory. Checks the return value, aborts if no more
871 * memory is available.
874 GNUNET_xrealloc_ (void *ptr, size_t n, const char *filename, int linenumber);
877 * Free memory. Merely a wrapper for the case that we
878 * want to keep track of allocations. Don't use GNUNET_xfree_
879 * directly. Use the GNUNET_free macro.
881 * @param ptr pointer to memory to free
882 * @param filename where is this call being made (for debugging)
883 * @param linenumber line where this call is being made (for debugging)
886 GNUNET_xfree_ (void *ptr, const char *filename, int linenumber);
890 * Dup a string. Don't call GNUNET_xstrdup_ directly. Use the GNUNET_strdup macro.
891 * @param str string to duplicate
892 * @param filename where is this call being made (for debugging)
893 * @param linenumber line where this call is being made (for debugging)
894 * @return the duplicated string
897 GNUNET_xstrdup_ (const char *str, const char *filename, int linenumber);
900 * Dup partially a string. Don't call GNUNET_xstrndup_ directly. Use the GNUNET_strndup macro.
902 * @param str string to duplicate
903 * @param len length of the string to duplicate
904 * @param filename where is this call being made (for debugging)
905 * @param linenumber line where this call is being made (for debugging)
906 * @return the duplicated string
909 GNUNET_xstrndup_ (const char *str, size_t len, const char *filename,
913 * Grow an array, the new elements are zeroed out.
914 * Grows old by (*oldCount-newCount)*elementSize
915 * bytes and sets *oldCount to newCount.
917 * Don't call GNUNET_xgrow_ directly. Use the GNUNET_array_grow macro.
919 * @param old address of the pointer to the array
921 * @param elementSize the size of the elements of the array
922 * @param oldCount address of the number of elements in the *old array
923 * @param newCount number of elements in the new array, may be 0 (then *old will be NULL afterwards)
924 * @param filename where is this call being made (for debugging)
925 * @param linenumber line where this call is being made (for debugging)
928 GNUNET_xgrow_ (void **old, size_t elementSize, unsigned int *oldCount,
929 unsigned int newCount, const char *filename, int linenumber);
933 * Create a copy of the given message.
935 * @param msg message to copy
936 * @return duplicate of the message
938 struct GNUNET_MessageHeader *
939 GNUNET_copy_message (const struct GNUNET_MessageHeader *msg);
942 #if __STDC_VERSION__ < 199901L
944 #define __func__ __FUNCTION__
946 #define __func__ "<unknown>"
953 #if 0 /* keep Emacsens' auto-indent happy */
963 #endif /*GNUNET_COMMON_H_ */