From: Martin Quinson Date: Fri, 26 Feb 2016 01:30:25 +0000 (+0100) Subject: asprintf is never used: bprintf is so swag X-Git-Tag: v3_13~669 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/e993852fadf35937e44a7b37eaecad48588b5a2e asprintf is never used: bprintf is so swag --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 5d6c4064d9..c2cd7594ff 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -319,14 +319,12 @@ CHECK_FUNCTION_EXISTS(getdtablesize HAVE_GETDTABLESIZE) CHECK_FUNCTION_EXISTS(sysconf HAVE_SYSCONF) CHECK_FUNCTION_EXISTS(popen HAVE_POPEN) -CHECK_SYMBOL_EXISTS(asprintf stdio.h HAVE_ASPRINTF) CHECK_SYMBOL_EXISTS(vasprintf stdio.h HAVE_VASPRINTF) if(MINGW) - # The detection of asprintf fails on MinGW, assumingly because it's + # The detection of vasprintf fails on MinGW, assumingly because it's # defined as an inline function in stdio.h instead of a regular # function. So force the result to be 1 despite of the test. - set(HAVE_ASPRINTF 1) set(HAVE_VASPRINTF 1) endif() @@ -747,25 +745,7 @@ endif() ## System checks ## -### check for asprintf function familly -if(HAVE_ASPRINTF) - SET(simgrid_need_asprintf "") - SET(NEED_ASPRINTF 0) -else() - SET(simgrid_need_asprintf "#define SIMGRID_NEED_ASPRINTF 1") - SET(NEED_ASPRINTF 1) -endif() - -if(HAVE_VASPRINTF) - SET(simgrid_need_vasprintf "") - SET(NEED_VASPRINTF 0) -else() - SET(simgrid_need_vasprintf "#define SIMGRID_NEED_VASPRINTF 1") - SET(NEED_VASPRINTF 1) -endif() - ### check for addr2line - find_path(ADDR2LINE NAMES addr2line PATHS NO_DEFAULT_PATHS ) if(ADDR2LINE) set(ADDR2LINE "${ADDR2LINE}/addr2line") diff --git a/include/simgrid_config.h.in b/include/simgrid_config.h.in index 7e118863d9..abc09ae935 100644 --- a/include/simgrid_config.h.in +++ b/include/simgrid_config.h.in @@ -56,14 +56,6 @@ /* Define to 1 if you have the header file. */ #cmakedefine HAVE_SYS_TIME_H @HAVE_SYS_TIME_H@ -/* Define to 1 if mmalloc is compiled in. */ -#cmakedefine HAVE_MMALLOC @HAVE_MMALLOC@ - -/* Get the config */ -#undef SIMGRID_NEED_ASPRINTF -#undef SIMGRID_NEED_VASPRINTF -@simgrid_need_asprintf@ -@simgrid_need_vasprintf@ /* Whether mallocators were enabled in ccmake or not. */ #define MALLOCATOR_COMPILED_IN @MALLOCATOR_IS_WANTED@ @@ -80,9 +72,13 @@ /* If __thread is available */ #cmakedefine HAVE_THREAD_LOCAL_STORAGE @HAVE_THREAD_LOCAL_STORAGE@ +/* Define to 1 if mmalloc is compiled in. */ +#cmakedefine HAVE_MMALLOC @HAVE_MMALLOC@ /* If Model-Checking support was requested */ #cmakedefine HAVE_MC @HAVE_MC@ #cmakedefine SIMGRID_HAVE_LIBSIG @SIMGRID_HAVE_LIBSIG@ +#cmakedefine HAVE_VASPRINTF @HAVE_VASPRINTF@ + #endif /* SIMGRID_PUBLIC_CONFIG_H */ diff --git a/include/xbt/misc.h b/include/xbt/misc.h index e1e454b210..6c7f6c9fb1 100644 --- a/include/xbt/misc.h +++ b/include/xbt/misc.h @@ -40,37 +40,15 @@ XBT_PUBLIC_DATA(xbt_dynar_t) sg_cmdline; /* snprintf related functions */ /** @addtogroup XBT_str * @{ */ -/** @brief print to allocated string (reimplemented when not provided by the system) - * - * The functions asprintf() and vasprintf() are analogues of - * sprintf() and vsprintf(), except that they allocate a string large - * enough to hold the output including the terminating null byte, and - * return a pointer to it via the first parameter. This pointer - * should be passed to free(3) to release the allocated storage when - * it is no longer needed. - */ -#if defined(SIMGRID_NEED_ASPRINTF)||defined(DOXYGEN) -XBT_PUBLIC(int) asprintf(char **ptr, const char *fmt, /*args */ - ...) XBT_ATTRIB_PRINTF(2, 3); -#endif -/** @brief print to allocated string (reimplemented when not provided by the system) - * - * See asprintf() - */ -#if defined(SIMGRID_NEED_VASPRINTF)||defined(DOXYGEN) -XBT_PUBLIC(int) vasprintf(char **ptr, const char *fmt, va_list ap); -#endif /** @brief print to allocated string * - * Works just like vasprintf(), but returns a pointer to the newly - * created string, or aborts on error. + * Similar to vasprintf(), but returns a pointer to the newly created string (or aborts on error). */ XBT_PUBLIC(char *) bvprintf(const char *fmt, va_list ap); /** @brief print to allocated string * - * Works just like asprintf(), but returns a pointer to the newly - * created string, or aborts on error. + * Similar to asprintf(), but returns a pointer to the newly created string (or aborts on error). */ XBT_PUBLIC(char *) bprintf(const char *fmt, ...) XBT_ATTRIB_PRINTF(1, 2); /** @} */ diff --git a/include/xbt/sysdep.h b/include/xbt/sysdep.h index 0d09d8a338..682e1e2713 100644 --- a/include/xbt/sysdep.h +++ b/include/xbt/sysdep.h @@ -56,15 +56,6 @@ XBT_LOG_NEW_CATEGORY(xbt, "All XBT categories (simgrid toolbox)"); XBT_LOG_EXTERNAL_CATEGORY(xbt); #endif -/* these ones live in str.h, but redeclare them here so that we do - not need to load the whole str.h and its heavy dependencies */ -#ifndef __USE_GNU /* do not redeclare existing headers */ -XBT_PUBLIC(int) asprintf(char **ptr, const char *fmt, /*args */ - ...) XBT_ATTRIB_PRINTF(2, 3); -XBT_PUBLIC(int) vasprintf(char **ptr, const char *fmt, va_list ap); -#endif -XBT_PUBLIC(char *) bprintf(const char *fmt, ...) XBT_ATTRIB_PRINTF(1, 2); - /** @addtogroup XBT_syscall * @brief Malloc and associated functions, killing the program on error (with \ref XBT_ex) * diff --git a/src/portable.h b/src/portable.h index 7e7acfbec7..570a599076 100644 --- a/src/portable.h +++ b/src/portable.h @@ -89,15 +89,7 @@ # include #endif -/* prototype of GNU functions */ -#if (defined(__GNUC__) && !defined(__cplusplus)) -XBT_PUBLIC(int) asprintf(char **ptr, const char *fmt, /*args */ ...); -XBT_PUBLIC(int) vasprintf(char **ptr, const char *fmt, va_list ap); -#endif - -/* - * What we need to extract the backtrace in exception handling code - */ +/* What we need to extract the backtrace in exception handling code */ #ifdef HAVE_EXECINFO_H # include #endif diff --git a/src/smpi/smpi_group.cpp b/src/smpi/smpi_group.cpp index 22fd814ff5..2c7bff8b37 100644 --- a/src/smpi/smpi_group.cpp +++ b/src/smpi/smpi_group.cpp @@ -84,21 +84,17 @@ void smpi_group_destroy(MPI_Group group) void smpi_group_set_mapping(MPI_Group group, int index, int rank) { - char * key; int * val_rank; if (rank < group->size) { group->rank_to_index_map[rank] = index; if (index!=MPI_UNDEFINED ) { val_rank = (int *) malloc(sizeof(int)); - *val_rank = rank; - int size = asprintf(&key, "%d", index); - if (size!=-1){ - xbt_dict_set(group->index_to_rank_map, key, val_rank, NULL); - free(key); - } else { - xbt_die("could not allocate memory for asprintf"); - } + *val_rank = rank; + + char * key = bprintf("%d", index); + xbt_dict_set(group->index_to_rank_map, key, val_rank, NULL); + free(key); } } } @@ -116,13 +112,10 @@ int smpi_group_index(MPI_Group group, int rank) int smpi_group_rank(MPI_Group group, int index) { int * ptr_rank = NULL; - char * key; - int size = asprintf(&key, "%d", index); - if (size!=-1){ - ptr_rank = static_cast(xbt_dict_get_or_null(group->index_to_rank_map, key)); - xbt_free(key); - }else - xbt_die("could not allocate memory for asprintf"); + char * key = bprintf("%d", index); + ptr_rank = static_cast(xbt_dict_get_or_null(group->index_to_rank_map, key)); + xbt_free(key); + if (!ptr_rank) return MPI_UNDEFINED; return *ptr_rank; diff --git a/src/smpi/smpi_replay.cpp b/src/smpi/smpi_replay.cpp index 4648703d05..1024d3decb 100644 --- a/src/smpi/smpi_replay.cpp +++ b/src/smpi/smpi_replay.cpp @@ -34,33 +34,28 @@ static void log_timed_action (const char *const *action, double clock){ } -static xbt_dynar_t get_reqq_self(){ - char * key; - - int size = asprintf(&key, "%d", smpi_process_index()); - if(size==-1) - xbt_die("could not allocate memory for asprintf"); +static xbt_dynar_t get_reqq_self() +{ + char * key = bprintf("%d", smpi_process_index()); xbt_dynar_t dynar_mpi_request = (xbt_dynar_t) xbt_dict_get(reqq, key); free(key); return dynar_mpi_request; } -static void set_reqq_self(xbt_dynar_t mpi_request){ - char * key; - - int size = asprintf(&key, "%d", smpi_process_index()); - if(size==-1) - xbt_die("could not allocate memory for asprintf"); +static void set_reqq_self(xbt_dynar_t mpi_request) +{ + char * key = bprintf("%d", smpi_process_index()); xbt_dict_set(reqq, key, mpi_request, free); free(key); } //allocate a single buffer for all sends, growing it if needed -void* smpi_get_tmp_sendbuffer(int size){ +void* smpi_get_tmp_sendbuffer(int size) +{ if (!smpi_process_get_replaying()) - return xbt_malloc(size); + return xbt_malloc(size); if (sendbuffer_size(xbt_realloc(sendbuffer,size)); sendbuffer_size=size; diff --git a/src/xbt/snprintf.c b/src/xbt/snprintf.c index 0f288432ff..5053d29e87 100644 --- a/src/xbt/snprintf.c +++ b/src/xbt/snprintf.c @@ -1,4 +1,4 @@ -/* Copyright (c) 2005-2010, 2012-2015. The SimGrid Team. +/* Copyright (c) 2005-2010, 2012-2016. The SimGrid Team. * All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it @@ -36,67 +36,7 @@ * - return value semantics per ISO/IEC 9899:1999 ("ISO C99"); * - written in standard ISO/ANSI C - requires an ANSI C compiler. * - * SUPPORTED CONVERSION SPECIFIERS AND DATA TYPES - * - * This snprintf only supports the following conversion specifiers: - * s, c, d, u, o, x, X, p (and synonyms: i, D, U, O - see below) - * with flags: '-', '+', ' ', '0' and '#'. - * An asterisk is supported for field width as well as precision. - * - * Length modifiers 'h' (short int), 'l' (long int), - * and 'll' (long long int) are supported. - * NOTE: - * If macro SNPRINTF_LONGLONG_SUPPORT is not defined (default) the - * length modifier 'll' is recognized but treated the same as 'l', - * which may cause argument value truncation! Defining - * SNPRINTF_LONGLONG_SUPPORT requires that your system's sprintf also - * handles length modifier 'll'. long long int is a language extension - * which may not be portable. - * - * Conversion of numeric data (conversion specifiers d, u, o, x, X, p) - * with length modifiers (none or h, l, ll) is left to the system routine - * sprintf, but all handling of flags, field width and precision as well as - * c and s conversions is done very carefully by this portable routine. - * If a string precision (truncation) is specified (e.g. %.8s) it is - * guaranteed the string beyond the specified precision will not be referenced. - * - * Length modifiers h, l and ll are ignored for c and s conversions (data - * types wint_t and wchar_t are not supported). - * - * The following common synonyms for conversion characters are supported: - * - i is a synonym for d - * - D is a synonym for ld, explicit length modifiers are ignored - * - U is a synonym for lu, explicit length modifiers are ignored - * - O is a synonym for lo, explicit length modifiers are ignored - * The D, O and U conversion characters are nonstandard, they are supported - * for backward compatibility only, and should not be used for new code. - * - * The following is specifically NOT supported: - * - flag ' (thousands' grouping character) is recognized but ignored - * - numeric conversion specifiers: f, e, E, g, G and synonym F, - * as well as the new a and A conversion specifiers - * - length modifier 'L' (long double) and 'q' (quad - use 'll' instead) - * - wide character/string conversions: lc, ls, and nonstandard - * synonyms C and S - * - writeback of converted string length: conversion character n - * - the n$ specification for direct reference to n-th argument - * - locales - * - * It is permitted for str_m to be zero, and it is permitted to specify NULL - * pointer for resulting string argument if str_m is zero (as per ISO C99). - * - * The return value is the number of characters which would be generated - * for the given input, excluding the trailing null. If this value - * is greater or equal to str_m, not all characters from the result - * have been stored in str, output bytes beyond the (str_m-1) -th character - * are discarded. If str_m is greater than zero it is guaranteed - * the resulting string will be null-terminated. - * - * NOTE that this matches the ISO C99, OpenBSD, and GNU C library 2.1, - * but is different from some older and vendor implementations, - * and is also different from XPG, XSH5, SUSv2 specifications. - * For historical discussion on changes in the semantics and standards - * of snprintf see printf(3) man page in the Linux programmers manual. + * [...] * * Routines asprintf and vasprintf return a pointer (in the ptr argument) * to a buffer sufficiently large to hold the resulting string. This pointer @@ -105,176 +45,16 @@ * will return -1 and set ptr to be a NULL pointer. These two routines are a * GNU C library extensions (glibc). * - * Routines asnprintf and vasnprintf are similar to asprintf and vasprintf, - * yet, like snprintf and vsnprintf counterparts, will write at most str_m-1 - * characters into the allocated output string, the last character in the - * allocated buffer then gets the terminating null. If the formatted string - * length (the return value) is greater than or equal to the str_m argument, - * the resulting string was truncated and some of the formatted characters - * were discarded. These routines present a handy way to limit the amount - * of allocated memory to some sane value. - * * AVAILABILITY * http://www.ijs.si/software/snprintf/ - * - * REVISION HISTORY - * 1999-04 V0.9 Mark Martinec - * - initial version, some modifications after comparing printf - * man pages for Digital Unix 4.0, Solaris 2.6 and HPUX 10, - * and checking how Perl handles sprintf (differently!); - * 1999-04-09 V1.0 Mark Martinec - * - added main test program, fixed remaining inconsistencies, - * added optional (long long int) support; - * 1999-04-12 V1.1 Mark Martinec - * - support the 'p' conversion (pointer to void); - * - if a string precision is specified - * make sure the string beyond the specified precision - * will not be referenced (e.g. by strlen); - * 1999-04-13 V1.2 Mark Martinec - * - support synonyms %D=%ld, %U=%lu, %O=%lo; - * - speed up the case of long format string with few conversions; - * 1999-06-30 V1.3 Mark Martinec - * - fixed runaway loop (eventually crashing when str_l wraps - * beyond 2^31) while copying format string without - * conversion specifiers to a buffer that is too short - * (thanks to Edwin Young for - * spotting the problem); - * - added macros PORTABLE_SNPRINTF_VERSION_(MAJOR|MINOR) - * to snprintf.h - * 2000-02-14 V2.0 (never released) Mark Martinec - * - relaxed license terms: The Artistic License now applies. - * You may still apply the GNU GENERAL PUBLIC LICENSE - * as was distributed with previous versions, if you prefer; - * - changed REVISION HISTORY dates to use ISO 8601 date format; - * - added vsnprintf (patch also independently proposed by - * Caolan McNamara 2000-05-04, and Keith M Willenson 2000-06-01) - * 2000-06-27 V2.1 Mark Martinec - * - removed POSIX check for str_m<1; value 0 for str_m is - * allowed by ISO C99 (and GNU C library 2.1) - (pointed out - * on 2000-05-04 by Caolan McNamara, caolan@ csn dot ul dot ie). - * Besides relaxed license this change in standards adherence - * is the main reason to bump up the major version number; - * - added nonstandard routines asnprintf, vasnprintf, asprintf, - * vasprintf that dynamically allocate storage for the - * resulting string; these routines are not compiled by default, - * see comments where NEED_V?ASN?PRINTF macros are defined; - * - autoconf contributed by Caolan McNamara - * 2000-10-06 V2.2 Mark Martinec - * - BUG FIX: the %c conversion used a temporary variable - * that was no longer in scope when referenced, - * possibly causing incorrect resulting character; - * - BUG FIX: make precision and minimal field width unsigned - * to handle huge values (2^31 <= n < 2^32) correctly; - * also be more careful in the use of signed/unsigned/size_t - * internal variables - probably more careful than many - * vendor implementations, but there may still be a case - * where huge values of str_m, precision or minimal field - * could cause incorrect behaviour; - * - use separate variables for signed/unsigned arguments, - * and for short/int, long, and long long argument lengths - * to avoid possible incompatibilities on certain - * computer architectures. Also use separate variable - * arg_sign to hold sign of a numeric argument, - * to make code more transparent; - * - some fiddling with zero padding and "0x" to make it - * Linux compatible; - * - systematically use macros fast_memcpy and fast_memset - * instead of case-by-case hand optimization; determine some - * breakeven string lengths for different architectures; - * - terminology change: 'format' -> 'conversion specifier', - * 'C9x' -> 'ISO/IEC 9899:1999 ("ISO C99")', - * 'alternative form' -> 'alternate form', - * 'data type modifier' -> 'length modifier'; - * - several comments rephrased and new ones added; - * - make compiler not complain about 'credits' defined but - * not used; */ -/* Define NEED_V?ASN?PRINTF macros if you need library extension - * routines asprintf, vasprintf, asnprintf, vasnprintf respectively, - * and your system library does not provide them. They are all small - * wrapper routines around portable_vsnprintf. Defining any of the four - * NEED_V?ASN?PRINTF macros automatically turns off NEED_SNPRINTF_ONLY - * and turns on PREFER_PORTABLE_SNPRINTF. - * - * Watch for name conflicts with the system library if these routines - * are already present there. - * - * NOTE: vasprintf and vasnprintf routines need va_copy() from stdarg.h, as - * specified by C99, to be able to traverse the same list of arguments twice. - * I don't know of any other standard and portable way of achieving the same. - * With some versions of gcc you may use __va_copy(). You might even get away - * with "ap2 = ap", in this case you must not call va_end(ap2) ! - * #define va_copy(ap2,ap) ap2 = ap - */ -/* #define NEED_ASPRINTF */ -/* #define NEED_VASPRINTF */ - - -/* ============================================= */ -/* NO USER SERVICABLE PARTS FOLLOWING THIS POINT */ -/* ============================================= */ - -#include "src/portable.h" /* to get a working stdarg.h */ +#include "xbt/sysdep.h" /* xbt_abort() */ +#include "simgrid_config.h" /* Do we need vasprintf? */ -#include -#include -#include -#include -#include - -#include -#include -#include "xbt/str.h" - -#if defined(NEED_ASPRINTF) -int asprintf(char **ptr, const char *fmt, /*args */ ...); -#endif -#if defined(NEED_VASPRINTF) +#if !defined(HAVE_VASPRINTF) +#include /* vsnprintf */ int vasprintf(char **ptr, const char *fmt, va_list ap); -#endif - - - /* FIXME: better place */ -#include "xbt/sysdep.h" - -/* declarations */ - -/* - Old copyright - snprintf.c, v2.2: Mark Martinec, - snprintf.c, v2.2: Copyright 1999, Mark Martinec. Frontier Artistic License applies. - snprintf.c, v2.2: http://www.ijs.si/software/snprintf -*/ - -#if defined(NEED_ASPRINTF) -int asprintf(char **ptr, const char *fmt, /*args */ ...) -{ - va_list ap; - size_t str_m; - int str_l; - - *ptr = NULL; - va_start(ap, fmt); /* measure the required size */ - str_l = portable_vsnprintf(NULL, (size_t) 0, fmt, ap); - va_end(ap); - assert(str_l >= 0); /* possible integer overflow if str_m > INT_MAX */ - *ptr = (char *) xbt_malloc(str_m = (size_t) str_l + 1); - if (*ptr == NULL) { - errno = ENOMEM; - str_l = -1; - } else { - int str_l2; - va_start(ap, fmt); - str_l2 = portable_vsnprintf(*ptr, str_m, fmt, ap); - va_end(ap); - assert(str_l2 == str_l); - } - return str_l; -} -#endif - -#if defined(NEED_VASPRINTF) int vasprintf(char **ptr, const char *fmt, va_list ap) { size_t str_m; @@ -287,15 +67,12 @@ int vasprintf(char **ptr, const char *fmt, va_list ap) str_l = vsnprintf(NULL, (size_t) 0, fmt, ap2); /*get required size */ va_end(ap2); } - assert(str_l >= 0); /* possible integer overflow if str_m > INT_MAX */ + xbt_assert(str_l >= 0); /* possible integer overflow if str_m > INT_MAX */ *ptr = (char *) xbt_malloc(str_m = (size_t) str_l + 1); - if (*ptr == NULL) { - errno = ENOMEM; - str_l = -1; - } else { - int str_l2 = vsnprintf(*ptr, str_m, fmt, ap); - assert(str_l2 == str_l); - } + + int str_l2 = vsnprintf(*ptr, str_m, fmt, ap); + assert(str_l2 == str_l); + return str_l; } #endif @@ -316,10 +93,9 @@ char *bvprintf(const char *fmt, va_list ap) char *bprintf(const char *fmt, ...) { va_list ap; - char *res; va_start(ap, fmt); - res = bvprintf(fmt, ap); + char *res = bvprintf(fmt, ap); va_end(ap); return res; } diff --git a/tools/cmake/PrintArgs.cmake b/tools/cmake/PrintArgs.cmake index 1727555e40..e991a291c5 100644 --- a/tools/cmake/PrintArgs.cmake +++ b/tools/cmake/PrintArgs.cmake @@ -5,8 +5,6 @@ if(enable_print_message) message("CMAKE_HOME_DIRECTORY ........: ${CMAKE_HOME_DIRECTORY}") message("") message("PTH_STACKGROWTH .............: ${PTH_STACKGROWTH}") - message("need_asprintf ...............: ${simgrid_need_asprintf}") - message("need_vasprintf ..............: ${simgrid_need_vasprintf}") message("HAVE_VA_COPY ................: ${HAVE_VA_COPY}") message("") message("\#define pth_skaddr_makecontext(skaddr,sksize) (${makecontext_addr})") @@ -39,7 +37,6 @@ if(enable_print_message) message("HAVE_SYSCONF ................: ${HAVE_SYSCONF}") message("HAVE_POPEN ..................: ${HAVE_POPEN}") message("HAVE_MAKECONTEXT ............: ${HAVE_MAKECONTEXT}") - message("HAVE_ASPRINTF ...............: ${HAVE_ASPRINTF}") message("HAVE_VASPRINTF ..............: ${HAVE_VASPRINTF}") message("HAVE_MMAP ...................: ${HAVE_MMAP}") message("HAVE_PRIVATIZATION ..........: ${HAVE_PRIVATIZATION}") diff --git a/tools/cmake/src/internal_config.h.in b/tools/cmake/src/internal_config.h.in index 4cff92476e..25d4c5ef86 100644 --- a/tools/cmake/src/internal_config.h.in +++ b/tools/cmake/src/internal_config.h.in @@ -160,9 +160,6 @@ /* Define to 1 if you have the header file. */ #cmakedefine HAVE_WINDOWS_H @HAVE_WINDOWS_H@ -/* enable the asprintf replacement */ -#cmakedefine NEED_ASPRINTF @NEED_ASPRINTF@ - /* enable the vasprintf replacement */ #cmakedefine NEED_VASPRINTF @NEED_VASPRINTF@