[Xfce4-commits] <squeeze:master> Store entry property data on sizeof aligned
Peter de Ridder
noreply at xfce.org
Mon Aug 22 22:16:01 CEST 2011
Updating branch refs/heads/master
to 93a294dc61ac7ccdd6e2a276520db444983c5156 (commit)
from a5fa5beafeb47facb590a2a3e98812d50b973217 (commit)
commit 93a294dc61ac7ccdd6e2a276520db444983c5156
Author: Peter de Ridder <peter at xfce.org>
Date: Mon Aug 22 22:14:05 2011 +0200
Store entry property data on sizeof aligned
libsqueeze/archive-iter.c | 204 +++++++++++++----------------------------
libsqueeze/archive.c | 24 +++++
libsqueeze/libsqueeze-view.h | 2 +
libsqueeze/parser.c | 120 +++++++++++++++++++++++-
libsqueeze/parser.h | 4 +
libsqueeze/support-reader.c | 2 +-
libsqueeze/support-template.c | 14 +++
libsqueeze/support-template.h | 4 +
8 files changed, 228 insertions(+), 146 deletions(-)
diff --git a/libsqueeze/archive-iter.c b/libsqueeze/archive-iter.c
index e07addb..97060f9 100644
--- a/libsqueeze/archive-iter.c
+++ b/libsqueeze/archive-iter.c
@@ -901,6 +901,7 @@ static void
lsq_archive_entry_props_free(const LSQArchive *archive, LSQArchiveEntry *entry)
{
guint i;
+ guint offset;
gpointer props_iter = entry->props;
/* free the properties */
if(props_iter)
@@ -912,15 +913,9 @@ lsq_archive_entry_props_free(const LSQArchive *archive, LSQArchiveEntry *entry)
{
case(G_TYPE_STRING):
/* free only strings */
- g_free(*(gchar **)props_iter);
- *(gchar **)props_iter = NULL;
- props_iter = ((gchar **)props_iter) + 1;
- break;
- case(G_TYPE_UINT):
- props_iter = ((guint *)props_iter) + 1;
- break;
- case(G_TYPE_UINT64):
- props_iter = ((guint64 *)props_iter) + 1;
+ offset = lsq_archive_get_entry_property_offset(archive, i+LSQ_ARCHIVE_PROP_USER);
+ g_free(((gchar **)props_iter)[offset]);
+ ((gchar **)props_iter)[offset] = NULL;
break;
}
}
@@ -1274,7 +1269,7 @@ lsq_archive_entry_get_prop_str(const LSQArchive *archive, const LSQArchiveEntry
{
const gchar *retval = NULL;
gpointer props_iter = NULL;
- guint n;
+ guint offset;
switch(i)
{
@@ -1288,22 +1283,12 @@ lsq_archive_entry_get_prop_str(const LSQArchive *archive, const LSQArchiveEntry
props_iter = entry->props;
if(props_iter)
{
- for(n = 0; n < (i-LSQ_ARCHIVE_PROP_USER); ++n)
- {
- switch(lsq_archive_get_entry_property_type(archive, n+LSQ_ARCHIVE_PROP_USER))
- {
- case G_TYPE_STRING:
- props_iter = ((gchar **)props_iter) + 1;;
- break;
- case G_TYPE_UINT:
- props_iter = ((guint *)props_iter) + 1;;
- break;
- case G_TYPE_UINT64:
- props_iter = ((guint64 *)props_iter) + 1;;
- break;
- }
- }
- retval = (*((gchar **)props_iter));
+ if (lsq_archive_get_entry_property_type(archive, i) == G_TYPE_STRING)
+ {
+ offset = lsq_archive_get_entry_property_offset(archive, i);
+
+ retval = (((gchar **)props_iter)[offset]);
+ }
}
break;
}
@@ -1314,75 +1299,42 @@ inline static guint
lsq_archive_entry_get_prop_uint(const LSQArchive *archive, const LSQArchiveEntry *entry, guint i)
{
gpointer props_iter = entry->props;
- guint n;
+ guint offset;
if(!props_iter)
return 0;
- for(n = 0; n < (i-LSQ_ARCHIVE_PROP_USER); ++n)
- {
- switch(lsq_archive_get_entry_property_type(archive, n))
- {
- case G_TYPE_STRING:
- props_iter = ((gchar **)props_iter) + 1;;
- break;
- case G_TYPE_UINT:
- props_iter = ((guint *)props_iter) + 1;;
- break;
- case G_TYPE_UINT64:
- props_iter = ((guint64 *)props_iter) + 1;;
- break;
- }
- }
- return (*((guint *)props_iter));
+
+ if (lsq_archive_get_entry_property_type(archive, i) != G_TYPE_UINT)
+ return 0;
+
+ offset = lsq_archive_get_entry_property_offset(archive, i);
+
+ return (((guint *)props_iter)[offset]);
}
inline static guint64
lsq_archive_entry_get_prop_uint64(const LSQArchive *archive, const LSQArchiveEntry *entry, guint i)
{
gpointer props_iter = entry->props;
- guint n;
+ guint offset;
if(!props_iter)
return 0;
- for(n = 0; n < (i-LSQ_ARCHIVE_PROP_USER); ++n)
- {
- switch(lsq_archive_get_entry_property_type(archive, n+LSQ_ARCHIVE_PROP_USER))
- {
- case G_TYPE_STRING:
- props_iter = ((gchar **)props_iter) + 1;;
- break;
- case G_TYPE_UINT:
- props_iter = ((guint *)props_iter) + 1;;
- break;
- case G_TYPE_UINT64:
- props_iter = ((guint64 *)props_iter) + 1;;
- break;
- }
- }
- return (*((guint64 *)props_iter));
+
+ if (lsq_archive_get_entry_property_type(archive, i) != G_TYPE_UINT64)
+ return 0;
+
+ offset = lsq_archive_get_entry_property_offset(archive, i);
+
+ return (((guint64 *)props_iter)[offset]);
}
static gpointer
lsq_archive_entry_get_props(const LSQArchive *archive, LSQArchiveEntry *entry)
{
guint size = 0;
- guint i;
if(!entry->props)
{
- for(i = 0; i < (lsq_archive_n_entry_properties(archive)-LSQ_ARCHIVE_PROP_USER); ++i)
- {
- switch(lsq_archive_get_entry_property_type(archive, i+LSQ_ARCHIVE_PROP_USER))
- {
- case G_TYPE_STRING:
- size += sizeof(gchar *);
- break;
- case G_TYPE_UINT:
- size += sizeof(guint);
- break;
- case G_TYPE_UINT64:
- size += sizeof(guint64);
- break;
- }
- }
+ size = lsq_archive_entry_properties_size(archive);
entry->props = g_malloc0(size);
}
@@ -1394,98 +1346,67 @@ static void
lsq_archive_entry_set_prop_str(const LSQArchive *archive, LSQArchiveEntry *entry, guint n, const gchar *str_val)
{
gpointer props_iter = lsq_archive_entry_get_props(archive, entry);
- guint i;
+ guint offset;
- for(i = 0; i < (n-LSQ_ARCHIVE_PROP_USER); ++i)
- {
- switch(lsq_archive_get_entry_property_type(archive, i+LSQ_ARCHIVE_PROP_USER))
- {
- case G_TYPE_STRING:
- props_iter = ((gchar **)props_iter) + 1;;
- break;
- case G_TYPE_UINT:
- props_iter = ((guint *)props_iter) + 1;;
- break;
- case G_TYPE_UINT64:
- props_iter = ((guint64 *)props_iter) + 1;;
- break;
- }
- }
- g_free(*((gchar **)props_iter));
- (*((gchar **)props_iter)) = g_strdup(str_val);
+ if (lsq_archive_get_entry_property_type(archive, n) != G_TYPE_STRING)
+ return;
+
+ offset = lsq_archive_get_entry_property_offset(archive, n);
+
+ g_free(((gchar **)props_iter)[offset]);
+ ((gchar **)props_iter)[offset] = g_strdup(str_val);
}
static void
lsq_archive_entry_set_prop_uint(const LSQArchive *archive, LSQArchiveEntry *entry, guint n, guint int_val)
{
gpointer props_iter = lsq_archive_entry_get_props(archive, entry);
- guint i;
+ guint offset;
- for(i = 0; i < (n-LSQ_ARCHIVE_PROP_USER); ++i)
- {
- switch(lsq_archive_get_entry_property_type(archive, i+LSQ_ARCHIVE_PROP_USER))
- {
- case G_TYPE_STRING:
- props_iter = ((gchar **)props_iter) + 1;;
- break;
- case G_TYPE_UINT:
- props_iter = ((guint *)props_iter) + 1;;
- break;
- case G_TYPE_UINT64:
- props_iter = ((guint64 *)props_iter) + 1;;
- break;
- }
- }
- (*((guint *)props_iter)) = int_val;
+ if (lsq_archive_get_entry_property_type(archive, n) != G_TYPE_UINT)
+ return;
+
+ offset = lsq_archive_get_entry_property_offset(archive, n);
+
+ ((guint *)props_iter)[offset] = int_val;
}
static void
lsq_archive_entry_set_prop_uint64(const LSQArchive *archive, LSQArchiveEntry *entry, guint n, guint64 int64_val)
{
gpointer props_iter = lsq_archive_entry_get_props(archive, entry);
- guint i;
+ guint offset;
- for(i = 0; i < (n-LSQ_ARCHIVE_PROP_USER); ++i)
- {
- switch(lsq_archive_get_entry_property_type(archive, i+LSQ_ARCHIVE_PROP_USER))
- {
- case G_TYPE_STRING:
- props_iter = ((gchar **)props_iter) + 1;;
- break;
- case G_TYPE_UINT:
- props_iter = ((guint *)props_iter) + 1;;
- break;
- case G_TYPE_UINT64:
- props_iter = ((guint64 *)props_iter) + 1;;
- break;
- }
- }
- (*((guint64 *)props_iter)) = int64_val;
+ if (lsq_archive_get_entry_property_type(archive, n) != G_TYPE_UINT64)
+ return;
+
+ offset = lsq_archive_get_entry_property_offset(archive, n);
+
+ ((guint64 *)props_iter)[offset] = int64_val;
}
static void
lsq_archive_entry_set_propsv(const LSQArchive *archive, LSQArchiveEntry *entry, gpointer *props)
{
gpointer props_iter = lsq_archive_entry_get_props(archive, entry);
+ guint offset;
guint i;
for(i=0; i < (lsq_archive_n_entry_properties(archive) - LSQ_ARCHIVE_PROP_USER); ++i)
{
+ offset = lsq_archive_get_entry_property_offset(archive, i+LSQ_ARCHIVE_PROP_USER);
switch(lsq_archive_get_entry_property_type(archive, i+LSQ_ARCHIVE_PROP_USER))
{
case G_TYPE_STRING:
- g_free(*((gchar **)props_iter));
+ g_free(((gchar **)props_iter)[offset]);
//(*((gchar **)props_iter)) = g_strdup((const gchar*)props[i]);
- (*((gchar **)props_iter)) = (gchar*)props[i];
- props_iter = ((gchar **)props_iter) + 1;;
+ ((gchar **)props_iter)[offset] = (gchar*)props[i];
break;
case G_TYPE_UINT:
- (*((guint *)props_iter)) = *((const guint *)props[i]);
- props_iter = ((guint *)props_iter) + 1;;
+ ((guint *)props_iter)[offset] = *((const guint *)props[i]);
break;
case G_TYPE_UINT64:
- (*((guint64 *)props_iter)) = *((const guint64 *)props[i]);
- props_iter = ((guint64 *)props_iter) + 1;;
+ ((guint64 *)props_iter)[offset] = *((const guint64 *)props[i]);
break;
}
}
@@ -1495,24 +1416,23 @@ static void
lsq_archive_entry_set_propsva(const LSQArchive *archive, LSQArchiveEntry *entry, va_list ap)
{
gpointer props_iter = lsq_archive_entry_get_props(archive, entry);
+ guint offset;
guint i;
for(i=0; i < (lsq_archive_n_entry_properties(archive) - LSQ_ARCHIVE_PROP_USER); ++i)
{
+ offset = lsq_archive_get_entry_property_offset(archive, i+LSQ_ARCHIVE_PROP_USER);
switch(lsq_archive_get_entry_property_type(archive, i+LSQ_ARCHIVE_PROP_USER))
{
case G_TYPE_STRING:
- g_free(*((gchar **)props_iter));
- (*((gchar **)props_iter)) = g_strdup(va_arg(ap, gchar*));
- props_iter = ((gchar **)props_iter) + 1;;
+ g_free(((gchar **)props_iter)[offset]);
+ ((gchar **)props_iter)[offset] = g_strdup(va_arg(ap, gchar*));
break;
case G_TYPE_UINT:
- (*((guint *)props_iter)) = va_arg(ap, guint);
- props_iter = ((guint *)props_iter) + 1;;
+ ((guint *)props_iter)[offset] = va_arg(ap, guint);
break;
case G_TYPE_UINT64:
- (*((guint64 *)props_iter)) = va_arg(ap, guint64);
- props_iter = ((guint64 *)props_iter) + 1;;
+ ((guint64 *)props_iter)[offset] = va_arg(ap, guint64);
break;
}
}
diff --git a/libsqueeze/archive.c b/libsqueeze/archive.c
index 6fb3bab..e2c55f0 100644
--- a/libsqueeze/archive.c
+++ b/libsqueeze/archive.c
@@ -242,6 +242,21 @@ lsq_archive_get_entry_property_type(const LSQArchive *archive, guint n)
}
}
+guint
+lsq_archive_get_entry_property_offset(const LSQArchive *archive, guint n)
+{
+ switch(n)
+ {
+ case LSQ_ARCHIVE_PROP_FILENAME:
+ case LSQ_ARCHIVE_PROP_MIME_TYPE:
+ g_return_val_if_reached(0);
+ break;
+ default:
+ return lsq_support_template_get_property_offset(archive->priv->s_template, n - LSQ_ARCHIVE_PROP_USER);
+ break;
+ }
+}
+
/*
* lsq_archive_get_entry_property_name:
*
@@ -264,6 +279,15 @@ lsq_archive_get_entry_property_name(const LSQArchive *archive, guint n)
}
}
+guint
+lsq_archive_entry_properties_size(const LSQArchive *archive)
+{
+#ifdef DEBUG
+ g_return_val_if_fail(archive, 0);
+#endif
+ return lsq_support_template_get_properties_size(archive->priv->s_template);
+}
+
/*
* lsq_archive_get_filename:
* @archive: LSQArchive object
diff --git a/libsqueeze/libsqueeze-view.h b/libsqueeze/libsqueeze-view.h
index d8fef13..1c674ad 100644
--- a/libsqueeze/libsqueeze-view.h
+++ b/libsqueeze/libsqueeze-view.h
@@ -36,8 +36,10 @@ gchar *lsq_archive_iter_get_path(const LSQArchiveIter *archive);
LSQArchiveIter *lsq_archive_get_iter(LSQArchive *archive, const gchar *path);
GType lsq_archive_get_entry_property_type(const LSQArchive *archive, guint n);
+guint lsq_archive_get_entry_property_offset(const LSQArchive *archive, guint n);
const gchar *lsq_archive_get_entry_property_name(const LSQArchive *archive, guint n);
guint lsq_archive_n_entry_properties(const LSQArchive *archive);
+guint lsq_archive_entry_properties_size(const LSQArchive *archive);
gboolean lsq_archive_can_stop(const LSQArchive *archive);
gboolean lsq_archive_stop(const LSQArchive *archive);
diff --git a/libsqueeze/parser.c b/libsqueeze/parser.c
index 420330b..c9586a2 100644
--- a/libsqueeze/parser.c
+++ b/libsqueeze/parser.c
@@ -30,6 +30,70 @@
G_DEFINE_ABSTRACT_TYPE(LSQParser, lsq_parser, G_TYPE_OBJECT);
+struct _LSQTypeStorageGroup
+{
+ guint size_of;
+ guint n_types;
+ GType *types;
+};
+typedef struct _LSQTypeStorageGroup LSQTypeStorageGroup;
+static LSQTypeStorageGroup *storage_groups = NULL;
+static guint storage_group_count = 0;
+
+static void
+lsq_storage_group_add(GType type, guint size_of)
+{
+ guint i;
+ LSQTypeStorageGroup *_storage_groups;
+ GType *_types;
+
+ for (i = 0; i < storage_group_count; ++i)
+ {
+ if (storage_groups[i].size_of <= size_of)
+ break;
+ }
+
+ g_return_if_fail(i <= storage_group_count);
+
+ if (i == storage_group_count ||
+ storage_groups[i].size_of < size_of)
+ {
+ _storage_groups = g_new0(LSQTypeStorageGroup, storage_group_count+1);
+
+ memcpy(_storage_groups, storage_groups, sizeof(LSQTypeStorageGroup) * i);
+ memcpy(_storage_groups + i + 1, storage_groups + i, sizeof(LSQTypeStorageGroup) * (storage_group_count - i));
+
+ g_free(storage_groups);
+ storage_groups = _storage_groups;
+ ++storage_group_count;
+
+ storage_groups[i].size_of = size_of;
+ storage_groups[i].n_types = 0;
+ storage_groups[i].types = NULL;
+ }
+
+ _types = g_new(GType, storage_groups[i].n_types+1);
+
+ memcpy(_types, storage_groups[i].types, sizeof(GType) * storage_groups[i].n_types);
+
+ g_free(storage_groups[i].types);
+ storage_groups[i].types = _types;
+ _types[storage_groups[i].n_types] = type;
+ ++storage_groups[i].n_types;
+}
+
+static gboolean
+lsq_storage_group_has_type(LSQTypeStorageGroup *storage_group, GType type)
+{
+ guint i;
+ for (i = 0; i < storage_group->n_types; ++i)
+ {
+ if (storage_group->types[i] == type)
+ return TRUE;
+ }
+ return FALSE;
+}
+
static void
lsq_parser_init(LSQParser *self)
{
@@ -38,7 +102,18 @@ lsq_parser_init(LSQParser *self)
static void
lsq_parser_class_init(LSQParserClass *klass)
{
- klass->get_context = NULL;
+ klass->get_context = NULL;
+
+ lsq_storage_group_add(G_TYPE_CHAR, sizeof(gchar));
+ lsq_storage_group_add(G_TYPE_DOUBLE, sizeof(gdouble));
+ lsq_storage_group_add(G_TYPE_FLOAT, sizeof(gfloat));
+ lsq_storage_group_add(G_TYPE_INT, sizeof(gint));
+ lsq_storage_group_add(G_TYPE_INT64, sizeof(gint64));
+ lsq_storage_group_add(G_TYPE_LONG, sizeof(glong));
+ lsq_storage_group_add(G_TYPE_STRING, sizeof(gchar*));
+ lsq_storage_group_add(G_TYPE_UINT, sizeof(guint));
+ lsq_storage_group_add(G_TYPE_UINT64, sizeof(guint64));
+ lsq_storage_group_add(G_TYPE_ULONG, sizeof(gulong));
}
LSQParserContext*
@@ -74,14 +149,23 @@ lsq_parser_get_property_type(LSQParser *parser, guint nr)
return parser->property_types[nr];
}
+guint
+lsq_parser_get_property_offset(LSQParser *parser, guint nr)
+{
+ g_return_val_if_fail(nr < parser->n_properties, 0);
+ return parser->property_offset[nr];
+}
+
void
lsq_parser_set_property_type(LSQParser *parser, guint nr, GType type)
{
+ guint i, j;
+ guint size_of, offset = 0;
+
if(nr >= parser->n_properties)
{
GType *new_list = g_new(GType, nr+1);
- guint i;
- for(i=0; i < parser->n_properties; i++)
+ for(i=0; i < parser->n_properties; ++i)
{
new_list[i] = parser->property_types[i];
}
@@ -92,7 +176,37 @@ lsq_parser_set_property_type(LSQParser *parser, guint nr, GType type)
g_free(parser->property_types);
parser->property_types = new_list;
parser->n_properties = nr+1;
+
+ g_free(parser->property_offset);
+ parser->property_offset = g_new(guint, nr+1);
}
+
parser->property_types[nr] = type;
+
+ for (i = 0; i < storage_group_count; ++i)
+ {
+ size_of = storage_groups[i].size_of;
+ for (j = 0; j < parser->n_properties; ++j)
+ {
+ if (lsq_storage_group_has_type(&storage_groups[i], parser->property_types[j]))
+ {
+ guint align = offset % size_of;
+ if (align)
+ offset += size_of - align;
+
+ /* Store the offset as count of block sized size_of */
+ parser->property_offset[j] = offset / size_of;
+ offset += size_of;
+ }
+ }
+ }
+
+ parser->properties_size = offset;
+}
+
+guint
+lsq_parser_get_properties_size(LSQParser *parser)
+{
+ return parser->properties_size;
}
diff --git a/libsqueeze/parser.h b/libsqueeze/parser.h
index 2ae41e7..33c8d38 100644
--- a/libsqueeze/parser.h
+++ b/libsqueeze/parser.h
@@ -54,6 +54,8 @@ struct _LSQParser
guint n_properties;
GType *property_types;
+ guint *property_offset;
+ guint properties_size;
};
@@ -77,6 +79,8 @@ void lsq_parser_parse(LSQParser *, LSQParserContext *);
guint lsq_parser_n_properties(LSQParser *);
GType lsq_parser_get_property_type(LSQParser *, guint);
void lsq_parser_set_property_type(LSQParser *, guint, GType);
+guint lsq_parser_get_property_offset(LSQParser *, guint);
+guint lsq_parser_get_properties_size(LSQParser *);
G_END_DECLS
diff --git a/libsqueeze/support-reader.c b/libsqueeze/support-reader.c
index af78c93..cb83387 100644
--- a/libsqueeze/support-reader.c
+++ b/libsqueeze/support-reader.c
@@ -285,7 +285,7 @@ lsq_support_reader_parse_file(const gchar *filename)
s_template->parser = parser;
#ifdef DEBUG
- if (NULL != s_template->supported)
+ if (s_template->supported)
{
g_debug("%s supported\n", _mime_types[i]);
}
diff --git a/libsqueeze/support-template.c b/libsqueeze/support-template.c
index 119f557..2257d6c 100644
--- a/libsqueeze/support-template.c
+++ b/libsqueeze/support-template.c
@@ -35,6 +35,13 @@ lsq_support_template_get_property_type(LSQSupportTemplate *templ, guint nr)
return lsq_parser_get_property_type(templ->parser, nr);
}
+guint
+lsq_support_template_get_property_offset(LSQSupportTemplate *templ, guint nr)
+{
+ g_return_val_if_fail(templ->parser, 0);
+ return lsq_parser_get_property_offset(templ->parser, nr);
+}
+
const gchar *
lsq_support_template_get_property_name(LSQSupportTemplate *templ, guint nr)
{
@@ -54,3 +61,10 @@ lsq_support_template_get_n_properties (LSQSupportTemplate *templ)
n_props = templ->n_properties;
return n_props;
}
+
+guint
+lsq_support_template_get_properties_size(LSQSupportTemplate *templ)
+{
+ g_return_val_if_fail(templ->parser, 0);
+ return lsq_parser_get_properties_size(templ->parser);
+}
diff --git a/libsqueeze/support-template.h b/libsqueeze/support-template.h
index 62493c8..4b479fc 100644
--- a/libsqueeze/support-template.h
+++ b/libsqueeze/support-template.h
@@ -67,10 +67,14 @@ struct _LSQSupportTemplate
GType
lsq_support_template_get_property_type(LSQSupportTemplate *s_template, guint n);
+guint
+lsq_support_template_get_property_offset(LSQSupportTemplate *s_template, guint n);
const gchar *
lsq_support_template_get_property_name(LSQSupportTemplate *s_template, guint n);
guint
lsq_support_template_get_n_properties (LSQSupportTemplate *s_template);
+guint
+lsq_support_template_get_properties_size (LSQSupportTemplate *s_template);
#endif /* __SUPPORT_TEMPLATE_H__ */
More information about the Xfce4-commits
mailing list