From 3807506aeb96dd46c9f656cdddddace4b3597bf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A5rd=20Skaflestad?= Date: Sat, 21 Apr 2012 11:34:11 +0200 Subject: [PATCH] Hide memory management aspects of struct WellControls. --- opm/core/newwells.h | 3 ++- opm/core/utility/newwells.c | 54 ++++++++++++++++++++++++++++++++----- 2 files changed, 49 insertions(+), 8 deletions(-) diff --git a/opm/core/newwells.h b/opm/core/newwells.h index 3e14fb9b..381b31e5 100644 --- a/opm/core/newwells.h +++ b/opm/core/newwells.h @@ -59,10 +59,11 @@ enum SurfaceComponent { WATER = 0, OIL = 1, GAS = 2 }; struct WellControls { int num; /** Number of controls. */ - int cpty; /** Allocated capacity, for internal use only. */ enum WellControlType *type; /** Array of control types. */ double *target; /** Array of control targets. */ int current; /** Index of current active control. */ + + void *data; /** Internal management structure. */ }; diff --git a/opm/core/utility/newwells.c b/opm/core/utility/newwells.c index 55a82ffb..3960bb82 100644 --- a/opm/core/utility/newwells.c +++ b/opm/core/utility/newwells.c @@ -41,14 +41,42 @@ #include #include +struct WellControlMgmt { + int cpty; +}; + + +static void +destroy_ctrl_mgmt(struct WellControlMgmt *m) +{ + free(m); +} + + +static struct WellControlMgmt * +create_ctrl_mgmt(void) +{ + struct WellControlMgmt *m; + + m = malloc(1 * sizeof *m); + + if (m != NULL) { + m->cpty = 0; + } + + return m; +} + + /* ---------------------------------------------------------------------- */ static void well_controls_destroy(struct WellControls *ctrl) /* ---------------------------------------------------------------------- */ { if (ctrl != NULL) { - free(ctrl->target); - free(ctrl->type); + destroy_ctrl_mgmt(ctrl->data); + free (ctrl->target); + free (ctrl->type); } free(ctrl); @@ -67,10 +95,16 @@ well_controls_create(void) if (ctrl != NULL) { /* Initialise empty control set */ ctrl->num = 0; - ctrl->cpty = 0; ctrl->type = NULL; ctrl->target = NULL; ctrl->current = -1; + + ctrl->data = create_ctrl_mgmt(); + + if (ctrl->data == NULL) { + well_controls_destroy(ctrl); + ctrl = NULL; + } } return ctrl; @@ -85,6 +119,8 @@ well_controls_reserve(int nctrl, struct WellControls *ctrl) int c, ok; void *type, *target; + struct WellControlMgmt *m; + type = realloc(ctrl->type , nctrl * sizeof *ctrl->type ); target = realloc(ctrl->target, nctrl * sizeof *ctrl->target); @@ -93,12 +129,13 @@ well_controls_reserve(int nctrl, struct WellControls *ctrl) if (target != NULL) { ctrl->target = target; ok++; } if (ok == 2) { - for (c = ctrl->cpty; c < nctrl; c++) { + m = ctrl->data; + for (c = m->cpty; c < nctrl; c++) { ctrl->type [c] = BHP; ctrl->target[c] = -1.0; } - ctrl->cpty = nctrl; + m->cpty = nctrl; } return ok == 2; @@ -393,12 +430,15 @@ append_well_controls(enum WellControlType type , { int ok, alloc; + struct WellControlMgmt *m; + assert (ctrl != NULL); - ok = ctrl->num < ctrl->cpty; + m = ctrl->data; + ok = ctrl->num < m->cpty; if (! ok) { - alloc = alloc_size(ctrl->num, 1, ctrl->cpty); + alloc = alloc_size(ctrl->num, 1, m->cpty); ok = well_controls_reserve(alloc, ctrl); }