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 0x00090200
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 512-bit hashcode
243 struct GNUNET_HashCode
245 uint32_t bits[512 / 8 / sizeof (uint32_t)]; /* = 16 */
251 * @brief 256-bit hashcode
253 struct GNUNET_CRYPTO_ShortHashCode
255 uint32_t bits[256 / 8 / sizeof (uint32_t)]; /* = 8 */
260 * The identity of the host (basically the SHA-512 hashcode of
263 struct GNUNET_PeerIdentity
265 struct GNUNET_HashCode hashPubKey;
267 GNUNET_NETWORK_STRUCT_END
270 * Function called with a filename.
273 * @param filename complete filename (absolute path)
274 * @return GNUNET_OK to continue to iterate,
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);
328 * @param kind how serious is the error?
329 * @param message what is the message (format string)
330 * @param ... arguments for format string
333 GNUNET_log_nocheck (enum GNUNET_ErrorType kind, const char *message, ...);
336 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
337 #define _GNUNET_BOOLEAN_EXPR(expr) \
339 int _gnunet_boolean_var_; \
341 _gnunet_boolean_var_ = 1; \
343 _gnunet_boolean_var_ = 0; \
344 _gnunet_boolean_var_; \
346 #define GN_LIKELY(expr) (__builtin_expect (_GNUNET_BOOLEAN_EXPR(expr), 1))
347 #define GN_UNLIKELY(expr) (__builtin_expect (_GNUNET_BOOLEAN_EXPR(expr), 0))
349 #define GN_LIKELY(expr) (expr)
350 #define GN_UNLIKELY(expr) (expr)
353 #if !defined(GNUNET_LOG_CALL_STATUS)
354 #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(...)
403 * Abort the process, generate a core dump if possible.
406 GNUNET_abort (void) GNUNET_NORETURN;
409 * Ignore the next n calls to the log function.
411 * @param n number of log calls to ignore (could be negative)
412 * @param check_reset GNUNET_YES to assert that the log skip counter is currently zero
415 GNUNET_log_skip (int n, int check_reset);
421 * @param comp default component to use
422 * @param loglevel what types of messages should be logged
423 * @param logfile change logging to logfile (use NULL to keep stderr)
424 * @return GNUNET_OK on success, GNUNET_SYSERR if logfile could not be opened
427 GNUNET_log_setup (const char *comp, const char *loglevel, const char *logfile);
431 * Add a custom logger.
433 * @param logger log function
434 * @param logger_cls closure for logger
437 GNUNET_logger_add (GNUNET_Logger logger, void *logger_cls);
441 * Remove a custom logger.
443 * @param logger log function
444 * @param logger_cls closure for logger
447 GNUNET_logger_remove (GNUNET_Logger logger, void *logger_cls);
451 * Convert a short hash value to a string (for printing debug messages).
452 * This is one of the very few calls in the entire API that is
455 * @param hc the short hash code
459 GNUNET_short_h2s (const struct GNUNET_CRYPTO_ShortHashCode * hc);
463 * Convert a short hash value to a string (for printing debug messages).
464 * This prints all 104 characters of a hashcode!
465 * This is one of the very few calls in the entire API that is
468 * @param hc the short hash code
472 GNUNET_short_h2s_full (const struct GNUNET_CRYPTO_ShortHashCode * hc);
476 * Convert a hash value to a string (for printing debug messages).
477 * This is one of the very few calls in the entire API that is
480 * @param hc the hash code
484 GNUNET_h2s (const struct GNUNET_HashCode * hc);
488 * Convert a hash value to a string (for printing debug messages).
489 * This prints all 104 characters of a hashcode!
490 * This is one of the very few calls in the entire API that is
493 * @param hc the hash code
497 GNUNET_h2s_full (const struct GNUNET_HashCode * hc);
501 * Convert a peer identity to a string (for printing debug messages).
502 * This is one of the very few calls in the entire API that is
505 * @param pid the peer identity
506 * @return string form of the pid; will be overwritten by next
507 * call to GNUNET_i2s.
510 GNUNET_i2s (const struct GNUNET_PeerIdentity *pid);
513 * Convert a peer identity to a string (for printing debug messages).
514 * This is one of the very few calls in the entire API that is
517 * @param pid the peer identity
518 * @return string form of the pid; will be overwritten by next
519 * call to GNUNET_i2s.
522 GNUNET_i2s_full (const struct GNUNET_PeerIdentity *pid);
525 * Convert a "struct sockaddr*" (IPv4 or IPv6 address) to a string
526 * (for printing debug messages). This is one of the very few calls
527 * in the entire API that is NOT reentrant!
529 * @param addr the address
530 * @param addrlen the length of the address
531 * @return nicely formatted string for the address
532 * will be overwritten by next call to GNUNET_a2s.
535 GNUNET_a2s (const struct sockaddr *addr, socklen_t addrlen);
538 * Convert error type to string.
540 * @param kind type to convert
541 * @return string corresponding to the type
544 GNUNET_error_type_to_string (enum GNUNET_ErrorType kind);
548 * Use this for fatal errors that cannot be handled
550 #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)
553 * Use this for fatal errors that cannot be handled
555 #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)
558 * Use this for internal assertion violations that are
559 * not fatal (can be handled) but should not occur.
561 #define GNUNET_break(cond) do { if (! (cond)) { GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Assertion failed at %s:%d.\n"), __FILE__, __LINE__); } } while(0)
564 * Use this for assertion violations caused by other
565 * peers (i.e. protocol violations). We do not want to
566 * confuse end-users (say, some other peer runs an
567 * older, broken or incompatible GNUnet version), but
568 * we still want to see these problems during
569 * development and testing. "OP == other peer".
571 #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)
574 * Log an error message at log-level 'level' that indicates
575 * a failure of the command 'cmd' with the message given
576 * by strerror(errno).
578 #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)
581 * Log an error message at log-level 'level' that indicates
582 * a failure of the command 'cmd' with the message given
583 * by strerror(errno).
585 #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)
588 * Log an error message at log-level 'level' that indicates
589 * a failure of the command 'cmd' with the message given
590 * by strerror(errno).
592 #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)
595 * Log an error message at log-level 'level' that indicates
596 * a failure of the command 'cmd' with the message given
597 * by strerror(errno).
599 #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)
601 /* ************************* endianess conversion ****************** */
604 * Convert unsigned 64-bit integer to host-byte-order.
605 * @param n the value in network byte order
606 * @return the same value in host byte order
609 GNUNET_ntohll (uint64_t n);
612 * Convert unsigned 64-bit integer to network-byte-order.
613 * @param n the value in host byte order
614 * @return the same value in network byte order
617 GNUNET_htonll (uint64_t n);
620 * Convert double to network-byte-order.
621 * @param d the value in network byte order
622 * @return the same value in host byte order
625 GNUNET_hton_double (double d);
628 * Convert double to host-byte-order
629 * @param d the value in network byte order
630 * @return the same value in host byte order
633 GNUNET_ntoh_double (double d);
635 /* ************************* allocation functions ****************** */
638 * Maximum allocation with GNUNET_malloc macro.
640 #define GNUNET_MAX_MALLOC_CHECKED (1024 * 1024 * 40)
643 * Wrapper around malloc. Allocates size bytes of memory.
644 * The memory will be zero'ed out.
646 * @param size the number of bytes to allocate, must be
647 * smaller than 40 MB.
648 * @return pointer to size bytes of memory, never NULL (!)
650 #define GNUNET_malloc(size) GNUNET_xmalloc_(size, __FILE__, __LINE__)
653 * Allocate and initialize a block of memory.
655 * @param buf data to initalize the block with
656 * @param size the number of bytes in buf (and size of the allocation)
657 * @return pointer to size bytes of memory, never NULL (!)
659 #define GNUNET_memdup(buf,size) GNUNET_xmemdup_(buf, size, __FILE__, __LINE__)
662 * Wrapper around malloc. Allocates size bytes of memory.
663 * The memory will be zero'ed out.
665 * @param size the number of bytes to allocate
666 * @return pointer to size bytes of memory, NULL if we do not have enough memory
668 #define GNUNET_malloc_large(size) GNUNET_xmalloc_unchecked_(size, __FILE__, __LINE__)
671 * Wrapper around realloc. Rellocates size bytes of memory.
673 * @param ptr the pointer to reallocate
674 * @param size the number of bytes to reallocate
675 * @return pointer to size bytes of memory
677 #define GNUNET_realloc(ptr, size) GNUNET_xrealloc_(ptr, size, __FILE__, __LINE__)
680 * Wrapper around free. Frees the memory referred to by ptr.
681 * Note that is is generally better to free memory that was
682 * allocated with GNUNET_array_grow using GNUNET_array_grow(mem, size, 0) instead of GNUNET_free.
684 * @param ptr location where to free the memory. ptr must have
685 * been returned by GNUNET_strdup, GNUNET_strndup, GNUNET_malloc or GNUNET_array_grow earlier.
687 #define GNUNET_free(ptr) GNUNET_xfree_(ptr, __FILE__, __LINE__)
690 * Free the memory pointed to by ptr if ptr is not NULL.
691 * Equivalent to if (ptr!=null)GNUNET_free(ptr).
693 * @param ptr the location in memory to free
695 #define GNUNET_free_non_null(ptr) do { void * __x__ = ptr; if (__x__ != NULL) { GNUNET_free(__x__); } } while(0)
698 * Wrapper around GNUNET_strdup. Makes a copy of the zero-terminated string
701 * @param a pointer to a zero-terminated string
702 * @return a copy of the string including zero-termination
704 #define GNUNET_strdup(a) GNUNET_xstrdup_(a,__FILE__,__LINE__)
707 * Wrapper around GNUNET_strndup. Makes a partial copy of the string
710 * @param a pointer to a string
711 * @param length of the string to duplicate
712 * @return a partial copy of the string including zero-termination
714 #define GNUNET_strndup(a,length) GNUNET_xstrndup_(a,length,__FILE__,__LINE__)
717 * Grow a well-typed (!) array. This is a convenience
718 * method to grow a vector <tt>arr</tt> of size <tt>size</tt>
719 * to the new (target) size <tt>tsize</tt>.
722 * Example (simple, well-typed stack):
725 * static struct foo * myVector = NULL;
726 * static int myVecLen = 0;
728 * static void push(struct foo * elem) {
729 * GNUNET_array_grow(myVector, myVecLen, myVecLen+1);
730 * memcpy(&myVector[myVecLen-1], elem, sizeof(struct foo));
733 * static void pop(struct foo * elem) {
734 * if (myVecLen == 0) die();
735 * memcpy(elem, myVector[myVecLen-1], sizeof(struct foo));
736 * GNUNET_array_grow(myVector, myVecLen, myVecLen-1);
740 * @param arr base-pointer of the vector, may be NULL if size is 0;
741 * will be updated to reflect the new address. The TYPE of
742 * arr is important since size is the number of elements and
743 * not the size in bytes
744 * @param size the number of elements in the existing vector (number
745 * of elements to copy over)
746 * @param tsize the target size for the resulting vector, use 0 to
747 * free the vector (then, arr will be NULL afterwards).
749 #define GNUNET_array_grow(arr,size,tsize) GNUNET_xgrow_((void**)&arr, sizeof(arr[0]), &size, tsize, __FILE__, __LINE__)
752 * Append an element to a list (growing the
755 #define GNUNET_array_append(arr,size,element) do { GNUNET_array_grow(arr,size,size+1); arr[size-1] = element; } while(0)
758 * Like snprintf, just aborts if the buffer is of insufficient size.
760 * @param buf pointer to buffer that is written to
761 * @param size number of bytes in buf
762 * @param format format strings
763 * @param ... data for format string
764 * @return number of bytes written to buf or negative value on error
767 GNUNET_snprintf (char *buf, size_t size, const char *format, ...);
771 * Like asprintf, just portable.
773 * @param buf set to a buffer of sufficient size (allocated, caller must free)
774 * @param format format string (see printf, fprintf, etc.)
775 * @param ... data for format string
776 * @return number of bytes in "*buf" excluding 0-termination
779 GNUNET_asprintf (char **buf, const char *format, ...);
782 /* ************** internal implementations, use macros above! ************** */
785 * Allocate memory. Checks the return value, aborts if no more
786 * memory is available. Don't use GNUNET_xmalloc_ directly. Use the
787 * GNUNET_malloc macro.
788 * The memory will be zero'ed out.
790 * @param size number of bytes to allocate
791 * @param filename where is this call being made (for debugging)
792 * @param linenumber line where this call is being made (for debugging)
793 * @return allocated memory, never NULL
796 GNUNET_xmalloc_ (size_t size, const char *filename, int linenumber);
800 * Allocate and initialize memory. Checks the return value, aborts if no more
801 * memory is available. Don't use GNUNET_xmemdup_ directly. Use the
802 * GNUNET_memdup macro.
804 * @param buf buffer to initialize from (must contain size bytes)
805 * @param size number of bytes to allocate
806 * @param filename where is this call being made (for debugging)
807 * @param linenumber line where this call is being made (for debugging)
808 * @return allocated memory, never NULL
811 GNUNET_xmemdup_ (const void *buf, size_t size, const char *filename,
816 * Allocate memory. This function does not check if the allocation
817 * request is within reasonable bounds, allowing allocations larger
818 * than 40 MB. If you don't expect the possibility of very large
819 * allocations, use GNUNET_malloc instead. The memory will be zero'ed
822 * @param size number of bytes to allocate
823 * @param filename where is this call being made (for debugging)
824 * @param linenumber line where this call is being made (for debugging)
825 * @return pointer to size bytes of memory, NULL if we do not have enough memory
828 GNUNET_xmalloc_unchecked_ (size_t size, const char *filename, int linenumber);
831 * Reallocate memory. Checks the return value, aborts if no more
832 * memory is available.
835 GNUNET_xrealloc_ (void *ptr, size_t n, const char *filename, int linenumber);
838 * Free memory. Merely a wrapper for the case that we
839 * want to keep track of allocations. Don't use GNUNET_xfree_
840 * directly. Use the GNUNET_free macro.
842 * @param ptr pointer to memory to free
843 * @param filename where is this call being made (for debugging)
844 * @param linenumber line where this call is being made (for debugging)
847 GNUNET_xfree_ (void *ptr, const char *filename, int linenumber);
851 * Dup a string. Don't call GNUNET_xstrdup_ directly. Use the GNUNET_strdup macro.
852 * @param str string to duplicate
853 * @param filename where is this call being made (for debugging)
854 * @param linenumber line where this call is being made (for debugging)
855 * @return the duplicated string
858 GNUNET_xstrdup_ (const char *str, const char *filename, int linenumber);
861 * Dup partially a string. Don't call GNUNET_xstrndup_ directly. Use the GNUNET_strndup macro.
863 * @param str string to duplicate
864 * @param len length of the string to duplicate
865 * @param filename where is this call being made (for debugging)
866 * @param linenumber line where this call is being made (for debugging)
867 * @return the duplicated string
870 GNUNET_xstrndup_ (const char *str, size_t len, const char *filename,
874 * Grow an array, the new elements are zeroed out.
875 * Grows old by (*oldCount-newCount)*elementSize
876 * bytes and sets *oldCount to newCount.
878 * Don't call GNUNET_xgrow_ directly. Use the GNUNET_array_grow macro.
880 * @param old address of the pointer to the array
882 * @param elementSize the size of the elements of the array
883 * @param oldCount address of the number of elements in the *old array
884 * @param newCount number of elements in the new array, may be 0 (then *old will be NULL afterwards)
885 * @param filename where is this call being made (for debugging)
886 * @param linenumber line where this call is being made (for debugging)
889 GNUNET_xgrow_ (void **old, size_t elementSize, unsigned int *oldCount,
890 unsigned int newCount, const char *filename, int linenumber);
894 * Create a copy of the given message.
896 * @param msg message to copy
897 * @return duplicate of the message
899 struct GNUNET_MessageHeader *
900 GNUNET_copy_message (const struct GNUNET_MessageHeader *msg);
903 #if __STDC_VERSION__ < 199901L
905 #define __func__ __FUNCTION__
907 #define __func__ "<unknown>"
914 #if 0 /* keep Emacsens' auto-indent happy */
924 #endif /*GNUNET_COMMON_H_ */