[sheepdog] [PATCH 6/6] sheep, collie: use xzalloc instead of zalloc

Liu Yuan namei.unix at gmail.com
Sun Jan 27 08:43:47 CET 2013


From: Liu Yuan <tailai.ly at taobao.com>

We can't survive zalloc() failure, then just use xzalloc to panic out when out
of memory.

Signed-off-by: Liu Yuan <tailai.ly at taobao.com>
---
 collie/collie.c           |    7 +------
 collie/vdi.c              |   15 ++------------
 lib/event.c               |    5 +----
 sheep/cluster/corosync.c  |   23 +++++----------------
 sheep/group.c             |   21 ++-----------------
 sheep/object_list_cache.c |    8 +-------
 sheep/vdi.c               |   49 +++++++++------------------------------------
 sheep/work.c              |    5 +----
 8 files changed, 22 insertions(+), 111 deletions(-)

diff --git a/collie/collie.c b/collie/collie.c
index d8b0f06..a84baba 100644
--- a/collie/collie.c
+++ b/collie/collie.c
@@ -57,12 +57,7 @@ static int update_node_list(int max_nodes, uint32_t epoch)
 		return -1;
 
 	size = sizeof(*ent) * max_nodes;
-	buf = zalloc(size);
-	if (!buf) {
-		ret = -1;
-		goto out;
-	}
-
+	buf = xzalloc(size);
 	sd_init_req((struct sd_req *)&hdr, SD_OP_GET_NODE_LIST);
 	hdr.request_ver = epoch;
 
diff --git a/collie/vdi.c b/collie/vdi.c
index 3547783..7109be9 100644
--- a/collie/vdi.c
+++ b/collie/vdi.c
@@ -296,12 +296,7 @@ static void parse_objs(uint64_t oid, obj_parser_func_t func, void *data, unsigne
 	int i, fd, ret, cb_ret;
 	char *buf;
 
-	buf = zalloc(size);
-	if (!buf) {
-		fprintf(stderr, "Failed to allocate memory\n");
-		return;
-	}
-
+	buf = xzalloc(size);
 	for (i = 0; i < sd_nodes_nr; i++) {
 		struct sd_req hdr;
 		struct sd_rsp *rsp = (struct sd_rsp *)&hdr;
@@ -648,13 +643,7 @@ static int vdi_clone(int argc, char **argv)
 	if (ret != EXIT_SUCCESS || !vdi_cmd_data.prealloc)
 		goto out;
 
-	buf = zalloc(SD_DATA_OBJ_SIZE);
-	if (!buf) {
-		fprintf(stderr, "Failed to allocate memory\n");
-		ret = EXIT_SYSFAIL;
-		goto out;
-	}
-
+	buf = xzalloc(SD_DATA_OBJ_SIZE);
 	max_idx = DIV_ROUND_UP(inode->vdi_size, SD_DATA_OBJ_SIZE);
 
 	for (idx = 0; idx < max_idx; idx++) {
diff --git a/lib/event.c b/lib/event.c
index a5b1eca..8b84a53 100644
--- a/lib/event.c
+++ b/lib/event.c
@@ -98,10 +98,7 @@ int register_event(int fd, event_handler_t h, void *data)
 	struct epoll_event ev;
 	struct event_info *ei;
 
-	ei = zalloc(sizeof(*ei));
-	if (!ei)
-		return -ENOMEM;
-
+	ei = xzalloc(sizeof(*ei));
 	ei->fd = fd;
 	ei->handler = h;
 	ei->data = data;
diff --git a/sheep/cluster/corosync.c b/sheep/cluster/corosync.c
index 2372faf..176ac83 100644
--- a/sheep/cluster/corosync.c
+++ b/sheep/cluster/corosync.c
@@ -492,10 +492,7 @@ static void cdrv_cpg_deliver(cpg_handle_t handle,
 		/* fall through */
 	case COROSYNC_MSG_TYPE_BLOCK:
 	case COROSYNC_MSG_TYPE_NOTIFY:
-		cevent = zalloc(sizeof(*cevent));
-		if (!cevent)
-			panic("failed to allocate memory\n");
-
+		cevent = xzalloc(sizeof(*cevent));
 		if (cmsg->type == COROSYNC_MSG_TYPE_BLOCK)
 			cevent->type = COROSYNC_EVENT_TYPE_BLOCK;
 		else
@@ -504,9 +501,7 @@ static void cdrv_cpg_deliver(cpg_handle_t handle,
 		cevent->sender = cmsg->sender;
 		cevent->msg_len = cmsg->msg_len;
 		if (cmsg->msg_len) {
-			cevent->msg = zalloc(cmsg->msg_len);
-			if (!cevent->msg)
-				panic("failed to allocate memory\n");
+			cevent->msg = xzalloc(cmsg->msg_len);
 			memcpy(cevent->msg, cmsg->msg, cmsg->msg_len);
 		} else
 			cevent->msg = NULL;
@@ -514,9 +509,7 @@ static void cdrv_cpg_deliver(cpg_handle_t handle,
 		queue_event(cevent);
 		break;
 	case COROSYNC_MSG_TYPE_LEAVE:
-		cevent = zalloc(sizeof(*cevent));
-		if (!cevent)
-			panic("failed to allocate memory\n");
+		cevent = xzalloc(sizeof(*cevent));
 		cevent->type = COROSYNC_EVENT_TYPE_LEAVE;
 
 		master = is_master(&cmsg->sender);
@@ -628,10 +621,7 @@ static void cdrv_cpg_confchg(cpg_handle_t handle,
 			free(cevent);
 		}
 
-		cevent = zalloc(sizeof(*cevent));
-		if (!cevent)
-			panic("failed to allocate memory\n");
-
+		cevent = xzalloc(sizeof(*cevent));
 		master = is_master(&left_sheep[i]);
 		if (master >= 0)
 		/* Master is down before new nodes finish joining.
@@ -647,10 +637,7 @@ static void cdrv_cpg_confchg(cpg_handle_t handle,
 
 	/* dispatch join_handler */
 	for (i = 0; i < joined_list_entries; i++) {
-		cevent = zalloc(sizeof(*cevent));
-		if (!cevent)
-			panic("failed to allocate memory\n");
-
+		cevent = xzalloc(sizeof(*cevent));
 		cevent->type = COROSYNC_EVENT_TYPE_JOIN_REQUEST;
 		cevent->sender = joined_sheep[i];
 		queue_event(cevent);
diff --git a/sheep/group.c b/sheep/group.c
index f32f625..9598ae5 100644
--- a/sheep/group.c
+++ b/sheep/group.c
@@ -238,12 +238,7 @@ static struct vdi_op_message *prepare_cluster_msg(struct request *req,
 
 	assert(size <= SD_MAX_EVENT_BUF_SIZE);
 
-	msg = zalloc(size);
-	if (!msg) {
-		sd_eprintf("failed to allocate memory\n");
-		return NULL;
-	}
-
+	msg = xzalloc(size);
 	memcpy(&msg->req, &req->rq, sizeof(struct sd_req));
 	memcpy(&msg->rsp, &req->rp, sizeof(struct sd_rsp));
 
@@ -265,9 +260,6 @@ static void cluster_op_done(struct work *work)
 	sd_dprintf("%s (%p)\n", op_name(req->op), req);
 
 	msg = prepare_cluster_msg(req, &size);
-	if (!msg)
-		panic();
-
 	sys->cdrv->unblock(msg, size);
 
 	free(msg);
@@ -322,9 +314,6 @@ void queue_cluster_request(struct request *req)
 		size_t size;
 
 		msg = prepare_cluster_msg(req, &size);
-		if (!msg)
-			return;
-
 		list_add_tail(&req->pending_list, &sys->pending_notify_list);
 
 		msg->rsp.result = SD_RES_SUCCESS;
@@ -615,13 +604,7 @@ static int get_vdis_from(struct sd_node *node)
 		goto out;
 
 	rlen = SD_DATA_OBJ_SIZE; /* FIXME */
-	vc = zalloc(rlen);
-	if (!vc) {
-		sd_printf(SDOG_ERR, "unable to allocate memory\n");
-		ret = SD_RES_NO_MEM;
-		goto out;
-	}
-
+	vc = xzalloc(rlen);
 	sd_init_req(&hdr, SD_OP_GET_VDI_COPIES);
 	hdr.data_length = rlen;
 	ret = sheep_exec_req(&node->nid, &hdr, (char *)vc);
diff --git a/sheep/object_list_cache.c b/sheep/object_list_cache.c
index a830bab..20093f3 100644
--- a/sheep/object_list_cache.c
+++ b/sheep/object_list_cache.c
@@ -112,13 +112,7 @@ int objlist_cache_insert(uint64_t oid)
 {
 	struct objlist_cache_entry *entry, *p;
 
-	entry = zalloc(sizeof(*entry));
-
-	if (!entry) {
-		sd_eprintf("no memory to allocate cache entry.\n");
-		return -1;
-	}
-
+	entry = xzalloc(sizeof(*entry));
 	entry->oid = oid;
 	rb_init_node(&entry->node);
 
diff --git a/sheep/vdi.c b/sheep/vdi.c
index 6133e4d..d8e2104 100644
--- a/sheep/vdi.c
+++ b/sheep/vdi.c
@@ -166,12 +166,7 @@ int vdi_exist(uint32_t vid)
 	int ret = 1;
 	int nr_copies;
 
-	inode = zalloc(sizeof(*inode));
-	if (!inode) {
-		ret = 0;
-		goto out;
-	}
-
+	inode = xzalloc(sizeof(*inode));
 	nr_copies = get_vdi_copy_number(vid);
 
 	ret = read_object(vid_to_vdi_oid(vid), (char *)inode,
@@ -203,27 +198,12 @@ static int create_vdi_obj(struct vdi_iocb *iocb, uint32_t new_vid,
 	unsigned long block_size = SD_DATA_OBJ_SIZE;
 	const char *name = iocb->name;
 
-	new = zalloc(sizeof(*new));
-	if (!new) {
-		sd_eprintf("failed to allocate memory\n");
-		goto out;
-	}
+	new = xzalloc(sizeof(*new));
+	if (iocb->base_vid)
+		base = xzalloc(sizeof(*base));
 
-	if (iocb->base_vid) {
-		base = zalloc(sizeof(*base));
-		if (!base) {
-			sd_eprintf("failed to allocate memory\n");
-			goto out;
-		}
-	}
-
-	if (iocb->create_snapshot && cur_vid != iocb->base_vid) {
-		cur = zalloc(SD_INODE_HEADER_SIZE);
-		if (!cur) {
-			sd_eprintf("failed to allocate memory\n");
-			goto out;
-		}
-	}
+	if (iocb->create_snapshot && cur_vid != iocb->base_vid)
+		cur = xzalloc(SD_INODE_HEADER_SIZE);
 
 	if (iocb->base_vid) {
 		ret = read_object(vid_to_vdi_oid(iocb->base_vid), (char *)base,
@@ -576,12 +556,7 @@ static int delete_inode(struct deletion_work *dw)
 	struct sheepdog_inode *inode = NULL;
 	int ret = SD_RES_SUCCESS;
 
-	inode = zalloc(sizeof(*inode));
-	if (!inode) {
-		sd_eprintf("no memory to allocate inode.\n");
-		goto out;
-	}
-
+	inode = xzalloc(sizeof(*inode));
 	ret = read_object(vid_to_vdi_oid(dw->vid), (char *)inode,
 			  SD_INODE_HEADER_SIZE, 0, dw->nr_copies);
 	if (ret != SD_RES_SUCCESS) {
@@ -808,15 +783,9 @@ static int start_deletion(struct request *req, uint32_t vid)
 	bool cloned;
 	uint32_t root_vid;
 
-	dw = zalloc(sizeof(struct deletion_work));
-	if (!dw)
-		goto err;
-
+	dw = xzalloc(sizeof(struct deletion_work));
 	/* buf is to store vdi id of every object */
-	dw->buf = zalloc(SD_INODE_SIZE - SD_INODE_HEADER_SIZE);
-	if (!dw->buf)
-		goto err;
-
+	dw->buf = xzalloc(SD_INODE_SIZE - SD_INODE_HEADER_SIZE);
 	dw->count = 0;
 	dw->vid = vid;
 	dw->req = req;
diff --git a/sheep/work.c b/sheep/work.c
index bf3dccf..ba88859 100644
--- a/sheep/work.c
+++ b/sheep/work.c
@@ -209,10 +209,7 @@ struct work_queue *init_work_queue(const char *name, bool ordered)
 	int ret;
 	struct worker_info *wi;
 
-	wi = zalloc(sizeof(*wi));
-	if (!wi)
-		return NULL;
-
+	wi = xzalloc(sizeof(*wi));
 	wi->name = name;
 	wi->ordered = ordered;
 
-- 
1.7.9.5




More information about the sheepdog mailing list