configure: create some convenience AC_SUBST's for the global includes
[oweals/cde.git] / cde / lib / tt / mini_isam / isindfreel.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these libraries and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /*%%  (c) Copyright 1993, 1994 Hewlett-Packard Company                   */
24 /*%%  (c) Copyright 1993, 1994 International Business Machines Corp.     */
25 /*%%  (c) Copyright 1993, 1994 Sun Microsystems, Inc.                    */
26 /*%%  (c) Copyright 1993, 1994 Novell, Inc.                              */
27 /*%%  $XConsortium: isindfreel.c /main/3 1995/10/23 11:41:03 rswiston $                                                          */
28
29 /*
30  * Copyright (c) 1988 by Sun Microsystems, Inc.
31  */
32
33 /*
34  * isfreelist.c
35  *
36  * Description:
37  *      Free list maintenance functions
38  */
39
40 #include "isam_impl.h"
41
42 extern Bufhdr *_isdisk_fix();
43
44 /*
45  * blkno = _isfreel_alloc()
46  *
47  * Allocate a new index page.
48  */
49
50 Blkno 
51 _isindfreel_alloc(Fcb *fcb)
52 {
53     Bufhdr              *pbhdr;
54     char                *p;
55     int                 npointers;
56     Blkno               blkno;
57
58     if (fcb->indfreelist == FREELIST_NOPAGE) {
59
60         /* 
61          * We must write something to the buffer, or we will get 
62          * segmentation fault when using mapped I/O.
63          */
64         fcb->indsize = _extend_file(fcb, fcb->indfd, fcb->indsize);
65
66         return (fcb->indsize - 1);
67     }
68
69     pbhdr = _isdisk_fix(fcb, fcb->indfd, fcb->indfreelist, ISFIXWRITE);
70     p = pbhdr->isb_buffer;
71     
72     npointers = ldshort(p + FL_NPOINTERS_OFF);
73     
74     if (npointers > 0) {
75         blkno = ldblkno(p + FL_POINTERS_OFF + npointers * BLKNOSIZE);
76         npointers--;
77         stshort((short)npointers, p + FL_NPOINTERS_OFF);
78         
79         return (ldblkno(p + FL_POINTERS_OFF + npointers * BLKNOSIZE));
80     }
81     else {
82         blkno = fcb->indfreelist;
83         fcb->indfreelist = ldblkno(p + FL_NEXT_OFF);
84         
85         return (blkno);
86     }
87 }
88
89 /*
90  * _isfreel_free()
91  *
92  * Free an index page.
93  */
94
95 void
96 _isindfreel_free(Fcb *fcb, Blkno blkno)
97 {
98     Bufhdr              *pbhdr;
99     char                *p;
100     int                 npointers;
101
102     if (fcb->indfreelist != FREELIST_NOPAGE) {
103         pbhdr = _isdisk_fix(fcb, fcb->indfd, fcb->indfreelist, ISFIXWRITE);
104         p = pbhdr->isb_buffer;
105
106         npointers = ldshort(p + FL_NPOINTERS_OFF);
107         
108         if (npointers < FL_MAXNPOINTERS) {
109             stblkno(blkno, p + FL_POINTERS_OFF + npointers * BLKNOSIZE);
110             npointers++;
111             stshort((short)npointers, p + FL_NPOINTERS_OFF);
112
113             return;
114         }
115     }
116
117
118     pbhdr = _isdisk_fix(fcb, fcb->indfd, blkno, ISFIXWRITE);
119     p = pbhdr->isb_buffer;
120     
121     /* Mark page to indicate that it is in the free list. */
122     stshort((short)PT_FREELIST, p + FL_TYPE_OFF);
123
124     stshort((short)0, p + FL_NPOINTERS_OFF);
125     stblkno(fcb->indfreelist, p + FL_NEXT_OFF);
126
127     fcb->indfreelist = blkno;
128 }