Skip to content

Commit adb0718

Browse files
committed
Refactor alloc_lproc to use grow_array helper
1 parent 26608b7 commit adb0718

File tree

2 files changed

+8
-13
lines changed

2 files changed

+8
-13
lines changed

lib/common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1331,7 +1331,7 @@ struct lsof_context {
13311331
struct lproc *procs;
13321332
/** Length and capacity of `procs` */
13331333
size_t procs_size;
1334-
size_t procs_cap;
1334+
int procs_cap;
13351335

13361336
/** Pointer to current file */
13371337
struct lfile *cur_file;

lib/proc.c

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -236,24 +236,19 @@ void alloc_lproc(struct lsof_context *ctx, int pid, /* Process ID */
236236
int pss, /* process select state */
237237
int sf) /* process select flags */
238238
{
239-
static int sz = 0;
240-
241239
if (!Lproc) {
242-
if (!(Lproc = (struct lproc *)malloc(
243-
(MALLOC_S)(LPROCINCR * sizeof(struct lproc))))) {
240+
if (grow_array((void **)&Lproc, &ctx->procs_cap, sizeof(struct lproc),
241+
LPROCINCR)) {
244242
(void)fprintf(stderr,
245243
"%s: no malloc space for %d local proc structures\n",
246244
Pn, LPROCINCR);
247245
Error(ctx);
248246
}
249-
sz = LPROCINCR;
250-
} else if ((Nlproc + 1) > sz) {
251-
sz += LPROCINCR;
252-
if (!(Lproc = (struct lproc *)realloc(
253-
(MALLOC_P *)Lproc, (MALLOC_S)(sz * sizeof(struct lproc))))) {
254-
(void)fprintf(stderr,
255-
"%s: no realloc space for %d local proc structures\n",
256-
Pn, sz);
247+
} else if ((Nlproc + 1) > ctx->procs_cap) {
248+
if (grow_array((void **)&Lproc, &ctx->procs_cap, sizeof(struct lproc),
249+
LPROCINCR)) {
250+
(void)fprintf(
251+
stderr, "%s: no realloc space for local proc structures\n", Pn);
257252
Error(ctx);
258253
}
259254
}

0 commit comments

Comments
 (0)