diff options
author | Steven Rostedt <srostedt@redhat.com> | 2010-01-11 21:12:22 -0500 |
---|---|---|
committer | Steven Rostedt <rostedt@goodmis.org> | 2010-01-11 21:12:22 -0500 |
commit | 021ca6ab5c2dc9307ef7d413bc6fbce86de871fa (patch) | |
tree | c64f1be6f5d00ecb349f0cd0892e5e2403a24438 | |
parent | 9df63eaa95d85d86e540b4181bee460cbd7bb535 (diff) |
trace-cmd: Add reference counter to tracecmd_input
Because different parts of an application may reference a tracecmd_input
handle, add a reference counter to make it easier to know when to free
the handle.
All parts of an application that allocate or calls tracecmd_ref on the
handle must now call tracecmd_close on it too.
Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
-rw-r--r-- | trace-cmd.h | 1 | ||||
-rw-r--r-- | trace-input.c | 33 |
2 files changed, 34 insertions, 0 deletions
diff --git a/trace-cmd.h b/trace-cmd.h index 18d615c..c468cf7 100644 --- a/trace-cmd.h +++ b/trace-cmd.h | |||
@@ -45,6 +45,7 @@ struct tracecmd_input *tracecmd_alloc(const char *file); | |||
45 | struct tracecmd_input *tracecmd_alloc_fd(int fd); | 45 | struct tracecmd_input *tracecmd_alloc_fd(int fd); |
46 | struct tracecmd_input *tracecmd_open(const char *file); | 46 | struct tracecmd_input *tracecmd_open(const char *file); |
47 | struct tracecmd_input *tracecmd_open_fd(int fd); | 47 | struct tracecmd_input *tracecmd_open_fd(int fd); |
48 | void tracecmd_ref(struct tracecmd_input *handle); | ||
48 | void tracecmd_close(struct tracecmd_input *handle); | 49 | void tracecmd_close(struct tracecmd_input *handle); |
49 | int tracecmd_read_headers(struct tracecmd_input *handle); | 50 | int tracecmd_read_headers(struct tracecmd_input *handle); |
50 | int tracecmd_long_size(struct tracecmd_input *handle); | 51 | int tracecmd_long_size(struct tracecmd_input *handle); |
diff --git a/trace-input.c b/trace-input.c index 13896eb..d123c0d 100644 --- a/trace-input.c +++ b/trace-input.c | |||
@@ -52,6 +52,7 @@ struct tracecmd_input { | |||
52 | int page_size; | 52 | int page_size; |
53 | int read_page; | 53 | int read_page; |
54 | int cpus; | 54 | int cpus; |
55 | int ref; | ||
55 | struct cpu_data *cpu_data; | 56 | struct cpu_data *cpu_data; |
56 | 57 | ||
57 | /* file information */ | 58 | /* file information */ |
@@ -1496,6 +1497,7 @@ struct tracecmd_input *tracecmd_alloc_fd(int fd) | |||
1496 | memset(handle, 0, sizeof(*handle)); | 1497 | memset(handle, 0, sizeof(*handle)); |
1497 | 1498 | ||
1498 | handle->fd = fd; | 1499 | handle->fd = fd; |
1500 | handle->ref = 1; | ||
1499 | 1501 | ||
1500 | if (do_read_check(handle, buf, 3)) | 1502 | if (do_read_check(handle, buf, 3)) |
1501 | goto failed_read; | 1503 | goto failed_read; |
@@ -1605,6 +1607,29 @@ struct tracecmd_input *tracecmd_open(const char *file) | |||
1605 | return tracecmd_open_fd(fd); | 1607 | return tracecmd_open_fd(fd); |
1606 | } | 1608 | } |
1607 | 1609 | ||
1610 | /** | ||
1611 | * tracecmd_ref - add a reference to the handle | ||
1612 | * @handle: input handle for the trace.dat file | ||
1613 | * | ||
1614 | * Some applications may share a handle between parts of | ||
1615 | * the application. Let those parts add reference counters | ||
1616 | * to the handle, and the last one to close it will free it. | ||
1617 | */ | ||
1618 | void tracecmd_ref(struct tracecmd_input *handle) | ||
1619 | { | ||
1620 | if (!handle) | ||
1621 | return; | ||
1622 | |||
1623 | handle->ref++; | ||
1624 | } | ||
1625 | |||
1626 | /** | ||
1627 | * tracecmd_close - close and free the trace.dat handle | ||
1628 | * @handle: input handle for the trace.dat file | ||
1629 | * | ||
1630 | * Close the file descriptor of the handle and frees | ||
1631 | * the resources allocated by the handle. | ||
1632 | */ | ||
1608 | void tracecmd_close(struct tracecmd_input *handle) | 1633 | void tracecmd_close(struct tracecmd_input *handle) |
1609 | { | 1634 | { |
1610 | int cpu; | 1635 | int cpu; |
@@ -1612,6 +1637,14 @@ void tracecmd_close(struct tracecmd_input *handle) | |||
1612 | if (!handle) | 1637 | if (!handle) |
1613 | return; | 1638 | return; |
1614 | 1639 | ||
1640 | if (handle->ref <= 0) { | ||
1641 | warning("tracecmd: bad ref count on handle\n"); | ||
1642 | return; | ||
1643 | } | ||
1644 | |||
1645 | if (--handle->ref) | ||
1646 | return; | ||
1647 | |||
1615 | for (cpu = 0; cpu < handle->cpus; cpu++) { | 1648 | for (cpu = 0; cpu < handle->cpus; cpu++) { |
1616 | struct record *rec; | 1649 | struct record *rec; |
1617 | /* | 1650 | /* |