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_SYSERR to abort iteration with error!
276 typedef int (*GNUNET_FileNameCallback) (void *cls, const char *filename);
279 /* ****************************** logging ***************************** */
284 enum GNUNET_ErrorType
286 GNUNET_ERROR_TYPE_UNSPECIFIED = -1,
287 GNUNET_ERROR_TYPE_NONE = 0,
288 GNUNET_ERROR_TYPE_ERROR = 1,
289 GNUNET_ERROR_TYPE_WARNING = 2,
290 GNUNET_ERROR_TYPE_INFO = 4,
291 GNUNET_ERROR_TYPE_DEBUG = 8,
292 GNUNET_ERROR_TYPE_INVALID = 16,
293 GNUNET_ERROR_TYPE_BULK = 32
298 * User-defined handler for log messages.
301 * @param kind severeity
302 * @param component what component is issuing the message?
303 * @param date when was the message logged?
304 * @param message what is the message
306 typedef void (*GNUNET_Logger) (void *cls, enum GNUNET_ErrorType kind,
307 const char *component, const char *date,
308 const char *message);
312 * Get the number of log calls that are going to be skipped
314 * @return number of log calls to be ignored
317 GNUNET_get_log_skip ();
319 #if !defined(GNUNET_CULL_LOGGING)
321 GNUNET_get_log_call_status (int caller_level, const char *comp,
322 const char *file, const char *function, int line);
329 * @param kind how serious is the error?
330 * @param message what is the message (format string)
331 * @param ... arguments for format string
334 GNUNET_log_nocheck (enum GNUNET_ErrorType kind, const char *message, ...);
337 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
338 #define _GNUNET_BOOLEAN_EXPR(expr) \
340 int _gnunet_boolean_var_; \
342 _gnunet_boolean_var_ = 1; \
344 _gnunet_boolean_var_ = 0; \
345 _gnunet_boolean_var_; \
347 #define GN_LIKELY(expr) (__builtin_expect (_GNUNET_BOOLEAN_EXPR(expr), 1))
348 #define GN_UNLIKELY(expr) (__builtin_expect (_GNUNET_BOOLEAN_EXPR(expr), 0))
350 #define GN_LIKELY(expr) (expr)
351 #define GN_UNLIKELY(expr) (expr)
354 #if !defined(GNUNET_LOG_CALL_STATUS)
355 #define GNUNET_LOG_CALL_STATUS -1
359 * Log function that specifies an alternative component.
360 * This function should be used by plugins.
362 * @param kind how serious is the error?
363 * @param comp component responsible for generating the message
364 * @param message what is the message (format string)
365 * @param ... arguments for format string
368 GNUNET_log_from_nocheck (enum GNUNET_ErrorType kind, const char *comp,
369 const char *message, ...);
371 #if !defined(GNUNET_CULL_LOGGING)
372 #define GNUNET_log_from(kind,comp,...) do { int log_line = __LINE__;\
373 static int log_call_enabled = GNUNET_LOG_CALL_STATUS;\
374 if ((GNUNET_EXTRA_LOGGING > 0) || ((GNUNET_ERROR_TYPE_DEBUG & (kind)) == 0)) { \
375 if (GN_UNLIKELY(log_call_enabled == -1))\
376 log_call_enabled = GNUNET_get_log_call_status ((kind) & (~GNUNET_ERROR_TYPE_BULK), (comp), __FILE__, __FUNCTION__, log_line); \
377 if (GN_UNLIKELY(GNUNET_get_log_skip () > 0)) { GNUNET_log_skip (-1, GNUNET_NO); }\
379 if (GN_UNLIKELY(log_call_enabled))\
380 GNUNET_log_from_nocheck ((kind), comp, __VA_ARGS__); \
385 #define GNUNET_log(kind,...) do { int log_line = __LINE__;\
386 static int log_call_enabled = GNUNET_LOG_CALL_STATUS;\
387 if ((GNUNET_EXTRA_LOGGING > 0) || ((GNUNET_ERROR_TYPE_DEBUG & (kind)) == 0)) { \
388 if (GN_UNLIKELY(log_call_enabled == -1))\
389 log_call_enabled = GNUNET_get_log_call_status ((kind) & (~GNUNET_ERROR_TYPE_BULK), NULL, __FILE__, __FUNCTION__, log_line);\
390 if (GN_UNLIKELY(GNUNET_get_log_skip () > 0)) { GNUNET_log_skip (-1, GNUNET_NO); }\
392 if (GN_UNLIKELY(log_call_enabled))\
393 GNUNET_log_nocheck ((kind), __VA_ARGS__); \
398 #define GNUNET_log(...)
399 #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,
417 * Log error message about invalid configuration option value.
419 * @param kind log level
420 * @param section section with invalid option
421 * @param option name of invalid option
422 * @param required what is required that is invalid about the option
425 GNUNET_log_config_invalid (enum GNUNET_ErrorType kind,
428 const char *required);
432 * Abort the process, generate a core dump if possible.
435 GNUNET_abort (void) GNUNET_NORETURN;
438 * Ignore the next n calls to the log function.
440 * @param n number of log calls to ignore (could be negative)
441 * @param check_reset GNUNET_YES to assert that the log skip counter is currently zero
444 GNUNET_log_skip (int n, int check_reset);
450 * @param comp default component to use
451 * @param loglevel what types of messages should be logged
452 * @param logfile change logging to logfile (use NULL to keep stderr)
453 * @return GNUNET_OK on success, GNUNET_SYSERR if logfile could not be opened
456 GNUNET_log_setup (const char *comp, const char *loglevel, const char *logfile);
460 * Add a custom logger.
462 * @param logger log function
463 * @param logger_cls closure for logger
466 GNUNET_logger_add (GNUNET_Logger logger, void *logger_cls);
470 * Remove a custom logger.
472 * @param logger log function
473 * @param logger_cls closure for logger
476 GNUNET_logger_remove (GNUNET_Logger logger, void *logger_cls);
480 * Convert a short hash value to a string (for printing debug messages).
481 * This is one of the very few calls in the entire API that is
484 * @param hc the short hash code
488 GNUNET_short_h2s (const struct GNUNET_CRYPTO_ShortHashCode * hc);
492 * Convert a short hash value to a string (for printing debug messages).
493 * This prints all 104 characters of a hashcode!
494 * This is one of the very few calls in the entire API that is
497 * @param hc the short hash code
501 GNUNET_short_h2s_full (const struct GNUNET_CRYPTO_ShortHashCode * hc);
505 * Convert a hash value to a string (for printing debug messages).
506 * This is one of the very few calls in the entire API that is
509 * @param hc the hash code
513 GNUNET_h2s (const struct GNUNET_HashCode * hc);
517 * Convert a hash value to a string (for printing debug messages).
518 * This prints all 104 characters of a hashcode!
519 * This is one of the very few calls in the entire API that is
522 * @param hc the hash code
526 GNUNET_h2s_full (const struct GNUNET_HashCode * hc);
530 * Convert a peer identity to a string (for printing debug messages).
531 * This is one of the very few calls in the entire API that is
534 * @param pid the peer identity
535 * @return string form of the pid; will be overwritten by next
536 * call to GNUNET_i2s.
539 GNUNET_i2s (const struct GNUNET_PeerIdentity *pid);
542 * Convert a peer identity to a string (for printing debug messages).
543 * This is one of the very few calls in the entire API that is
546 * @param pid the peer identity
547 * @return string form of the pid; will be overwritten by next
548 * call to GNUNET_i2s.
551 GNUNET_i2s_full (const struct GNUNET_PeerIdentity *pid);
554 * Convert a "struct sockaddr*" (IPv4 or IPv6 address) to a string
555 * (for printing debug messages). This is one of the very few calls
556 * in the entire API that is NOT reentrant!
558 * @param addr the address
559 * @param addrlen the length of the address
560 * @return nicely formatted string for the address
561 * will be overwritten by next call to GNUNET_a2s.
564 GNUNET_a2s (const struct sockaddr *addr, socklen_t addrlen);
567 * Convert error type to string.
569 * @param kind type to convert
570 * @return string corresponding to the type
573 GNUNET_error_type_to_string (enum GNUNET_ErrorType kind);
577 * Use this for fatal errors that cannot be handled
579 #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)
587 * Use this for internal assertion violations that are
588 * not fatal (can be handled) but should not occur.
590 #define GNUNET_break(cond) do { if (! (cond)) { GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Assertion failed at %s:%d.\n"), __FILE__, __LINE__); } } while(0)
593 * Use this for assertion violations caused by other
594 * peers (i.e. protocol violations). We do not want to
595 * confuse end-users (say, some other peer runs an
596 * older, broken or incompatible GNUnet version), but
597 * we still want to see these problems during
598 * development and testing. "OP == other peer".
600 #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)
603 * Log an error message at log-level 'level' that indicates
604 * a failure of the command 'cmd' with the message given
605 * by strerror(errno).
607 #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)
610 * Log an error message at log-level 'level' that indicates
611 * a failure of the command 'cmd' with the message given
612 * by strerror(errno).
614 #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)
617 * Log an error message at log-level 'level' that indicates
618 * a failure of the command 'cmd' with the message given
619 * by strerror(errno).
621 #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)
624 * Log an error message at log-level 'level' that indicates
625 * a failure of the command 'cmd' with the message given
626 * by strerror(errno).
628 #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)
630 /* ************************* endianess conversion ****************** */
633 * Convert unsigned 64-bit integer to host-byte-order.
634 * @param n the value in network byte order
635 * @return the same value in host byte order
638 GNUNET_ntohll (uint64_t n);
641 * Convert unsigned 64-bit integer to network-byte-order.
642 * @param n the value in host byte order
643 * @return the same value in network byte order
646 GNUNET_htonll (uint64_t n);
649 * Convert double to network-byte-order.
650 * @param d the value in network byte order
651 * @return the same value in host byte order
654 GNUNET_hton_double (double d);
657 * Convert double to host-byte-order
658 * @param d the value in network byte order
659 * @return the same value in host byte order
662 GNUNET_ntoh_double (double d);
664 /* ************************* allocation functions ****************** */
667 * Maximum allocation with GNUNET_malloc macro.
669 #define GNUNET_MAX_MALLOC_CHECKED (1024 * 1024 * 40)
672 * Allocate a struct or union of the given 'type'.
673 * Wrapper around GNUNET_malloc that returns a pointer
674 * to the newly created object of the correct type.
676 * @param type name of the struct or union, i.e. pass 'struct Foo'.
678 #define GNUNET_new(type) (type *) GNUNET_malloc (sizeof (type))
681 * Wrapper around malloc. Allocates size bytes of memory.
682 * The memory will be zero'ed out.
684 * @param size the number of bytes to allocate, must be
685 * smaller than 40 MB.
686 * @return pointer to size bytes of memory, never NULL (!)
688 #define GNUNET_malloc(size) GNUNET_xmalloc_(size, __FILE__, __LINE__)
691 * Allocate and initialize a block of memory.
693 * @param buf data to initalize the block with
694 * @param size the number of bytes in buf (and size of the allocation)
695 * @return pointer to size bytes of memory, never NULL (!)
697 #define GNUNET_memdup(buf,size) GNUNET_xmemdup_(buf, size, __FILE__, __LINE__)
700 * Wrapper around malloc. Allocates size bytes of memory.
701 * The memory will be zero'ed out.
703 * @param size the number of bytes to allocate
704 * @return pointer to size bytes of memory, NULL if we do not have enough memory
706 #define GNUNET_malloc_large(size) GNUNET_xmalloc_unchecked_(size, __FILE__, __LINE__)
709 * Wrapper around realloc. Rellocates size bytes of memory.
711 * @param ptr the pointer to reallocate
712 * @param size the number of bytes to reallocate
713 * @return pointer to size bytes of memory
715 #define GNUNET_realloc(ptr, size) GNUNET_xrealloc_(ptr, size, __FILE__, __LINE__)
718 * Wrapper around free. Frees the memory referred to by ptr.
719 * Note that is is generally better to free memory that was
720 * allocated with GNUNET_array_grow using GNUNET_array_grow(mem, size, 0) instead of GNUNET_free.
722 * @param ptr location where to free the memory. ptr must have
723 * been returned by GNUNET_strdup, GNUNET_strndup, GNUNET_malloc or GNUNET_array_grow earlier.
725 #define GNUNET_free(ptr) GNUNET_xfree_(ptr, __FILE__, __LINE__)
728 * Free the memory pointed to by ptr if ptr is not NULL.
729 * Equivalent to if (ptr!=null)GNUNET_free(ptr).
731 * @param ptr the location in memory to free
733 #define GNUNET_free_non_null(ptr) do { void * __x__ = ptr; if (__x__ != NULL) { GNUNET_free(__x__); } } while(0)
736 * Wrapper around GNUNET_strdup. Makes a copy of the zero-terminated string
739 * @param a pointer to a zero-terminated string
740 * @return a copy of the string including zero-termination
742 #define GNUNET_strdup(a) GNUNET_xstrdup_(a,__FILE__,__LINE__)
745 * Wrapper around GNUNET_strndup. Makes a partial copy of the string
748 * @param a pointer to a string
749 * @param length of the string to duplicate
750 * @return a partial copy of the string including zero-termination
752 #define GNUNET_strndup(a,length) GNUNET_xstrndup_(a,length,__FILE__,__LINE__)
755 * Grow a well-typed (!) array. This is a convenience
756 * method to grow a vector <tt>arr</tt> of size <tt>size</tt>
757 * to the new (target) size <tt>tsize</tt>.
760 * Example (simple, well-typed stack):
763 * static struct foo * myVector = NULL;
764 * static int myVecLen = 0;
766 * static void push(struct foo * elem) {
767 * GNUNET_array_grow(myVector, myVecLen, myVecLen+1);
768 * memcpy(&myVector[myVecLen-1], elem, sizeof(struct foo));
771 * static void pop(struct foo * elem) {
772 * if (myVecLen == 0) die();
773 * memcpy(elem, myVector[myVecLen-1], sizeof(struct foo));
774 * GNUNET_array_grow(myVector, myVecLen, myVecLen-1);
778 * @param arr base-pointer of the vector, may be NULL if size is 0;
779 * will be updated to reflect the new address. The TYPE of
780 * arr is important since size is the number of elements and
781 * not the size in bytes
782 * @param size the number of elements in the existing vector (number
783 * of elements to copy over)
784 * @param tsize the target size for the resulting vector, use 0 to
785 * free the vector (then, arr will be NULL afterwards).
787 #define GNUNET_array_grow(arr,size,tsize) GNUNET_xgrow_((void**)&arr, sizeof(arr[0]), &size, tsize, __FILE__, __LINE__)
790 * Append an element to a list (growing the
793 #define GNUNET_array_append(arr,size,element) do { GNUNET_array_grow(arr,size,size+1); arr[size-1] = element; } while(0)
796 * Like snprintf, just aborts if the buffer is of insufficient size.
798 * @param buf pointer to buffer that is written to
799 * @param size number of bytes in buf
800 * @param format format strings
801 * @param ... data for format string
802 * @return number of bytes written to buf or negative value on error
805 GNUNET_snprintf (char *buf, size_t size, const char *format, ...);
809 * Like asprintf, just portable.
811 * @param buf set to a buffer of sufficient size (allocated, caller must free)
812 * @param format format string (see printf, fprintf, etc.)
813 * @param ... data for format string
814 * @return number of bytes in "*buf" excluding 0-termination
817 GNUNET_asprintf (char **buf, const char *format, ...);
820 /* ************** internal implementations, use macros above! ************** */
823 * Allocate memory. Checks the return value, aborts if no more
824 * memory is available. Don't use GNUNET_xmalloc_ directly. Use the
825 * GNUNET_malloc macro.
826 * The memory will be zero'ed out.
828 * @param size number of bytes to allocate
829 * @param filename where is this call being made (for debugging)
830 * @param linenumber line where this call is being made (for debugging)
831 * @return allocated memory, never NULL
834 GNUNET_xmalloc_ (size_t size, const char *filename, int linenumber);
838 * Allocate and initialize memory. Checks the return value, aborts if no more
839 * memory is available. Don't use GNUNET_xmemdup_ directly. Use the
840 * GNUNET_memdup macro.
842 * @param buf buffer to initialize from (must contain size bytes)
843 * @param size number of bytes to allocate
844 * @param filename where is this call being made (for debugging)
845 * @param linenumber line where this call is being made (for debugging)
846 * @return allocated memory, never NULL
849 GNUNET_xmemdup_ (const void *buf, size_t size, const char *filename,
854 * Allocate memory. This function does not check if the allocation
855 * request is within reasonable bounds, allowing allocations larger
856 * than 40 MB. If you don't expect the possibility of very large
857 * allocations, use GNUNET_malloc instead. The memory will be zero'ed
860 * @param size number of bytes to allocate
861 * @param filename where is this call being made (for debugging)
862 * @param linenumber line where this call is being made (for debugging)
863 * @return pointer to size bytes of memory, NULL if we do not have enough memory
866 GNUNET_xmalloc_unchecked_ (size_t size, const char *filename, int linenumber);
869 * Reallocate memory. Checks the return value, aborts if no more
870 * memory is available.
873 GNUNET_xrealloc_ (void *ptr, size_t n, const char *filename, int linenumber);
876 * Free memory. Merely a wrapper for the case that we
877 * want to keep track of allocations. Don't use GNUNET_xfree_
878 * directly. Use the GNUNET_free macro.
880 * @param ptr pointer to memory to free
881 * @param filename where is this call being made (for debugging)
882 * @param linenumber line where this call is being made (for debugging)
885 GNUNET_xfree_ (void *ptr, const char *filename, int linenumber);
889 * Dup a string. Don't call GNUNET_xstrdup_ directly. Use the GNUNET_strdup macro.
890 * @param str string to duplicate
891 * @param filename where is this call being made (for debugging)
892 * @param linenumber line where this call is being made (for debugging)
893 * @return the duplicated string
896 GNUNET_xstrdup_ (const char *str, const char *filename, int linenumber);
899 * Dup partially a string. Don't call GNUNET_xstrndup_ directly. Use the GNUNET_strndup macro.
901 * @param str string to duplicate
902 * @param len length of the string to duplicate
903 * @param filename where is this call being made (for debugging)
904 * @param linenumber line where this call is being made (for debugging)
905 * @return the duplicated string
908 GNUNET_xstrndup_ (const char *str, size_t len, const char *filename,
912 * Grow an array, the new elements are zeroed out.
913 * Grows old by (*oldCount-newCount)*elementSize
914 * bytes and sets *oldCount to newCount.
916 * Don't call GNUNET_xgrow_ directly. Use the GNUNET_array_grow macro.
918 * @param old address of the pointer to the array
920 * @param elementSize the size of the elements of the array
921 * @param oldCount address of the number of elements in the *old array
922 * @param newCount number of elements in the new array, may be 0 (then *old will be NULL afterwards)
923 * @param filename where is this call being made (for debugging)
924 * @param linenumber line where this call is being made (for debugging)
927 GNUNET_xgrow_ (void **old, size_t elementSize, unsigned int *oldCount,
928 unsigned int newCount, const char *filename, int linenumber);
932 * Create a copy of the given message.
934 * @param msg message to copy
935 * @return duplicate of the message
937 struct GNUNET_MessageHeader *
938 GNUNET_copy_message (const struct GNUNET_MessageHeader *msg);
941 #if __STDC_VERSION__ < 199901L
943 #define __func__ __FUNCTION__
945 #define __func__ "<unknown>"
952 #if 0 /* keep Emacsens' auto-indent happy */
962 #endif /*GNUNET_COMMON_H_ */