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