perf evsel: Introduce perf_evsel__{in,ex}it
authorArnaldo Carvalho de Melo <acme@redhat.com>
Tue, 18 Jan 2011 23:41:45 +0000 (21:41 -0200)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Mon, 24 Jan 2011 15:18:05 +0000 (13:18 -0200)
Out of the {con,des}structor, as in interpreted language bindings we will
need to go back from the wrapper object to the real thing. In that case
using container_of will save us to have an extra pointer in the perf_evsel
struct.

Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Mike Galbraith <efault@gmx.de>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: Tom Zanussi <tzanussi@gmail.com>
LKML-Reference: <new-submission>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/util/evlist.c
tools/perf/util/evlist.h
tools/perf/util/evsel.c
tools/perf/util/evsel.h

index 4b3b84cd71a129145030581e9db922c330bfb4f5..df0610e9c61b500284d5efb32761da91a9fa5847 100644 (file)
@@ -6,17 +6,21 @@
 #include <linux/bitops.h>
 #include <linux/hash.h>
 
+void perf_evlist__init(struct perf_evlist *evlist)
+{
+       int i;
+
+       for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i)
+               INIT_HLIST_HEAD(&evlist->heads[i]);
+       INIT_LIST_HEAD(&evlist->entries);
+}
+
 struct perf_evlist *perf_evlist__new(void)
 {
        struct perf_evlist *evlist = zalloc(sizeof(*evlist));
 
-       if (evlist != NULL) {
-               int i;
-
-               for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i)
-                       INIT_HLIST_HEAD(&evlist->heads[i]);
-               INIT_LIST_HEAD(&evlist->entries);
-       }
+       if (evlist != NULL)
+               perf_evlist__init(evlist);
 
        return evlist;
 }
@@ -33,11 +37,18 @@ static void perf_evlist__purge(struct perf_evlist *evlist)
        evlist->nr_entries = 0;
 }
 
-void perf_evlist__delete(struct perf_evlist *evlist)
+void perf_evlist__exit(struct perf_evlist *evlist)
 {
-       perf_evlist__purge(evlist);
        free(evlist->mmap);
        free(evlist->pollfd);
+       evlist->mmap = NULL;
+       evlist->pollfd = NULL;
+}
+
+void perf_evlist__delete(struct perf_evlist *evlist)
+{
+       perf_evlist__purge(evlist);
+       perf_evlist__exit(evlist);
        free(evlist);
 }
 
index 28712063db97a57958da7cb1b122e424e604514c..acbe48eac6084879fae254d47d8860c75f9af334 100644 (file)
@@ -24,6 +24,8 @@ struct perf_evlist {
 struct perf_evsel;
 
 struct perf_evlist *perf_evlist__new(void);
+void perf_evlist__init(struct perf_evlist *evlist);
+void perf_evlist__exit(struct perf_evlist *evlist);
 void perf_evlist__delete(struct perf_evlist *evlist);
 
 void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry);
index a85ae12845eac68dd14f9ee8b4435311092637e4..76ab553637d671fb89980eb9d5566c6854867934 100644 (file)
 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
 #define SID(e, x, y) xyarray__entry(e->id, x, y)
 
+void perf_evsel__init(struct perf_evsel *evsel,
+                     struct perf_event_attr *attr, int idx)
+{
+       evsel->idx         = idx;
+       evsel->attr        = *attr;
+       INIT_LIST_HEAD(&evsel->node);
+}
+
 struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
 {
        struct perf_evsel *evsel = zalloc(sizeof(*evsel));
 
-       if (evsel != NULL) {
-               evsel->idx         = idx;
-               evsel->attr        = *attr;
-               INIT_LIST_HEAD(&evsel->node);
-       }
+       if (evsel != NULL)
+               perf_evsel__init(evsel, attr, idx);
 
        return evsel;
 }
@@ -87,11 +92,16 @@ int perf_evlist__alloc_mmap(struct perf_evlist *evlist, int ncpus)
        return evlist->mmap != NULL ? 0 : -ENOMEM;
 }
 
-void perf_evsel__delete(struct perf_evsel *evsel)
+void perf_evsel__exit(struct perf_evsel *evsel)
 {
        assert(list_empty(&evsel->node));
        xyarray__delete(evsel->fd);
        xyarray__delete(evsel->id);
+}
+
+void perf_evsel__delete(struct perf_evsel *evsel)
+{
+       perf_evsel__exit(evsel);
        free(evsel);
 }
 
index 667ee4e2e35e66b00b3ebecc86184ec0f7b6ecfb..7962e7587deadf0bf647150c34a4c5085f59e02e 100644 (file)
@@ -52,6 +52,9 @@ struct thread_map;
 struct perf_evlist;
 
 struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx);
+void perf_evsel__init(struct perf_evsel *evsel,
+                     struct perf_event_attr *attr, int idx);
+void perf_evsel__exit(struct perf_evsel *evsel);
 void perf_evsel__delete(struct perf_evsel *evsel);
 
 int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads);