[sysprof: 22/63] libsysprof-capture: Use sysprof_{steal, clear}_pointer() instead of GLib



commit 1d865c5c8ea58725988ca7b06447b45ece66a588
Author: Philip Withnall <withnall endlessm com>
Date:   Wed Jul 1 17:41:03 2020 +0100

    libsysprof-capture: Use sysprof_{steal,clear}_pointer() instead of GLib
    
    They work exactly the same way as the GLib functions.
    
    Signed-off-by: Philip Withnall <withnall endlessm com>
    
    Helps: #40

 src/libsysprof-capture/mapped-ring-buffer.c         |  5 +++--
 src/libsysprof-capture/sysprof-capture-condition.c  |  2 +-
 src/libsysprof-capture/sysprof-capture-cursor.c     |  7 ++++---
 src/libsysprof-capture/sysprof-capture-reader.c     |  4 ++--
 src/libsysprof-capture/sysprof-capture-writer-cat.c |  4 +++-
 src/libsysprof-capture/sysprof-capture-writer.c     |  6 +++---
 src/libsysprof-capture/sysprof-collector.c          |  5 +++--
 src/libsysprof-capture/sysprof-macros-internal.h    | 11 +++++++++++
 8 files changed, 30 insertions(+), 14 deletions(-)
---
diff --git a/src/libsysprof-capture/mapped-ring-buffer.c b/src/libsysprof-capture/mapped-ring-buffer.c
index 81a37f9..b2e4e6a 100644
--- a/src/libsysprof-capture/mapped-ring-buffer.c
+++ b/src/libsysprof-capture/mapped-ring-buffer.c
@@ -30,6 +30,7 @@
 #include <unistd.h>
 
 #include "sysprof-capture-util-private.h"
+#include "sysprof-macros-internal.h"
 #include "sysprof-platform.h"
 
 #include "mapped-ring-buffer.h"
@@ -214,7 +215,7 @@ mapped_ring_buffer_new_reader (size_t buffer_size)
   self->map = map;
   self->page_size = page_size;
 
-  return g_steal_pointer (&self);
+  return sysprof_steal_pointer (&self);
 }
 
 MappedRingBuffer *
@@ -319,7 +320,7 @@ mapped_ring_buffer_new_writer (int fd)
   self->map = map;
   self->page_size = page_size;
 
-  return g_steal_pointer (&self);
+  return sysprof_steal_pointer (&self);
 }
 
 static void
diff --git a/src/libsysprof-capture/sysprof-capture-condition.c 
b/src/libsysprof-capture/sysprof-capture-condition.c
index 7e2ee2f..debb189 100644
--- a/src/libsysprof-capture/sysprof-capture-condition.c
+++ b/src/libsysprof-capture/sysprof-capture-condition.c
@@ -221,7 +221,7 @@ sysprof_capture_condition_init (void)
 
   self->ref_count = 1;
 
-  return g_steal_pointer (&self);
+  return sysprof_steal_pointer (&self);
 }
 
 /* Returns NULL on allocation failure. */
diff --git a/src/libsysprof-capture/sysprof-capture-cursor.c b/src/libsysprof-capture/sysprof-capture-cursor.c
index 5a81d79..3786d67 100644
--- a/src/libsysprof-capture/sysprof-capture-cursor.c
+++ b/src/libsysprof-capture/sysprof-capture-cursor.c
@@ -64,6 +64,7 @@
 #include "sysprof-capture-cursor.h"
 #include "sysprof-capture-reader.h"
 #include "sysprof-capture-util-private.h"
+#include "sysprof-macros-internal.h"
 
 #define READ_DELEGATE(f) ((ReadDelegate)(f))
 
@@ -80,8 +81,8 @@ struct _SysprofCaptureCursor
 static void
 sysprof_capture_cursor_finalize (SysprofCaptureCursor *self)
 {
-  g_clear_pointer (&self->conditions, g_ptr_array_unref);
-  g_clear_pointer (&self->reader, sysprof_capture_reader_unref);
+  sysprof_clear_pointer (&self->conditions, g_ptr_array_unref);
+  sysprof_clear_pointer (&self->reader, sysprof_capture_reader_unref);
   free (self);
 }
 
@@ -97,7 +98,7 @@ sysprof_capture_cursor_init (void)
   self->conditions = g_ptr_array_new_with_free_func ((GDestroyNotify) sysprof_capture_condition_unref);
   self->ref_count = 1;
 
-  return g_steal_pointer (&self);
+  return sysprof_steal_pointer (&self);
 }
 
 /**
diff --git a/src/libsysprof-capture/sysprof-capture-reader.c b/src/libsysprof-capture/sysprof-capture-reader.c
index 3553efc..1ea372d 100644
--- a/src/libsysprof-capture/sysprof-capture-reader.c
+++ b/src/libsysprof-capture/sysprof-capture-reader.c
@@ -820,7 +820,7 @@ sysprof_capture_reader_read_jitmap (SysprofCaptureReader *self)
 
   self->pos += jitmap->frame.len;
 
-  return g_steal_pointer (&ret);
+  return sysprof_steal_pointer (&ret);
 }
 
 const SysprofCaptureSample *
@@ -1309,7 +1309,7 @@ sysprof_capture_reader_list_files (SysprofCaptureReader *self)
     g_ptr_array_add (ar, g_strdup (key));
   g_ptr_array_add (ar, NULL);
 
-  return (char **)g_ptr_array_free (g_steal_pointer (&ar), FALSE);
+  return (char **)g_ptr_array_free (sysprof_steal_pointer (&ar), FALSE);
 }
 
 bool
diff --git a/src/libsysprof-capture/sysprof-capture-writer-cat.c 
b/src/libsysprof-capture/sysprof-capture-writer-cat.c
index d86ba7a..b752b9c 100644
--- a/src/libsysprof-capture/sysprof-capture-writer-cat.c
+++ b/src/libsysprof-capture/sysprof-capture-writer-cat.c
@@ -65,6 +65,8 @@
 #include <sysprof-capture.h>
 #include <unistd.h>
 
+#include "sysprof-macros-internal.h"
+
 typedef struct
 {
   uint64_t src;
@@ -81,7 +83,7 @@ static void
 translate_table_clear (GArray       **tables,
                        unsigned int   table)
 {
-  g_clear_pointer (&tables[table], g_array_unref);
+  sysprof_clear_pointer (&tables[table], g_array_unref);
 }
 
 static int
diff --git a/src/libsysprof-capture/sysprof-capture-writer.c b/src/libsysprof-capture/sysprof-capture-writer.c
index e74e6c6..84cd86e 100644
--- a/src/libsysprof-capture/sysprof-capture-writer.c
+++ b/src/libsysprof-capture/sysprof-capture-writer.c
@@ -171,7 +171,7 @@ sysprof_capture_writer_finalize (SysprofCaptureWriter *self)
 {
   if (self != NULL)
     {
-      g_clear_pointer (&self->periodic_flush, g_source_destroy);
+      sysprof_clear_pointer (&self->periodic_flush, g_source_destroy);
 
       sysprof_capture_writer_flush (self);
 
@@ -1154,7 +1154,7 @@ sysprof_capture_writer_create_reader (SysprofCaptureWriter  *self,
   if ((ret = sysprof_capture_reader_new_from_fd (copy, error)))
     sysprof_capture_reader_set_stat (ret, &self->stat);
 
-  return g_steal_pointer (&ret);
+  return sysprof_steal_pointer (&ret);
 }
 
 /**
@@ -1516,7 +1516,7 @@ sysprof_capture_writer_set_flush_delay (SysprofCaptureWriter *self,
 
   g_return_if_fail (self != NULL);
 
-  g_clear_pointer (&self->periodic_flush, g_source_destroy);
+  sysprof_clear_pointer (&self->periodic_flush, g_source_destroy);
 
   if (timeout_seconds == 0)
     return;
diff --git a/src/libsysprof-capture/sysprof-collector.c b/src/libsysprof-capture/sysprof-collector.c
index 482d536..f398df0 100644
--- a/src/libsysprof-capture/sysprof-collector.c
+++ b/src/libsysprof-capture/sysprof-collector.c
@@ -77,6 +77,7 @@
 
 #include "sysprof-capture-util-private.h"
 #include "sysprof-collector.h"
+#include "sysprof-macros-internal.h"
 
 #define MAX_UNWIND_DEPTH 128
 #define CREATRING      "CreatRing\0"
@@ -179,7 +180,7 @@ request_writer (void)
         }
     }
 
-  return g_steal_pointer (&buffer);
+  return sysprof_steal_pointer (&buffer);
 }
 
 static void
@@ -207,7 +208,7 @@ sysprof_collector_free (void *data)
 
   if (collector != NULL && collector != COLLECTOR_INVALID)
     {
-      MappedRingBuffer *buffer = g_steal_pointer (&collector->buffer);
+      MappedRingBuffer *buffer = sysprof_steal_pointer (&collector->buffer);
 
       if (buffer != NULL)
         {
diff --git a/src/libsysprof-capture/sysprof-macros-internal.h 
b/src/libsysprof-capture/sysprof-macros-internal.h
index f227a23..eacec0d 100644
--- a/src/libsysprof-capture/sysprof-macros-internal.h
+++ b/src/libsysprof-capture/sysprof-macros-internal.h
@@ -60,3 +60,14 @@
 #pragma once
 
 #define sysprof_assert_not_reached() assert (false)
+
+#define sysprof_steal_pointer(pp) __extension__ ({__typeof(*(pp)) _p = *(pp); *(pp) = NULL; _p;})
+
+#define sysprof_clear_pointer(pp, destroy) \
+  do { \
+    __typeof((pp)) _pp = (pp); \
+    __typeof(*(pp)) _p = *_pp; \
+    *_pp = NULL; \
+    if (_p != NULL) \
+      (destroy) (_p); \
+  } while (0)


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