L
Do{
If(some){
Return
}
If(some 2){
Return
}
....
}While(0)
Size: a a a
L
СС
L
foo = get_foo();
if (foo) {
bar = get_bar();
if (bar) {
/* do shit with foo & bar */
put_bar(bar);
}
put_foo(foo);
}
L
СС
foo = get_foo();
if (foo) {
bar = get_bar();
if (bar) {
/* do shit with foo & bar */
put_bar(bar);
}
put_foo(foo);
}
К1
void do_cool_shit() {
Foo *foo = get_foo(); /* foo must be released when not needed */
/* here we do shit with foo that may fail */
Bar *bar = get_bar(); /* also must be released */
/* here we do some other shit with foo & bar that may fail */
/* ok now we're done */
put_bar(bar);
put_foo(foo);
}L
К1
L
L
L
BV
L
L
L
typedef int (*worker_fn)(Foo *, Bar *);
int do_work(Foo *foo, Bar *bar);
int with_foo_and_bar(Foo *foo, worker_fn worker) {
Bar *bar = get_bar();
if (!bar)
return -ESHITHAPPENED;
int result = worker(foo, bar);
put_bar(bar);
return result;
}
int with_foo(worker_fn worker) {
Foo *foo = get_foo();
if (!foo)
return -ESHITHAPPENED;
int result = with_foo_and_bar(foo, worker);
put_foo(foo);
return result;
}
/* ... */
with_foo(do_work);
L
VK
typedef int (*worker_fn)(Foo *, Bar *);
int do_work(Foo *foo, Bar *bar);
int with_foo_and_bar(Foo *foo, worker_fn worker) {
Bar *bar = get_bar();
if (!bar)
return -ESHITHAPPENED;
int result = worker(foo, bar);
put_bar(bar);
return result;
}
int with_foo(worker_fn worker) {
Foo *foo = get_foo();
if (!foo)
return -ESHITHAPPENED;
int result = with_foo_and_bar(foo, worker);
put_foo(foo);
return result;
}
/* ... */
with_foo(do_work);
L
VK
L