Open64 (mfef90, whirl2f, and IR tools)  TAG: version-openad; SVN changeset: 916
cifspace.c
Go to the documentation of this file.
00001 /*
00002 
00003   Copyright (C) 2000, 2001 Silicon Graphics, Inc.  All Rights Reserved.
00004 
00005   This program is free software; you can redistribute it and/or modify it
00006   under the terms of version 2.1 of the GNU Lesser General Public License 
00007   as published by the Free Software Foundation.
00008 
00009   This program is distributed in the hope that it would be useful, but
00010   WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
00012 
00013   Further, this software is distributed without any warranty that it is
00014   free of the rightful claim of any third person regarding infringement 
00015   or the like.  Any license provided herein, whether implied or 
00016   otherwise, applies only to this software file.  Patent licenses, if
00017   any, provided herein do not apply to combinations of this program with 
00018   other software, or any other product whatsoever.  
00019 
00020   You should have received a copy of the GNU Lesser General Public 
00021   License along with this program; if not, write the Free Software 
00022   Foundation, Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, 
00023   USA.
00024 
00025   Contact information:  Silicon Graphics, Inc., 1600 Amphitheatre Pky,
00026   Mountain View, CA 94043, or:
00027 
00028   http://www.sgi.com
00029 
00030   For further information regarding this notice, see:
00031 
00032   http://oss.sgi.com/projects/GenInfo/NoticeExplan
00033 
00034 */
00035 
00036 
00037 static char USMID[] = "@(#) libcif/cifspace.c   30.2    07/26/96 07:19:13";
00038 
00039 
00040 /* --------------------------------------------------------------------------
00041  * CIF memory management routines
00042  * Tabs are set up to be read with tab spacing = 3
00043  * --------------------------------------------------------------------------
00044  */
00045 
00046 #define CIF_VERSION 3
00047 
00048 #ifdef _ABSOFT
00049 #include "cif.h"
00050 #else
00051 #include <cif.h>
00052 #endif
00053 
00054 #include <stdlib.h>
00055 #include <memory.h>
00056 #include <stdio.h>
00057 #include <stdlib.h>
00058 
00059 #include "cif_int.h"
00060 
00061 #define MEMSIZE(X)      ((X<CIF_BUFSIZE?CIF_BUFSIZE:X))
00062 #define ROUND(X)                (((X+sizeof(long)-1)/sizeof(long)*sizeof(long)))
00063 
00064 /* -------------------------------------------------------------------------
00065  * _Cif_memtbl constructs the _Cif_memarea table.
00066  * -------------------------------------------------------------------------
00067  */
00068 int _Cif_memtbl ()
00069 {
00070         int me, size;
00071 
00072         size = CIF_MEM_BUMP * sizeof(struct _Cif_Mem_Area);
00073         if ((_Cif_memarea = (struct _Cif_Mem_Area *) malloc (size)) == NULL)
00074                 return (CIF_NOMEM);
00075         _Cif_memasize = CIF_MEM_BUMP;
00076         for (me = 1; me < CIF_MEM_BUMP; me++) {
00077                 _Cif_memarea[me].used = NO;
00078                 _Cif_memarea[me].mbp = NULL;
00079         }
00080         _Cif_memarea[0].used = YES;
00081         return (0);
00082 
00083 }
00084 
00085 /* -------------------------------------------------------------------------
00086  * "_Cif_mementry" retrieves an open entry in _Cif_Memarea.  It returns either
00087  * a negative status or a positive index into the table.
00088  * -------------------------------------------------------------------------
00089  */
00090 
00091 int _Cif_mementry(bsize)
00092 unsigned bsize;                         /* size of buffer required */
00093 {
00094 
00095         int me, oldsize;
00096 
00097         /* Look for unused entry with buffer attached.  If none available, look
00098          * for unused entry without a buffer and get one.  Otherwise expand the
00099          * table, get an entry and a buffer.
00100          */
00101 
00102         for (me = 1; me < _Cif_memasize; me++) {
00103                 if (_Cif_memarea[me].used == NO && _Cif_memarea[me].mbp != NULL &&
00104                         _Cif_memarea[me].msize >= bsize) break;
00105         }
00106         if (me >= _Cif_memasize) {
00107                 for (me = 1; me < _Cif_memasize; me++) {
00108                         if (_Cif_memarea[me].used == NO && _Cif_memarea[me].mbp == NULL)
00109                                 break;
00110                 }
00111         }
00112         if (me >= _Cif_memasize) {
00113                 oldsize = _Cif_memasize;
00114                 _Cif_memasize += CIF_MEM_BUMP;
00115                 if ((_Cif_memarea = (struct _Cif_Mem_Area *)realloc(_Cif_memarea,
00116                                 _Cif_memasize * sizeof(struct _Cif_Mem_Area))) == NULL)
00117                         return (CIF_NOMEM);
00118                 for (me = oldsize; me < _Cif_memasize; me++) {
00119                         _Cif_memarea[me].used = NO;
00120                         _Cif_memarea[me].mbp = NULL;
00121                 }
00122                 me = oldsize;
00123         }
00124         if (_Cif_memarea[me].mbp == NULL) {
00125                 if ((_Cif_memarea[me].mbp = malloc (bsize)) == NULL)
00126                         return (CIF_NOMEM);
00127                 _Cif_memarea[me].msize = bsize;
00128         }
00129         _Cif_memarea[me].used = YES;
00130         _Cif_memarea[me].nme = 0;
00131         _Cif_memarea[me].mused = 0;
00132         return (me);
00133 
00134 }
00135 
00136 /* --------------------------------------------------------------------------
00137  * "_Cif_managed_space" acquires space to contain record structures when the
00138  * file memory management mode is CIF_MEM_MANAGED.
00139  * --------------------------------------------------------------------------
00140  */
00141 
00142 char *_Cif_managed_space
00143 #ifdef __STDC__
00144 (unsigned int size, int cifd)
00145 #else
00146 (size, cifd)
00147 unsigned int size;
00148 int cifd;
00149 #endif
00150 {
00151 
00152         int me, newme;  /* _Cif_memarea index */
00153         char *bp;               /* pointer to memory area returned */
00154 
00155         /*
00156          * Round size to a "long" boundary.  If enough space in current buffer,
00157          * use it, otherwise get a new buffer.
00158          */
00159 
00160         size = ROUND(size);
00161         me = _Cif_filetbl[cifd].lme;
00162         if (_Cif_memarea[me].mused + size > _Cif_memarea[me].msize) {
00163                 newme = _Cif_mementry(MEMSIZE(size));
00164                 if (newme < 0)
00165                         return ((char *)NULL);
00166                 _Cif_memarea[me].nme = _Cif_filetbl[cifd].lme = newme;
00167                 me = newme;
00168         }
00169         bp = _Cif_memarea[me].mbp + _Cif_memarea[me].mused;
00170         _Cif_memarea[me].mused += size;
00171         return (bp);
00172 }
00173 
00174 /* --------------------------------------------------------------------------
00175  * "_Cif_fixed_space" acquires space to contain record structures when the
00176  * file memory management mode is CIF_MEM_FIXED.
00177  * --------------------------------------------------------------------------
00178  */
00179 
00180 char *_Cif_fixed_space
00181 #ifdef __STDC__
00182 (unsigned int size, int cifd)
00183 #else
00184 (size, cifd)
00185 unsigned int size;
00186 int cifd;
00187 #endif
00188 {
00189 
00190         int me;
00191         char *bp;
00192 
00193         /*
00194          * Round size to a "long" boundary.  If enough space in current buffer,
00195          * use it, otherwise make the buffer bigger.
00196          */
00197 
00198         size = ROUND(size);
00199         me = _Cif_filetbl[cifd].fme;
00200         if (_Cif_memarea[me].mused + size > _Cif_memarea[me].msize) {
00201                 _Cif_memarea[me].msize += size;
00202                 _Cif_memarea[me].mbp = realloc (_Cif_memarea[me].mbp,
00203                                                 _Cif_memarea[me].msize);
00204         }
00205         bp = _Cif_memarea[me].mbp + _Cif_memarea[me].mused;
00206         _Cif_memarea[me].mused += size;
00207         return (bp);
00208 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines