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
807640e4
Commit
807640e4
authored
Feb 03, 2016
by
Hisham
Browse files
Shorten the code using the err() function.
parent
5c593fae
Changes
1
Hide whitespace changes
Inline
Side-by-side
XAlloc.c
View file @
807640e4
...
...
@@ -5,22 +5,24 @@
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <unistd.h>
#include <err.h>
#include <stdlib.h>
#include <string.h>
/*{
#include <stdlib.h>
}*/
static
char
oomMessage
[]
=
"Out of memory!
\n
"
;
static
inline
void
fail
()
{
curs_set
(
1
);
endwin
();
err
(
1
,
NULL
);
}
void
*
xMalloc
(
size_t
size
)
{
void
*
data
=
malloc
(
size
);
if
(
!
data
&&
size
>
0
)
{
curs_set
(
1
);
endwin
();
write
(
2
,
oomMessage
,
sizeof
oomMessage
-
1
);
exit
(
1
);
fail
();
}
return
data
;
}
...
...
@@ -28,10 +30,7 @@ void* xMalloc(size_t size) {
void
*
xCalloc
(
size_t
nmemb
,
size_t
size
)
{
void
*
data
=
calloc
(
nmemb
,
size
);
if
(
!
data
)
{
curs_set
(
1
);
endwin
();
write
(
2
,
oomMessage
,
sizeof
oomMessage
-
1
);
exit
(
1
);
fail
();
}
return
data
;
}
...
...
@@ -39,10 +38,7 @@ void* xCalloc(size_t nmemb, size_t size) {
void
*
xRealloc
(
void
*
ptr
,
size_t
size
)
{
void
*
data
=
realloc
(
ptr
,
size
);
if
(
!
data
&&
size
>
0
)
{
curs_set
(
1
);
endwin
();
write
(
2
,
oomMessage
,
sizeof
oomMessage
-
1
);
exit
(
1
);
fail
();
}
return
data
;
}
...
...
@@ -50,10 +46,7 @@ void* xRealloc(void* ptr, size_t size) {
char
*
xStrdup
(
const
char
*
str
)
{
char
*
data
=
strdup
(
str
);
if
(
!
data
&&
str
)
{
curs_set
(
1
);
endwin
();
write
(
2
,
oomMessage
,
sizeof
oomMessage
-
1
);
exit
(
1
);
fail
();
}
return
data
;
}
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