Skip to content

Commit 5c3045e

Browse files
committed
Move static variables to lsof_context in Darwin dproc.c
- Move process information buffers (Fds, Pids, NbPids, NbFds) to ctx->dialect - Move thread buffers (Threads, NbThreads) to ctx->dialect (DARWINV>=900) - Move fileport buffers (Fps, NbFps) to ctx->dialect (PROC_PIDLISTFILEPORTS) - Move vnode recording (Vips, NVips) to ctx->dialect - Replace manual malloc/realloc with grow_array() helper for Vips - Remove redundant NbVips field (bytes can be calculated from NVips) - Add convenience macros in dlsof.h for accessing context fields - Rename struct vips_info to struct darwin_vips_info for namespacing All 14 test cases pass successfully.
1 parent f3cbd37 commit 5c3045e

File tree

2 files changed

+45
-41
lines changed

2 files changed

+45
-41
lines changed

lib/dialects/darwin/dlsof.h

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,48 @@ struct sfile {
128128
# define offsetof(type, member) ((size_t)(&((type *)0)->member))
129129
# endif /* !defined(offsetof) */
130130

131-
struct lsof_context_dialect {};
131+
struct lsof_context_dialect {
132+
/* Process information buffers */
133+
struct proc_fdinfo *fds; /* FD buffer */
134+
int *pids; /* PID buffer */
135+
int nb_pids; /* bytes allocated to Pids */
136+
int nb_fds; /* bytes allocated to FDs */
137+
138+
# if DARWINV >= 900
139+
int nb_threads; /* Threads bytes allocated */
140+
uint64_t *threads; /* Thread buffer */
141+
# endif /* DARWINV>=900 */
142+
143+
# if defined(PROC_PIDLISTFILEPORTS)
144+
struct proc_fileportinfo *fps; /* fileport buffer */
145+
int nb_fps; /* bytes allocated to fileports */
146+
# endif /* PROC_PIDLISTFILEPORTS */
147+
148+
/* Vnode info recording */
149+
struct darwin_vips_info {
150+
dev_t dev;
151+
unsigned long ino;
152+
} *vips; /* recorded vnodes */
153+
int n_vips; /* entries allocated to Vips (capacity) */
154+
};
155+
156+
/* Macros to access dialect-specific context fields */
157+
# define Fds (ctx->dialect.fds)
158+
# define Pids (ctx->dialect.pids)
159+
# define NbPids (ctx->dialect.nb_pids)
160+
# define NbFds (ctx->dialect.nb_fds)
161+
162+
# if DARWINV >= 900
163+
# define Threads (ctx->dialect.threads)
164+
# define NbThreads (ctx->dialect.nb_threads)
165+
# endif /* DARWINV>=900 */
166+
167+
# if defined(PROC_PIDLISTFILEPORTS)
168+
# define Fps (ctx->dialect.fps)
169+
# define NbFps (ctx->dialect.nb_fps)
170+
# endif /* PROC_PIDLISTFILEPORTS */
171+
172+
# define Vips (ctx->dialect.vips)
173+
# define NVips (ctx->dialect.n_vips)
132174

133175
#endif /* DARWIN_LSOF_H */

lib/dialects/darwin/dproc.c

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -56,37 +56,6 @@ static char copyright[] = "@(#) Copyright 2005-2007 Apple Inc. and Purdue "
5656
*/
5757
#endif /* PROC_PIDLISTFILEPORTS */
5858

59-
/*
60-
* Local static variables
61-
*/
62-
63-
static struct proc_fdinfo *Fds = (struct proc_fdinfo *)NULL; /* FD buffer */
64-
static int NbPids = 0; /* bytes allocated to Pids */
65-
static int NbFds = 0; /* bytes allocated to FDs */
66-
static int *Pids = (int *)NULL; /* PID buffer */
67-
68-
#if DARWINV >= 900
69-
static int NbThreads = 0; /* Threads bytes allocated */
70-
static uint64_t *Threads = (uint64_t *)NULL; /* Thread buffer */
71-
#endif /* DARWINV>=900 */
72-
73-
#if defined(PROC_PIDLISTFILEPORTS)
74-
static struct proc_fileportinfo *Fps =
75-
(struct proc_fileportinfo *)NULL; /* fileport buffer */
76-
static int NbFps = 0; /* bytes allocated to fileports */
77-
#endif /* PROC_PIDLISTFILEPORTS */
78-
79-
/*
80-
* Local structure definitions
81-
*/
82-
83-
static struct vips_info {
84-
dev_t dev;
85-
ino_t ino;
86-
} *Vips = (struct vips_info *)NULL; /* recorded vnodes */
87-
static int NbVips = 0; /* bytes allocated to Vips */
88-
static int NVips = 0; /* entries allocated to Vips */
89-
9059
/*
9160
* Local function prototypes
9261
*/
@@ -133,18 +102,11 @@ static void enter_vn_text(struct lsof_context *ctx, /* context */
133102
* Record the entry of the vnode information.
134103
*/
135104
if (i >= NVips) {
136-
137105
/*
138106
* Allocate space for recording the vnode information.
139107
*/
140-
NVips += VIPS_INCR;
141-
NbVips += (int)(VIPS_INCR * sizeof(struct vips_info));
142-
if (!Vips)
143-
Vips = (struct vips_info *)malloc((MALLOC_S)NbVips);
144-
else
145-
Vips =
146-
(struct vips_info *)realloc((MALLOC_P *)Vips, (MALLOC_S)NbVips);
147-
if (!Vips) {
108+
if (grow_array((void **)&Vips, &NVips, sizeof(struct darwin_vips_info),
109+
VIPS_INCR)) {
148110
(void)fprintf(stderr, "%s: PID %d: no text recording space\n", Pn,
149111
Lp->pid);
150112
Error(ctx);

0 commit comments

Comments
 (0)