* [PATCH 2/2] malloc -> calloc and fix some memory leaks.
@ 2010-08-02 3:43 Yin Kangkai
2010-08-02 9:00 ` Kay Sievers
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Yin Kangkai @ 2010-08-02 3:43 UTC (permalink / raw)
To: linux-hotplug
From fd81bea16d3e1e93aebc8cb80ec16ae9c20c6e41 Mon Sep 17 00:00:00 2001
From: Yin Kangkai <kangkai.yin@intel.com>
Date: Mon, 2 Aug 2010 11:22:49 +0800
Subject: [PATCH 2/2] malloc -> calloc and fix some memory leaks.
calloc so that we do not need to memset each of them.
Signed-off-by: Yin Kangkai <kangkai.yin@intel.com>
---
udev/udev-rules.c | 24 ++++++++++++++++--------
udev/udevd.c | 6 ++++--
2 files changed, 20 insertions(+), 10 deletions(-)
diff --git a/udev/udev-rules.c b/udev/udev-rules.c
index 6d32e73..072b31b 100644
--- a/udev/udev-rules.c
+++ b/udev/udev-rules.c
@@ -1751,23 +1751,27 @@ struct udev_rules *udev_rules_new(struct udev *udev, int resolve_names)
struct udev_list_entry *file_loop, *file_tmp;
struct token end_token;
- rules = malloc(sizeof(struct udev_rules));
+ rules = calloc(1, sizeof(struct udev_rules));
if (rules = NULL)
return NULL;
- memset(rules, 0x00, sizeof(struct udev_rules));
rules->udev = udev;
rules->resolve_names = resolve_names;
udev_list_init(&file_list);
/* init token array and string buffer */
- rules->tokens = malloc(PREALLOC_TOKEN * sizeof(struct token));
- if (rules->tokens = NULL)
+ rules->tokens = calloc(PREALLOC_TOKEN, sizeof(struct token));
+ if (rules->tokens = NULL) {
+ free(rules);
return NULL;
+ }
rules->token_max = PREALLOC_TOKEN;
- rules->buf = malloc(PREALLOC_STRBUF);
- if (rules->buf = NULL)
+ rules->buf = calloc(1, PREALLOC_STRBUF);
+ if (rules->buf = NULL) {
+ free(rules->tokens);
+ free(rules);
return NULL;
+ }
rules->buf_max = PREALLOC_STRBUF;
/* offset 0 is always '\0' */
rules->buf[0] = '\0';
@@ -1775,9 +1779,13 @@ struct udev_rules *udev_rules_new(struct udev *udev, int resolve_names)
dbg(udev, "prealloc %zu bytes tokens (%u * %zu bytes), %zu bytes buffer\n",
rules->token_max * sizeof(struct token), rules->token_max, sizeof(struct token), rules->buf_max);
- rules->trie_nodes = malloc(PREALLOC_TRIE * sizeof(struct trie_node));
- if (rules->trie_nodes = NULL)
+ rules->trie_nodes = calloc(PREALLOC_TRIE, sizeof(struct trie_node));
+ if (rules->trie_nodes = NULL) {
+ free(rules->buf);
+ free(rules->tokens);
+ free(rules);
return NULL;
+ }
rules->trie_nodes_max = PREALLOC_TRIE;
/* offset 0 is the trie root, with an empty string */
memset(rules->trie_nodes, 0x00, sizeof(struct trie_node));
diff --git a/udev/udevd.c b/udev/udevd.c
index 95d4ad8..b882479 100644
--- a/udev/udevd.c
+++ b/udev/udevd.c
@@ -227,8 +227,10 @@ static void worker_new(struct event *event)
udev_monitor_enable_receiving(worker_monitor);
worker = calloc(1, sizeof(struct worker));
- if (worker = NULL)
+ if (worker = NULL) {
+ udev_monitor_unref(worker_monitor);
return;
+ }
/* worker + event reference */
worker->refcount = 2;
worker->udev = event->udev;
@@ -655,7 +657,7 @@ static int handle_inotify(struct udev *udev)
if ((ioctl(pfd[FD_INOTIFY].fd, FIONREAD, &nbytes) < 0) || (nbytes <= 0))
return 0;
- buf = malloc(nbytes);
+ buf = calloc(1, nbytes);
if (buf = NULL) {
err(udev, "error getting buffer for inotify\n");
return -1;
--
1.6.5
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH 2/2] malloc -> calloc and fix some memory leaks.
2010-08-02 3:43 [PATCH 2/2] malloc -> calloc and fix some memory leaks Yin Kangkai
@ 2010-08-02 9:00 ` Kay Sievers
2010-08-02 10:00 ` Yin Kangkai
2010-08-02 10:13 ` Kay Sievers
2 siblings, 0 replies; 4+ messages in thread
From: Kay Sievers @ 2010-08-02 9:00 UTC (permalink / raw)
To: linux-hotplug
On Mon, Aug 2, 2010 at 05:43, Yin Kangkai <kangkai.yin@linux.intel.com> wrote:
> From fd81bea16d3e1e93aebc8cb80ec16ae9c20c6e41 Mon Sep 17 00:00:00 2001
> From: Yin Kangkai <kangkai.yin@intel.com>
> Date: Mon, 2 Aug 2010 11:22:49 +0800
> Subject: [PATCH 2/2] malloc -> calloc and fix some memory leaks.
>
> calloc so that we do not need to memset each of them.
Any reason to change things to calloc() where no memset() is done or needed?
Kay
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH 2/2] malloc -> calloc and fix some memory leaks.
2010-08-02 3:43 [PATCH 2/2] malloc -> calloc and fix some memory leaks Yin Kangkai
2010-08-02 9:00 ` Kay Sievers
@ 2010-08-02 10:00 ` Yin Kangkai
2010-08-02 10:13 ` Kay Sievers
2 siblings, 0 replies; 4+ messages in thread
From: Yin Kangkai @ 2010-08-02 10:00 UTC (permalink / raw)
To: linux-hotplug
On 2010-08-02, 11:00 +0200, Kay Sievers wrote:
> On Mon, Aug 2, 2010 at 05:43, Yin Kangkai <kangkai.yin@linux.intel.com> wrote:
> > From fd81bea16d3e1e93aebc8cb80ec16ae9c20c6e41 Mon Sep 17 00:00:00 2001
> > From: Yin Kangkai <kangkai.yin@intel.com>
> > Date: Mon, 2 Aug 2010 11:22:49 +0800
> > Subject: [PATCH 2/2] malloc -> calloc and fix some memory leaks.
> >
> > calloc so that we do not need to memset each of them.
>
> Any reason to change things to calloc() where no memset() is done or needed?
I changed the malloc and memset pair of udev_rules allocation, and for
consistence, I finished the others :)
Maybe it's only my personal taste that I like to memset memory once I
malloc success. If this is not necessary, I can split the patch.
Thanks,
Kangkai
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH 2/2] malloc -> calloc and fix some memory leaks.
2010-08-02 3:43 [PATCH 2/2] malloc -> calloc and fix some memory leaks Yin Kangkai
2010-08-02 9:00 ` Kay Sievers
2010-08-02 10:00 ` Yin Kangkai
@ 2010-08-02 10:13 ` Kay Sievers
2 siblings, 0 replies; 4+ messages in thread
From: Kay Sievers @ 2010-08-02 10:13 UTC (permalink / raw)
To: linux-hotplug
On Mon, Aug 2, 2010 at 12:00, Yin Kangkai <kangkai.yin@linux.intel.com> wrote:
> On 2010-08-02, 11:00 +0200, Kay Sievers wrote:
>> On Mon, Aug 2, 2010 at 05:43, Yin Kangkai <kangkai.yin@linux.intel.com> wrote:
>> > From fd81bea16d3e1e93aebc8cb80ec16ae9c20c6e41 Mon Sep 17 00:00:00 2001
>> > From: Yin Kangkai <kangkai.yin@intel.com>
>> > Date: Mon, 2 Aug 2010 11:22:49 +0800
>> > Subject: [PATCH 2/2] malloc -> calloc and fix some memory leaks.
>> >
>> > calloc so that we do not need to memset each of them.
>>
>> Any reason to change things to calloc() where no memset() is done or needed?
>
> I changed the malloc and memset pair of udev_rules allocation, and for
> consistence, I finished the others :)
>
> Maybe it's only my personal taste that I like to memset memory once I
> malloc success. If this is not necessary, I can split the patch.
Nah, I mean memset(), and calloc() is only necessary if the memory is
read, and not always overwritten anyway by the next operation, like
read() or similar. I don't think that needs to be 'fixed'.
Kay
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2010-08-02 10:13 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-08-02 3:43 [PATCH 2/2] malloc -> calloc and fix some memory leaks Yin Kangkai
2010-08-02 9:00 ` Kay Sievers
2010-08-02 10:00 ` Yin Kangkai
2010-08-02 10:13 ` Kay Sievers
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).