Ticket #4233: 0002-Add-exact-addition-and-summation-functions-for-size_.patch

File 0002-Add-exact-addition-and-summation-functions-for-size_.patch, 4.0 KB (added by mansour, 8 years ago)
  • src/common/util.c

    From 1a13a02c7517aa0f6dca396b87620b1e9501b71f Mon Sep 17 00:00:00 2001
    From: Mansour Moufid <mansourmoufid@gmail.com>
    Date: Thu, 13 Oct 2011 16:57:09 -0400
    Subject: [PATCH 2/3] Add exact addition and summation functions for size_t.
    
    Implement exact addition and summation functions for the size_t type
    (`tor_add_sizet' and `tor_sum_sizet' respectively)
    in the file `util.c'; documented in `util.h'.
    ---
     src/common/util.c |   38 ++++++++++++++++++++++++++++
     src/common/util.h |   72 +++++++++++++++++++++++++++++++++++++++++++++++++++++
     2 files changed, 110 insertions(+), 0 deletions(-)
    
    diff --git a/src/common/util.c b/src/common/util.c
    index b8e76bd..46268d8 100644
    a b n_bits_set_u8(uint8_t v) 
    443443  return nybble_table[v & 15] + nybble_table[v>>4];
    444444}
    445445
     446/* Safely add two \c size_t type arguments. */
     447int
     448tor_add_sizet(size_t *const sump, const size_t a, const size_t b)
     449{
     450  tor_assert(sump != NULL);
     451  *sump = 0;
     452  if (a <= SIZE_T_MAX - b) {
     453    /* success */
     454    *sump = a+b;
     455    return 0;
     456  }
     457  return 1;
     458}
     459
     460/* Safely sum a variadic list of \c size_t type arguments. */
     461size_t
     462tor_sum_sizet(size_t *const sump, const size_t n, ...)
     463{
     464  va_list ap;
     465  size_t i, x;
     466
     467  tor_assert(sump != NULL);
     468  *sump = 0;
     469
     470  va_start(ap, n);
     471  for(i = 0; i < n; i++) {
     472    /* accumulate each argument successively */
     473    x = va_arg(ap, size_t);
     474    if (tor_add_sizet(sump, *sump, x)) {
     475      *sump = 0;
     476      break;
     477    }
     478  }
     479  va_end(ap);
     480
     481  return i;
     482}
     483
    446484/* =====
    447485 * String manipulation
    448486 * ===== */
  • src/common/util.h

    diff --git a/src/common/util.h b/src/common/util.h
    index 9e2fd45..d953526 100644
    a b  
    1717#include "di_ops.h"
    1818#include <stdio.h>
    1919#include <stdlib.h>
     20#include <stdarg.h>
    2021
    2122#ifndef O_BINARY
    2223#define O_BINARY 0
    int n_bits_set_u8(uint8_t v); 
    167168 * overflow. */
    168169#define CEIL_DIV(a,b) (((a)+(b)-1)/(b))
    169170
     171/** Safely add two \c size_t type arguments.
     172 *
     173 * @param sump
     174 *   A pointer to an accumulator of type \c size_t which will store
     175 *   the sum of the next two parameters.
     176 *   Must be non-NULL.
     177 * @param a
     178 *   First operand.
     179 * @param b
     180 *   Second operand.
     181 *
     182 * On success, *\b sump contains the sum of \b a and \b b, i.e.
     183 * \f$ *sump \gets a + b \f$.
     184 * Otherwise, *\b sump is set to 0.
     185 *
     186 * ``Success'' is defined as the addition being exact, i.e.:
     187 * \f$ a \oplus b = a+b \f$,
     188 * where \f$ \oplus \f$ represents machine integer addition.
     189 *
     190 * @return
     191 *   0 on success; 1 otherwise.
     192 *
     193 * Example:
     194 * \code
     195 *   size_t n;
     196 *   if (tor_add_sizet(&n, 1, 2))
     197 *     goto oops;
     198 *   else
     199 *     puts("n equals 3");
     200 * \endcode
     201 */
     202int tor_add_sizet(size_t *const sump, const size_t a, const size_t b);
     203
     204/** Safely sum a variable-length list of \c size_t type arguments.
     205 *
     206 * @param sump
     207 *   A pointer to an accumulator of type \c size_t which will store
     208 *   the sum of all \b n arguments after the third.
     209 *   Must be non-NULL.
     210 * @param n
     211 *   A constant of type \c size_t representing the number of summands.
     212 * @param ...
     213 *   A variable-length list of summands, \f$ x_i \f$, \f$ i = 0,\ldots,n-1 \f$.
     214 *
     215 * On success, *\b sump contains the accumulated sum of all \b n
     216 * arguments (after the first three), i.e.:
     217 * \f$ *sump \gets \sum_{i=0}^{n-1} x_i \f$.
     218 * Otherwise, *\b sump is set to 0.
     219 *
     220 * ``Success'' is defined as all partial sums being exact, i.e.:
     221 * \f[
     222 *   \bigoplus_{i=0}^k x_i = \sum_{i=0}^k x_i ,\quad
     223 *   \forall k=0,\ldots,n-1
     224 * \f]
     225 * where \f$ \oplus \f$ represents machine integer addition.
     226 *
     227 * @return
     228 *   the number of arguments successfully summed (i.e. \b n on success,
     229 *   less than \b n otherwise).
     230 *
     231 * Example:
     232 * \code
     233 *   size_t n;
     234 *   if (tor_sum_sizet(&n, 4, 9, 10, 11, 12) != 4)
     235 *     goto oops;
     236 *   else
     237 *     puts("n equals 42");
     238 * \endcode
     239 */
     240size_t tor_sum_sizet(size_t *const sump, const size_t n, ...);
     241
    170242/* String manipulation */
    171243
    172244/** Allowable characters in a hexadecimal string. */