From: stevenj Date: Wed, 2 Jun 2010 00:36:32 +0000 (-0400) Subject: added API for free_f_data X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=1b5ed59403af4549dca8d78b86b1841751460f2e;p=nlopt.git added API for free_f_data darcs-hash:20100602003632-c8de0-b834db82c3ae5ccebd59fb656dd52f6d13f3bdb1.gz --- diff --git a/api/f77api.c b/api/f77api.c index a3c9e1a..2961e4b 100644 --- a/api/f77api.c +++ b/api/f77api.c @@ -22,7 +22,7 @@ #include -#include "nlopt-internal.h" +#include "nlopt.h" /*-----------------------------------------------------------------------*/ /* wrappers around f77 procedures */ diff --git a/api/f77funcs_.h b/api/f77funcs_.h index 388a40c..61e29df 100644 --- a/api/f77funcs_.h +++ b/api/f77funcs_.h @@ -36,7 +36,7 @@ void F77_(nlo_create,NLO_CREATE)(nlopt_opt *opt, int *alg, int *n) if (*n < 0) *opt = NULL; else { *opt = nlopt_create((nlopt_algorithm) *alg, (unsigned) *n); - (*opt)->free_f_data = 1; + nlopt_set_free_f_data(*opt, 1); } } @@ -59,7 +59,6 @@ void F77_(nlo_set_min_objective,NLO_SET_MIN_OBJECTIVE)( d->f = f; d->f_data = f_data; *ret = (int) nlopt_set_min_objective(*opt, f77_func_wrap, d); - /* FIXME: memory leak in nlopt_destroy */ } void F77_(nlo_set_max_objective,NLO_SET_MAX_OBJECTIVE)( diff --git a/api/nlopt.h b/api/nlopt.h index 2f0975b..ec3ed54 100644 --- a/api/nlopt.h +++ b/api/nlopt.h @@ -248,6 +248,11 @@ NLOPT_EXTERN nlopt_result nlopt_set_initial_step1(nlopt_opt opt, double dx); NLOPT_EXTERN nlopt_result nlopt_get_initial_step(const nlopt_opt opt, const double *x, double *dx); +/* set to 1: nlopt_destroy should call free() on all of the f_data pointers + (for the objective, constraints, etcetera) ... mainly for internal use */ +NLOPT_EXTERN nlopt_result nlopt_set_free_f_data(nlopt_opt opt, int val); +NLOPT_EXTERN int nlopt_get_free_f_data(nlopt_opt opt); + /*************************** DEPRECATED API **************************/ /* The new "object-oriented" API is preferred, since it allows us to gracefully add new features and algorithm-specific options in a diff --git a/api/options.c b/api/options.c index 890fbb9..b217827 100644 --- a/api/options.c +++ b/api/options.c @@ -172,7 +172,6 @@ nlopt_result nlopt_set_min_objective(nlopt_opt opt, nlopt_func f, void *f_data) { if (opt) { opt->f = f; opt->f_data = f_data; - if (f_data) opt->free_f_data = 0; opt->maximize = 0; if (nlopt_isinf(opt->stopval) && opt->stopval > 0) opt->stopval = -HUGE_VAL; /* switch default from max to min */ @@ -185,7 +184,6 @@ nlopt_result nlopt_set_max_objective(nlopt_opt opt, nlopt_func f, void *f_data) { if (opt) { opt->f = f; opt->f_data = f_data; - if (f_data) opt->free_f_data = 0; opt->maximize = 1; if (nlopt_isinf(opt->stopval) && opt->stopval < 0) opt->stopval = +HUGE_VAL; /* switch default from min to max */ @@ -307,7 +305,6 @@ nlopt_result nlopt_add_inequality_constraint(nlopt_opt opt, && opt->algorithm != NLOPT_GN_ORIG_DIRECT && opt->algorithm != NLOPT_GN_ORIG_DIRECT_L) return NLOPT_INVALID_ARGS; - if (fc_data) opt->free_f_data = 0; return add_constraint(&opt->m, &opt->m_alloc, &opt->fc, fc, fc_data, tol); } @@ -330,7 +327,6 @@ nlopt_result nlopt_add_equality_constraint(nlopt_opt opt, /* equality constraints (h(x) = 0) only via some algorithms */ if (!AUGLAG_ALG(opt->algorithm) && opt->algorithm != NLOPT_GN_ISRES) return NLOPT_INVALID_ARGS; - if (h_data) opt->free_f_data = 0; return add_constraint(&opt->p, &opt->p_alloc, &opt->h, h, h_data, tol); } @@ -527,3 +523,7 @@ nlopt_result nlopt_set_default_initial_step(nlopt_opt opt, const double *x) } /*************************************************************************/ + +GETSET(free_f_data, int, free_f_data) + +/*************************************************************************/