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 #if defined(BUILD_OS_DARWIN) 00055 #include <stdlib.h> 00056 #else /* defined(BUILD_OS_DARWIN) */ 00057 #include <malloc.h> 00058 #endif /* defined(BUILD_OS_DARWIN) */ 00059 #include <memory.h> 00060 #include <stdio.h> 00061 #include <stdlib.h> 00062 00063 #include "cif_int.h" 00064 00065 #define MEMSIZE(X) ((X<CIF_BUFSIZE?CIF_BUFSIZE:X)) 00066 #define ROUND(X) (((X+sizeof(long)-1)/sizeof(long)*sizeof(long))) 00067 00068 /* ------------------------------------------------------------------------- 00069 * _Cif_memtbl constructs the _Cif_memarea table. 00070 * ------------------------------------------------------------------------- 00071 */ 00072 int _Cif_memtbl () 00073 { 00074 int me, size; 00075 00076 size = CIF_MEM_BUMP * sizeof(struct _Cif_Mem_Area); 00077 if ((_Cif_memarea = (struct _Cif_Mem_Area *) malloc (size)) == NULL) 00078 return (CIF_NOMEM); 00079 _Cif_memasize = CIF_MEM_BUMP; 00080 for (me = 1; me < CIF_MEM_BUMP; me++) { 00081 _Cif_memarea[me].used = NO; 00082 _Cif_memarea[me].mbp = NULL; 00083 } 00084 _Cif_memarea[0].used = YES; 00085 return (0); 00086 00087 } 00088 00089 /* ------------------------------------------------------------------------- 00090 * "_Cif_mementry" retrieves an open entry in _Cif_Memarea. It returns either 00091 * a negative status or a positive index into the table. 00092 * ------------------------------------------------------------------------- 00093 */ 00094 00095 int _Cif_mementry(bsize) 00096 unsigned bsize; /* size of buffer required */ 00097 { 00098 00099 int me, oldsize; 00100 00101 /* Look for unused entry with buffer attached. If none available, look 00102 * for unused entry without a buffer and get one. Otherwise expand the 00103 * table, get an entry and a buffer. 00104 */ 00105 00106 for (me = 1; me < _Cif_memasize; me++) { 00107 if (_Cif_memarea[me].used == NO && _Cif_memarea[me].mbp != NULL && 00108 _Cif_memarea[me].msize >= bsize) break; 00109 } 00110 if (me >= _Cif_memasize) { 00111 for (me = 1; me < _Cif_memasize; me++) { 00112 if (_Cif_memarea[me].used == NO && _Cif_memarea[me].mbp == NULL) 00113 break; 00114 } 00115 } 00116 if (me >= _Cif_memasize) { 00117 oldsize = _Cif_memasize; 00118 _Cif_memasize += CIF_MEM_BUMP; 00119 if ((_Cif_memarea = (struct _Cif_Mem_Area *)realloc(_Cif_memarea, 00120 _Cif_memasize * sizeof(struct _Cif_Mem_Area))) == NULL) 00121 return (CIF_NOMEM); 00122 for (me = oldsize; me < _Cif_memasize; me++) { 00123 _Cif_memarea[me].used = NO; 00124 _Cif_memarea[me].mbp = NULL; 00125 } 00126 me = oldsize; 00127 } 00128 if (_Cif_memarea[me].mbp == NULL) { 00129 if ((_Cif_memarea[me].mbp = malloc (bsize)) == NULL) 00130 return (CIF_NOMEM); 00131 _Cif_memarea[me].msize = bsize; 00132 } 00133 _Cif_memarea[me].used = YES; 00134 _Cif_memarea[me].nme = 0; 00135 _Cif_memarea[me].mused = 0; 00136 return (me); 00137 00138 } 00139 00140 /* -------------------------------------------------------------------------- 00141 * "_Cif_managed_space" acquires space to contain record structures when the 00142 * file memory management mode is CIF_MEM_MANAGED. 00143 * -------------------------------------------------------------------------- 00144 */ 00145 00146 char *_Cif_managed_space 00147 #ifdef __STDC__ 00148 (unsigned int size, int cifd) 00149 #else 00150 (size, cifd) 00151 unsigned int size; 00152 int cifd; 00153 #endif 00154 { 00155 00156 int me, newme; /* _Cif_memarea index */ 00157 char *bp; /* pointer to memory area returned */ 00158 00159 /* 00160 * Round size to a "long" boundary. If enough space in current buffer, 00161 * use it, otherwise get a new buffer. 00162 */ 00163 00164 size = ROUND(size); 00165 me = _Cif_filetbl[cifd].lme; 00166 if (_Cif_memarea[me].mused + size > _Cif_memarea[me].msize) { 00167 newme = _Cif_mementry(MEMSIZE(size)); 00168 if (newme < 0) 00169 return ((char *)NULL); 00170 _Cif_memarea[me].nme = _Cif_filetbl[cifd].lme = newme; 00171 me = newme; 00172 } 00173 bp = _Cif_memarea[me].mbp + _Cif_memarea[me].mused; 00174 _Cif_memarea[me].mused += size; 00175 return (bp); 00176 } 00177 00178 /* -------------------------------------------------------------------------- 00179 * "_Cif_fixed_space" acquires space to contain record structures when the 00180 * file memory management mode is CIF_MEM_FIXED. 00181 * -------------------------------------------------------------------------- 00182 */ 00183 00184 char *_Cif_fixed_space 00185 #ifdef __STDC__ 00186 (unsigned int size, int cifd) 00187 #else 00188 (size, cifd) 00189 unsigned int size; 00190 int cifd; 00191 #endif 00192 { 00193 00194 int me; 00195 char *bp; 00196 00197 /* 00198 * Round size to a "long" boundary. If enough space in current buffer, 00199 * use it, otherwise make the buffer bigger. 00200 */ 00201 00202 size = ROUND(size); 00203 me = _Cif_filetbl[cifd].fme; 00204 if (_Cif_memarea[me].mused + size > _Cif_memarea[me].msize) { 00205 _Cif_memarea[me].msize += size; 00206 _Cif_memarea[me].mbp = realloc (_Cif_memarea[me].mbp, 00207 _Cif_memarea[me].msize); 00208 } 00209 bp = _Cif_memarea[me].mbp + _Cif_memarea[me].mused; 00210 _Cif_memarea[me].mused += size; 00211 return (bp); 00212 }
1.5.6