Debian Codegraph @

packages/seacas/applications/nem_slice/elb_allo.C

raw
1 /*
2 * Copyright(C) 1999-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
9 #include "elb_allo.h"
10 #include <cstdarg> // for va_end, va_arg, va_list, etc
11 #include <cstddef> // for size_t
12 #include <cstdio> // for stderr
13 #include <cstdlib> // for exit, malloc
14 #include <fmt/ostream.h>
15
16 namespace {
17 void *smalloc(size_t n);
18 }
19
20 /******************************************************************************
21 *
22 * Dynamic Allocation of Multidimensional Arrays
23 *-----------------------------------------------------------------------------
24 *
25 * Example Usage:
26 *
27 * typedef struct
28 * { int bus1;
29 * int bus2;
30 * int dest;
31 * } POINT;
32 *
33 * POINT **points, corner;
34 *
35 * points = (POINT **) array_alloc (2, x, y, sizeof(POINT));
36 * ^ ^ ^
37 * | | |
38 * number of dimensions--+ | |
39 * first dimension max----+ |
40 * second dimension max------+
41 *
42 * (points may be now be used as if it were declared
43 * POINT points[x][y])
44 *
45 * This particular version is limited to dimensions of 3 or less.
46 *
47 * corner = points[2][3]; (refer to the structure as you would any array)
48 *
49 * free (points); (frees the entire structure in one fell swoop)
50 *
51 *****************************************************************************/
52 /*****************************************************************************/
53 /*****************************************************************************/
54 void *array_alloc(int numdim, ...)
55 {
56 struct dim
57 {
58 size_t index; /* Number of elements in the dimension */
59 size_t total; /* Total number of elements */
60 size_t size; /* Size of a single element in bytes */
61 size_t off; /* offset from beginning of array */
62 } dim[3]; /* Info about each dimension */
63 va_list va; /* Current pointer in the argument list */
64
65 va_start(va, numdim);
66
67 if (numdim <= 0) {
68 fmt::print(stderr, "array_alloc ERROR: number of dimensions, {}, is <=0\n", numdim);
69 va_end(va);
70 return nullptr;
71 }
72 if (numdim > 3) {
73 fmt::print(stderr, "array_alloc ERROR: number of dimensions, {}, is > 3\n", numdim);
74 va_end(va);
75 return nullptr;
76 }
77
78 dim[0].index = va_arg(va, size_t);
79
80 if (dim[0].index == 0) {
81 va_end(va);
82 return nullptr;
83 }
84
85 dim[0].total = dim[0].index;
86 dim[0].size = sizeof(void *);
87 dim[0].off = 0;
88 for (int i = 1; i < numdim; i++) {
89 dim[i].index = va_arg(va, size_t);
90 if (dim[i].index == 0) {
91 va_end(va);
92 return nullptr;
93 }
94
95 dim[i].total = dim[i - 1].total * dim[i].index;
96 dim[i].size = sizeof(void *);
97 dim[i].off = dim[i - 1].off + dim[i - 1].total * dim[i - 1].size;
98 }
99 dim[numdim - 1].size = va_arg(va, size_t);
100 va_end(va);
101
102 /* Round up the last offset value so data is properly aligned. */
103 dim[numdim - 1].off = dim[numdim - 1].size *
104 ((dim[numdim - 1].off + dim[numdim - 1].size - 1) / dim[numdim - 1].size);
105
106 size_t total = dim[numdim - 1].off + dim[numdim - 1].total * dim[numdim - 1].size;
107
108 void *dfield = smalloc(total);
109 char *field = reinterpret_cast<char *>(dfield);
110
111 for (int i = 0; i < numdim - 1; i++) {
112 char **ptr = reinterpret_cast<char **>(field + dim[i].off);
113 char *data = (field + dim[i + 1].off);
114 for (size_t j = 0; j < dim[i].total; j++) {
115 ptr[j] = data + j * dim[i + 1].size * dim[i + 1].index;
116 }
117 }
118 return (dfield);
119 }
120
121 /* Safe version of malloc. Does not initialize memory .*/
122
123 /*****************************************************************************/
124 /*****************************************************************************/
125 /*****************************************************************************/
126 namespace {
127 void *smalloc(size_t n)
128 {
129 void *pntr = nullptr; /* return value */
130
131 if (n == 0) {
132 pntr = nullptr;
133 }
134 else {
135 pntr = malloc(n);
136 }
137
138 if (pntr == nullptr && n != 0) {
139 fmt::print(stderr,
140 "smalloc: Out of space - number of bytes "
141 "requested = {}\n",
142 fmt::group_digits(n));
143 exit(0);
144 }
145 return (pntr);
146 }
147 } // namespace