* [PATCH 2/3] mtd-utils: add xasprintf() helper
2010-09-29 23:30 [PATCH 1/3] mtd-utils: new memory wrappers Mike Frysinger
@ 2010-09-29 23:30 ` Mike Frysinger
2010-09-30 5:28 ` Mike Frysinger
2010-09-29 23:30 ` [PATCH 3/3] mkfs.jffs2: use new " Mike Frysinger
` (2 subsequent siblings)
3 siblings, 1 reply; 15+ messages in thread
From: Mike Frysinger @ 2010-09-29 23:30 UTC (permalink / raw)
To: linux-mtd
Signed-off-by: Mike Frysinger <vapier@gentoo.org>
---
include/xalloc.h | 20 ++++++++++++++++++++
1 files changed, 20 insertions(+), 0 deletions(-)
diff --git a/include/xalloc.h b/include/xalloc.h
index 5b8a4c3..0e501e0 100644
--- a/include/xalloc.h
+++ b/include/xalloc.h
@@ -77,4 +77,24 @@ static char *xstrdup(const char *s)
return t;
}
+#ifdef _GNU_SOURCE
+#include <stdarg.h>
+
+__attribute__((unused))
+static int xasprintf(char **strp, const char *fmt, ...)
+{
+ int cnt;
+ va_list ap;
+
+ va_start(ap, fmt);
+ cnt = vasprintf(strp, fmt, ap);
+ va_end(ap);
+
+ if (cnt == -1)
+ sys_errmsg_and_die("asprintf(...) failed");
+
+ return cnt;
+}
+#endif
+
#endif /* !__MTD_UTILS_XALLOC_H__ */
--
1.7.2.3
^ permalink raw reply related [flat|nested] 15+ messages in thread* [PATCH 2/3] mtd-utils: add xasprintf() helper
2010-09-29 23:30 ` [PATCH 2/3] mtd-utils: add xasprintf() helper Mike Frysinger
@ 2010-09-30 5:28 ` Mike Frysinger
0 siblings, 0 replies; 15+ messages in thread
From: Mike Frysinger @ 2010-09-30 5:28 UTC (permalink / raw)
To: linux-mtd
Signed-off-by: Mike Frysinger <vapier@gentoo.org>
---
v2
- use shorter "die" func
include/xalloc.h | 20 ++++++++++++++++++++
1 files changed, 20 insertions(+), 0 deletions(-)
diff --git a/include/xalloc.h b/include/xalloc.h
index 5d145d9..d49a1ae 100644
--- a/include/xalloc.h
+++ b/include/xalloc.h
@@ -77,4 +77,24 @@ static char *xstrdup(const char *s)
return t;
}
+#ifdef _GNU_SOURCE
+#include <stdarg.h>
+
+__attribute__((unused))
+static int xasprintf(char **strp, const char *fmt, ...)
+{
+ int cnt;
+ va_list ap;
+
+ va_start(ap, fmt);
+ cnt = vasprintf(strp, fmt, ap);
+ va_end(ap);
+
+ if (cnt == -1)
+ sys_errmsg_die("asprintf(...) failed");
+
+ return cnt;
+}
+#endif
+
#endif /* !__MTD_UTILS_XALLOC_H__ */
--
1.7.2.3
^ permalink raw reply related [flat|nested] 15+ messages in thread
* [PATCH 3/3] mkfs.jffs2: use new xasprintf() helper
2010-09-29 23:30 [PATCH 1/3] mtd-utils: new memory wrappers Mike Frysinger
2010-09-29 23:30 ` [PATCH 2/3] mtd-utils: add xasprintf() helper Mike Frysinger
@ 2010-09-29 23:30 ` Mike Frysinger
2010-09-30 4:59 ` [PATCH 1/3] mtd-utils: new memory wrappers Artem Bityutskiy
2010-09-30 5:27 ` [PATCH 1/3 v2] " Mike Frysinger
3 siblings, 0 replies; 15+ messages in thread
From: Mike Frysinger @ 2010-09-29 23:30 UTC (permalink / raw)
To: linux-mtd
Signed-off-by: Mike Frysinger <vapier@gentoo.org>
---
mkfs.jffs2.c | 12 ++++++------
1 files changed, 6 insertions(+), 6 deletions(-)
diff --git a/mkfs.jffs2.c b/mkfs.jffs2.c
index 1ea3598..528a7fe 100644
--- a/mkfs.jffs2.c
+++ b/mkfs.jffs2.c
@@ -391,14 +391,14 @@ static struct filesystem_entry *recursive_add_host_directory(
continue;
}
- asprintf(&hpath, "%s/%s", hostpath, dp->d_name);
+ xasprintf(&hpath, "%s/%s", hostpath, dp->d_name);
if (lstat(hpath, &sb)) {
perror_msg_and_die("%s", hpath);
}
if (strcmp(targetpath, "/") == 0) {
- asprintf(&tpath, "%s%s", targetpath, dp->d_name);
+ xasprintf(&tpath, "%s%s", targetpath, dp->d_name);
} else {
- asprintf(&tpath, "%s/%s", targetpath, dp->d_name);
+ xasprintf(&tpath, "%s/%s", targetpath, dp->d_name);
}
switch (sb.st_mode & S_IFMT) {
@@ -489,7 +489,7 @@ static int interpret_table_entry(struct filesystem_entry *root, char *line)
error_msg_and_die("Device table entries require absolute paths");
}
- asprintf(&hostpath, "%s%s", rootdir, name);
+ xasprintf(&hostpath, "%s%s", rootdir, name);
/* Check if this file already exists... */
switch (type) {
@@ -556,8 +556,8 @@ static int interpret_table_entry(struct filesystem_entry *root, char *line)
char *dname, *hpath;
for (i = start; i < count; i++) {
- asprintf(&dname, "%s%lu", name, i);
- asprintf(&hpath, "%s/%s%lu", rootdir, name, i);
+ xasprintf(&dname, "%s%lu", name, i);
+ xasprintf(&hpath, "%s/%s%lu", rootdir, name, i);
rdev = makedev(major, minor + (i * increment - start));
add_host_filesystem_entry(dname, hpath, uid, gid,
mode, rdev, parent);
--
1.7.2.3
^ permalink raw reply related [flat|nested] 15+ messages in thread* Re: [PATCH 1/3] mtd-utils: new memory wrappers
2010-09-29 23:30 [PATCH 1/3] mtd-utils: new memory wrappers Mike Frysinger
2010-09-29 23:30 ` [PATCH 2/3] mtd-utils: add xasprintf() helper Mike Frysinger
2010-09-29 23:30 ` [PATCH 3/3] mkfs.jffs2: use new " Mike Frysinger
@ 2010-09-30 4:59 ` Artem Bityutskiy
2010-09-30 5:15 ` Mike Frysinger
2010-09-30 5:27 ` [PATCH 1/3 v2] " Mike Frysinger
3 siblings, 1 reply; 15+ messages in thread
From: Artem Bityutskiy @ 2010-09-30 4:59 UTC (permalink / raw)
To: Mike Frysinger; +Cc: linux-mtd
On Wed, 2010-09-29 at 19:30 -0400, Mike Frysinger wrote:
> The mkfs.jffs2 program has local wrappers for memory related functions
> that are useful beyond mkfs.jffs2, so break them out into a common header.
>
> Signed-off-by: Mike Frysinger <vapier@gentoo.org>
> ---
> include/common.h | 8 +++++
> include/xalloc.h | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
> mkfs.jffs2.c | 41 ---------------------------
> 3 files changed, 88 insertions(+), 41 deletions(-)
> create mode 100644 include/xalloc.h
>
> diff --git a/include/common.h b/include/common.h
> index 472315e..6e27d62 100644
> --- a/include/common.h
> +++ b/include/common.h
> @@ -58,6 +58,9 @@ extern "C" {
> fprintf(stderr, "%s: error!: " fmt "\n", PROGRAM_NAME, ##__VA_ARGS__); \
> -1; \
> })
> +#define errmsg_and_die(fmt, ...) do { \
> + exit(errmsg(fmt, ##__VA_ARGS__)); \
> +} while(0)
>
> /* System error messages */
> #define sys_errmsg(fmt, ...) ({ \
> @@ -69,6 +72,9 @@ extern "C" {
> fprintf(stderr, "error %d (%s)\n", _err, strerror(_err)); \
> -1; \
> })
> +#define sys_errmsg_and_die(fmt, ...) do { \
> + exit(sys_errmsg(fmt, ##__VA_ARGS__)); \
> +} while(0)
Often these funcs are used with longish strings, so it is better to try
keeping the function names short. Thus, I suggest you to use
'err_msg_die()' etc. IOW, without the '_and' part.
If you are OK with this, I can just tweak your patch. But you can also
re-send it - whatever you prefer.
--
Best Regards,
Artem Bityutskiy (Артём Битюцкий)
^ permalink raw reply [flat|nested] 15+ messages in thread* Re: [PATCH 1/3] mtd-utils: new memory wrappers
2010-09-30 4:59 ` [PATCH 1/3] mtd-utils: new memory wrappers Artem Bityutskiy
@ 2010-09-30 5:15 ` Mike Frysinger
2010-09-30 5:48 ` Artem Bityutskiy
0 siblings, 1 reply; 15+ messages in thread
From: Mike Frysinger @ 2010-09-30 5:15 UTC (permalink / raw)
To: dedekind1; +Cc: linux-mtd
On Thu, Sep 30, 2010 at 00:59, Artem Bityutskiy wrote:
> On Wed, 2010-09-29 at 19:30 -0400, Mike Frysinger wrote:
>> The mkfs.jffs2 program has local wrappers for memory related functions
>> that are useful beyond mkfs.jffs2, so break them out into a common header.
>> ---
>> include/common.h | 8 +++++
>> include/xalloc.h | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
>> mkfs.jffs2.c | 41 ---------------------------
>> 3 files changed, 88 insertions(+), 41 deletions(-)
>> create mode 100644 include/xalloc.h
>>
>> diff --git a/include/common.h b/include/common.h
>> index 472315e..6e27d62 100644
>> --- a/include/common.h
>> +++ b/include/common.h
>> @@ -58,6 +58,9 @@ extern "C" {
>> fprintf(stderr, "%s: error!: " fmt "\n", PROGRAM_NAME, ##__VA_ARGS__); \
>> -1; \
>> })
>> +#define errmsg_and_die(fmt, ...) do { \
>> + exit(errmsg(fmt, ##__VA_ARGS__)); \
>> +} while(0)
>>
>> /* System error messages */
>> #define sys_errmsg(fmt, ...) ({ \
>> @@ -69,6 +72,9 @@ extern "C" {
>> fprintf(stderr, "error %d (%s)\n", _err, strerror(_err)); \
>> -1; \
>> })
>> +#define sys_errmsg_and_die(fmt, ...) do { \
>> + exit(sys_errmsg(fmt, ##__VA_ARGS__)); \
>> +} while(0)
>
> Often these funcs are used with longish strings, so it is better to try
> keeping the function names short. Thus, I suggest you to use
> 'err_msg_die()' etc. IOW, without the '_and' part.
as i'm sure you noticed, i didnt make these up. i simply moved them
from existing mtd-utils code.
> If you are OK with this, I can just tweak your patch. But you can also
> re-send it - whatever you prefer.
since this cascades into another patch, i'll resubmit
-mike
^ permalink raw reply [flat|nested] 15+ messages in thread* Re: [PATCH 1/3] mtd-utils: new memory wrappers
2010-09-30 5:15 ` Mike Frysinger
@ 2010-09-30 5:48 ` Artem Bityutskiy
2010-09-30 5:59 ` Mike Frysinger
0 siblings, 1 reply; 15+ messages in thread
From: Artem Bityutskiy @ 2010-09-30 5:48 UTC (permalink / raw)
To: Mike Frysinger; +Cc: linux-mtd
On Thu, 2010-09-30 at 01:15 -0400, Mike Frysinger wrote:
> as i'm sure you noticed, i didnt make these up. i simply moved them
> from existing mtd-utils code.
Actually no, I did not notice!
--
Best Regards,
Artem Bityutskiy (Артём Битюцкий)
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH 1/3] mtd-utils: new memory wrappers
2010-09-30 5:48 ` Artem Bityutskiy
@ 2010-09-30 5:59 ` Mike Frysinger
2010-09-30 6:01 ` Artem Bityutskiy
0 siblings, 1 reply; 15+ messages in thread
From: Mike Frysinger @ 2010-09-30 5:59 UTC (permalink / raw)
To: dedekind1; +Cc: linux-mtd
On Thu, Sep 30, 2010 at 01:48, Artem Bityutskiy wrote:
> On Thu, 2010-09-30 at 01:15 -0400, Mike Frysinger wrote:
>> as i'm sure you noticed, i didnt make these up. i simply moved them
>> from existing mtd-utils code.
>
> Actually no, I did not notice!
np ... only one or two utils uses this, and easy enough to convert
them. before i go further though, a question on direction ...
mkfs.jffs2 has a lot of helpers that can be moved to common.h. but it
implements things using C functions & va_args. the common.h header
takes the CPP approach of expanding the variable args. which way to
go to ultimately unify the code base !?
-mike
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH 1/3] mtd-utils: new memory wrappers
2010-09-30 5:59 ` Mike Frysinger
@ 2010-09-30 6:01 ` Artem Bityutskiy
2010-09-30 6:18 ` Mike Frysinger
2010-10-01 1:43 ` Mike Frysinger
0 siblings, 2 replies; 15+ messages in thread
From: Artem Bityutskiy @ 2010-09-30 6:01 UTC (permalink / raw)
To: Mike Frysinger; +Cc: linux-mtd
On Thu, 2010-09-30 at 01:59 -0400, Mike Frysinger wrote:
> On Thu, Sep 30, 2010 at 01:48, Artem Bityutskiy wrote:
> > On Thu, 2010-09-30 at 01:15 -0400, Mike Frysinger wrote:
> >> as i'm sure you noticed, i didnt make these up. i simply moved them
> >> from existing mtd-utils code.
> >
> > Actually no, I did not notice!
>
> np ... only one or two utils uses this, and easy enough to convert
> them. before i go further though, a question on direction ...
>
> mkfs.jffs2 has a lot of helpers that can be moved to common.h. but it
> implements things using C functions & va_args. the common.h header
> takes the CPP approach of expanding the variable args. which way to
> go to ultimately unify the code base !?
I have not strong opinion, how about creating common.c ?
--
Best Regards,
Artem Bityutskiy (Артём Битюцкий)
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH 1/3] mtd-utils: new memory wrappers
2010-09-30 6:01 ` Artem Bityutskiy
@ 2010-09-30 6:18 ` Mike Frysinger
2010-09-30 6:56 ` Artem Bityutskiy
2010-10-01 1:43 ` Mike Frysinger
1 sibling, 1 reply; 15+ messages in thread
From: Mike Frysinger @ 2010-09-30 6:18 UTC (permalink / raw)
To: dedekind1; +Cc: linux-mtd
On Thu, Sep 30, 2010 at 02:01, Artem Bityutskiy wrote:
> On Thu, 2010-09-30 at 01:59 -0400, Mike Frysinger wrote:
>> On Thu, Sep 30, 2010 at 01:48, Artem Bityutskiy wrote:
>> > On Thu, 2010-09-30 at 01:15 -0400, Mike Frysinger wrote:
>> >> as i'm sure you noticed, i didnt make these up. i simply moved them
>> >> from existing mtd-utils code.
>> >
>> > Actually no, I did not notice!
>>
>> np ... only one or two utils uses this, and easy enough to convert
>> them. before i go further though, a question on direction ...
>>
>> mkfs.jffs2 has a lot of helpers that can be moved to common.h. but it
>> implements things using C functions & va_args. the common.h header
>> takes the CPP approach of expanding the variable args. which way to
>> go to ultimately unify the code base !?
>
> I have not strong opinion, how about creating common.c ?
issues (not good or bad, but just are):
- change "#define PROGRAM_NAME" to "const char program_name[]"
- errors thrown up by libraries would be flagged as part of the
program, and not part of the library (i.e. libubifs)
- unused functions would take up space in final exe
the first is easy to change. the 2nd i think is OK (and maybe even
less confusing for the user when reviewing error output). the 3rd i'd
like to make sure we address.
two possible solutions to 3rd one. one ugly but portable, one nice
but gnu-specific. although we already use gnu-specific code and
flags, so maybe that isnt an issue (no one has complained yet).
ugly: create lib/common/ and put every function into its own file and
merge into libmtd.a. linker only pulls in objects (funcs) when
needed.
nice: use -ffunction-sections -fdata-sections -Wl,--gc-sections.
additional advantage of saving space across all linked in library code
and not just common.h stuff.
-mike
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH 1/3] mtd-utils: new memory wrappers
2010-09-30 6:18 ` Mike Frysinger
@ 2010-09-30 6:56 ` Artem Bityutskiy
2010-10-01 0:47 ` Mike Frysinger
0 siblings, 1 reply; 15+ messages in thread
From: Artem Bityutskiy @ 2010-09-30 6:56 UTC (permalink / raw)
To: Mike Frysinger; +Cc: linux-mtd
On Thu, 2010-09-30 at 02:18 -0400, Mike Frysinger wrote:
> issues (not good or bad, but just are):
> - change "#define PROGRAM_NAME" to "const char program_name[]"
> - errors thrown up by libraries would be flagged as part of the
> program, and not part of the library (i.e. libubifs)
> - unused functions would take up space in final exe
>
> the first is easy to change. the 2nd i think is OK (and maybe even
> less confusing for the user when reviewing error output). the 3rd i'd
> like to make sure we address.
>
> two possible solutions to 3rd one. one ugly but portable, one nice
> but gnu-specific. although we already use gnu-specific code and
> flags, so maybe that isnt an issue (no one has complained yet).
>
> ugly: create lib/common/ and put every function into its own file and
> merge into libmtd.a. linker only pulls in objects (funcs) when
> needed.
> nice: use -ffunction-sections -fdata-sections -Wl,--gc-sections.
> additional advantage of saving space across all linked in library code
> and not just common.h stuff.
Sorry, I never dealt with -ffunction-sections stuff, I do not know the
price of this, so I basically cannot judge. But of course removing
unused code is a good thing to do. So you seem to be more competent in
these question, just go ahead with you nice cleanups!
--
Best Regards,
Artem Bityutskiy (Артём Битюцкий)
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH 1/3] mtd-utils: new memory wrappers
2010-09-30 6:56 ` Artem Bityutskiy
@ 2010-10-01 0:47 ` Mike Frysinger
0 siblings, 0 replies; 15+ messages in thread
From: Mike Frysinger @ 2010-10-01 0:47 UTC (permalink / raw)
To: dedekind1; +Cc: linux-mtd
On Thu, Sep 30, 2010 at 02:56, Artem Bityutskiy wrote:
> On Thu, 2010-09-30 at 02:18 -0400, Mike Frysinger wrote:
>> nice: use -ffunction-sections -fdata-sections -Wl,--gc-sections.
>> additional advantage of saving space across all linked in library code
>> and not just common.h stuff.
>
> Sorry, I never dealt with -ffunction-sections stuff, I do not know the
> price of this, so I basically cannot judge. But of course removing
> unused code is a good thing to do. So you seem to be more competent in
> these question, just go ahead with you nice cleanups!
i get nice results even now with these options.
total shrinkage in bytes: 103298(.text) 1344(.data) 128(.bss)
individual totals:
docfdisk -42 -16
doc_loadbios -42 -16
flashcp -34 -8
flash_erase -4746 -32
flash_info -42 -16
flash_lock -42 -16
flash_otp_dump -42 -16
flash_otp_info -34 -16
flash_unlock -42 -16
ftl_check -42 -16
ftl_format -42 -16
jffs2dump -34 -16
mkfs.jffs2 -1466 -32
mkfs.ubifs -5106 -24
mtd_debug -66 -16
nanddump -34 -16
nandtest -66 -16
nandwrite -34 -32 -32
nftldump -34 -16 -32
nftl_format -42 -16
recv_image -914 -16 -32
rfddump -34 -16
rfdformat -34 -16
serve_image -2770 -16 -32
sumtool -66 -32
ubirsvol -5514 -80
ubicrc32 -1794 -56
mtdinfo -14298 -88
ubimkvol -5258 -72
ubiupdatevol -7986 -80
ubinize -1866 -48
ubirename -6586 -72
ubidetach -9842 -80
ubirmvol -6170 -72
ubiformat -15018 -56
ubinfo -5002 -88
ubiattach -8114 -88
-mike
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH 1/3] mtd-utils: new memory wrappers
2010-09-30 6:01 ` Artem Bityutskiy
2010-09-30 6:18 ` Mike Frysinger
@ 2010-10-01 1:43 ` Mike Frysinger
1 sibling, 0 replies; 15+ messages in thread
From: Mike Frysinger @ 2010-10-01 1:43 UTC (permalink / raw)
To: dedekind1; +Cc: linux-mtd
On Thu, Sep 30, 2010 at 02:01, Artem Bityutskiy wrote:
> On Thu, 2010-09-30 at 01:59 -0400, Mike Frysinger wrote:
>> On Thu, Sep 30, 2010 at 01:48, Artem Bityutskiy wrote:
>> > On Thu, 2010-09-30 at 01:15 -0400, Mike Frysinger wrote:
>> >> as i'm sure you noticed, i didnt make these up. i simply moved them
>> >> from existing mtd-utils code.
>> >
>> > Actually no, I did not notice!
>>
>> np ... only one or two utils uses this, and easy enough to convert
>> them. before i go further though, a question on direction ...
>>
>> mkfs.jffs2 has a lot of helpers that can be moved to common.h. but it
>> implements things using C functions & va_args. the common.h header
>> takes the CPP approach of expanding the variable args. which way to
>> go to ultimately unify the code base !?
>
> I have not strong opinion, how about creating common.c ?
ok, i played a bit with a common.c. ultimately, i could not get it to
be smaller than a CPP approach on my x86_64 system. so i'll stick
with the CPP stuff.
-mike
^ permalink raw reply [flat|nested] 15+ messages in thread
* [PATCH 1/3 v2] mtd-utils: new memory wrappers
2010-09-29 23:30 [PATCH 1/3] mtd-utils: new memory wrappers Mike Frysinger
` (2 preceding siblings ...)
2010-09-30 4:59 ` [PATCH 1/3] mtd-utils: new memory wrappers Artem Bityutskiy
@ 2010-09-30 5:27 ` Mike Frysinger
2010-09-30 5:54 ` Artem Bityutskiy
3 siblings, 1 reply; 15+ messages in thread
From: Mike Frysinger @ 2010-09-30 5:27 UTC (permalink / raw)
To: linux-mtd
The mkfs.jffs2 program has local wrappers for memory related functions
that are useful beyond mkfs.jffs2, so break them out into a common header.
Signed-off-by: Mike Frysinger <vapier@gentoo.org>
---
v2
- shorten "die" func names
include/common.h | 8 +++++
include/xalloc.h | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
mkfs.jffs2.c | 41 ---------------------------
3 files changed, 88 insertions(+), 41 deletions(-)
create mode 100644 include/xalloc.h
diff --git a/include/common.h b/include/common.h
index 472315e..9f49f55 100644
--- a/include/common.h
+++ b/include/common.h
@@ -58,6 +58,9 @@ extern "C" {
fprintf(stderr, "%s: error!: " fmt "\n", PROGRAM_NAME, ##__VA_ARGS__); \
-1; \
})
+#define errmsg_die(fmt, ...) do { \
+ exit(errmsg(fmt, ##__VA_ARGS__)); \
+} while(0)
/* System error messages */
#define sys_errmsg(fmt, ...) ({ \
@@ -69,6 +72,9 @@ extern "C" {
fprintf(stderr, "error %d (%s)\n", _err, strerror(_err)); \
-1; \
})
+#define sys_errmsg_die(fmt, ...) do { \
+ exit(sys_errmsg(fmt, ##__VA_ARGS__)); \
+} while(0)
/* Warnings */
#define warnmsg(fmt, ...) do { \
@@ -103,6 +109,8 @@ simple_strtoX(strtoll, long int)
simple_strtoX(strtoul, unsigned long int)
simple_strtoX(strtoull, unsigned long int)
+#include "xalloc.h"
+
#ifdef __cplusplus
}
#endif
diff --git a/include/xalloc.h b/include/xalloc.h
new file mode 100644
index 0000000..5d145d9
--- /dev/null
+++ b/include/xalloc.h
@@ -0,0 +1,80 @@
+/*
+ * memory wrappers
+ *
+ * Copyright (c) Artem Bityutskiy, 2007, 2008
+ * Copyright 2001, 2002 Red Hat, Inc.
+ * 2001 David A. Schleef <ds@lineo.com>
+ * 2002 Axis Communications AB
+ * 2001, 2002 Erik Andersen <andersen@codepoet.org>
+ * 2004 University of Szeged, Hungary
+ * 2006 KaiGai Kohei <kaigai@ak.jp.nec.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
+ * the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#ifndef __MTD_UTILS_XALLOC_H__
+#define __MTD_UTILS_XALLOC_H__
+
+#include <stdlib.h>
+#include <string.h>
+
+/*
+ * Mark these functions as unused so that gcc does not emit warnings
+ * when people include this header but don't use every function.
+ */
+
+__attribute__((unused))
+static void *xmalloc(size_t size)
+{
+ void *ptr = malloc(size);
+
+ if (ptr == NULL && size != 0)
+ sys_errmsg_die("malloc(%zu) failed", size);
+ return ptr;
+}
+
+__attribute__((unused))
+static void *xcalloc(size_t nmemb, size_t size)
+{
+ void *ptr = calloc(nmemb, size);
+
+ if (ptr == NULL && nmemb != 0 && size != 0)
+ sys_errmsg_die("calloc(%zu, %zu) failed", nmemb, size);
+ return ptr;
+}
+
+__attribute__((unused))
+static void *xrealloc(void *ptr, size_t size)
+{
+ ptr = realloc(ptr, size);
+ if (ptr == NULL && size != 0)
+ sys_errmsg_die("realloc(%p, %zu) failed", ptr, size);
+ return ptr;
+}
+
+__attribute__((unused))
+static char *xstrdup(const char *s)
+{
+ char *t;
+
+ if (s == NULL)
+ return NULL;
+ t = strdup(s);
+ if (t == NULL)
+ sys_errmsg_die("strdup(%p) failed", s);
+ return t;
+}
+
+#endif /* !__MTD_UTILS_XALLOC_H__ */
diff --git a/mkfs.jffs2.c b/mkfs.jffs2.c
index 1abe09c..1ea3598 100644
--- a/mkfs.jffs2.c
+++ b/mkfs.jffs2.c
@@ -110,7 +110,6 @@ static int squash_uids = 0;
static int squash_perms = 0;
static int fake_times = 0;
int target_endian = __BYTE_ORDER;
-static const char *const memory_exhausted = "memory exhausted";
uint32_t find_hardlink(struct filesystem_entry *e)
{
@@ -197,46 +196,6 @@ static void perror_msg_and_die(const char *s, ...)
exit(EXIT_FAILURE);
}
-#ifndef DMALLOC
-extern void *xmalloc(size_t size)
-{
- void *ptr = malloc(size);
-
- if (ptr == NULL && size != 0)
- error_msg_and_die(memory_exhausted);
- return ptr;
-}
-
-extern void *xcalloc(size_t nmemb, size_t size)
-{
- void *ptr = calloc(nmemb, size);
-
- if (ptr == NULL && nmemb != 0 && size != 0)
- error_msg_and_die(memory_exhausted);
- return ptr;
-}
-
-extern void *xrealloc(void *ptr, size_t size)
-{
- ptr = realloc(ptr, size);
- if (ptr == NULL && size != 0)
- error_msg_and_die(memory_exhausted);
- return ptr;
-}
-
-extern char *xstrdup(const char *s)
-{
- char *t;
-
- if (s == NULL)
- return NULL;
- t = strdup(s);
- if (t == NULL)
- error_msg_and_die(memory_exhausted);
- return t;
-}
-#endif
-
extern char *xreadlink(const char *path)
{
static const int GROWBY = 80; /* how large we will grow strings by */
--
1.7.2.3
^ permalink raw reply related [flat|nested] 15+ messages in thread* Re: [PATCH 1/3 v2] mtd-utils: new memory wrappers
2010-09-30 5:27 ` [PATCH 1/3 v2] " Mike Frysinger
@ 2010-09-30 5:54 ` Artem Bityutskiy
0 siblings, 0 replies; 15+ messages in thread
From: Artem Bityutskiy @ 2010-09-30 5:54 UTC (permalink / raw)
To: Mike Frysinger; +Cc: linux-mtd
On Thu, 2010-09-30 at 01:27 -0400, Mike Frysinger wrote:
> The mkfs.jffs2 program has local wrappers for memory related functions
> that are useful beyond mkfs.jffs2, so break them out into a common header.
>
> Signed-off-by: Mike Frysinger <vapier@gentoo.org>
> ---
> v2
Pushed this one and 2/4 v2 and 3/3 to mtd-utils, thanks!
--
Best Regards,
Artem Bityutskiy (Артём Битюцкий)
^ permalink raw reply [flat|nested] 15+ messages in thread