[sheepdog] [PATCH 2/2] collie: use xmalloc() where it is suitable than raw malloc()

Hitoshi Mitake mitake.hitoshi at gmail.com
Sun Feb 24 10:20:09 CET 2013


This patch replaces malloc() calls of collie with xmalloc() where fail
of memory allocation directly causes a fatal error.

Cc: Harry Wei <harryxiyou at gmail.com>
Signed-off-by: Hitoshi Mitake <mitake.hitoshi at lab.ntt.co.jp>
---
 collie/cluster.c  |    4 +---
 collie/treeview.c |    6 +-----
 collie/vdi.c      |   42 +++++++-----------------------------------
 3 files changed, 9 insertions(+), 43 deletions(-)

diff --git a/collie/cluster.c b/collie/cluster.c
index 10ced3b..86da2ed 100644
--- a/collie/cluster.c
+++ b/collie/cluster.c
@@ -300,9 +300,7 @@ static int list_snap(void)
 	struct sd_rsp *rsp = (struct sd_rsp *)&hdr;
 	void *buf;
 
-	buf = malloc(SD_DATA_OBJ_SIZE);
-	if (!buf)
-		return EXIT_SYSFAIL;
+	buf = xmalloc(SD_DATA_OBJ_SIZE);
 
 	fd = connect_to(sdhost, sdport);
 	if (fd < 0)
diff --git a/collie/treeview.c b/collie/treeview.c
index 21b4cc1..cb3bd87 100644
--- a/collie/treeview.c
+++ b/collie/treeview.c
@@ -57,11 +57,7 @@ static struct vdi_tree *new_vdi(const char *name, const char *label,
 {
 	struct vdi_tree *vdi;
 
-	vdi = malloc(sizeof(struct vdi_tree));
-	if (!vdi) {
-		fprintf(stderr, "Failed to allocate memory\n");
-		return NULL;
-	}
+	vdi = xmalloc(sizeof(struct vdi_tree));
 	pstrcpy(vdi->name, sizeof(vdi->name), name);
 	pstrcpy(vdi->label, sizeof(vdi->label), label);
 	vdi->vid = vid;
diff --git a/collie/vdi.c b/collie/vdi.c
index 3fb7d68..b82baca 100644
--- a/collie/vdi.c
+++ b/collie/vdi.c
@@ -534,12 +534,7 @@ static int vdi_create(int argc, char **argv)
 	if (ret != EXIT_SUCCESS || !vdi_cmd_data.prealloc)
 		goto out;
 
-	inode = malloc(sizeof(*inode));
-	if (!inode) {
-		fprintf(stderr, "Failed to allocate memory\n");
-		ret = EXIT_SYSFAIL;
-		goto out;
-	}
+	inode = xmalloc(sizeof(*inode));
 
 	ret = sd_read_object(vid_to_vdi_oid(vid), inode, sizeof(*inode), 0, true);
 	if (ret != SD_RES_SUCCESS) {
@@ -626,12 +621,7 @@ static int vdi_clone(int argc, char **argv)
 		goto out;
 	}
 
-	inode = malloc(sizeof(*inode));
-	if (!inode) {
-		fprintf(stderr, "Failed to allocate memory\n");
-		ret = EXIT_SYSFAIL;
-		goto out;
-	}
+	inode = xmalloc(sizeof(*inode));
 
 	ret = read_vdi_obj(src_vdi, vdi_cmd_data.snapshot_id,
 			   vdi_cmd_data.snapshot_tag, &base_vid, inode,
@@ -1075,11 +1065,7 @@ static int vdi_setattr(int argc, char **argv)
 
 	value = argv[optind++];
 	if (!value && !vdi_cmd_data.delete) {
-		value = malloc(SD_MAX_VDI_ATTR_VALUE_LEN);
-		if (!value) {
-			fprintf(stderr, "Failed to allocate memory\n");
-			return EXIT_SYSFAIL;
-		}
+		value = xmalloc(SD_MAX_VDI_ATTR_VALUE_LEN);
 
 		offset = 0;
 reread:
@@ -1189,12 +1175,7 @@ static int vdi_read(int argc, char **argv)
 	}
 
 	inode = malloc(sizeof(*inode));
-	buf = malloc(SD_DATA_OBJ_SIZE);
-	if (!inode || !buf) {
-		fprintf(stderr, "Failed to allocate memory\n");
-		ret = EXIT_SYSFAIL;
-		goto out;
-	}
+	buf = xmalloc(SD_DATA_OBJ_SIZE);
 
 	ret = read_vdi_obj(vdiname, vdi_cmd_data.snapshot_id,
 			   vdi_cmd_data.snapshot_tag, NULL, inode,
@@ -1276,13 +1257,8 @@ static int vdi_write(int argc, char **argv)
 		}
 	}
 
-	inode = malloc(sizeof(*inode));
-	buf = malloc(SD_DATA_OBJ_SIZE);
-	if (!inode || !buf) {
-		fprintf(stderr, "Failed to allocate memory\n");
-		ret = EXIT_SYSFAIL;
-		goto out;
-	}
+	inode = xmalloc(sizeof(*inode));
+	buf = xmalloc(SD_DATA_OBJ_SIZE);
 
 	ret = read_vdi_obj(vdiname, 0, "", &vid, inode, SD_INODE_SIZE);
 	if (ret != EXIT_SUCCESS)
@@ -1377,11 +1353,7 @@ static void *read_object_from(const struct sd_vnode *vnode, uint64_t oid)
 	char name[128];
 	void *buf;
 
-	buf = malloc(SD_DATA_OBJ_SIZE);
-	if (!buf) {
-		fprintf(stderr, "Failed to allocate memory\n");
-		exit(EXIT_SYSFAIL);
-	}
+	buf = xmalloc(SD_DATA_OBJ_SIZE);
 
 	addr_to_str(name, sizeof(name), vnode->nid.addr, 0);
 	fd = connect_to(name, vnode->nid.port);
-- 
1.7.5.1




More information about the sheepdog mailing list