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
This commit is contained in:
Philip Withnall
2020-07-01 17:45:09 +01:00
parent 1d865c5c8e
commit e26eae5bcf
3 changed files with 33 additions and 12 deletions

View File

@ -199,7 +199,10 @@ sysprof_capture_condition_match (const SysprofCaptureCondition *self,
if (frame->type != SYSPROF_CAPTURE_FRAME_FILE_CHUNK) if (frame->type != SYSPROF_CAPTURE_FRAME_FILE_CHUNK)
return false; 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: default:
break; break;
@ -301,7 +304,7 @@ sysprof_capture_condition_finalize (SysprofCaptureCondition *self)
break; break;
case SYSPROF_CAPTURE_CONDITION_WHERE_FILE: case SYSPROF_CAPTURE_CONDITION_WHERE_FILE:
g_free (self->u.where_file); free (self->u.where_file);
break; break;
default: default:
@ -518,7 +521,12 @@ sysprof_capture_condition_new_where_file (const char *path)
return NULL; return NULL;
self->type = SYSPROF_CAPTURE_CONDITION_WHERE_FILE; 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; return self;
} }

View File

@ -127,7 +127,7 @@ sysprof_capture_reader_finalize (SysprofCaptureReader *self)
{ {
close (self->fd); close (self->fd);
free (self->buf); free (self->buf);
g_free (self->filename); free (self->filename);
free (self); free (self);
} }
} }
@ -283,7 +283,7 @@ sysprof_capture_reader_new (const char *filename,
return NULL; return NULL;
} }
self->filename = g_strdup (filename); self->filename = strdup (filename);
return self; return self;
} }
@ -1086,7 +1086,7 @@ sysprof_capture_reader_save_as (SysprofCaptureReader *self,
} }
if (self->filename == NULL) if (self->filename == NULL)
self->filename = g_strdup (filename); self->filename = strdup (filename);
close (fd); close (fd);
@ -1179,7 +1179,7 @@ sysprof_capture_reader_copy (SysprofCaptureReader *self)
*copy = *self; *copy = *self;
copy->ref_count = 1; copy->ref_count = 1;
copy->filename = g_strdup (self->filename); copy->filename = strdup (self->filename);
copy->fd = fd; copy->fd = fd;
copy->end_time = self->end_time; copy->end_time = self->end_time;
copy->st_buf = self->st_buf; copy->st_buf = self->st_buf;
@ -1189,7 +1189,7 @@ sysprof_capture_reader_copy (SysprofCaptureReader *self)
if (copy->buf == NULL) if (copy->buf == NULL)
{ {
close (fd); close (fd);
g_free (copy->filename); free (copy->filename);
free (copy); free (copy);
return NULL; return NULL;
} }
@ -1337,7 +1337,7 @@ sysprof_capture_reader_read_file_fd (SysprofCaptureReader *self,
if (!(file = sysprof_capture_reader_read_file (self))) if (!(file = sysprof_capture_reader_read_file (self)))
return false; return false;
if (g_strcmp0 (path, file->path) != 0) if (strcmp (path, file->path) != 0)
goto skip; goto skip;
buf = file->data; buf = file->data;
@ -1399,7 +1399,7 @@ sysprof_capture_reader_find_file (SysprofCaptureReader *self,
if (!(fc = sysprof_capture_reader_read_file (self))) if (!(fc = sysprof_capture_reader_read_file (self)))
break; break;
if (g_strcmp0 (path, fc->path) == 0) if (strcmp (path, fc->path) == 0)
return fc; return fc;
continue; continue;

View File

@ -333,6 +333,19 @@ sysprof_capture_writer_flush_jitmap (SysprofCaptureWriter *self)
return true; 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 static bool
sysprof_capture_writer_lookup_jitmap (SysprofCaptureWriter *self, sysprof_capture_writer_lookup_jitmap (SysprofCaptureWriter *self,
const char *name, const char *name,
@ -345,7 +358,7 @@ sysprof_capture_writer_lookup_jitmap (SysprofCaptureWriter *self,
assert (name != NULL); assert (name != NULL);
assert (addr != 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++) 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); assert (self->addr_buf_pos <= sizeof self->addr_buf);
/* Now place the address into the hashtable */ /* 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 */ /* Start from the current hash bucket and go forward */
for (i = hash; i < G_N_ELEMENTS (self->addr_hash); i++) for (i = hash; i < G_N_ELEMENTS (self->addr_hash); i++)