[sheepdog] [PATCH 1/6] sheep: use xmalloc instead of malloc

MORITA Kazutaka morita.kazutaka at lab.ntt.co.jp
Wed Apr 24 05:30:14 CEST 2013


At Mon, 22 Apr 2013 23:34:10 +0800,
Liu Yuan wrote:
> 
> From: Liu Yuan <tailai.ly at taobao.com>
> 
> Signed-off-by: Liu Yuan <tailai.ly at taobao.com>
> ---
>  sheep/vdi.c |   29 ++++-------------------------
>  1 file changed, 4 insertions(+), 25 deletions(-)
> 
> diff --git a/sheep/vdi.c b/sheep/vdi.c
> index 5f23846..e4d3c9e 100644
> --- a/sheep/vdi.c
> +++ b/sheep/vdi.c
> @@ -311,12 +311,7 @@ static int find_first_vdi(unsigned long start, unsigned long end,
>  	bool vdi_found = false;
>  	int nr_copies;
>  
> -	inode = malloc(SD_INODE_HEADER_SIZE);
> -	if (!inode) {
> -		sd_eprintf("failed to allocate memory");
> -		goto out;
> -	}
> -
> +	inode = xmalloc(SD_INODE_HEADER_SIZE);
>  	for (i = start; i >= end; i--) {
>  		nr_copies = get_vdi_copy_number(i);
>  		ret = read_object(vid_to_vdi_oid(i), (char *)inode,
> @@ -369,7 +364,6 @@ static int find_first_vdi(unsigned long start, unsigned long end,
>  
>  out_free_inode:
>  	free(inode);
> -out:
>  	return ret;
>  }
>  
> @@ -619,12 +613,7 @@ static void delete_one(struct work *work)
>  
>  	sd_dprintf("%d %d, %16x", dw->done, dw->count, vdi_id);
>  
> -	inode = malloc(sizeof(*inode));
> -	if (!inode) {
> -		sd_eprintf("failed to allocate memory");
> -		goto out;
> -	}
> -
> +	inode = xmalloc(sizeof(*inode));
>  	nr_copies = get_vdi_copy_number(vdi_id);
>  	ret = read_backend_object(vid_to_vdi_oid(vdi_id),
>  			  (void *)inode, sizeof(*inode), 0, nr_copies);
> @@ -708,12 +697,7 @@ static int fill_vdi_list(struct deletion_work *dw, uint32_t root_vid)
>  	uint32_t vid;
>  	int nr_copies;
>  
> -	inode = malloc(SD_INODE_HEADER_SIZE);
> -	if (!inode) {
> -		sd_eprintf("failed to allocate memory");
> -		goto err;
> -	}
> -
> +	inode = xmalloc(SD_INODE_HEADER_SIZE);
>  	dw->buf[dw->count++] = root_vid;
>  again:
>  	vid = dw->buf[done++];
> @@ -753,12 +737,7 @@ static uint64_t get_vdi_root(uint32_t vid, bool *cloned)
>  
>  	*cloned = false;
>  
> -	inode = malloc(SD_INODE_HEADER_SIZE);
> -	if (!inode) {
> -		sd_eprintf("failed to allocate memory");
> -		vid = 0;
> -		goto out;
> -	}
> +	inode = xmalloc(SD_INODE_HEADER_SIZE);

This calls panic() in the case of OOM, but stopping the server is the
last resort.  Please explain why we cannot survive by returning
SD_NO_MEM.

Thanks,

Kazutaka



More information about the sheepdog mailing list