[sysprof: 23/63] libsysprof-capture: Use libc string functions rather than GLib ones



commit e26eae5bcf31720d873febabbb7f387c2d137fd3
Author: Philip Withnall <withnall endlessm com>
Date:   Wed Jul 1 17:45:09 2020 +0100

    libsysprof-capture: Use libc string functions rather than GLib ones
    
    Use `strcmp()` and `strdup()` rather than `g_strcmp0()` and
    `g_strdup()`. In the latter case, this makes no difference. In the
    former case it means we potentially need to do some additional `NULL`
    checks before calling it; although most of the call sites use
    fixed-length arrays, so no `NULL` check is needed.
    
    Signed-off-by: Philip Withnall <withnall endlessm com>
    
    Helps: #40

 src/libsysprof-capture/sysprof-capture-condition.c | 14 +++++++++++---
 src/libsysprof-capture/sysprof-capture-reader.c    | 14 +++++++-------
 src/libsysprof-capture/sysprof-capture-writer.c    | 17 +++++++++++++++--
 3 files changed, 33 insertions(+), 12 deletions(-)
---
diff --git a/src/libsysprof-capture/sysprof-capture-condition.c 
b/src/libsysprof-capture/sysprof-capture-condition.c
index debb189..27141db 100644
--- a/src/libsysprof-capture/sysprof-capture-condition.c
+++ b/src/libsysprof-capture/sysprof-capture-condition.c
@@ -199,7 +199,10 @@ sysprof_capture_condition_match (const SysprofCaptureCondition *self,
       if (frame->type != SYSPROF_CAPTURE_FRAME_FILE_CHUNK)
         return false;
 
-      return g_strcmp0 (((const SysprofCaptureFileChunk *)frame)->path, self->u.where_file) == 0;
+      if (self->u.where_file == NULL)
+        return false;
+
+      return strcmp (((const SysprofCaptureFileChunk *)frame)->path, self->u.where_file) == 0;
 
     default:
       break;
@@ -301,7 +304,7 @@ sysprof_capture_condition_finalize (SysprofCaptureCondition *self)
       break;
 
     case SYSPROF_CAPTURE_CONDITION_WHERE_FILE:
-      g_free (self->u.where_file);
+      free (self->u.where_file);
       break;
 
     default:
@@ -518,7 +521,12 @@ sysprof_capture_condition_new_where_file (const char *path)
     return NULL;
 
   self->type = SYSPROF_CAPTURE_CONDITION_WHERE_FILE;
-  self->u.where_file = g_strdup (path);
+  self->u.where_file = strdup (path);
+  if (self->u.where_file == NULL)
+    {
+      free (self);
+      return NULL;
+    }
 
   return self;
 }
diff --git a/src/libsysprof-capture/sysprof-capture-reader.c b/src/libsysprof-capture/sysprof-capture-reader.c
index 1ea372d..3243f80 100644
--- a/src/libsysprof-capture/sysprof-capture-reader.c
+++ b/src/libsysprof-capture/sysprof-capture-reader.c
@@ -127,7 +127,7 @@ sysprof_capture_reader_finalize (SysprofCaptureReader *self)
     {
       close (self->fd);
       free (self->buf);
-      g_free (self->filename);
+      free (self->filename);
       free (self);
     }
 }
@@ -283,7 +283,7 @@ sysprof_capture_reader_new (const char   *filename,
       return NULL;
     }
 
-  self->filename = g_strdup (filename);
+  self->filename = strdup (filename);
 
   return self;
 }
@@ -1086,7 +1086,7 @@ sysprof_capture_reader_save_as (SysprofCaptureReader  *self,
     }
 
   if (self->filename == NULL)
-    self->filename = g_strdup (filename);
+    self->filename = strdup (filename);
 
   close (fd);
 
@@ -1179,7 +1179,7 @@ sysprof_capture_reader_copy (SysprofCaptureReader *self)
   *copy = *self;
 
   copy->ref_count = 1;
-  copy->filename = g_strdup (self->filename);
+  copy->filename = strdup (self->filename);
   copy->fd = fd;
   copy->end_time = self->end_time;
   copy->st_buf = self->st_buf;
@@ -1189,7 +1189,7 @@ sysprof_capture_reader_copy (SysprofCaptureReader *self)
   if (copy->buf == NULL)
     {
       close (fd);
-      g_free (copy->filename);
+      free (copy->filename);
       free (copy);
       return NULL;
     }
@@ -1337,7 +1337,7 @@ sysprof_capture_reader_read_file_fd (SysprofCaptureReader *self,
       if (!(file = sysprof_capture_reader_read_file (self)))
         return false;
 
-      if (g_strcmp0 (path, file->path) != 0)
+      if (strcmp (path, file->path) != 0)
         goto skip;
 
       buf = file->data;
@@ -1399,7 +1399,7 @@ sysprof_capture_reader_find_file (SysprofCaptureReader *self,
           if (!(fc = sysprof_capture_reader_read_file (self)))
             break;
 
-          if (g_strcmp0 (path, fc->path) == 0)
+          if (strcmp (path, fc->path) == 0)
             return fc;
 
           continue;
diff --git a/src/libsysprof-capture/sysprof-capture-writer.c b/src/libsysprof-capture/sysprof-capture-writer.c
index 84cd86e..cb8c90a 100644
--- a/src/libsysprof-capture/sysprof-capture-writer.c
+++ b/src/libsysprof-capture/sysprof-capture-writer.c
@@ -333,6 +333,19 @@ sysprof_capture_writer_flush_jitmap (SysprofCaptureWriter *self)
   return true;
 }
 
+/* djb hash */
+static unsigned int
+str_hash (const char *str)
+{
+  const uint8_t *p;
+  uint32_t h = 5381;
+
+  for (p = (const uint8_t *) str; *p != '\0'; p++)
+    h = (h << 5) + h + *p;
+
+  return h;
+}
+
 static bool
 sysprof_capture_writer_lookup_jitmap (SysprofCaptureWriter  *self,
                                       const char            *name,
@@ -345,7 +358,7 @@ sysprof_capture_writer_lookup_jitmap (SysprofCaptureWriter  *self,
   assert (name != NULL);
   assert (addr != NULL);
 
-  hash = g_str_hash (name) % G_N_ELEMENTS (self->addr_hash);
+  hash = str_hash (name) % G_N_ELEMENTS (self->addr_hash);
 
   for (i = hash; i < G_N_ELEMENTS (self->addr_hash); i++)
     {
@@ -426,7 +439,7 @@ sysprof_capture_writer_insert_jitmap (SysprofCaptureWriter *self,
   assert (self->addr_buf_pos <= sizeof self->addr_buf);
 
   /* Now place the address into the hashtable */
-  hash = g_str_hash (str) % G_N_ELEMENTS (self->addr_hash);
+  hash = str_hash (str) % G_N_ELEMENTS (self->addr_hash);
 
   /* Start from the current hash bucket and go forward */
   for (i = hash; i < G_N_ELEMENTS (self->addr_hash); i++)


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