Branch: Tag:

2008-03-28

2008-03-28 20:17:13 by Martin Stjernholm <mast@lysator.liu.se>

Make sure gmp uses the same malloc implementation as the rest of pike does.
This might solve problems with multiple crt's on Windows, and it enables
better use of dlmalloc if that's enabled.

Rev: src/modules/Gmp/mpz_glue.c:1.172

2:   || This file is part of Pike. For copyright information see COPYRIGHT.   || Pike is distributed under GPL, LGPL and MPL. See the file COPYING   || for more information. - || $Id: mpz_glue.c,v 1.171 2008/02/27 23:59:24 grubba Exp $ + || $Id: mpz_glue.c,v 1.172 2008/03/28 20:17:13 mast Exp $   */      #include "global.h"
2104:   #define tMpz_binop_type tFuncV(tNone, tMpz_arg, tMpz_ret)   #define tMpz_cmpop_type tFunc(tMixed, tInt01)    + static void *pike_mp_alloc (size_t alloc_size) + { +  void *ret = malloc (alloc_size); +  if (!ret) +  /* According to gmp docs, we're neither allowed to return zero nor +  * longjmp here. */ +  Pike_fatal ("Failed to allocate %"PRINTSIZET"db in gmp library.\n", +  alloc_size); +  return ret; + } +  + static void *pike_mp_realloc (void *ptr, size_t old_size, size_t new_size) + { +  void *ret = realloc (ptr, new_size); +  if (!ret) +  /* According to gmp docs, we're neither allowed to return zero nor +  * longjmp here. */ +  Pike_fatal ("Failed to reallocate %"PRINTSIZET"db block " +  "to %"PRINTSIZET"db in gmp library.\n", old_size, new_size); +  return ret; + } +  + static void *pike_mp_free (void *ptr, size_t size) + { +  free (ptr); + } +    #define MPZ_DEFS() \    ADD_STORAGE(MP_INT); \    \
2202: Inside #if defined(USE_GMP) || defined(USE_GMP2) and #if defined(PIKE_DEBUG)
   fatal("Gmp.mpz initialized twice!\n");    }   #endif /* PIKE_DEBUG */ +  +  /* Make sure that gmp uses the same malloc functions as we do since +  * we got code that frees blocks allocated inside gmp (e.g. +  * mpf.get_string). This also ensures that gmp uses dlmalloc if we +  * do on Windows. In case gmp already uses the same malloc, this is +  * essentially just a NOP. */ +  mp_set_memory_functions (pike_mp_alloc, pike_mp_realloc, pike_mp_free); +     start_new_program();       MPZ_DEFS();