[sysprof/wip/visualizers] reader: add sp_capture_reader_copy()



commit b8f465659b4c947597f8305b319470b4d2edd4a3
Author: Christian Hergert <chergert redhat com>
Date:   Sun Sep 25 00:23:28 2016 -0700

    reader: add sp_capture_reader_copy()
    
    This function allows copying a capture so that we can do
    additional reads. This does, however, copy the buffers which
    might be more memory than we want for large usage. We can
    tweak things as we go to figure out the cursors.

 lib/sp-capture-reader.c |   35 +++++++++++++++++++++++++++++++++++
 lib/sp-capture-reader.h |    1 +
 2 files changed, 36 insertions(+), 0 deletions(-)
---
diff --git a/lib/sp-capture-reader.c b/lib/sp-capture-reader.c
index f8f7a1a..3c845d5 100644
--- a/lib/sp-capture-reader.c
+++ b/lib/sp-capture-reader.c
@@ -812,3 +812,38 @@ sp_capture_reader_get_start_time (SpCaptureReader *self)
 
   return self->header.time;
 }
+
+/**
+ * sp_capture_reader_copy:
+ *
+ * This function makes a copy of the reader. Since readers use
+ * positioned reads with pread(), this allows you to have multiple
+ * readers with the shared file descriptor. This uses dup() to create
+ * another file descriptor.
+ *
+ * Returns: (transfer full): A copy of @self with a new file-descriptor.
+ */
+SpCaptureReader *
+sp_capture_reader_copy (SpCaptureReader *self)
+{
+  SpCaptureReader *copy;
+  int fd;
+
+  g_return_val_if_fail (self != NULL, NULL);
+
+  if (-1 == (fd = dup (self->fd)))
+    return NULL;
+
+  copy = g_new0 (SpCaptureReader, 1);
+
+  *copy = *self;
+
+  copy->ref_count = 1;
+  copy->filename = g_strdup (self->filename);
+  copy->fd = fd;
+
+  copy->buf = g_malloc (self->bufsz);
+  memcpy (copy->buf, self->buf, self->bufsz);
+
+  return copy;
+}
diff --git a/lib/sp-capture-reader.h b/lib/sp-capture-reader.h
index f8160f3..bf1d152 100644
--- a/lib/sp-capture-reader.h
+++ b/lib/sp-capture-reader.h
@@ -29,6 +29,7 @@ SpCaptureReader                   *sp_capture_reader_new                 (const
                                                                           GError             **error);
 SpCaptureReader                   *sp_capture_reader_new_from_fd         (int                  fd,
                                                                           GError             **error);
+SpCaptureReader                   *sp_capture_reader_copy                (SpCaptureReader     *self);
 SpCaptureReader                   *sp_capture_reader_ref                 (SpCaptureReader     *self);
 void                               sp_capture_reader_unref               (SpCaptureReader     *self);
 const gchar                       *sp_capture_reader_get_filename        (SpCaptureReader     *self);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]