libsrc/alloc.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 
00022 /**********************************************************************
00023  * @file  alloc.cxx
00024  * @brief xxx
00025  *
00026  * (to be defined)
00027  *
00028  **********************************************************************/
00029 
00030 /*
00031  * $Log: alloc.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/11 11:53:40  jrush
00037  * Added missing autotools internal file.
00038  *
00039  * Revision 1.10  2004/09/04 16:02:17  jrush
00040  * Added Doxygen headers before each and every function definition.
00041  *
00042  * Revision 1.9  2002/07/26 04:30:29  jrush
00043  * Replaced gerror() with assert()
00044  *
00045  * Revision 1.8  2002/05/28 04:22:29  jrush
00046  * Adjusted source files to comply with GPL licensing.
00047  *
00048  * Revision 1.7  2002/05/28 02:49:42  jrush
00049  * Made static any functions and global variables private to this source file
00050  * and commented out any unused functions.
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  * Revision 1.2  2002/01/31 11:32:47  jrush
00082  * Ran C source thru 'ident' program; added void return type to those
00083  * functions missing any return type whatsoever; and converted args
00084  * from old K&R to new ANSI C inline style.  Also added basic DOxygen
00085  * source-markup header to top of each file, as well as a copyright
00086  * notice at the bottom.
00087  *
00088  */
00089 
00090 #include <unistd.h>
00091 #include <sys/errno.h>
00092 #include "udanax.h"
00093 
00094 //extern int errno;
00095 extern int ingrimreaper;
00096 
00097 extern int incrementalallocsize /* = INCREMENTALALLOCSIZE */;
00098 extern int allocsize /* = ALLOCSIZE */;
00099 
00100 static HEADER base;
00101 static HEADER *allocp = NULL;
00102 static HEADER baseallocated;
00103 
00104 static int flag = 0;
00105 static char *zzalloctop = 0, *zzallocbot = 0;
00106 static int alloccount = 0;
00107 
00115     void
00116 lookatalloc()
00117 {
00118     lookatalloc2(&baseallocated);
00119 }
00120 
00128     static int
00129 statusofalloc(
00130     char * /*c*/)
00131 {
00132     unsigned number, maxsize, cumsize;
00133 
00134 /* checkalloc (c); */
00135     number = maxsize = cumsize = 0;
00136 
00137     HEADER *r;
00138     for (r = base.s.ptr; flag && (r != &base); r = r->s.ptr) {
00139         maxsize = (maxsize > r->s.size ? maxsize : r->s.size);
00140         cumsize += r->s.size;
00141         number++;
00142     }
00143     L("%d free hunks %d maxsize %d total free\n", number, maxsize, cumsize);
00144     return true;
00145 }
00146 
00154     static int
00155 validallocthinge(
00156     char *ptr)
00157 {
00158     return 1;
00159 
00160 //    if (!flag)
00161 //        return true;
00162 //
00163 //    HEADER *p = (HEADER *) ptr;
00164 //    if (p->s.size == 0 && p != * (HEADER *)zzalloctop) {
00165 //        L("validallocthinge called with ptr = %x bottom = %x top = %x\n", ptr, zzallocbot, zzalloctop);
00166 //        statusofalloc("");
00167 //        assert(0); // attempt to free zero size thing
00168 //    }
00169 //
00170 //    if (flag && (unsigned) ptr > (unsigned) zzalloctop || (unsigned) ptr < (unsigned) zzallocbot) {
00171 //        L("validallocthinge called with ptr = %x bottom = %x top = %xn", ptr, zzallocbot, zzalloctop);
00172 //        assert(0); // bad validity check in alloc
00173 //        return 0;
00174 //    }
00175 //    return true;
00176 }
00177 
00185     int
00186 checkalloc(
00187     char *c)
00188 {
00189     HEADER *r, *oldr;
00190     unsigned size;
00191 
00192     if (!flag)
00193         return /*BUG: original code returned nothing, I guess I'll return a zero*/ 0;
00194 
00195     if (debug && *c)
00196         L("%s", c);
00197 
00198     unsigned oldsize = 0;
00199     for (oldr = r = base.s.ptr; flag && (r != &base) && (r != (HEADER *) zzalloctop); r = r + r->s.size) {
00200         size = r->s.size;
00201         validallocthinge((char *) r);
00202         if (r->s.size == 0 && r != (HEADER *) zzalloctop) {
00203             L("checkalloc glorphed with ptr = %x bottom = %x top = %x\n", (int) r, (int) zzallocbot, (int) zzalloctop);
00204             statusofalloc("");
00205             assert(0); // found zero size in alloced stuff
00206             return 1;
00207         }
00208         oldr = r;
00209         oldsize = size;
00210     }
00211     return true;
00212 }
00213 
00221     static HEADER *
00222 weresurethatthisisourmorecore()
00223 {
00224     HEADER *up;
00225     int temp;
00226 
00227     if (flag)
00228         return NULL;
00229 
00230     char *cp   = (char *) sbrk(allocsize);
00231     zzalloctop = (char *)cp + allocsize;
00232     zzallocbot = (char *)cp;
00233 
00234     if ((int) cp == -1) {
00235         flag = 1;
00236         return NULL;
00237     }
00238 
00239     up   = (HEADER *) cp;
00240     temp = up->s.size = ((unsigned) allocsize) / sizeof(HEADER);
00241 
00242     if (temp == 0)
00243         assert(0); // this dumb fucker doesnt do arithmetic right
00244 
00245     baseallocated.s.ptr = (HEADER *) cp;
00246     baseallocated.s.size = temp;
00247     ffree((char *)(cp + sizeof(HEADER) /* up +1 */ ));
00248     flag = 1;
00249 
00250     return allocp;
00251 }
00252 
00260     int *
00261 falloc(
00262     unsigned nbytes)
00263 {
00264     register HEADER *p, *q;
00265     register unsigned nunits;
00266 
00267 /* if (errno) { if (errno != ENOTTY) perror ("xumain/backend/alloc"); errno = 0; } */
00268 
00269     if (ingrimreaper) {
00270         L("really error in falloc called from under grimreaper\n");
00271         assert(0); // falloc called from under grimreaper
00272     }
00273 
00274     assert(nbytes > 0); // falloc called with nbytes = 0
00275 
00276     //L("falloc called with %d\n",nbytes);
00277 
00278     alloccount++;
00279     nunits = 1 + (nbytes + sizeof(HEADER) - 1) / sizeof(HEADER);
00280     if ((q = allocp) == NULL) {
00281         base.s.ptr = allocp = q = &base;
00282         base.s.size = 0;
00283     } else {
00284 /* checkalloc(""); */
00285     }
00286     for (p = q->s.ptr;; q = p, p = p->s.ptr) {
00287         if (p->s.size >= nunits) {
00288             if (p->s.size == nunits)
00289                 q->s.ptr = p->s.ptr;
00290             else {
00291                 p->s.size -= nunits;
00292                 p += p->s.size;
00293                 p->s.size = nunits;
00294             }
00295             p->s.ptr = NULL;
00296             allocp = q;
00297             return (int *) (p + 1);
00298         }
00299         if (p == allocp)
00300             if ((p = weresurethatthisisourmorecore()) == NULL)
00301                 return NULL;
00302     }
00303 }
00304 
00312     void
00313 ffree(
00314     char *ap)
00315 {
00316     register HEADER *p, *q;
00317 
00318 /* if (errno) { if (errno != ENOTTY) perror ("xumain/backend/free"); errno = 0; } */
00319     p = (HEADER *) ap - 1;
00320     if (flag) {
00321 
00322         if (!validallocthinge((char *) p))
00323             assert(0); // wierd free in alloc
00324     }
00325 
00326     // memset(ap, 0, (p->s.size - 1) * sizeof(HEADER));
00327 
00328     for (q = allocp; !(p > q && p < q->s.ptr); q = q->s.ptr) {
00329         if (q >= q->s.ptr && (p > q || p < q->s.ptr)) {
00330             break;
00331         }
00332     }
00333 
00334     if (p + p->s.size == q->s.ptr) {
00335         p->s.size += q->s.ptr->s.size;
00336         p->s.ptr = q->s.ptr->s.ptr;
00337     } else {
00338         p->s.ptr = q->s.ptr;
00339     }
00340     if (q + q->s.size == p) {
00341         q->s.size += p->s.size;
00342         q->s.ptr = p->s.ptr;
00343     } else {
00344         q->s.ptr = p;
00345     }
00346     allocp = q;
00347 /* checkalloc(""); */
00348 }

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