Skip to content

Instantly share code, notes, and snippets.

@necto
Created January 5, 2015 21:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save necto/152027a3c021fafd082c to your computer and use it in GitHub Desktop.
Save necto/152027a3c021fafd082c to your computer and use it in GitHub Desktop.
{
int nr;
CriuVethPair **a, *p;
p = malloc(sizeof(*p));
if (!p)
goto er;
criu_veth_pair__init(p);
p->if_in = strdup(in);
if (!p->if_in)
goto er_p;
p->if_out = strdup(out);
if (!p->if_out)
goto er_i;
nr = opts->n_veths + 1;
a = realloc(opts->veths, nr * sizeof(p));
if (!a)
goto er_o;
a[nr - 1] = p;
opts->veths = a;
opts->n_veths = nr;
return 0;
er_o:
free(p->if_out);
er_i:
free(p->if_in);
er_p:
free(p);
er:
return -ENOMEM;
}
// ---vs---
{
vector<shared_ptr<CriuVethPair>> a = opts->veths;
try {
shared_ptr<CriuVethPair> p = new CruiVethPair; //throws if failed to allocate
// constructor calls everything else: criu_veth_pair__init(p);
a.push(p); //throws if failed to fit a new element. (reallocates automatically)
opts->n_veths = a.size();
return 0;
} catch (...) {
return -ENOMEM;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment