libsysprof-capture: Stop using GArray in SysprofCaptureCondition

The code is well-suited to directly using `calloc()` instead, since the
arrays never grow dynamically.

Signed-off-by: Philip Withnall <withnall@endlessm.com>

Helps: #40
This commit is contained in:
Philip Withnall
2020-07-01 17:28:53 +01:00
parent 0e4b3f52b1
commit e96e35b6f1

View File

@ -60,6 +60,7 @@
#include <assert.h> #include <assert.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include "sysprof-capture-condition.h" #include "sysprof-capture-condition.h"
@ -93,13 +94,22 @@ struct _SysprofCaptureCondition
volatile int ref_count; volatile int ref_count;
SysprofCaptureConditionType type; SysprofCaptureConditionType type;
union { union {
GArray *where_type_in; struct {
SysprofCaptureFrameType *data;
size_t len;
} where_type_in;
struct { struct {
int64_t begin; int64_t begin;
int64_t end; int64_t end;
} where_time_between; } where_time_between;
GArray *where_pid_in; struct {
GArray *where_counter_in; int32_t *data;
size_t len;
} where_pid_in;
struct {
unsigned int *data;
size_t len;
} where_counter_in;
struct { struct {
SysprofCaptureCondition *left; SysprofCaptureCondition *left;
SysprofCaptureCondition *right; SysprofCaptureCondition *right;
@ -126,9 +136,9 @@ sysprof_capture_condition_match (const SysprofCaptureCondition *self,
sysprof_capture_condition_match (self->u.or.right, frame); sysprof_capture_condition_match (self->u.or.right, frame);
case SYSPROF_CAPTURE_CONDITION_WHERE_TYPE_IN: case SYSPROF_CAPTURE_CONDITION_WHERE_TYPE_IN:
for (size_t i = 0; i < self->u.where_type_in->len; i++) for (size_t i = 0; i < self->u.where_type_in.len; i++)
{ {
if (frame->type == g_array_index (self->u.where_type_in, SysprofCaptureFrameType, i)) if (frame->type == self->u.where_type_in.data[i])
return true; return true;
} }
return false; return false;
@ -137,9 +147,9 @@ sysprof_capture_condition_match (const SysprofCaptureCondition *self,
return (frame->time >= self->u.where_time_between.begin && frame->time <= self->u.where_time_between.end); return (frame->time >= self->u.where_time_between.begin && frame->time <= self->u.where_time_between.end);
case SYSPROF_CAPTURE_CONDITION_WHERE_PID_IN: case SYSPROF_CAPTURE_CONDITION_WHERE_PID_IN:
for (size_t i = 0; i < self->u.where_pid_in->len; i++) for (size_t i = 0; i < self->u.where_pid_in.len; i++)
{ {
if (frame->pid == g_array_index (self->u.where_pid_in, int32_t, i)) if (frame->pid == self->u.where_pid_in.data[i])
return true; return true;
} }
return false; return false;
@ -149,9 +159,9 @@ sysprof_capture_condition_match (const SysprofCaptureCondition *self,
{ {
const SysprofCaptureCounterSet *set = (SysprofCaptureCounterSet *)frame; const SysprofCaptureCounterSet *set = (SysprofCaptureCounterSet *)frame;
for (size_t i = 0; i < self->u.where_counter_in->len; i++) for (size_t i = 0; i < self->u.where_counter_in.len; i++)
{ {
unsigned int counter = g_array_index (self->u.where_counter_in, unsigned int, i); unsigned int counter = self->u.where_counter_in.data[i];
for (unsigned int j = 0; j < set->n_values; j++) for (unsigned int j = 0; j < set->n_values; j++)
{ {
@ -171,9 +181,9 @@ sysprof_capture_condition_match (const SysprofCaptureCondition *self,
{ {
const SysprofCaptureCounterDefine *def = (SysprofCaptureCounterDefine *)frame; const SysprofCaptureCounterDefine *def = (SysprofCaptureCounterDefine *)frame;
for (size_t i = 0; i < self->u.where_counter_in->len; i++) for (size_t i = 0; i < self->u.where_counter_in.len; i++)
{ {
unsigned int counter = g_array_index (self->u.where_counter_in, unsigned int, i); unsigned int counter = self->u.where_counter_in.data[i];
for (unsigned int j = 0; j < def->n_counters; j++) for (unsigned int j = 0; j < def->n_counters; j++)
{ {
@ -232,8 +242,8 @@ sysprof_capture_condition_copy (const SysprofCaptureCondition *self)
case SYSPROF_CAPTURE_CONDITION_WHERE_TYPE_IN: case SYSPROF_CAPTURE_CONDITION_WHERE_TYPE_IN:
return sysprof_capture_condition_new_where_type_in ( return sysprof_capture_condition_new_where_type_in (
self->u.where_type_in->len, self->u.where_type_in.len,
(const SysprofCaptureFrameType *)(void *)self->u.where_type_in->data); self->u.where_type_in.data);
case SYSPROF_CAPTURE_CONDITION_WHERE_TIME_BETWEEN: case SYSPROF_CAPTURE_CONDITION_WHERE_TIME_BETWEEN:
return sysprof_capture_condition_new_where_time_between ( return sysprof_capture_condition_new_where_time_between (
@ -242,13 +252,13 @@ sysprof_capture_condition_copy (const SysprofCaptureCondition *self)
case SYSPROF_CAPTURE_CONDITION_WHERE_PID_IN: case SYSPROF_CAPTURE_CONDITION_WHERE_PID_IN:
return sysprof_capture_condition_new_where_pid_in ( return sysprof_capture_condition_new_where_pid_in (
self->u.where_pid_in->len, self->u.where_pid_in.len,
(const int32_t *)(void *)self->u.where_pid_in->data); self->u.where_pid_in.data);
case SYSPROF_CAPTURE_CONDITION_WHERE_COUNTER_IN: case SYSPROF_CAPTURE_CONDITION_WHERE_COUNTER_IN:
return sysprof_capture_condition_new_where_counter_in ( return sysprof_capture_condition_new_where_counter_in (
self->u.where_counter_in->len, self->u.where_counter_in.len,
(const unsigned int *)(void *)self->u.where_counter_in->data); self->u.where_counter_in.data);
case SYSPROF_CAPTURE_CONDITION_WHERE_FILE: case SYSPROF_CAPTURE_CONDITION_WHERE_FILE:
return sysprof_capture_condition_new_where_file (self->u.where_file); return sysprof_capture_condition_new_where_file (self->u.where_file);
@ -276,18 +286,18 @@ sysprof_capture_condition_finalize (SysprofCaptureCondition *self)
break; break;
case SYSPROF_CAPTURE_CONDITION_WHERE_TYPE_IN: case SYSPROF_CAPTURE_CONDITION_WHERE_TYPE_IN:
g_array_free (self->u.where_type_in, TRUE); free (self->u.where_type_in.data);
break; break;
case SYSPROF_CAPTURE_CONDITION_WHERE_TIME_BETWEEN: case SYSPROF_CAPTURE_CONDITION_WHERE_TIME_BETWEEN:
break; break;
case SYSPROF_CAPTURE_CONDITION_WHERE_PID_IN: case SYSPROF_CAPTURE_CONDITION_WHERE_PID_IN:
g_array_free (self->u.where_pid_in, TRUE); free (self->u.where_pid_in.data);
break; break;
case SYSPROF_CAPTURE_CONDITION_WHERE_COUNTER_IN: case SYSPROF_CAPTURE_CONDITION_WHERE_COUNTER_IN:
g_array_free (self->u.where_counter_in, TRUE); free (self->u.where_counter_in.data);
break; break;
case SYSPROF_CAPTURE_CONDITION_WHERE_FILE: case SYSPROF_CAPTURE_CONDITION_WHERE_FILE:
@ -336,9 +346,11 @@ sysprof_capture_condition_new_where_type_in (unsigned int n_ty
return NULL; return NULL;
self->type = SYSPROF_CAPTURE_CONDITION_WHERE_TYPE_IN; self->type = SYSPROF_CAPTURE_CONDITION_WHERE_TYPE_IN;
self->u.where_type_in = g_array_sized_new (FALSE, FALSE, sizeof (SysprofCaptureFrameType), n_types); self->u.where_type_in.data = calloc (n_types, sizeof (SysprofCaptureFrameType));
g_array_set_size (self->u.where_type_in, n_types); if (self->u.where_type_in.data == NULL)
memcpy (self->u.where_type_in->data, types, sizeof (SysprofCaptureFrameType) * n_types); return NULL;
self->u.where_type_in.len = n_types;
memcpy (self->u.where_type_in.data, types, sizeof (SysprofCaptureFrameType) * n_types);
return self; return self;
} }
@ -382,9 +394,14 @@ sysprof_capture_condition_new_where_pid_in (unsigned int n_pids,
return NULL; return NULL;
self->type = SYSPROF_CAPTURE_CONDITION_WHERE_PID_IN; self->type = SYSPROF_CAPTURE_CONDITION_WHERE_PID_IN;
self->u.where_pid_in = g_array_sized_new (FALSE, FALSE, sizeof (int32_t), n_pids); self->u.where_pid_in.data = calloc (n_pids, sizeof (int32_t));
g_array_set_size (self->u.where_pid_in, n_pids); if (self->u.where_pid_in.data == NULL)
memcpy (self->u.where_pid_in->data, pids, sizeof (int32_t) * n_pids); {
free (self);
return NULL;
}
self->u.where_pid_in.len = n_pids;
memcpy (self->u.where_pid_in.data, pids, sizeof (int32_t) * n_pids);
return self; return self;
} }
@ -403,13 +420,16 @@ sysprof_capture_condition_new_where_counter_in (unsigned int n_counters,
return NULL; return NULL;
self->type = SYSPROF_CAPTURE_CONDITION_WHERE_COUNTER_IN; self->type = SYSPROF_CAPTURE_CONDITION_WHERE_COUNTER_IN;
self->u.where_counter_in = g_array_sized_new (FALSE, FALSE, sizeof (unsigned int), n_counters); self->u.where_counter_in.data = calloc (n_counters, sizeof (unsigned int));
if (n_counters > 0 && self->u.where_counter_in.data == NULL)
{
free (self);
return NULL;
}
self->u.where_counter_in.len = n_counters;
if (n_counters > 0) if (n_counters > 0)
{ memcpy (self->u.where_counter_in.data, counters, sizeof (unsigned int) * n_counters);
g_array_set_size (self->u.where_counter_in, n_counters);
memcpy (self->u.where_counter_in->data, counters, sizeof (unsigned int) * n_counters);
}
return self; return self;
} }