- commit
- 63e04b9f58b6030864d1c9746da3a24f10d35fae
- parent
- 148865386aca7f5dde559e2db3c5f7445e2e5ea8
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2025-12-05 06:38
add ecalloc helper
Diffstat
| M | src/stagit.c.in | 28 | +++++++++++++--------------- |
1 files changed, 13 insertions, 15 deletions
diff --git a/src/stagit.c.in b/src/stagit.c.in
@@ -100,6 +100,14 @@ FILE *efopen(const char *name, const char *flags) {
100 100 return fp;
101 101 }
102 102
-1 103 void *ecalloc(size_t n, size_t size) {
-1 104 void *result = calloc(n, size);
-1 105 if (!result) {
-1 106 err(EXIT_FAILURE, "calloc");
-1 107 }
-1 108 return result;
-1 109 }
-1 110
103 111 void deltainfo_free(struct deltainfo *di) {
104 112 if (!di) {
105 113 return;
@@ -132,11 +140,7 @@ void commitinfo_free(struct commitinfo *ci) {
132 140 }
133 141
134 142 struct commitinfo *commitinfo_getbyoid(const git_oid *id) {
135 -1 struct commitinfo *ci;
136 -1
137 -1 if (!(ci = calloc(1, sizeof(struct commitinfo)))) {
138 -1 err(1, "calloc");
139 -1 }
-1 143 struct commitinfo *ci = ecalloc(1, sizeof(struct commitinfo));
140 144
141 145 if (git_commit_lookup(&(ci->commit), repo, id)) {
142 146 goto err;
@@ -205,7 +209,7 @@ err:
205 209 }
206 210
207 211 struct commitstats *commitinfo_getstats(git_diff *diff) {
208 -1 struct commitstats *cs;
-1 212 struct commitstats *cs = ecalloc(1, sizeof(struct commitstats));
209 213 struct deltainfo *di;
210 214 const git_diff_delta *delta;
211 215 const git_diff_hunk *hunk;
@@ -213,13 +217,9 @@ struct commitstats *commitinfo_getstats(git_diff *diff) {
213 217 git_patch *patch = NULL;
214 218 size_t ndeltas, nhunks, nhunklines, i, j, k;
215 219
216 -1 if (!(cs = calloc(1, sizeof(struct commitstats)))) {
217 -1 err(1, "calloc");
218 -1 }
219 -1
220 220 ndeltas = git_diff_num_deltas(diff);
221 -1 if (ndeltas && !(cs->deltas = calloc(ndeltas, sizeof(struct deltainfo *)))) {
222 -1 err(1, "calloc");
-1 221 if (ndeltas) {
-1 222 cs->deltas = ecalloc(ndeltas, sizeof(struct deltainfo *));
223 223 }
224 224
225 225 for (i = 0; i < ndeltas; i++) {
@@ -227,9 +227,7 @@ struct commitstats *commitinfo_getstats(git_diff *diff) {
227 227 break;
228 228 }
229 229
230 -1 if (!(di = calloc(1, sizeof(struct deltainfo)))) {
231 -1 err(1, "calloc");
232 -1 }
-1 230 di = ecalloc(1, sizeof(struct deltainfo));
233 231 di->patch = patch;
234 232 cs->deltas[i] = di;
235 233