blob: 803a0058dc1358534731da56eae68bc82ffac240 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
#include "config.h"
#include <stdio.h>
#include <string.h>
/**
* tal/stack - stack of tal contextes (inspired by talloc_stack)
*
* Implement a stack of tal contexts. A new (empty) context is pushed on top
* of the stack using tal_newframe and it is popped/freed using tal_free().
* tal_curframe() can be used to get the stack's top context.
*
* tal_stack can be used to implement per-function temporary allocation context
* to help mitigating memory leaks, but unlike the plain tal approach it does not
* require the caller to pass a destination context for returning allocated
* values. Instead, allocated values are moved to the parent context using
* tal_steal(tal_parent(tmp_ctx), ptr).
*
* Example:
* #include <assert.h>
* #include <ccan/tal/stack/stack.h>
*
* static int *do_work(void)
* {
* int *retval = NULL;
* tal_t *tmp_ctx = tal_newframe();
*
* int *val = talz(tmp_ctx, int);
* assert(val != NULL);
*
* // ... do something with val ...
*
* if (retval >= 0) {
* // steal to parent cxt so it survives tal_free()
* tal_steal(tal_parent(tmp_ctx), val);
* retval = val;
* }
* tal_free(tmp_ctx);
* return retval;
* }
*
* int main(int argc, char *argv[])
* {
* tal_t *tmp_ctx = tal_newframe();
* int *val = do_work();
* if (val) {
* // ... do something with val ...
* }
* // val is eventually freed
* tal_free(tmp_ctx);
* return 0;
* }
*
* License: BSD-MIT
* Author: Delio Brignoli <brignoli.delio@gmail.com>
*/
int main(int argc, char *argv[])
{
/* Expect exactly one argument */
if (argc != 2)
return 1;
if (strcmp(argv[1], "depends") == 0) {
printf("ccan/tal\n");
return 0;
}
return 1;
}
|