| 1 |
/* @(#)osmemory.c 19.1 (ES0-DMD) 02/25/03 13:56:13 */ |
| 2 |
/*=========================================================================== |
| 3 |
Copyright (C) 1995 European Southern Observatory (ESO) |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or |
| 6 |
modify it under the terms of the GNU General Public License as |
| 7 |
published by the Free Software Foundation; either version 2 of |
| 8 |
the License, or (at your option) any later version. |
| 9 |
|
| 10 |
This program is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
GNU General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU General Public |
| 16 |
License along with this program; if not, write to the Free |
| 17 |
Software Foundation, Inc., 675 Massachusetss Ave, Cambridge, |
| 18 |
MA 02139, USA. |
| 19 |
|
| 20 |
Corresponding concerning ESO-MIDAS should be addressed as follows: |
| 21 |
Internet e-mail: [email protected] |
| 22 |
Postal address: European Southern Observatory |
| 23 |
Data Management Division |
| 24 |
Karl-Schwarzschild-Strasse 2 |
| 25 |
D 85748 Garching bei Muenchen |
| 26 |
GERMANY |
| 27 |
===========================================================================*/ |
| 28 |
|
| 29 |
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 30 |
.TYPE Module |
| 31 |
.IDENTIFICATION osmemory |
| 32 |
.AUTHOR Marc Wenger [CDS], Francois Ochsenbein [ESO-IPG] |
| 33 |
.LANGUAGE C |
| 34 |
.VERSION 1 Originally written by Marc Wenger (CDS). |
| 35 |
Adapted by Francois Ochsenbein 28-Oct-1986 |
| 36 |
.VERSION 2.0 13-Oct-1988: Adapted for VMS |
| 37 |
.VERSION 2.1 26-Jun-1990: Use SYS$EXPREG system service |
| 38 |
.KEYWORDS Memory Management |
| 39 |
.ENVIRONMENT VAX/VMS |
| 40 |
.COMMENTS |
| 41 |
These routines allow the management of memory to allocate |
| 42 |
pieces of memory, e.g. for temporary copy of |
| 43 |
strings. It's especially useful for string communication |
| 44 |
between FORTRAN and C. |
| 45 |
\begin{TeX} |
| 46 |
\\ |
| 47 |
Note that the allocated memory is never freed --- but may be |
| 48 |
reused. The memory is decomposed into ``areas'', in principle multiple |
| 49 |
of the page size (defined as MEM\_AMOUNT), and each area |
| 50 |
may contain several ``zones''. areas and zones are preceded |
| 51 |
by a `memory header' made of : |
| 52 |
\begin{itemize} |
| 53 |
\item the length of the area / zone |
| 54 |
\item chain pointer (to next for free zones, to preceding for occupied |
| 55 |
zones) |
| 56 |
\end{itemize} |
| 57 |
|
| 58 |
The complete memory structure is described via a ``memory packet'' |
| 59 |
internal to this module. |
| 60 |
|
| 61 |
{\bf Note}: In the Minimal Implementation, use just interfaces to |
| 62 |
malloc / free / realloc routines. |
| 63 |
\end{TeX} |
| 64 |
|
| 65 |
----------------------------------------------------------------------------*/ |
| 66 |
|
| 67 |
#define DEBUG 0 /* For debugging purposes */ |
| 68 |
|
| 69 |
#define MEM_AMOUNT 2048 /* Memory Amount Allocation */ |
| 70 |
|
| 71 |
#include <osdefos.h> |
| 72 |
#include <macrogen.h> |
| 73 |
#include <errno.h> |
| 74 |
#include <ssdef.h> |
| 75 |
|
| 76 |
MID_EXTERN int oserror; /* system error codes */ |
| 77 |
MID_EXTERN int vmserror; /* VMS-specific error code */ |
| 78 |
|
| 79 |
|
| 80 |
typedef struct header { |
| 81 |
long int nbytes ; /* Byte length, INCLUDING header size */ |
| 82 |
struct header *anext ; /* Chain pointer */ |
| 83 |
} HEADER ; |
| 84 |
#define NULL_HEADER NULL_PTR(HEADER) |
| 85 |
|
| 86 |
MID_STATIC struct { /* Initial describing packet */ |
| 87 |
long int lfree ; /* length of free zones */ |
| 88 |
HEADER *afree ; /* adr. of first free zone */ |
| 89 |
long int larea ; /* length of allocated zones */ |
| 90 |
HEADER *aused ; /* adr. of last used zone */ |
| 91 |
int nfree ; /* number of free zones */ |
| 92 |
} MMPKT = { 0,NULL_HEADER,0,NULL_HEADER, 0}; |
| 93 |
|
| 94 |
static HEADER *osmlink(), *osmget(); |
| 95 |
|
| 96 |
/*==========================================================================*/ |
| 97 |
static HEADER *osmlink(adri) |
| 98 |
/*+++ |
| 99 |
.PURPOSE Insert into the chain of free zones the adri zone. |
| 100 |
.RETURNS Address of previous zone |
| 101 |
.REMARKS Internal routine, not traced. |
| 102 |
---*/ |
| 103 |
HEADER *adri ; /* IN: adress of zone to insert */ |
| 104 |
{ |
| 105 |
register HEADER *adr ; |
| 106 |
|
| 107 |
/* Look where to place the new zone, such that |
| 108 |
* the sequence of addresses is respected */ |
| 109 |
|
| 110 |
for (adr = (HEADER *)(&MMPKT) ; |
| 111 |
(adr->anext) && (adri > adr->anext); adr = adr->anext) ; |
| 112 |
|
| 113 |
/* adr points to the preceding zone. Link */ |
| 114 |
|
| 115 |
adri->anext = adr->anext ; |
| 116 |
adr->anext = adri ; |
| 117 |
|
| 118 |
MMPKT.lfree += adri->nbytes; /* for statistics */ |
| 119 |
MMPKT.nfree++ ; /* for statistics */ |
| 120 |
|
| 121 |
return(adr) ; |
| 122 |
} |
| 123 |
|
| 124 |
/*==========================================================================*/ |
| 125 |
static HEADER *osmget(nbytes) |
| 126 |
/*+++ |
| 127 |
.PURPOSE Allocation and insertion of a new zone. |
| 128 |
The new zone is always a multiple of a page. |
| 129 |
.RETURNS Address of allocated area / NULL_HEADER if error. |
| 130 |
.REMARKS Internal routine, not traced. |
| 131 |
---*/ |
| 132 |
long int nbytes ; /* IN: Length (bytes) of this zone */ |
| 133 |
{ |
| 134 |
HEADER *adr[2] ; |
| 135 |
long int length; |
| 136 |
|
| 137 |
length = (nbytes + MEM_AMOUNT - 1)/MEM_AMOUNT; |
| 138 |
/* Integer number of pages */ |
| 139 |
length *= MEM_AMOUNT; |
| 140 |
|
| 141 |
vmserror = SYS$EXPREG(length/512, adr, 0, 0); |
| 142 |
if (!(vmserror&1)) |
| 143 |
{ oserror = ENOMEM; |
| 144 |
return(NULL_HEADER) ; |
| 145 |
} |
| 146 |
|
| 147 |
MMPKT.larea += length; /* For statistics */ |
| 148 |
|
| 149 |
adr[0]->nbytes = length; |
| 150 |
osmlink(adr[0]); /* Complete the chain of free zones */ |
| 151 |
|
| 152 |
return(adr[0]); |
| 153 |
} |
| 154 |
|
| 155 |
/*==========================================================================*/ |
| 156 |
char *osmmget(nbytes) |
| 157 |
/*+++ |
| 158 |
.PURPOSE Allocate a zone |
| 159 |
.RETURNS Address of usable zone, or NULL (failed); oserror contains |
| 160 |
then the error code. |
| 161 |
.REMARKS |
| 162 |
---*/ |
| 163 |
int nbytes ; /* IN: Required length (bytes) */ |
| 164 |
{ |
| 165 |
HEADER *adr, *aprevious, *aprefit, *adrfit; |
| 166 |
long int length, lenfit; |
| 167 |
|
| 168 |
adrfit = NULL_HEADER, lenfit = 999999999; |
| 169 |
|
| 170 |
/* Adjust the length: add header and express in header units */ |
| 171 |
|
| 172 |
length = (nbytes + 2*sizeof(HEADER) - 1) / sizeof(HEADER); |
| 173 |
length *= sizeof(HEADER); |
| 174 |
|
| 175 |
if (MMPKT.lfree >= length) |
| 176 |
/* Look for the best matching zone */ |
| 177 |
{ |
| 178 |
for (adr = MMPKT.afree , aprevious = (HEADER *)(&MMPKT); adr; |
| 179 |
aprevious = adr , adr = adr->anext) |
| 180 |
{ if (adr->nbytes < length) continue; |
| 181 |
if (adr->nbytes < lenfit) /* Better fit */ |
| 182 |
{ adrfit = adr, aprefit = aprevious; |
| 183 |
lenfit = adr->nbytes ; |
| 184 |
if (adr->nbytes == length) /* Exact match! */ |
| 185 |
break ; |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
/* Here, adrfit contains the address of the best available zone, |
| 191 |
* or NULL --- lenfit contains the total length of this zone */ |
| 192 |
|
| 193 |
/* If no zone is acceptable, then allocate a new area */ |
| 194 |
|
| 195 |
if (!adrfit) |
| 196 |
{ if(!osmget(length)) |
| 197 |
return(NULL_PTR(char)) ; |
| 198 |
return(osmmget((int)length)) ; |
| 199 |
/* The result of 1 recursive call is returned. Should always work! */ |
| 200 |
} |
| 201 |
|
| 202 |
/* If too few space remains, increase the number of asked bytes */ |
| 203 |
|
| 204 |
lenfit = adrfit->nbytes - length; /* Free bytes */ |
| 205 |
if (lenfit <= sizeof(HEADER)) length += lenfit; |
| 206 |
|
| 207 |
/* In the found zone is larger than the required number of bytes, |
| 208 |
* the begginning is free. The actual allocation is at the |
| 209 |
* end of the found zone. */ |
| 210 |
|
| 211 |
if ((adrfit->nbytes -= length) == 0) /* If the free zone exactly matches, |
| 212 |
* just one less free zone */ |
| 213 |
{ MMPKT.nfree-- ; |
| 214 |
aprefit->anext = adrfit->anext ; |
| 215 |
} |
| 216 |
|
| 217 |
MMPKT.lfree -= length ; /* statistics */ |
| 218 |
|
| 219 |
/* adrfit->nbytes contains the exact remaining length |
| 220 |
* Now, compute address of allocated zone, and store. */ |
| 221 |
|
| 222 |
adr = (HEADER *)((char *)adrfit + adrfit->nbytes); |
| 223 |
adr->nbytes = length ; |
| 224 |
adr->anext = MMPKT.aused, MMPKT.aused = adr; |
| 225 |
/* Insert in chains of allocated pieces */ |
| 226 |
|
| 227 |
/* TEST : Lists of : */ |
| 228 |
|
| 229 |
#if DEBUG |
| 230 |
list(adr, nbytes); |
| 231 |
#endif |
| 232 |
|
| 233 |
return((char*)(++adr)); |
| 234 |
} |
| 235 |
|
| 236 |
/*==========================================================================*/ |
| 237 |
int osmmfree(address) |
| 238 |
/*+++ |
| 239 |
.PURPOSE Free a zone allocated via osmmget |
| 240 |
.RETURNS 0 / -1 if error |
| 241 |
.REMARKS A null address to free returns 0 (no error). |
| 242 |
A -1 address frees the last zone (unstack). |
| 243 |
---*/ |
| 244 |
HEADER *address ; /* IN: adress to free */ |
| 245 |
{ |
| 246 |
HEADER *aprevious, *adr ; |
| 247 |
|
| 248 |
if (address == NULL_HEADER) return(0); |
| 249 |
adr = (address == (HEADER *)(-1) ? MMPKT.aused : address - 1); |
| 250 |
|
| 251 |
/* Check if was allocated */ |
| 252 |
|
| 253 |
for (aprevious = (HEADER *)(&MMPKT.larea); |
| 254 |
(aprevious->anext) && (aprevious->anext != adr); |
| 255 |
aprevious = aprevious->anext) ; |
| 256 |
|
| 257 |
if (aprevious->anext == NULL_HEADER) |
| 258 |
{ oserror = EFAULT; |
| 259 |
return(-1); |
| 260 |
} |
| 261 |
|
| 262 |
/* Remove from allocated areas */ |
| 263 |
|
| 264 |
aprevious->anext = adr->anext; |
| 265 |
|
| 266 |
|
| 267 |
/* insert the zone in the "Free" chain */ |
| 268 |
|
| 269 |
aprevious = osmlink(adr); |
| 270 |
|
| 271 |
|
| 272 |
/* Try to join consecutive zones: */ |
| 273 |
|
| 274 |
/* 1. with preceding zone */ |
| 275 |
|
| 276 |
if ((char *) aprevious + aprevious->nbytes == (char *)adr) |
| 277 |
{ aprevious->anext = adr->anext ; |
| 278 |
aprevious->nbytes += adr->nbytes ; |
| 279 |
MMPKT.nfree-- ; |
| 280 |
adr = aprevious ; |
| 281 |
} |
| 282 |
/* 2. with following zone */ |
| 283 |
|
| 284 |
if ((char *)adr + adr->nbytes == (char *)adr->anext) |
| 285 |
{ adr->nbytes += (adr->anext)->nbytes ; |
| 286 |
adr->anext = (adr->anext)->anext ; |
| 287 |
MMPKT.nfree-- ; |
| 288 |
} |
| 289 |
#if DEBUG |
| 290 |
list(adr, 0); |
| 291 |
#endif |
| 292 |
|
| 293 |
return(0); |
| 294 |
} |
| 295 |
|
| 296 |
/*==========================================================================*/ |
| 297 |
char *osmmexp(address, nbytes) |
| 298 |
/*+++ |
| 299 |
.PURPOSE Reallocates a piece of memory allocated via osmmget |
| 300 |
.RETURNS Address of new zone, or NULL if failed. |
| 301 |
.REMARKS Values are copied to new zone. |
| 302 |
---*/ |
| 303 |
HEADER *address ; /* MOD: adress to realloc */ |
| 304 |
int nbytes; /* IN: New length */ |
| 305 |
{ |
| 306 |
HEADER *adrnew, *a; |
| 307 |
int i; |
| 308 |
|
| 309 |
if (nbytes <= 0) |
| 310 |
{ oserror = EINVAL; |
| 311 |
return(NULL_PTR(char)); /* Impossible !!! */ |
| 312 |
} |
| 313 |
|
| 314 |
a = address; |
| 315 |
if (a == NULL_HEADER) |
| 316 |
return(osmmget(nbytes)); /* Only allocate */ |
| 317 |
|
| 318 |
/* If shorter length, no reallocation */ |
| 319 |
if ( (--a)->nbytes >= (nbytes + sizeof(HEADER))) |
| 320 |
return((char *)address); |
| 321 |
|
| 322 |
/* Allocate a new area */ |
| 323 |
if (!(adrnew = (HEADER *)osmmget(nbytes))) |
| 324 |
return(NULL_PTR(char)); |
| 325 |
|
| 326 |
/* Copy the old string into the new one, then free the old one |
| 327 |
* Don't forget that nbytes includes also the header ! |
| 328 |
*/ |
| 329 |
|
| 330 |
i = a->nbytes - sizeof(HEADER); /* Number of bytes to copy */ |
| 331 |
oscopy((char *)adrnew, (char *)address, i); |
| 332 |
|
| 333 |
/* Free the old memory */ |
| 334 |
osmmfree(address); |
| 335 |
|
| 336 |
return( (char *)adrnew); |
| 337 |
} |
| 338 |
|
| 339 |
#if DEBUG |
| 340 |
static int list(a,n) |
| 341 |
HEADER *a; |
| 342 |
int n; |
| 343 |
{ |
| 344 |
HEADER *x; |
| 345 |
static char deb[25]; |
| 346 |
char *p; |
| 347 |
int i; |
| 348 |
|
| 349 |
printf("------Just allocated address %d with %d bytes for %d asked\n", |
| 350 |
a, a->nbytes, n); |
| 351 |
|
| 352 |
printf("Total allocated bytes: %d\n", MMPKT.larea); |
| 353 |
|
| 354 |
printf("%d Free zones (Total = %d bytes)\n", MMPKT.nfree, MMPKT.lfree); |
| 355 |
for (x = MMPKT.afree; x; x = x->anext) |
| 356 |
{ |
| 357 |
printf("Zones %d: bytes=%d\n", x, x->nbytes); |
| 358 |
} |
| 359 |
printf("--------------------------------\n"); |
| 360 |
|
| 361 |
return(0); |
| 362 |
|
| 363 |
} |
| 364 |
#endif |