1 /*  basics.c  */
2 
3 #include "../IVL.h"
4 
5 #define   MYDEBUG 0
6 
7 /*--------------------------------------------------------------------*/
8 /*
9    -----------------------
10    simplest constructor
11 
12    created -- 95sep22, cca
13    -----------------------
14 */
15 IVL *
IVL_new(void)16 IVL_new (
17    void
18 ) {
19 IVL   *ivl ;
20 
21 ALLOCATE(ivl, struct _IVL, 1) ;
22 IVL_setDefaultFields(ivl) ;
23 
24 return(ivl) ; }
25 
26 /*--------------------------------------------------------------------*/
27 /*
28    -----------------------
29    set the default fields
30 
31    created -- 95sep22, cca
32    -----------------------
33 */
34 void
IVL_setDefaultFields(IVL * ivl)35 IVL_setDefaultFields (
36    IVL   *ivl
37 ) {
38 if ( ivl == NULL ) {
39    fprintf(stderr, "\n fatal error in IVL_setDefaultFields(%p)"
40            "\n bad input", ivl) ;
41    exit(-1) ;
42 }
43 ivl->type     = IVL_NOTYPE ;
44 ivl->maxnlist = 0          ;
45 ivl->nlist    = 0          ;
46 ivl->tsize    = 0          ;
47 ivl->sizes    = NULL       ;
48 ivl->p_vec    = NULL       ;
49 ivl->incr     = IVL_INCR   ;
50 ivl->chunk   = NULL       ;
51 
52 return ; }
53 
54 /*--------------------------------------------------------------------*/
55 /*
56    --------------------------------------------------
57    clear the data fields, releasing allocated storage
58 
59    created -- 95sep22, cca
60    --------------------------------------------------
61 */
62 void
IVL_clearData(IVL * ivl)63 IVL_clearData (
64    IVL   *ivl
65 ) {
66 /*
67    ---------------
68    check the input
69    ---------------
70 */
71 if ( ivl == NULL ) {
72    fprintf(stderr, "\n fatal error in IVL_clearData(%p)"
73            "\n bad input\n", ivl) ;
74    exit(-1) ;
75 }
76 /*
77    ----------------------------------------------------
78    switch over the storage type to free list entries.
79    action is taken when type is IVL_SOLO or IVL_CHUNKED
80    ----------------------------------------------------
81 */
82 switch ( ivl->type ) {
83 case IVL_SOLO : {
84    int   ilist ;
85    for ( ilist = 0 ; ilist < ivl->nlist ; ilist++ ) {
86       if ( ivl->p_vec[ilist] != NULL ) {
87          IVfree(ivl->p_vec[ilist]) ;
88          ivl->p_vec[ilist] = NULL ;
89          ivl->tsize -= ivl->sizes[ilist] ;
90       }
91    }
92    } break ;
93 case IVL_CHUNKED : {
94    Ichunk   *chunk ;
95    while ( (chunk = ivl->chunk) != NULL ) {
96       ivl->chunk = chunk->next ;
97       if ( chunk->base != NULL ) {
98          IVfree(chunk->base) ;
99          chunk->base = NULL ;
100       }
101       FREE(chunk) ;
102    }
103    } break ;
104 case IVL_NOTYPE  :
105 case IVL_UNKNOWN :
106    break ;
107 default :
108    fprintf(stderr, "\n fatal error in IVL_clearData(%p)"
109            "\n invalid type = %d\n", ivl, ivl->type) ;
110    exit(-1) ;
111 }
112 /*
113    -----------------------------------------------
114    free storage for the sizes[] and p_vec[] arrays
115    -----------------------------------------------
116 */
117 if ( ivl->sizes != NULL ) {
118    IVfree(ivl->sizes) ;
119    ivl->sizes = NULL ;
120 }
121 if ( ivl->p_vec != NULL ) {
122    PIVfree(ivl->p_vec) ;
123    ivl->p_vec = NULL ;
124 }
125 ivl->nlist = ivl->maxnlist = 0 ;
126 /*
127    ----------------------
128    set the default fields
129    ----------------------
130 */
131 IVL_setDefaultFields(ivl) ;
132 
133 return ; }
134 
135 /*--------------------------------------------------------------------*/
136 /*
137    ------------------------------------------
138    destructor, free's the object and its data
139 
140    created -- 95sep22, cca
141    ------------------------------------------
142 */
143 IVL *
IVL_free(IVL * ivl)144 IVL_free (
145    IVL   *ivl
146 ) {
147 if ( ivl == NULL ) {
148    fprintf(stderr, "\n fatal error in IVL_free(%p)"
149            "\n bad input\n", ivl) ;
150    exit(-1) ;
151 }
152 IVL_clearData(ivl) ;
153 FREE(ivl) ;
154 
155 return(NULL) ; }
156 
157 /*--------------------------------------------------------------------*/
158