Skip to content

Commit

Permalink
try not patching pari; start a branch
Browse files Browse the repository at this point in the history
The idea is to reset the gmp memory functions just after calling the
init function for any 3rd party library that sets them.  The only one is
pari, at the moment.
  • Loading branch information
DanGrayson committed May 24, 2016
1 parent 51eb8d0 commit f6e2f61
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 7 deletions.
16 changes: 10 additions & 6 deletions M2/Macaulay2/d/M2inits.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ void *realloc_function (void *s, size_t old, size_t new) {
return p;
}

void our_gmp_set_memory_functions() {
/* Instead of calling __gmp_set_memory_functions we set the values ourselves. That way we can
patch mpir so the function __gmp_set_memory_functions does nothing, leaving us in control, even though
pari calls it, for example. */
__gmp_allocate_func = (void *(*) (size_t)) getmem_atomic;
__gmp_reallocate_func = (void *(*) (void *, size_t, size_t)) getmoremem_atomic;
__gmp_free_func = freememlen;
}

int M2inits_firsttime = 1;
void enterM2(void) {
/* this function is called initially, and also again after we call a third party library that sets gmp's memory allocation routines */
Expand All @@ -67,12 +76,7 @@ void enterM2(void) {
initializeGMP_Cwrapper(); /* this calls factory's initializeGMP() in factory/initgmp.cc, which will call __gmp_set_memory_functions just once */
}
#if 1
/* Instead of calling __gmp_set_memory_functions we set the values ourselves. That way we can
patch mpir so the function __gmp_set_memory_functions does nothing, leaving us in control, even though
pari calls it, for example. */
__gmp_allocate_func = (void *(*) (size_t)) getmem_atomic;
__gmp_reallocate_func = (void *(*) (void *, size_t, size_t)) getmoremem_atomic;
__gmp_free_func = freememlen;
our_gmp_set_memory_functions();
#else
__gmp_set_memory_functions ( /* tell gmp to use gc for memory allocation, with our error messages */
/* this function is located in mpir-1.2.1/mp_set_fns.c */
Expand Down
1 change: 1 addition & 0 deletions M2/Macaulay2/d/M2inits.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ extern void M2inits1(), M2inits2();
extern void enterM2();
extern void check_M2init();
extern int M2inits_run, M2inits_firsttime;
extern void our_gmp_set_memory_functions();

/* get size_t */
#include <stddef.h>
Expand Down
1 change: 1 addition & 0 deletions M2/Macaulay2/d/pari-c.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ static void initpari() {
}
if (gen_0 == NULL /* && FALSE /-* groan */ ) {
pari_init_opts( PARISIZE, MAXPRIME, init_flags);
our_gmp_set_memory_functions(); /* undo the setting of the gmp memory functions done by pari_init_opts */
self_initialized = TRUE;
}
enterM2(); /* pari_init sets the memory allocation routines for gmp, so we have to set them back */
Expand Down
1 change: 1 addition & 0 deletions M2/Macaulay2/d/pari-c.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
extern "C" {
#endif

#include <M2inits.h>
#define VARLEN 1
typedef struct { unsigned int n; __mpz_struct *el[VARLEN]; } mpz_col;
typedef struct { unsigned int n; mpz_col *el[VARLEN]; } mpz_mat;
Expand Down
2 changes: 1 addition & 1 deletion M2/libraries/mpir/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ PRECONFIGURE = autoconf

# URL = http://www.mpir.org
URL = http://www.math.uiuc.edu/Macaulay2/Downloads/OtherSourceCode
PATCHFILE = @abs_srcdir@/patch-$(VERSION)
# PATCHFILE = @abs_srcdir@/patch-$(VERSION)

# increase the limit, because two of the tests run by mpir's "make check" will
# not run in just 400000:
Expand Down

0 comments on commit f6e2f61

Please sign in to comment.