realloc.c
Upload User: qaz666999
Upload Date: 2022-08-06
Package Size: 2570k
Code Size: 2k
Category:

Algorithm

Development Platform:

Unix_Linux

  1. /* _mpz_realloc -- make the mpz_t have NEW_ALLOC digits allocated.
  2. Copyright 1991, 1993, 1994, 1995, 2000, 2001, 2008 Free Software Foundation,
  3. Inc.
  4. This file is part of the GNU MP Library.
  5. The GNU MP Library is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU Lesser General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or (at your
  8. option) any later version.
  9. The GNU MP Library is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
  12. License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include "gmp.h"
  18. #include "gmp-impl.h"
  19. void *
  20. _mpz_realloc (mpz_ptr m, mp_size_t new_alloc)
  21. {
  22.   mp_ptr mp;
  23.   /* Never allocate zero space. */
  24.   new_alloc = MAX (new_alloc, 1);
  25.   if (sizeof (mp_size_t) == sizeof (int))
  26.     {
  27.       if (UNLIKELY (new_alloc > ULONG_MAX / GMP_NUMB_BITS))
  28. {
  29.   fprintf (stderr, "gmp: overflow in mpz typen");
  30.   abort ();
  31. }
  32.     }
  33.   else
  34.     {
  35.       if (UNLIKELY (new_alloc > INT_MAX))
  36. {
  37.   fprintf (stderr, "gmp: overflow in mpz typen");
  38.   abort ();
  39. }
  40.     }
  41.   mp = __GMP_REALLOCATE_FUNC_LIMBS (PTR(m), ALLOC(m), new_alloc);
  42.   PTR(m) = mp;
  43.   ALLOC(m) = new_alloc;
  44.   /* Don't create an invalid number; if the current value doesn't fit after
  45.      reallocation, clear it to 0.  */
  46.   if (ABSIZ(m) > new_alloc)
  47.     SIZ(m) = 0;
  48.   return (void *) mp;
  49. }