1 /*  basics.c  */
2 
3 #include "../SubMtxManager.h"
4 
5 #define   MYDEBUG 0
6 
7 /*--------------------------------------------------------------------*/
8 /*
9    -----------------------
10    simplest constructor
11 
12    created -- 98may02, cca
13    -----------------------
14 */
15 SubMtxManager *
SubMtxManager_new(void)16 SubMtxManager_new (
17    void
18 ) {
19 SubMtxManager   *manager ;
20 
21 ALLOCATE(manager, struct _SubMtxManager, 1) ;
22 SubMtxManager_setDefaultFields(manager) ;
23 
24 return(manager) ; }
25 
26 /*--------------------------------------------------------------------*/
27 /*
28    -----------------------
29    set the default fields
30 
31    created -- 98may02, cca
32    -----------------------
33 */
34 void
SubMtxManager_setDefaultFields(SubMtxManager * manager)35 SubMtxManager_setDefaultFields (
36    SubMtxManager   *manager
37 ) {
38 if ( manager == NULL ) {
39    fprintf(stderr,
40            "\n fatal error in SubMtxManager_setDefaultFields(%p)"
41            "\n bad input", manager) ;
42    exit(-1) ;
43 }
44 manager->head            = NULL ;
45 manager->lock            = NULL ;
46 manager->mode            =   0  ;
47 manager->nactive         =   0  ;
48 manager->nbytesactive    =   0  ;
49 manager->nbytesrequested =   0  ;
50 manager->nbytesalloc     =   0  ;
51 manager->nrequests       =   0  ;
52 manager->nreleases       =   0  ;
53 manager->nlocks          =   0  ;
54 manager->nunlocks        =   0  ;
55 
56 return ; }
57 
58 /*--------------------------------------------------------------------*/
59 /*
60    --------------------------------------------------
61    clear the data fields, releasing allocated storage
62 
63    created -- 98may02, cca
64    --------------------------------------------------
65 */
66 void
SubMtxManager_clearData(SubMtxManager * manager)67 SubMtxManager_clearData (
68    SubMtxManager   *manager
69 ) {
70 SubMtx   *mtx ;
71 /*
72    ---------------
73    check the input
74    ---------------
75 */
76 if ( manager == NULL ) {
77    fprintf(stderr, "\n fatal error in SubMtxManager_clearData(%p)"
78            "\n bad input\n", manager) ;
79    exit(-1) ;
80 }
81 /*
82    ------------------------
83    free the working storage
84    ------------------------
85 */
86 while ( (mtx = manager->head) != NULL ) {
87    manager->head = mtx->next ;
88    SubMtx_free(mtx) ;
89 }
90 if ( manager->lock != NULL ) {
91 /*
92    -------------------------
93    destroy and free the lock
94    -------------------------
95 */
96    Lock_free(manager->lock) ;
97 }
98 /*
99    ----------------------
100    set the default fields
101    ----------------------
102 */
103 SubMtxManager_setDefaultFields(manager) ;
104 
105 return ; }
106 
107 /*--------------------------------------------------------------------*/
108 /*
109    ------------------------------------------
110    destructor, free's the object and its data
111 
112    created -- 98may02, cca
113    ------------------------------------------
114 */
115 void
SubMtxManager_free(SubMtxManager * manager)116 SubMtxManager_free (
117    SubMtxManager   *manager
118 ) {
119 if ( manager == NULL ) {
120    fprintf(stderr, "\n fatal error in SubMtxManager_free(%p)"
121            "\n bad input\n", manager) ;
122    exit(-1) ;
123 }
124 SubMtxManager_clearData(manager) ;
125 FREE(manager) ;
126 
127 return ; }
128 
129 /*--------------------------------------------------------------------*/
130