]> err.no Git - systemd/commitdiff
use more appropriate alternatives to malloc()
authorAlan Jenkins <alan-jenkins@tuffmail.co.uk>
Tue, 21 Oct 2008 10:10:32 +0000 (11:10 +0100)
committerKay Sievers <kay.sievers@vrfy.org>
Tue, 21 Oct 2008 10:54:57 +0000 (12:54 +0200)
Use calloc to request cleared memory instead.
Kernel and libc conspire to make this more efficient.

Also, replace one malloc() + strcpy() with strdup().

Signed-off-by: Alan Jenkins <alan-jenkins@tuffmail.co.uk>
extras/collect/collect.c
extras/volume_id/lib/volume_id.c
udev/lib/libudev-ctrl.c
udev/lib/libudev-device.c
udev/lib/libudev-enumerate.c
udev/lib/libudev-monitor.c
udev/lib/libudev-queue.c
udev/lib/libudev.c
udev/udev-event.c
udev/udev-rules.c

index 355b85b5cfdb6199c63caa95bb1c5acf3b1d33a9..3a7e826e4dfd0ce80b914cdfd0196d1cb66ce529 100644 (file)
@@ -171,8 +171,7 @@ static int checkout(int fd)
                                if (debug)
                                        fprintf(stderr, "Found word %s\n", word);
                                him = malloc(sizeof (struct _mate));
-                               him->name = malloc(strlen(word) + 1);
-                               strcpy(him->name, word);
+                               him->name = strdup(word);
                                him->state = STATE_OLD;
                                udev_list_node_append(&him->node, &bunch);
                                word = NULL;
index 791be43515323f4e10be84883db5b46329d052a7..8f22509ca6be6ab718780dda3e4c428c4cbc0a9a 100644 (file)
@@ -483,10 +483,9 @@ struct volume_id *volume_id_open_fd(int fd)
 {
        struct volume_id *id;
 
-       id = malloc(sizeof(struct volume_id));
+       id = calloc(1, sizeof(struct volume_id));
        if (id == NULL)
                return NULL;
-       memset(id, 0x00, sizeof(struct volume_id));
 
        id->fd = fd;
 
index 848f5078783db4a2a4080df73b7eb08083c9d3b0..2e15db0be0ba1e6e210cb18a3857a1043beafb41 100644 (file)
@@ -72,10 +72,9 @@ struct udev_ctrl *udev_ctrl_new_from_socket(struct udev *udev, const char *socke
 {
        struct udev_ctrl *uctrl;
 
-       uctrl = malloc(sizeof(struct udev_ctrl));
+       uctrl = calloc(1, sizeof(struct udev_ctrl));
        if (uctrl == NULL)
                return NULL;
-       memset(uctrl, 0x00, sizeof(struct udev_ctrl));
        uctrl->refcount = 1;
        uctrl->udev = udev;
 
@@ -213,10 +212,9 @@ struct udev_ctrl_msg *udev_ctrl_receive_msg(struct udev_ctrl *uctrl)
        struct ucred *cred;
        char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
 
-       uctrl_msg = malloc(sizeof(struct udev_ctrl_msg));
+       uctrl_msg = calloc(1, sizeof(struct udev_ctrl_msg));
        if (uctrl_msg == NULL)
                return NULL;
-       memset(uctrl_msg, 0x00, sizeof(struct udev_ctrl_msg));
        uctrl_msg->refcount = 1;
        uctrl_msg->uctrl = uctrl;
 
index b54c7271e7f2d3a32e0eb756e72160795df8d6e4..e4cbd8e6895539c9ba9127124ecb0e3528e599bf 100644 (file)
@@ -236,10 +236,9 @@ struct udev_device *device_new(struct udev *udev)
        if (udev == NULL)
                return NULL;
 
-       udev_device = malloc(sizeof(struct udev_device));
+       udev_device = calloc(1, sizeof(struct udev_device));
        if (udev_device == NULL)
                return NULL;
-       memset(udev_device, 0x00, sizeof(struct udev_device));
        udev_device->refcount = 1;
        udev_device->udev = udev;
        udev_list_init(&udev_device->devlinks_list);
index b35a82d0ceda8819fece12360eaf4edb3bdf627e..e5a4ccee3b62c9c2642cfac6dd6344e7581c444d 100644 (file)
@@ -53,10 +53,9 @@ struct udev_enumerate *udev_enumerate_new(struct udev *udev)
 {
        struct udev_enumerate *udev_enumerate;
 
-       udev_enumerate = malloc(sizeof(struct udev_enumerate));
+       udev_enumerate = calloc(1, sizeof(struct udev_enumerate));
        if (udev_enumerate == NULL)
                return NULL;
-       memset(udev_enumerate, 0x00, (sizeof(struct udev_enumerate)));
        udev_enumerate->refcount = 1;
        udev_enumerate->udev = udev;
        udev_list_init(&udev_enumerate->devices_list);
index 6c6f4aa7e063447d3ae9bcc276775e204e4850eb..7f7f8460c26f1046554408f469a28c024203ce8a 100644 (file)
@@ -67,10 +67,9 @@ struct udev_monitor *udev_monitor_new_from_socket(struct udev *udev, const char
                return NULL;
        if (socket_path == NULL)
                return NULL;
-       udev_monitor = malloc(sizeof(struct udev_monitor));
+       udev_monitor = calloc(1, sizeof(struct udev_monitor));
        if (udev_monitor == NULL)
                return NULL;
-       memset(udev_monitor, 0x00, sizeof(struct udev_monitor));
        udev_monitor->refcount = 1;
        udev_monitor->udev = udev;
 
@@ -105,10 +104,9 @@ struct udev_monitor *udev_monitor_new_from_netlink(struct udev *udev)
 
        if (udev == NULL)
                return NULL;
-       udev_monitor = malloc(sizeof(struct udev_monitor));
+       udev_monitor = calloc(1, sizeof(struct udev_monitor));
        if (udev_monitor == NULL)
                return NULL;
-       memset(udev_monitor, 0x00, sizeof(struct udev_monitor));
        udev_monitor->refcount = 1;
        udev_monitor->udev = udev;
 
index f1c69fdb93e1bcec2d02ccba841f197259a349a9..ea75f22044eea20918480cd78274d8df198605d8 100644 (file)
@@ -45,10 +45,9 @@ struct udev_queue *udev_queue_new(struct udev *udev)
        if (udev == NULL)
                return NULL;
 
-       udev_queue = malloc(sizeof(struct udev_queue));
+       udev_queue = calloc(1, sizeof(struct udev_queue));
        if (udev_queue == NULL)
                return NULL;
-       memset(udev_queue, 0x00, sizeof(struct udev_queue));
        udev_queue->refcount = 1;
        udev_queue->udev = udev;
        udev_list_init(&udev_queue->queue_list);
index a09aa051b3a369139e7899618d40f73b7f95a931..fcb784580b3d0579bc59619fbbc4562acd5a6439 100644 (file)
@@ -96,10 +96,9 @@ struct udev *udev_new(void)
        char *config_file;
        FILE *f;
 
-       udev = malloc(sizeof(struct udev));
+       udev = calloc(1, sizeof(struct udev));
        if (udev == NULL)
                return NULL;
-       memset(udev, 0x00, (sizeof(struct udev)));
        udev->refcount = 1;
        udev->log_fn = log_stderr;
        udev->log_priority = LOG_ERR;
index 007a14ebe92e31a290f7c241dde5b7eb93948d03..05bb022d1b46f7d82b5b8b0b8cadefab65662b23 100644 (file)
@@ -34,10 +34,9 @@ struct udev_event *udev_event_new(struct udev_device *dev)
 {
        struct udev_event *event;
 
-       event = malloc(sizeof(struct udev_event));
+       event = calloc(1, sizeof(struct udev_event));
        if (event == NULL)
                return NULL;
-       memset(event, 0x00, sizeof(struct udev_event));
 
        event->dev = dev;
        event->udev = udev_device_get_udev(dev);
index 9b2a6ab3ece97fe20a2263a3e7d6b425e914f54f..32eed78d8a54bb1cabeeec3710fb70180cbb4675 100644 (file)
@@ -1772,10 +1772,9 @@ struct udev_rules *udev_rules_new(struct udev *udev, int resolve_names)
        struct udev_list_node file_list;
        struct udev_list_entry *file_loop, *file_tmp;
 
-       rules = malloc(sizeof(struct udev_rules));
+       rules = calloc(1, sizeof(struct udev_rules));
        if (rules == NULL)
                return rules;
-       memset(rules, 0x00, sizeof(struct udev_rules));
        rules->udev = udev;
        rules->resolve_names = resolve_names;
        udev_list_init(&file_list);