[CRIU] [PATCH 3/3] lib: Use x[re|m]alloc, xstrdup helpers

Pavel Emelyanov xemul at parallels.com
Tue Sep 23 10:08:38 PDT 2014


On 09/23/2014 09:00 PM, Cyrill Gorcunov wrote:

I doubt about this one. Do we want library to print memory
allocation errors to unknown log?

And, btw, where from will libcriu.so get the print_on_level
function?

> Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
> ---
>  lib/criu.c | 37 +++++++++++++++++++------------------
>  1 file changed, 19 insertions(+), 18 deletions(-)
> 
> diff --git a/lib/criu.c b/lib/criu.c
> index ad419ba1a633..7c5f3865e712 100644
> --- a/lib/criu.c
> +++ b/lib/criu.c
> @@ -12,6 +12,7 @@
>  #include <signal.h>
>  
>  #include "criu.h"
> +#include "xmalloc.h"
>  #include "rpc.pb-c.h"
>  #include "cr-service-const.h"
>  
> @@ -37,7 +38,7 @@ int criu_init_opts(void)
>  		criu_opts__free_unpacked(opts, NULL);
>  	}
>  
> -	opts = malloc(sizeof(CriuOpts));
> +	opts = xmalloc(sizeof(CriuOpts));
>  	if (opts == NULL) {
>  		perror("Can't allocate memory for criu opts");
>  		return -1;
> @@ -72,7 +73,7 @@ void criu_set_images_dir_fd(int fd)
>  
>  void criu_set_parent_images(char *path)
>  {
> -	opts->parent_img = strdup(path);
> +	opts->parent_img = xstrdup(path);
>  }
>  
>  void criu_set_track_mem(bool track_mem)
> @@ -149,7 +150,7 @@ void criu_set_log_level(int log_level)
>  
>  void criu_set_root(char *root)
>  {
> -	opts->root = strdup(root);
> +	opts->root = xstrdup(root);
>  }
>  
>  void criu_set_manage_cgroups(bool manage)
> @@ -160,7 +161,7 @@ void criu_set_manage_cgroups(bool manage)
>  
>  void criu_set_log_file(char *log_file)
>  {
> -	opts->log_file = strdup(log_file);
> +	opts->log_file = xstrdup(log_file);
>  }
>  
>  void criu_set_cpu_cap(unsigned int cap)
> @@ -174,11 +175,11 @@ int criu_set_exec_cmd(int argc, char *argv[])
>  	int i;
>  
>  	opts->n_exec_cmd = argc;
> -	opts->exec_cmd = malloc((argc) * sizeof(char *));
> +	opts->exec_cmd = xmalloc((argc) * sizeof(char *));
>  
>  	if (opts->exec_cmd) {
>  		for (i = 0; i < argc; i++) {
> -			opts->exec_cmd[i] = strdup(argv[i]);
> +			opts->exec_cmd[i] = xstrdup(argv[i]);
>  			if (!opts->exec_cmd[i]) {
>  				while (i > 0)
>  					free(opts->exec_cmd[i--]);
> @@ -200,20 +201,20 @@ int criu_add_ext_mount(char *key, char *val)
>  	int nr;
>  	ExtMountMap **a, *m;
>  
> -	m = malloc(sizeof(*m));
> +	m = xmalloc(sizeof(*m));
>  	if (!m)
>  		goto er;
>  	ext_mount_map__init(m);
>  
> -	m->key = strdup(key);
> +	m->key = xstrdup(key);
>  	if (!m->key)
>  		goto er_n;
> -	m->val = strdup(val);
> +	m->val = xstrdup(val);
>  	if (!m->val)
>  		goto er_k;
>  
>  	nr = opts->n_ext_mnt + 1;
> -	a = realloc(opts->ext_mnt, nr * sizeof(m));
> +	a = xrealloc(opts->ext_mnt, nr * sizeof(m));
>  	if (!a)
>  		goto er_v;
>  
> @@ -237,23 +238,23 @@ int criu_add_cg_root(char *ctrl, char *path)
>  	int nr;
>  	CgroupRoot **a, *root;
>  
> -	root = malloc(sizeof(*root));
> +	root = xmalloc(sizeof(*root));
>  	if (!root)
>  		goto er;
>  	cgroup_root__init(root);
>  
>  	if (ctrl) {
> -		root->ctrl = strdup(ctrl);
> +		root->ctrl = xstrdup(ctrl);
>  		if (!root->ctrl)
>  			goto er_r;
>  	}
>  
> -	root->path = strdup(path);
> +	root->path = xstrdup(path);
>  	if (!root->path)
>  		goto er_c;
>  
>  	nr = opts->n_cg_root + 1;
> -	a = realloc(opts->cg_root, nr * sizeof(root));
> +	a = xrealloc(opts->cg_root, nr * sizeof(root));
>  	if (!a)
>  		goto er_p;
>  
> @@ -277,20 +278,20 @@ int criu_add_veth_pair(char *in, char *out)
>  	int nr;
>  	CriuVethPair **a, *p;
>  
> -	p = malloc(sizeof(*p));
> +	p = xmalloc(sizeof(*p));
>  	if (!p)
>  		goto er;
>  	criu_veth_pair__init(p);
>  
> -	p->if_in = strdup(in);
> +	p->if_in = xstrdup(in);
>  	if (!p->if_in)
>  		goto er_p;
> -	p->if_out = strdup(out);
> +	p->if_out = xstrdup(out);
>  	if (!p->if_out)
>  		goto er_i;
>  
>  	nr = opts->n_veths + 1;
> -	a = realloc(opts->veths, nr * sizeof(p));
> +	a = xrealloc(opts->veths, nr * sizeof(p));
>  	if (!a)
>  		goto er_o;
>  
> 



More information about the CRIU mailing list