Debian Codegraph @

packages/seacas/applications/nem_spread/rf_allo.C

raw
1 /*
2 * Copyright(C) 1999-2020, 2022, 2023 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 #include "fmt/ostream.h"
9 #include <cstdarg> // for va_arg, va_end, va_list, etc
10 #include <cstddef> // for size_t
11 #include <cstdio> // for nullptr, stderr
12 #include <cstdlib> // for exit, free, malloc
13
14 /*#include "rf_allo.h"*/
15
16 #ifdef DEBUG
17 extern int Proc;
18 #endif
19
20 namespace {
21 void *smalloc(size_t n, char *filename, int lineno);
22 }
23
24 /******************************************************************************
25 *
26 * Dynamic Allocation of Multidimensional Arrays
27 *-----------------------------------------------------------------------------
28 *
29 * Example Usage:
30 *
31 * typedef struct
32 * { int bus1;
33 * int bus2;
34 * int dest;
35 * } POINT;
36 *
37 * POINT **points, corner;
38 *
39 * points = (POINT **) array_alloc (2, x, y, sizeof(POINT));
40 * ^ ^ ^
41 * | | |
42 * number of dimensions--+ | |
43 * | |
44 * first dimension max----+ |
45 * |
46 * second dimension max------+
47 *
48 * (points may be now be used as if it were declared
49 * POINT points[x][y])
50 *
51 * This particular version is limited to dimensions of 3 or less.
52 *
53 * corner = points[2][3]; (refer to the structure as you would any array)
54 *
55 * free (points); (frees the entire structure in one fell swoop)
56 *
57 *****************************************************************************/
58 /******************************************************************************
59 * The following section is a commented section containing
60 * an example main code:
61 *******************************************************************************
62 *double *array_alloc();
63 *main()
64 *{
65 * int ***temp;
66 * int *temp2;
67 * int i, j, k;
68 * int il, jl, kl;
69 *
70 * malloc_debug(2);
71 * il = 2;
72 * jl = 3;
73 * kl = 3;
74 * temp = (int ***) array_alloc(3,il,jl,kl,sizeof(int));
75 * for (i=0; i<il; i++) {
76 * for (j=0; j<jl; j++) {
77 * for (k=0; k<kl; k++) temp[i][j][k] = 1;
78 * }
79 * }
80 *
81 * temp2 = (int *) malloc(10*sizeof(int));
82 * for (i=0; i<10; i++) temp2[i] = 0;
83 *
84 * for (i=0; i<il; i++) {
85 * for (j=0; j<jl; j++) {
86 * for (k=0; k<kl; k++) fmt::print(" {}\n", temp[i][j][k]);
87 * }
88 * }
89 * malloc_verify();
90 *}
91 ******************************************************************************/
92
93 /*****************************************************************************/
94 /*****************************************************************************/
95
96 void *array_alloc(const char *file, int lineno, int numdim, ...)
97 {
98 struct dim
99 {
100 size_t index; /* Number of elements in the dimension */
101 size_t total; /* Total number of elements */
102 size_t size; /* Size of a single element in bytes */
103 size_t off; /* offset from beginning of array */
104 } dim[3]; /* Info about each dimension */
105 size_t total; /* Total size of the array */
106 void *dfield; /* ptr to avoid lint complaints */
107 char *field; /* The multi-dimensional array */
108 char **ptr; /* Pointer offset */
109 char *data; /* Data offset */
110 va_list va; /* Current pointer in the argument list */
111
112 va_start(va, numdim);
113
114 if (numdim <= 0) {
115 fmt::print(stderr, "{} ({}: {}) ERROR: number of dimensions, {}, is <=0\n", __func__, file,
116 lineno, numdim);
117 exit(1);
118 }
119 else if (numdim > 3) {
120 fmt::print(stderr, "{} ({}: {}) ERROR: number of dimensions, {}, is > 3\n", __func__, file,
121 lineno, numdim);
122 exit(1);
123 }
124
125 dim[0].index = va_arg(va, size_t);
126
127 if (dim[0].index == 0) {
128 #ifdef DEBUG
129 fmt::print(stderr,
130 "WARNING, {} ({}: {}) called with first "
131 "dimension == 0; will return nullptr\n",
132 __func__, file, lineno);
133 #endif
134 va_end(va);
135 return (nullptr);
136 }
137
138 dim[0].total = dim[0].index;
139 dim[0].size = sizeof(void *);
140 dim[0].off = 0;
141 for (int i = 1; i < numdim; i++) {
142 dim[i].index = va_arg(va, size_t);
143 if (dim[i].index == 0) {
144 fmt::print(stderr,
145 "WARNING: {} ({}: {}) called with dimension {} == 0, "
146 " will return nullptr\n",
147 __func__, file, lineno, i + 1);
148 va_end(va);
149 return (nullptr);
150 }
151 dim[i].total = dim[i - 1].total * dim[i].index;
152 dim[i].size = sizeof(void *);
153 dim[i].off = dim[i - 1].off + dim[i - 1].total * dim[i - 1].size;
154 }
155 dim[numdim - 1].size = va_arg(va, size_t);
156 va_end(va);
157
158 /* Round up the last offset value so data is properly aligned. */
159 dim[numdim - 1].off = dim[numdim - 1].size *
160 ((dim[numdim - 1].off + dim[numdim - 1].size - 1) / dim[numdim - 1].size);
161
162 total = dim[numdim - 1].off + dim[numdim - 1].total * dim[numdim - 1].size;
163
164 dfield = smalloc(total, const_cast<char *>(file), lineno);
165 field = reinterpret_cast<char *>(dfield);
166
167 for (int i = 0; i < numdim - 1; i++) {
168 ptr = reinterpret_cast<char **>(field + dim[i].off);
169 data = (field + dim[i + 1].off);
170 for (size_t j = 0; j < dim[i].total; j++) {
171 ptr[j] = data + j * dim[i + 1].size * dim[i + 1].index;
172 }
173 }
174
175 return (dfield);
176 }
177
178 /* Safe version of malloc. Does not initialize memory .*/
179
180 /* Modified by Scott Hutchinson (1421) 20 January 1993 */
181
182 /*****************************************************************************/
183 /*****************************************************************************/
184 /*****************************************************************************/
185
186 namespace {
187 void *smalloc(size_t n, char *filename, int lineno)
188
189 {
190 void *pntr = nullptr; /* return value */
191
192 if (n != 0) {
193 pntr = malloc(n);
194 }
195
196 if (pntr == nullptr && n != 0) {
197 fmt::print(stderr,
198 "{} (from {},{}) Out of space - number of bytes "
199 "requested = {}\n",
200 __func__, filename, lineno, fmt::group_digits(n));
201 exit(0);
202 }
203
204 return pntr;
205 }
206 } // namespace
207 /*****************************************************************************/
208 /*****************************************************************************/
209 /*****************************************************************************/
210
211 void safe_free(void **ptr)
212 {
213 /*
214 * This version of free calls the system's free function
215 * with maximum error checking. It also doesn't call free if ptr is
216 * the nullptr pointer.
217 */
218
219 if (*ptr != nullptr) {
220
221 free(*ptr);
222
223 /*
224 * Set the value of ptr to nullptr, so that further references
225 * to it will be flagged.
226 */
227
228 *ptr = nullptr;
229 }
230 } /* safe_free */
231
232 /*****************************************************************************/
233 /* END of rf_allo.c */
234 /*****************************************************************************/