github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/memory.c (about)

     1  /* Memory allocation routines.
     2  
     3  Copyright 1991, 1993, 1994, 2000-2002, 2012 Free Software Foundation, Inc.
     4  
     5  This file is part of the GNU MP Library.
     6  
     7  The GNU MP Library is free software; you can redistribute it and/or modify
     8  it under the terms of either:
     9  
    10    * the GNU Lesser General Public License as published by the Free
    11      Software Foundation; either version 3 of the License, or (at your
    12      option) any later version.
    13  
    14  or
    15  
    16    * the GNU General Public License as published by the Free Software
    17      Foundation; either version 2 of the License, or (at your option) any
    18      later version.
    19  
    20  or both in parallel, as here.
    21  
    22  The GNU MP Library is distributed in the hope that it will be useful, but
    23  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    24  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    25  for more details.
    26  
    27  You should have received copies of the GNU General Public License and the
    28  GNU Lesser General Public License along with the GNU MP Library.  If not,
    29  see https://www.gnu.org/licenses/.  */
    30  
    31  #include <stdio.h>
    32  #include <stdlib.h> /* for malloc, realloc, free */
    33  
    34  #include "gmp.h"
    35  #include "gmp-impl.h"
    36  
    37  
    38  void * (*__gmp_allocate_func) (size_t) = __gmp_default_allocate;
    39  void * (*__gmp_reallocate_func) (void *, size_t, size_t) = __gmp_default_reallocate;
    40  void   (*__gmp_free_func) (void *, size_t) = __gmp_default_free;
    41  
    42  
    43  /* Default allocation functions.  In case of failure to allocate/reallocate
    44     an error message is written to stderr and the program aborts.  */
    45  
    46  void *
    47  __gmp_default_allocate (size_t size)
    48  {
    49    void *ret;
    50  #ifdef DEBUG
    51    size_t req_size = size;
    52    size += 2 * GMP_LIMB_BYTES;
    53  #endif
    54    ret = malloc (size);
    55    if (ret == 0)
    56      {
    57        fprintf (stderr, "GNU MP: Cannot allocate memory (size=%lu)\n", (long) size);
    58        abort ();
    59      }
    60  
    61  #ifdef DEBUG
    62    {
    63      mp_ptr p = ret;
    64      p++;
    65      p[-1] = (0xdeadbeef << 31) + 0xdeafdeed;
    66      if (req_size % GMP_LIMB_BYTES == 0)
    67        p[req_size / GMP_LIMB_BYTES] = ~((0xdeadbeef << 31) + 0xdeafdeed);
    68      ret = p;
    69    }
    70  #endif
    71    return ret;
    72  }
    73  
    74  void *
    75  __gmp_default_reallocate (void *oldptr, size_t old_size, size_t new_size)
    76  {
    77    void *ret;
    78  
    79  #ifdef DEBUG
    80    size_t req_size = new_size;
    81  
    82    if (old_size != 0)
    83      {
    84        mp_ptr p = oldptr;
    85        if (p[-1] != (0xdeadbeef << 31) + 0xdeafdeed)
    86  	{
    87  	  fprintf (stderr, "gmp: (realloc) data clobbered before allocation block\n");
    88  	  abort ();
    89  	}
    90        if (old_size % GMP_LIMB_BYTES == 0)
    91  	if (p[old_size / GMP_LIMB_BYTES] != ~((0xdeadbeef << 31) + 0xdeafdeed))
    92  	  {
    93  	    fprintf (stderr, "gmp: (realloc) data clobbered after allocation block\n");
    94  	    abort ();
    95  	  }
    96        oldptr = p - 1;
    97      }
    98  
    99    new_size += 2 * GMP_LIMB_BYTES;
   100  #endif
   101  
   102    ret = realloc (oldptr, new_size);
   103    if (ret == 0)
   104      {
   105        fprintf (stderr, "GNU MP: Cannot reallocate memory (old_size=%lu new_size=%lu)\n", (long) old_size, (long) new_size);
   106        abort ();
   107      }
   108  
   109  #ifdef DEBUG
   110    {
   111      mp_ptr p = ret;
   112      p++;
   113      p[-1] = (0xdeadbeef << 31) + 0xdeafdeed;
   114      if (req_size % GMP_LIMB_BYTES == 0)
   115        p[req_size / GMP_LIMB_BYTES] = ~((0xdeadbeef << 31) + 0xdeafdeed);
   116      ret = p;
   117    }
   118  #endif
   119    return ret;
   120  }
   121  
   122  void
   123  __gmp_default_free (void *blk_ptr, size_t blk_size)
   124  {
   125  #ifdef DEBUG
   126    {
   127      mp_ptr p = blk_ptr;
   128      if (blk_size != 0)
   129        {
   130  	if (p[-1] != (0xdeadbeef << 31) + 0xdeafdeed)
   131  	  {
   132  	    fprintf (stderr, "gmp: (free) data clobbered before allocation block\n");
   133  	    abort ();
   134  	  }
   135  	if (blk_size % GMP_LIMB_BYTES == 0)
   136  	  if (p[blk_size / GMP_LIMB_BYTES] != ~((0xdeadbeef << 31) + 0xdeafdeed))
   137  	    {
   138  	      fprintf (stderr, "gmp: (free) data clobbered after allocation block\n");
   139  	      abort ();
   140  	    }
   141        }
   142      blk_ptr = p - 1;
   143    }
   144  #endif
   145    free (blk_ptr);
   146  }