Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
adam.huang
htop
Commits
1cfcc42a
Commit
1cfcc42a
authored
Feb 02, 2016
by
Hisham
Browse files
Reuse comm object if possible, avoid useless repetitions of free+strdup.
parent
301c346c
Changes
3
Hide whitespace changes
Inline
Side-by-side
Process.c
View file @
1cfcc42a
...
...
@@ -88,6 +88,7 @@ typedef struct Process_ {
pid_t ppid;
pid_t tgid;
char* comm;
int commLen;
int indent;
int basenameOffset;
...
...
Process.h
View file @
1cfcc42a
...
...
@@ -68,6 +68,7 @@ typedef struct Process_ {
pid_t
ppid
;
pid_t
tgid
;
char
*
comm
;
int
commLen
;
int
indent
;
int
basenameOffset
;
...
...
linux/LinuxProcessList.c
View file @
1cfcc42a
...
...
@@ -149,7 +149,7 @@ static inline unsigned long long LinuxProcess_adjustTime(unsigned long long t) {
return
(
unsigned
long
long
)
t
*
jiffytime
*
100
;
}
static
bool
LinuxProcessList_readStatFile
(
Process
*
process
,
const
char
*
dirname
,
const
char
*
name
,
char
*
command
)
{
static
bool
LinuxProcessList_readStatFile
(
Process
*
process
,
const
char
*
dirname
,
const
char
*
name
,
char
*
command
,
int
*
commLen
)
{
LinuxProcess
*
lp
=
(
LinuxProcess
*
)
process
;
char
filename
[
MAX_NAME
+
1
];
snprintf
(
filename
,
MAX_NAME
,
"%s/%s/stat"
,
dirname
,
name
);
...
...
@@ -175,6 +175,7 @@ static bool LinuxProcessList_readStatFile(Process *process, const char* dirname,
int
commsize
=
end
-
location
;
memcpy
(
command
,
location
,
commsize
);
command
[
commsize
]
=
'\0'
;
*
commLen
=
commsize
;
location
=
end
+
2
;
process
->
state
=
location
[
0
];
...
...
@@ -444,6 +445,16 @@ static void LinuxProcessList_readOomData(LinuxProcess* process, const char* dirn
fclose
(
file
);
}
static
void
setCommand
(
Process
*
process
,
const
char
*
command
,
int
len
)
{
if
(
process
->
comm
&&
process
->
commLen
<=
len
)
{
strncpy
(
process
->
comm
,
command
,
len
+
1
);
}
else
{
free
(
process
->
comm
);
process
->
comm
=
xStrdup
(
command
);
}
process
->
commLen
=
len
;
}
static
bool
LinuxProcessList_readCmdlineFile
(
Process
*
process
,
const
char
*
dirname
,
const
char
*
name
)
{
if
(
Process_isKernelThread
(
process
))
return
true
;
...
...
@@ -471,9 +482,8 @@ static bool LinuxProcessList_readCmdlineFile(Process* process, const char* dirna
tokenEnd
=
amtRead
;
}
command
[
amtRead
]
=
'\0'
;
free
(
process
->
comm
);
process
->
comm
=
strdup
(
command
);
process
->
basenameOffset
=
tokenEnd
;
setCommand
(
process
,
command
,
amtRead
);
return
true
;
}
...
...
@@ -539,7 +549,8 @@ static bool LinuxProcessList_recurseProcTree(LinuxProcessList* this, const char*
char
command
[
MAX_NAME
+
1
];
unsigned
long
long
int
lasttimes
=
(
lp
->
utime
+
lp
->
stime
);
if
(
!
LinuxProcessList_readStatFile
(
proc
,
dirname
,
name
,
command
))
int
commLen
=
0
;
if
(
!
LinuxProcessList_readStatFile
(
proc
,
dirname
,
name
,
command
,
&
commLen
))
goto
errorReadingProcess
;
if
(
settings
->
flags
&
PROCESS_FLAG_LINUX_IOPRIO
)
LinuxProcess_updateIOPriority
(
lp
);
...
...
@@ -589,14 +600,12 @@ static bool LinuxProcessList_recurseProcTree(LinuxProcessList* this, const char*
LinuxProcessList_readOomData
(
lp
,
dirname
,
name
);
if
(
proc
->
state
==
'Z'
)
{
free
(
proc
->
comm
);
proc
->
basenameOffset
=
-
1
;
proc
->
comm
=
strdup
(
comm
and
);
setCommand
(
proc
,
comm
and
,
comm
Len
);
}
else
if
(
Process_isThread
(
proc
))
{
if
(
settings
->
showThreadNames
||
Process_isKernelThread
(
proc
)
||
proc
->
state
==
'Z'
)
{
free
(
proc
->
comm
);
proc
->
basenameOffset
=
-
1
;
proc
->
comm
=
strdup
(
comm
and
);
setCommand
(
proc
,
comm
and
,
comm
Len
);
}
else
if
(
settings
->
showThreadNames
)
{
if
(
!
LinuxProcessList_readCmdlineFile
(
proc
,
dirname
,
name
))
goto
errorReadingProcess
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment