Debian Codegraph @

lib/util/mallocvar.c

raw
1 #include <limits.h>
2 #include <stdlib.h>
3
4 #include "pm_c_util.h"
5 #include "nstring.h"
6
7 #include "mallocvar.h"
8
9
10 static void *
11 mallocz(size_t const size) {
12 /*----------------------------------------------------------------------------
13 Same as malloc(), except it is legal to allocate zero bytes.
14 -----------------------------------------------------------------------------*/
15 return malloc(MAX(1, size));
16 }
17
18
19
20 static void
21 allocarrayNoHeap(void ** const rowIndex,
22 unsigned int const rows,
23 unsigned int const cols,
24 unsigned int const elementSize,
25 bool * const failedP) {
26
27 unsigned int rowsDone;
28
29 for (rowsDone = 0, *failedP = false; rowsDone < rows && !*failedP; ) {
30 void * rowSpace;
31
32 mallocProduct(&rowSpace, cols, elementSize);
33
34 if (rowSpace == NULL) {
35 unsigned int row;
36
37 *failedP = true;
38
39 /* unwind partially completed job */
40 for (row = 0; row < rowsDone; ++row)
41 free(rowIndex[row]);
42 } else
43 rowIndex[rowsDone++] = rowSpace;
44 }
45 }
46
47
48
49 static unsigned char *
50 allocRowHeap(unsigned int const rows,
51 unsigned int const cols,
52 unsigned int const size) {
53 /*----------------------------------------------------------------------------
54 Allocate a row heap. That's a chunk of memory for use in a
55 pm_mallocarray2 two-dimensional array to contain the rows.
56
57 The heap must fit 'rows' rows of 'cols' columns each of elements
58 'size' bytes in size.
59
60 Return NULL if we can't get the memory.
61 -----------------------------------------------------------------------------*/
62 unsigned char * retval;
63
64 if (cols != 0 && rows != 0 && UINT_MAX / cols / rows < size)
65 /* Too big even to request the memory ! */
66 retval = NULL;
67 else
68 retval = mallocz(rows * cols * size);
69
70 return retval;
71 }
72
73
74
75 void
76 pm_mallocarray2(void ** const resultP,
77 unsigned int const rows,
78 unsigned int const cols,
79 unsigned int const elementSize) {
80 /*----------------------------------------------------------------------------
81 Allocate an array of 'rows' rows of 'cols' columns each, with each
82 element 'size' bytes.
83
84 We use the C multidimensional array paradigm: The array is a row
85 index (array of pointers to rows) plus an array of elements for each
86 of those rows. So a[row][col] gives you the element of the two
87 dimensional array at Row 'row', Column 'col'.
88
89 Each array element is ideally aligned to an 'elementSize' boundary. But we
90 guarantee this only for 1, 2, 4, 8, and 16, because of limitations of libc
91 malloc() (which we use to allocate all the memory).
92
93 We tack on two extra elements to the end of the row index, transparent to
94 the user, for use in memory management (in particular, in destroying the
95 array). The first is a NULL pointer, so you can tell the vertical
96 dimension of the array. The second points to the row heap (see below).
97
98 We have two ways of allocating the space: fragmented and unfragmented. In
99 both, the row index (plus the extra elements) is in one block of memory.
100 In the fragmented format, each row is also in an independent memory block,
101 and the row heap pointer (see above) is NULL. In the unfragmented format,
102 all the rows are in a single block of memory called the row heap and the
103 row heap pointer points to that.
104
105 We use unfragmented format if possible, but if the allocation of the
106 row heap fails, we fall back to fragmented.
107 -----------------------------------------------------------------------------*/
108 void ** rowIndex;
109 bool failed;
110
111 MALLOCARRAY(rowIndex, rows + 1 + 1);
112 if (rowIndex == NULL)
113 failed = true;
114 else {
115 unsigned char * rowheap;
116
117 rowheap = allocRowHeap(cols, rows, elementSize);
118
119 if (rowheap) {
120 /* It's unfragmented format */
121
122 rowIndex[rows+1] = rowheap; /* Declare it unfragmented format */
123
124 if (rowheap) {
125 unsigned int row;
126
127 for (row = 0; row < rows; ++row)
128 rowIndex[row] = &(rowheap[row * cols * elementSize]);
129 }
130 failed = false;
131 } else {
132 /* We couldn't get the whole heap in one block, so try fragmented
133 format.
134 */
135 rowIndex[rows+1] = NULL; /* Declare it fragmented format */
136
137 allocarrayNoHeap(rowIndex, rows, cols, elementSize, &failed);
138 }
139 rowIndex[rows+0] = NULL; /* mark end of rows */
140 }
141 if (failed)
142 *resultP = NULL;
143 else
144 *resultP = rowIndex;
145 }
146
147
148
149 static unsigned int
150 array2RowCount(void ** const rowIndex) {
151 /*----------------------------------------------------------------------------
152 Return the number of rows in the 2-dimensional array.
153 -----------------------------------------------------------------------------*/
154 /* The end of the rows is marked by a null pointer where a row
155 pointer otherwise would be.
156 */
157
158 unsigned int row;
159
160 for (row = 0; rowIndex[row]; ++row);
161
162 return row;
163 }
164
165
166
167 void
168 pm_freearray2(void ** const rowIndex) {
169
170 unsigned int const rows = array2RowCount(rowIndex);
171
172 void * const rowheap = rowIndex[rows+1];
173
174 if (rowheap != NULL)
175 free(rowheap);
176 else {
177 /* Free each individually malloced row */
178 unsigned int row;
179 for (row = 0; row < rows; ++row)
180 pm_freerow(rowIndex[row]);
181 }
182 free(rowIndex);
183 }
184
185