summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuillaume Horel <guillaume.horel@gmail.com>2017-02-08 14:00:29 -0500
committerGuillaume Horel <guillaume.horel@gmail.com>2017-02-08 14:00:29 -0500
commit1e3db181f9721090440358a5601725ed4d159341 (patch)
tree466356de9d62e9a49c84c88e6e303574754384a5
parentf95331004542b4ca2673cdf02cf32beb1c73063c (diff)
downloadpyisda-1e3db181f9721090440358a5601725ed4d159341.tar.gz
malloc cleanups
-rw-r--r--pyisda/curve.pyx56
1 files changed, 27 insertions, 29 deletions
diff --git a/pyisda/curve.pyx b/pyisda/curve.pyx
index 57f7ecd..489c8e4 100644
--- a/pyisda/curve.pyx
+++ b/pyisda/curve.pyx
@@ -265,7 +265,7 @@ cdef class SpreadCurve(Curve):
cdef TDate start_date_c = pydate_to_TDate(start_date)
cdef int n_dates = len(end_dates)
cdef TDate* end_dates_c = <TDate*>malloc(n_dates * sizeof(TDate))
- self._thisptr = NULL
+ cdef TCurve* curve = NULL
cdef size_t i
if cash_settle_date_c < yc._thisptr.fBaseDate:
raise ValueError("cash_settle_date: {0} is anterior to yc's base_date: {1}".
@@ -277,33 +277,36 @@ cdef class SpreadCurve(Curve):
if JpmcdsStringToStubMethod(b"f/s", &stub_type) != 0:
raise ValueError("can't convert stub")
cdef long dc
- JpmcdsStringToDayCountConv('ACT/360', &dc)
with nogil:
- self._thisptr = JpmcdsCleanSpreadCurve(today_c,
- yc._thisptr,
- start_date_c,
- step_in_date_c,
- cash_settle_date_c,
- n_dates,
- end_dates_c,
- &coupon_rates[0],
- &upfront_rates[0],
- NULL,
- &recovery_rates[0],
- pay_accrued_on_default,
- NULL,
- dc,
- &stub_type,
- <long>'M',
- b'NONE')
- if self._thisptr == NULL:
- raise ValueError("something went wrong")
+ JpmcdsStringToDayCountConv('ACT/360', &dc)
+ curve = JpmcdsCleanSpreadCurve(today_c,
+ yc._thisptr,
+ start_date_c,
+ step_in_date_c,
+ cash_settle_date_c,
+ n_dates,
+ end_dates_c,
+ &coupon_rates[0],
+ &upfront_rates[0],
+ NULL,
+ &recovery_rates[0],
+ pay_accrued_on_default,
+ NULL,
+ dc,
+ &stub_type,
+ <long>'M',
+ b'NONE')
+ free(end_dates_c)
+ if curve == NULL:
+ raise ValueError("Didn't init the survival curve properly")
+ else:
+ self._thisptr = curve
survival_probability = Curve.__forward_zero_price
@classmethod
- def from_flat_hazard(cls, base_date, double rate, Basis basis = CONTINUOUS,
- str day_count_conv = 'Actual/365F'):
+ def from_flat_hazard(cls, base_date, double rate, Basis basis=CONTINUOUS,
+ str day_count_conv='Actual/365F'):
"""
Alternative constructor for flat hazard rate Curve.
@@ -322,12 +325,7 @@ cdef class SpreadCurve(Curve):
cdef TDate base_date_c = pydate_to_TDate(base_date)
cdef SpreadCurve sc = cls.__new__(cls)
cdef TDate max_date = 200000 # can go higher but this should be more than enough
- cdef TDate* dates = <TDate*>malloc(sizeof(TDate))
-
- cdef double* rates = <double*>malloc(sizeof(double))
- dates[0] = max_date
- rates[0] = rate
- sc._thisptr = JpmcdsMakeTCurve(base_date_c, dates, rates, 1,
+ sc._thisptr = JpmcdsMakeTCurve(base_date_c, &max_date, &rate, 1,
<double>basis, dcc(day_count_conv))
return sc