[BACK]Return to memory.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / gmp

Annotation of OpenXM_contrib/gmp/memory.c, Revision 1.1.1.2

1.1       maekawa     1: /* Memory allocation routines.
                      2:
1.1.1.2 ! maekawa     3: Copyright (C) 1991, 1993, 1994, 2000 Free Software Foundation, Inc.
1.1       maekawa     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
1.1.1.2 ! maekawa     8: it under the terms of the GNU Lesser General Public License as published by
        !             9: the Free Software Foundation; either version 2.1 of the License, or (at your
1.1       maekawa    10: option) any later version.
                     11:
                     12: The GNU MP Library is distributed in the hope that it will be useful, but
                     13: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
1.1.1.2 ! maekawa    14: or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
1.1       maekawa    15: License for more details.
                     16:
1.1.1.2 ! maekawa    17: You should have received a copy of the GNU Lesser General Public License
1.1       maekawa    18: along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
                     19: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
                     20: MA 02111-1307, USA. */
                     21:
                     22: #include <stdio.h>
1.1.1.2 ! maekawa    23: #include <stdlib.h> /* for malloc, realloc, free */
1.1       maekawa    24:
                     25: #include "gmp.h"
                     26: #include "gmp-impl.h"
                     27:
                     28: #ifdef __NeXT__
                     29: #define static
                     30: #endif
                     31:
1.1.1.2 ! maekawa    32:
        !            33: void * (*_mp_allocate_func) _PROTO ((size_t)) = _mp_default_allocate;
        !            34: void * (*_mp_reallocate_func) _PROTO ((void *, size_t, size_t))
1.1       maekawa    35:      = _mp_default_reallocate;
1.1.1.2 ! maekawa    36: void   (*_mp_free_func) _PROTO ((void *, size_t)) = _mp_default_free;
        !            37:
1.1       maekawa    38:
                     39: /* Default allocation functions.  In case of failure to allocate/reallocate
                     40:    an error message is written to stderr and the program aborts.  */
                     41:
                     42: void *
                     43: #if __STDC__
                     44: _mp_default_allocate (size_t size)
                     45: #else
                     46: _mp_default_allocate (size)
                     47:      size_t size;
                     48: #endif
                     49: {
                     50:   void *ret;
1.1.1.2 ! maekawa    51: #ifdef DEBUG
        !            52:   size_t req_size = size;
        !            53:   size += 2 * BYTES_PER_MP_LIMB;
        !            54: #endif
1.1       maekawa    55:   ret = malloc (size);
                     56:   if (ret == 0)
                     57:     {
                     58:       perror ("cannot allocate in gmp");
                     59:       abort ();
                     60:     }
1.1.1.2 ! maekawa    61:
        !            62: #ifdef DEBUG
        !            63:   {
        !            64:     mp_ptr p = ret;
        !            65:     p++;
        !            66:     p[-1] = (0xdeadbeef << 31) + 0xdeafdeed;
        !            67:     if (req_size % BYTES_PER_MP_LIMB == 0)
        !            68:       p[req_size / BYTES_PER_MP_LIMB] = ~((0xdeadbeef << 31) + 0xdeafdeed);
        !            69:     ret = p;
        !            70:   }
        !            71: #endif
1.1       maekawa    72:   return ret;
                     73: }
                     74:
                     75: void *
                     76: #if __STDC__
                     77: _mp_default_reallocate (void *oldptr, size_t old_size, size_t new_size)
                     78: #else
                     79: _mp_default_reallocate (oldptr, old_size, new_size)
                     80:      void *oldptr;
                     81:      size_t old_size;
                     82:      size_t new_size;
                     83: #endif
                     84: {
                     85:   void *ret;
                     86:
1.1.1.2 ! maekawa    87: #ifdef DEBUG
        !            88:   size_t req_size = new_size;
        !            89:
        !            90:   if (old_size != 0)
        !            91:     {
        !            92:       mp_ptr p = oldptr;
        !            93:       if (p[-1] != (0xdeadbeef << 31) + 0xdeafdeed)
        !            94:        {
        !            95:          fprintf (stderr, "gmp: (realloc) data clobbered before allocation block\n");
        !            96:          abort ();
        !            97:        }
        !            98:       if (old_size % BYTES_PER_MP_LIMB == 0)
        !            99:        if (p[old_size / BYTES_PER_MP_LIMB] != ~((0xdeadbeef << 31) + 0xdeafdeed))
        !           100:          {
        !           101:            fprintf (stderr, "gmp: (realloc) data clobbered after allocation block\n");
        !           102:            abort ();
        !           103:          }
        !           104:       oldptr = p - 1;
        !           105:     }
        !           106:
        !           107:   new_size += 2 * BYTES_PER_MP_LIMB;
        !           108: #endif
        !           109:
1.1       maekawa   110:   ret = realloc (oldptr, new_size);
                    111:   if (ret == 0)
                    112:     {
                    113:       perror ("cannot allocate in gmp");
                    114:       abort ();
                    115:     }
                    116:
1.1.1.2 ! maekawa   117: #ifdef DEBUG
        !           118:   {
        !           119:     mp_ptr p = ret;
        !           120:     p++;
        !           121:     p[-1] = (0xdeadbeef << 31) + 0xdeafdeed;
        !           122:     if (req_size % BYTES_PER_MP_LIMB == 0)
        !           123:       p[req_size / BYTES_PER_MP_LIMB] = ~((0xdeadbeef << 31) + 0xdeafdeed);
        !           124:     ret = p;
        !           125:   }
        !           126: #endif
1.1       maekawa   127:   return ret;
                    128: }
                    129:
                    130: void
                    131: #if __STDC__
                    132: _mp_default_free (void *blk_ptr, size_t blk_size)
                    133: #else
                    134: _mp_default_free (blk_ptr, blk_size)
                    135:      void *blk_ptr;
                    136:      size_t blk_size;
                    137: #endif
                    138: {
1.1.1.2 ! maekawa   139: #ifdef DEBUG
        !           140:   {
        !           141:     mp_ptr p = blk_ptr;
        !           142:     if (blk_size != 0)
        !           143:       {
        !           144:        if (p[-1] != (0xdeadbeef << 31) + 0xdeafdeed)
        !           145:          {
        !           146:            fprintf (stderr, "gmp: (free) data clobbered before allocation block\n");
        !           147:            abort ();
        !           148:          }
        !           149:        if (blk_size % BYTES_PER_MP_LIMB == 0)
        !           150:          if (p[blk_size / BYTES_PER_MP_LIMB] != ~((0xdeadbeef << 31) + 0xdeafdeed))
        !           151:            {
        !           152:              fprintf (stderr, "gmp: (free) data clobbered after allocation block\n");
        !           153:              abort ();
        !           154:            }
        !           155:       }
        !           156:     blk_ptr = p - 1;
        !           157:   }
        !           158: #endif
1.1       maekawa   159:   free (blk_ptr);
                    160: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>