libsrc/diskalloc.cxx

Go to the documentation of this file.
00001 /**********************************************************************
00002  * Copyright 2002 Jeff Rush <jrush@taupro.com>
00003  * Original Copyright 1979-2002 Udanax.com
00004  *
00005  * This file is part of the Udanax xanalogical storage system.
00006  *
00007  * Udanax is free software; you can redistribute it and/or modify it
00008  * under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * Udanax is distributed in the hope that it will be useful, but
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with Udanax; if not, write to the Free Software Foundation,
00019  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020  **********************************************************************/
00021 
00030 /* Modification History:
00031  * $Log: diskalloc.cxx,v $
00032  * Revision 1.12  2004/09/11 13:59:21  jrush
00033  * Changed all fprintf's to stderr to use the Nana library L() macro.  Also
00034  * removed a 2-3 minor compiler warnings.
00035  *
00036  * Revision 1.11  2004/09/04 16:02:17  jrush
00037  * Added Doxygen headers before each and every function definition.
00038  *
00039  * Revision 1.10  2002/07/26 04:30:29  jrush
00040  * Replaced gerror() with assert()
00041  *
00042  * Revision 1.9  2002/05/28 04:22:29  jrush
00043  * Adjusted source files to comply with GPL licensing.
00044  *
00045  * Revision 1.8  2002/05/28 02:49:42  jrush
00046  * Made static any functions and global variables private to this source file
00047  * and commented out any unused functions.
00048  *
00049  * Revision 1.7  2002/04/13 13:44:10  jrush
00050  * Renamed struct _diskheader to Diskheader, for C++ style.
00051  *
00052  * Revision 1.6  2002/04/12 22:53:20  jrush
00053  * Changed from using my setmem() to the clib's memset() and removed no
00054  * longer-needed usefull.cxx
00055  *
00056  * Revision 1.5  2002/04/12 11:56:42  jrush
00057  * Reorganized include file layout, renamed xanadu.h to udanax.h and
00058  * typecontext/typecrumcontext to C++ class Context/CrumContext.
00059  *
00060  * Revision 1.4  2002/04/06 19:51:30  jrush
00061  * Renamed TRUE/FALSE constant use to the C++ standard of true/false.
00062  *
00063  * Revision 1.3  2002/04/06 15:01:17  jrush
00064  * Changed INT to just 'int'.
00065  *
00066  * Revision 1.2  2002/02/14 09:27:43  jrush
00067  * Cleaned up source:
00068  *
00069  * 1. ran thru the indent tool to achieve a standard look,
00070  * 2. added structured comments at top for use with DOxygen reporting
00071  *    as well as CVS keywords,
00072  * 3. fixed compiler warnings re ambiguous assign/compares,
00073  *    needed casts and unused/uninitialized variables,
00074  * 4. fixed funcs that didn't specify a return type,
00075  * 5. centralized prototypes in protos.h, removing incomplete ones,
00076  * 6. cleaned up use of bool/BOOLEAN type to suit C++ type,
00077  * 7. fixed initializer nesting in tumbler constants,
00078  * 8. renamed vars that conflict with C++ keywords (new, this),
00079  * 9. fixed global/extern confusion re some global vars.
00080  *
00081  */
00082 
00083 #include <sys/types.h>
00084 #include <sys/stat.h>
00085 #include <fcntl.h>
00086 #include <unistd.h>
00087 #include <memory.h>
00088 #include "udanax.h"
00089 
00090 extern int enffiledes;
00091 extern bool isxumain;
00092 
00093 static bool maximumsetupsizehasbeenhit = false;
00094 int maximumsetupsize = 0; // ref from servers
00095 
00096 Diskheader diskheader; //ref by corediskout.cxx only
00097 
00098 static char mask[8] = { 1, 2, 4, 8, 16, 32, 64, 128 };
00099 
00100 //UNUSED static int // UNUSED
00101 //UNUSED isalloced(int n)
00102 //UNUSED {
00103 //UNUSED     return (diskheader.bitmap[n / 8] | mask[n % 8]);
00104 //UNUSED }
00105 
00113     typediskloafptr
00114 diskalloc()
00115 /* Find an unallocated loaf on the the disk, * allocate it (take it off the free list), and * return something thru
00116  * which I can deal with it. */
00117 {
00118     int i, j;
00119     typediskloafptr ret;
00120 
00121     for (i = 0; i < BITMAPSIZE && !diskheader.bitmap[i]; ++i)
00122         ;
00123 
00124     if (i == BITMAPSIZE)
00125         assert(0); // Out of diskalloc bitmap space
00126 
00127     for (j = 0; j < 8; ++j) {
00128         if (diskheader.bitmap[i] & mask[j]) {
00129             diskheader.bitmap[i] = diskheader.bitmap[i] & ~mask[j];
00130             break;
00131         }
00132     }
00133     ret.diskblocknumber = i * 8 + j;
00134     ret.insidediskblocknumber = 0 /* 1 */ ;
00135 
00136     if (maximumsetupsize && ret.diskblocknumber >= maximumsetupsize)
00137         maximumsetupsizehasbeenhit = true;
00138 
00139     return ret;
00140 }
00141 
00149 /* Return the named piece of disk to free space, * i.e. de-allocate it */
00150     void
00151 diskfree(
00152     typediskloafptrdigit loafptr)
00153 {
00154     if (loafptr == DISKPTRNULL)
00155         return;
00156 
00157     if (!goodblock(loafptr))
00158         assert(0); // Unallocated block in diskfree
00159 
00160     diskheader.bitmap[loafptr / 8] = diskheader.bitmap[loafptr / 8] | mask[loafptr % 8];
00161 }
00162 
00170     void
00171 diskset(
00172     typediskloafptrdigit loafptr)
00173 {
00174     if (loafptr == DISKPTRNULL)
00175         return;
00176 
00177     if (!goodblock(loafptr))
00178         assert(0); // Unallocated block in diskfree
00179 
00180     diskheader.bitmap[loafptr / 8] = diskheader.bitmap[loafptr / 8] & ~mask[loafptr % 8];
00181 }
00182 
00190     bool                                   /* false is not good enfilade file */
00191 readallocinfo(
00192     int fd)
00193 {
00194     if (lseek(fd, 0L, 0) < 0) {
00195         perror("lseek in readallocinfo");
00196         assert(0); // lseek failed
00197     }
00198 
00199     if (read(fd, (char *)&diskheader, sizeof(diskheader)) <= 0) {
00200         perror("read");
00201         assert(0); // read
00202     }
00203 
00204     bool ret = diskheader.hasenftops;
00205     if (ret) {
00206         enffiledes = fd;
00207         readpartialdiskalloctablefromdisk();
00208     } else {
00209         L("Old enffile invalid... re-initializing\n");
00210         initheader();
00211     }
00212 
00213     return ret;
00214 }
00215 
00223     void
00224 initheader()
00225 {
00226     memset(diskheader.bitmap, 0xFF, sizeof(diskheader.bitmap)); /* free all */
00227     clear(diskheader.bitmap, (NUMDISKLOAFSINHEADER + 3) / 8 + 1);
00228 
00229 /* unfree bitmap and granf and spanf */
00230 /* zzzz this rounds up a byte thus may reserve unused blocks fix by clearing bits ! */
00231 
00232     diskheader.hasenftops = false;
00233     initincorealloctables();
00234 }
00235 
00243     void
00244 diskallocexit(
00245     int fd)
00246 {
00247     diskheader.hasenftops = true;
00248     savepartialdiskalloctabletodisk();
00249     writeallocinfo(fd);
00250 }
00251 
00259     void
00260 writeallocinfo(
00261     int fd)
00262 {
00263     if (lseek(fd, 0L, 0) < 0) {
00264         perror("lseek in writeallocinfo");
00265         assert(0); // lseek failed
00266     }
00267 
00268     if (write(fd, (char *) &diskheader, sizeof(diskheader)) == -1) {
00269         perror("write in writeallocinfo");
00270         assert(0); // write failed
00271     }
00272 }
00273 
00281     bool
00282 goodblock(
00283     typediskloafptrdigit diskptr)
00284 {
00285     if (diskptr == 0)
00286         return false;
00287 
00288     return !(diskheader.bitmap[diskptr / 8] & mask[diskptr % 8]);
00289 }

Generated on Sun Aug 21 04:18:14 2005 for Udanax-Green by doxygen1.3.4