| 1 |
/* |
| 2 |
* Copyright(C) 1999-2021, 2024 National Technology & Engineering Solutions |
| 3 |
* of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with |
| 4 |
* NTESS, the U.S. Government retains certain rights in this software. |
| 5 |
* |
| 6 |
* See packages/seacas/LICENSE for details |
| 7 |
*/ |
| 8 |
/* |
| 9 |
*/ |
| 10 |
|
| 11 |
/* |
| 12 |
* NOTES ON MODULE EXMEMY: |
| 13 |
* |
| 14 |
* -2. The big assumption on which this is based is that a FTNINT |
| 15 |
* which is either an int or a long int, is the same size as |
| 16 |
* the void* returned by malloc. There are both compile-time |
| 17 |
* and run-time assertions that attempt to catch this. |
| 18 |
* |
| 19 |
* -1. The information below is the original implementation. At that |
| 20 |
* time, the only C routines linked with most executables were the |
| 21 |
* supes library itself and there were no other routines calling |
| 22 |
* 'sbrk' and life was good. This situation has changed with the |
| 23 |
* addition of the exodusII, netcdf, nemesis, and other libraries. |
| 24 |
* Some of these are C-based and do 'malloc' and 'free' |
| 25 |
* calls. Most man pages for 'malloc' and 'sbrk' warn of dire |
| 26 |
* consequences of mixing calls to 'malloc' and 'sbrk' and we have |
| 27 |
* had instances of memory corruption under the old model. To |
| 28 |
* eliminate this undefined and undesirable behavior, the EXMEMY |
| 29 |
* routine has been rewritten to use 'malloc' and 'free' instead |
| 30 |
* of 'sbrk' calls. The PAGESIZE manipulations have also been |
| 31 |
* removed since the underlying 'malloc' implementation is better |
| 32 |
* at dealing with this than we are (hopefully). |
| 33 |
* |
| 34 |
* The implementation of 'free' is somewhat of a kluge and |
| 35 |
* required changes to the calling 'mem_mgr' routines. Specifically, |
| 36 |
* |
| 37 |
* 1) A few routines were disabled -- mdget which is supposed to |
| 38 |
* get a large block of memory and parcel it out in smaller chunks. |
| 39 |
* This minimizes system calls, but is incompatible with 'free' since |
| 40 |
* memory passed to 'mddel' may not be on a boundary obtained |
| 41 |
* my malloc. |
| 42 |
* |
| 43 |
* 2) 'mdlong' is mapped to 'realloc'. A flag is passed into the |
| 44 |
* exmemy call which tells whether we are freeing memory or |
| 45 |
* changing size. The '*memret' argument is used to pass the flag |
| 46 |
* in to avoid changing the interface to exmemy. |
| 47 |
* |
| 48 |
* 3) 'deferred' mode is eliminated; all memory requests are |
| 49 |
* satisfied immediately. |
| 50 |
* |
| 51 |
* 4) 'mdcomp' is also eliminated. |
| 52 |
* |
| 53 |
* The interface has not been changed; the disabled calls are |
| 54 |
* just noops. |
| 55 |
* |
| 56 |
* ---Below here is historical notes--- |
| 57 |
* |
| 58 |
* 0. On a request for more memory, we ask for it in multiples of |
| 59 |
* PAGES of memory of size PAGESIZE. The reason is that on allocation, |
| 60 |
* the memory manager does keep track the voids in memory. Unfortunately, |
| 61 |
* the same cannot be said for the voids created on deallocation, so... |
| 62 |
* |
| 63 |
* 1. The SUPES memory handler, EXMEMY, normally deallocates memory |
| 64 |
* only if one asks for it a a location that is an exact page multiple. |
| 65 |
* Further, the memory manager used to try to release memory |
| 66 |
* from each memory block---this, of course, assumes that the OS |
| 67 |
* allows for gaps in a users data space. It so happens that VMS |
| 68 |
* does. I've changed the memory manager (cf. mem_mgr/mxgive) so that |
| 69 |
* it now only tries to release a void if it is at the top of the |
| 70 |
* program space. I release as much as I can on any given release |
| 71 |
* request. |
| 72 |
* |
| 73 |
* 2. Although it's not clear from the comments, or from the documentation |
| 74 |
* that I've read, on deallocation *memrtn is the amount of memory that |
| 75 |
* exmemy was *NOT* able to return to the system. Still unclear? |
| 76 |
* An example: Suppose that I tried to return 524 bytes to the system. |
| 77 |
* Ultimately, I would do this through a call to exmemy with a value |
| 78 |
* for *memreq of -524 / Numsize (remember we ask to give in numeric |
| 79 |
* storage units). Assuming that the amount actually returned |
| 80 |
* was 512 then *memrtn should be set to 12 divided by the NumSize |
| 81 |
* for most systems---see the comments below for the Cray strategy. |
| 82 |
* (memrtn is now the amount of memory released/allocated. If it is |
| 83 |
* less than zero, an error occurred) |
| 84 |
*/ |
| 85 |
|
| 86 |
/* |
| 87 |
* Assumptions: |
| 88 |
* 1. All systems have stdlib.h and define the prototype for malloc |
| 89 |
* 2. All systems are ANSI C compliant (have function prototypes) |
| 90 |
* 3. sizeof(FTNINT) == sizeof(void*) |
| 91 |
*/ |
| 92 |
|
| 93 |
#include <assert.h> |
| 94 |
#include <stdlib.h> |
| 95 |
/* |
| 96 |
* Define the Fortran/C interface for the system in question |
| 97 |
* See also itools/config/cf/fortranc.h, which defaults to float, int --pw |
| 98 |
*/ |
| 99 |
|
| 100 |
#include <fortranc.h> |
| 101 |
|
| 102 |
#define CT_ASSERT(e) extern char(*ct_assert(void))[sizeof(char[1 - 2 * !(e)])] |
| 103 |
/* If the following line causes a compile-time error, then there is a problem |
| 104 |
* which will cause the supes memory manager to not work correctly on this |
| 105 |
* platform. The error will be something similar to: |
| 106 |
* |
| 107 |
* exmemy.c(141): error: the size of an array must be greater than zero |
| 108 |
* CT_ASSERT(sizeof(FTNINT) == sizeof(void*)); |
| 109 |
* |
| 110 |
* Contact Greg Sjaardema, [email protected] for assistance. |
| 111 |
*/ |
| 112 |
CT_ASSERT(sizeof(FTNINT) == sizeof(void *)); |
| 113 |
|
| 114 |
#if defined(ADDC_) |
| 115 |
void exmemy_(FTNINT *memreq, FTNINT *locblk, FTNINT *memrtn) |
| 116 |
#else |
| 117 |
void exmemy(FTNINT *memreq, FTNINT *locblk, FTNINT *memrtn) |
| 118 |
#endif /* ADDC_ */ |
| 119 |
{ |
| 120 |
size_t numbytes; |
| 121 |
size_t NumSize; /* Get multiplier which allows us to */ |
| 122 |
/* convert everything in terms of Numeric Storage Units. */ |
| 123 |
NumSize = sizeof(FTNREAL) / sizeof(char); |
| 124 |
|
| 125 |
/* Normal allocation call */ |
| 126 |
if (*memreq > 0) { /* Then we need more memory. */ |
| 127 |
numbytes = (*memreq) * NumSize; |
| 128 |
|
| 129 |
size_t *block_location = (size_t *)malloc(numbytes); |
| 130 |
|
| 131 |
/* printf("%11x %11d\n", block_location, numbytes); */ |
| 132 |
/* convert back to numerical storage units */ |
| 133 |
*locblk = (FTNINT)((size_t)block_location / NumSize); |
| 134 |
|
| 135 |
/* See if we have lost any information in the conversion. For example, if |
| 136 |
* the pointer is a 64-bit quantity and a FTNINT is 32-bits, we may not be |
| 137 |
* able to recover the block_location... This should have been caught in |
| 138 |
* the ct_assert above, but if not, we check again here... |
| 139 |
*/ |
| 140 |
assert(block_location == (size_t *)((size_t)(*locblk) * NumSize)); |
| 141 |
|
| 142 |
if (block_location == NULL) { |
| 143 |
/* |
| 144 |
* Then the call to 'malloc' has failed, most likely due to |
| 145 |
* asking for more memory than what's available. |
| 146 |
*/ |
| 147 |
*memrtn = -1; |
| 148 |
} |
| 149 |
else { |
| 150 |
/* The last call for memory was successful! */ |
| 151 |
*memrtn = (FTNINT)(numbytes / NumSize); |
| 152 |
} |
| 153 |
} |
| 154 |
else { |
| 155 |
/* Otherwise, if *memrtn is negative, we want to give some back. */ |
| 156 |
|
| 157 |
/* |
| 158 |
* There are a few kludges used here to try to get the memory |
| 159 |
* system to work correctly with malloc/free instead of sbrk. |
| 160 |
* A flag is passed in via the memrtn variable: |
| 161 |
* -999 == free the memory block |
| 162 |
* -998 == realloc |
| 163 |
*/ |
| 164 |
|
| 165 |
/* Adjust '*locblk' to be the raw address. It is passed |
| 166 |
* in in terms of Numeric Storage Units. |
| 167 |
*/ |
| 168 |
size_t *block_location = (size_t *)((size_t)(*locblk) * NumSize); |
| 169 |
|
| 170 |
if (*memrtn == -999 || *memreq == 0) { |
| 171 |
/* Handle normal 'free' */ |
| 172 |
/* printf("FREE: %11x %11d\n", block_location, *memreq); */ |
| 173 |
free(block_location); |
| 174 |
*memrtn = -(*memreq); |
| 175 |
} |
| 176 |
else if (*memrtn == -998) { |
| 177 |
/* realloc call to shrink/grow memory */ |
| 178 |
numbytes = -(*memreq) * NumSize; |
| 179 |
/* printf("PRE: %11x %11d (realloc)\n", block_location, numbytes); */ |
| 180 |
size_t *new_location = realloc(block_location, numbytes); |
| 181 |
/* printf("POST: %11x %11d\n", new_location, numbytes); */ |
| 182 |
|
| 183 |
if (new_location == NULL && *memreq > 0) { |
| 184 |
/* |
| 185 |
* Then the call to 'realloc' has failed, most likely due to |
| 186 |
* asking for more memory than what's available. |
| 187 |
*/ |
| 188 |
*memrtn = -1; |
| 189 |
} |
| 190 |
else { |
| 191 |
*memrtn = -(*memreq); |
| 192 |
|
| 193 |
/* convert back to numerical storage units */ |
| 194 |
*locblk = (FTNINT)((size_t)new_location / NumSize); |
| 195 |
} |
| 196 |
/* If '*memrtn' flag was not set to -999 or -998, |
| 197 |
* ignore free request */ |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
#ifdef TEST |
| 203 |
#include <stdio.h> |
| 204 |
|
| 205 |
int main(int argc, char **argv) |
| 206 |
{ |
| 207 |
FTNINT locblk; |
| 208 |
FTNINT memrtn; |
| 209 |
size_t first = 0; |
| 210 |
size_t last = 0; |
| 211 |
|
| 212 |
assert(sizeof(FTNREAL) == sizeof(FTNINT)); |
| 213 |
|
| 214 |
printf("Size of FORTRAN int/real = %lu bytes\n", sizeof(FTNINT)); |
| 215 |
|
| 216 |
FTNINT memreq = 10000000; /* 10 Million words */ |
| 217 |
for (int i = 0; i < 1000; i++) { |
| 218 |
#if defined(ADDC_) |
| 219 |
exmemy_(&memreq, &locblk, &memrtn); |
| 220 |
#else |
| 221 |
exmemy(&memreq, &locblk, &memrtn); |
| 222 |
#endif /* ADDC_ */ |
| 223 |
printf("%4d: %11x %11d\n", i, locblk, memrtn); |
| 224 |
|
| 225 |
if (memrtn < 0) { |
| 226 |
FTNINT allocated; |
| 227 |
FTNINT overhead; |
| 228 |
allocated = i * (memreq / 1000000); |
| 229 |
overhead = (last - first - (i - 1) * memreq) * sizeof(FTNINT); |
| 230 |
printf("Total Allocation = %d MegaWords, %d MB\n", allocated, allocated * sizeof(FTNINT)); |
| 231 |
|
| 232 |
/* NOTE: 'last' is pointer to beginning of last block, not end */ |
| 233 |
printf("Overhead = %d bytes, %d bytes/allocation\n", overhead, overhead / (i - 1)); |
| 234 |
|
| 235 |
break; |
| 236 |
} |
| 237 |
else { |
| 238 |
last = locblk; |
| 239 |
} |
| 240 |
if (i == 0) { |
| 241 |
first = locblk; |
| 242 |
} |
| 243 |
} |
| 244 |
} |
| 245 |
#endif /* TEST */ |