GnuCOBOL  2.0
A free COBOL compiler
vbmemio.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2003 Trevor van Bremen
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 2.1,
7  * or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; see the file COPYING.LIB. If
16  * not, write to the Free Software Foundation, 51 Franklin Street, Fifth Floor
17  * Boston, MA 02110-1301 USA
18  */
19 
20 #include "isinternal.h"
21 
22 static struct VBLOCK *pslockfree = NULL;
23 static struct VBTREE *pstreefree = NULL;
24 
25 /* Globals */
26 
27 int iserrno = 0; /* Value of error is returned here */
28 int iserrio = 0; /* Contains value of last function called */
29 int isreclen = 0; /* Used for varlen tables */
30 int isrecnum = 0; /* Current row number */
31 
32 #ifdef VBDEBUG
33 static void
34 vb_error (const char *msg)
35 {
36  fprintf (stderr, "%s - Aborting\n", msg);
37  fflush (stderr);
38  exit (1);
39 }
40 #else
41 #define vb_error(x)
42 #endif
43 
44 void *
45 pvvbmalloc (const size_t size)
46 {
47  void *mptr;
48 
49  mptr = calloc (1, size);
50  if (unlikely(!mptr)) {
51  fprintf (stderr, "Cannot allocate %d bytes of memory - Aborting\n", size);
52  fflush (stderr);
53  exit (1);
54  }
55  return mptr;
56 }
57 
58 void
59 vvbfree (void *mptr)
60 {
61  if (mptr) {
62  free (mptr);
63  }
64 }
65 
66 struct VBLOCK *
68 {
69  struct VBLOCK *pslock = pslockfree;
70 
71  if (pslockfree != NULL) {
72  pslockfree = pslockfree->psnext;
73  memset (pslock, 0, sizeof (struct VBLOCK));
74  } else {
75  pslock = pvvbmalloc (sizeof (struct VBLOCK));
76  }
77  return pslock;
78 }
79 
80 void
81 vvblockfree (struct VBLOCK *pslock)
82 {
83  pslock->psnext = pslockfree;
84  pslockfree = pslock;
85 }
86 
87 struct VBTREE *
88 psvbtreeallocate (const int ihandle)
89 {
90  struct VBTREE *pstree = pstreefree;
91 
92  if (pstreefree == NULL) {
93  pstree = pvvbmalloc (sizeof (struct VBTREE));
94  } else {
95  pstreefree = pstreefree->psnext;
96  if (pstree->tnodenumber != -1) {
97  vb_error ("Treeallocated not free");
98  }
99  memset (pstree, 0, sizeof (struct VBTREE));
100  }
101  return pstree;
102 }
103 
104 void
105 vvbtreeallfree (const int ihandle, const int ikeynumber, struct VBTREE *pstree)
106 {
107  if (!pstree) {
108  return;
109  }
110  if (pstree->tnodenumber == -1) {
111  vb_error ("Treefreed not free");
112  }
113  vvbkeyallfree (ihandle, ikeynumber, pstree);
114  pstree->psnext = pstreefree;
115  pstreefree = pstree;
116  pstree->tnodenumber = -1;
117 }
118 
119 struct VBKEY *
120 psvbkeyallocate (const int ihandle, const int ikeynumber)
121 {
122  struct VBKEY *pskey;
123  struct DICTINFO *psvbptr;
124  int ilength = 0;
125 
126  psvbptr = psvbfile[ihandle];
127  pskey = psvbptr->pskeyfree[ikeynumber];
128  if (pskey == NULL) {
129  ilength = psvbptr->pskeydesc[ikeynumber]->k_len;
130  pskey = pvvbmalloc (sizeof (struct VBKEY) + ilength);
131  } else {
132  if (pskey->trownode != -1) {
133  vb_error ("Keyallocated not free");
134  }
135  psvbptr->pskeyfree[ikeynumber] =
136  psvbptr->pskeyfree[ikeynumber]->psnext;
137  memset (pskey, 0, (sizeof (struct VBKEY) + ilength));
138  }
139  return pskey;
140 }
141 
142 void
143 vvbkeyallfree (const int ihandle, const int ikeynumber, struct VBTREE *pstree)
144 {
145  struct DICTINFO *psvbptr;
146  struct VBKEY *pskeycurr;
147  struct VBKEY *pskeynext;
148 
149  psvbptr = psvbfile[ihandle];
150  pskeycurr = pstree->pskeyfirst;
151  while (pskeycurr) {
152  if (pskeycurr->trownode == -1) {
153  vb_error ("Keyfreed already free");
154  }
155  pskeynext = pskeycurr->psnext;
156  if (pskeycurr->pschild) {
157  vvbtreeallfree (ihandle, ikeynumber, pskeycurr->pschild);
158  }
159  pskeycurr->pschild = NULL;
160  pskeycurr->psnext = psvbptr->pskeyfree[ikeynumber];
161  psvbptr->pskeyfree[ikeynumber] = pskeycurr;
162  pskeycurr->trownode = -1;
163  pskeycurr = pskeynext;
164  }
165  pstree->pskeyfirst = NULL;
166  pstree->pskeylast = NULL;
167  pstree->pskeycurr = NULL;
168  pstree->ikeysinnode = 0;
169 }
170 
171 void
172 vvbkeyfree (const int ihandle, const int ikeynumber, struct VBKEY *pskey)
173 {
174  struct DICTINFO *psvbptr;
175 
176  if (pskey->trownode == -1) {
177  vb_error ("Keyfreed already free");
178  }
179  psvbptr = psvbfile[ihandle];
180  if (pskey->pschild) {
181  vvbtreeallfree (ihandle, ikeynumber, pskey->pschild);
182  }
183  pskey->pschild = NULL;
184  if (pskey->psnext) {
185  pskey->psnext->psprev = pskey->psprev;
186  }
187  if (pskey->psprev) {
188  pskey->psprev->psnext = pskey->psnext;
189  }
190  pskey->psnext = psvbptr->pskeyfree[ikeynumber];
191  psvbptr->pskeyfree[ikeynumber] = pskey;
192  pskey->trownode = -1;
193 }
194 
195 void
196 vvbkeyunmalloc (const int ihandle, const int ikeynumber)
197 {
198  struct DICTINFO *psvbptr;
199  struct VBKEY *pskeycurr;
200 /* RXW
201  int ilength;
202 */
203 
204  psvbptr = psvbfile[ihandle];
205  pskeycurr = psvbptr->pskeyfree[ikeynumber];
206 /* RXW
207  ilength = sizeof (struct VBKEY) + psvbptr->pskeydesc[ikeynumber]->k_len;
208 */
209  while (pskeycurr) {
210  psvbptr->pskeyfree[ikeynumber] =
211  psvbptr->pskeyfree[ikeynumber]->psnext;
212  vvbfree (pskeycurr);
213  pskeycurr = psvbptr->pskeyfree[ikeynumber];
214  }
215 }
int iserrio
Definition: vbmemio.c:28
static struct VBTREE * pstreefree
Definition: vbmemio.c:23
void vvblockfree(struct VBLOCK *pslock)
Definition: vbmemio.c:81
#define vb_error(x)
Definition: vbmemio.c:41
struct VBTREE * pschild
Definition: isinternal.h:327
struct VBKEY * pskeylast
Definition: isinternal.h:341
struct VBLOCK * psnext
Definition: isinternal.h:318
int ihandle
Definition: isinternal.h:319
int isreclen
Definition: vbmemio.c:29
struct VBLOCK * psvblockallocate(const int ihandle)
Definition: vbmemio.c:67
struct VBTREE * psnext
Definition: isinternal.h:338
struct VBTREE * psvbtreeallocate(const int ihandle)
Definition: vbmemio.c:88
void * pvvbmalloc(const size_t size)
Definition: vbmemio.c:45
#define unlikely(x)
Definition: common.h:437
struct VBKEY * pskeycurr
Definition: isinternal.h:342
EC ARGUMENT EC EC BOUND EC BOUND EC BOUND EC BOUND TABLE EC DATA EC DATA EC DATA PTR NULL
Definition: exception.def:95
struct VBKEY * pskeyfree[32]
Definition: isinternal.h:447
off_t tnodenumber
Definition: isinternal.h:343
off_t trownode
Definition: isinternal.h:328
unsigned int ikeysinnode
Definition: isinternal.h:346
struct VBKEY * psprev
Definition: isinternal.h:325
struct DICTINFO * psvbfile[128+1]
Definition: vblowlevel.c:23
static struct VBLOCK * pslockfree
Definition: vbmemio.c:22
void vvbfree(void *mptr)
Definition: vbmemio.c:59
struct VBTREE * pstree[32]
Definition: isinternal.h:446
void vvbkeyfree(const int ihandle, const int ikeynumber, struct VBKEY *pskey)
Definition: vbmemio.c:172
void vvbtreeallfree(const int ihandle, const int ikeynumber, struct VBTREE *pstree)
Definition: vbmemio.c:105
struct VBKEY * pskeyfirst
Definition: isinternal.h:340
void vvbkeyallfree(const int ihandle, const int ikeynumber, struct VBTREE *pstree)
Definition: vbmemio.c:143
struct VBKEY * psvbkeyallocate(const int ihandle, const int ikeynumber)
Definition: vbmemio.c:120
struct keydesc * pskeydesc[32]
Definition: isinternal.h:445
struct VBKEY * psnext
Definition: isinternal.h:324
void vvbkeyunmalloc(const int ihandle, const int ikeynumber)
Definition: vbmemio.c:196
int iserrno
Definition: vbmemio.c:27
int isrecnum
Definition: vbmemio.c:30