root/maint/gnulib/tests/test-nonblocking-misc.h

/* [previous][next][first][last][top][bottom][index][help] */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. init_data
  2. dbgfprintf
  3. dbgstrerror

   1 /* Test for nonblocking read and write.
   2 
   3    Copyright (C) 2011-2021 Free Software Foundation, Inc.
   4 
   5    This program is free software: you can redistribute it and/or modify
   6    it under the terms of the GNU General Public License as published by
   7    the Free Software Foundation; either version 3 of the License, or
   8    (at your option) any later version.
   9 
  10    This program is distributed in the hope that it will be useful,
  11    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13    GNU General Public License for more details.
  14 
  15    You should have received a copy of the GNU General Public License
  16    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
  17 
  18 /* Whether to print debugging messages.  */
  19 #define ENABLE_DEBUGGING 0
  20 
  21 /* Delay (in microseconds) to sleep when write() or read() returned -1 with
  22    errno = EAGAIN.  */
  23 #define SMALL_DELAY 10000
  24 
  25 /* Return a memory area, filled with the data to be transferred.  */
  26 static unsigned char *
  27 init_data (size_t data_block_size)
     /* [previous][next][first][last][top][bottom][index][help] */
  28 {
  29   unsigned char *data;
  30   unsigned int i;
  31 
  32   data = (unsigned char *) malloc (2 * data_block_size);
  33   ASSERT (data != NULL);
  34 
  35   for (i = 0; i < 2 * data_block_size; i++)
  36     data[i] = (unsigned char) (i * i + (7 * i) % 61 + 4);
  37 
  38   return data;
  39 }
  40 
  41 #if ENABLE_DEBUGGING
  42 # include <stdarg.h>
  43 static int dbgfprintf (FILE *fp, const char *format, ...)
     /* [previous][next][first][last][top][bottom][index][help] */
  44                       _GL_ATTRIBUTE_FORMAT_PRINTF_STANDARD (2, 3);
  45 static int
  46 dbgfprintf (FILE *fp, const char *format, ...)
  47 {
  48   /* Accumulate the entire line in a buffer, so that the output on fp
  49      is done atomically.  */
  50   char line[1024];
  51   size_t line_len;
  52   struct timeval current_time;
  53   va_list args;
  54   int ret;
  55 
  56   line_len = 0;
  57   gettimeofday (&current_time, NULL);
  58   ret = snprintf (line, sizeof (line), "%.6f ",
  59                   current_time.tv_sec + (double) current_time.tv_usec * 1e-6);
  60   if (ret < 0)
  61     return -1;
  62   line_len = strlen (line);
  63 
  64   va_start (args, format);
  65   ret = vsnprintf (line + line_len, sizeof (line) - line_len, format, args);
  66   va_end (args);
  67   if (ret < 0)
  68     return -1;
  69   line_len += strlen (line + line_len);
  70 
  71   ret = fwrite (line, 1, line_len, fp);
  72 
  73   /* Make sure the debugging information is output, so that the order of the
  74      messages reflects the timeline of events, and so that the output is not
  75      lost if the program crashes afterwards (relevant on mingw).  */
  76   fflush (fp);
  77   return ret;
  78 }
  79 #else
  80 # define dbgfprintf if (1) ; else fprintf
  81 #endif
  82 
  83 /* Return a textual description of the error code ERR, if FAILED is true.
  84    Return an empty string if FAILED is false.  */
  85 static const char *
  86 dbgstrerror (bool failed, int err)
     /* [previous][next][first][last][top][bottom][index][help] */
  87 {
  88   static char buf[256];
  89   if (failed)
  90     {
  91       sprintf (buf, " %d %s", err, strerror (err));
  92       return buf;
  93     }
  94   else
  95     return "";
  96 }
  97 
  98 #define TIMING_DECLS \
  99   struct timeval before_time; \
 100   struct timeval after_time; \
 101   double spent_time;
 102 #define START_TIMING \
 103   gettimeofday (&before_time, NULL);
 104 #define END_TIMING \
 105   gettimeofday (&after_time, NULL); \
 106   spent_time = \
 107     (after_time.tv_sec - before_time.tv_sec) \
 108     + ((double) after_time.tv_usec - (double) before_time.tv_usec) * 1e-6;

/* [previous][next][first][last][top][bottom][index][help] */