summaryrefslogtreecommitdiff
path: root/gtk/gtkbitmaskprivateimpl.h
diff options
context:
space:
mode:
authorNeil Roberts <neil@linux.intel.com>2012-02-09 04:53:43 +0100
committerBenjamin Otte <otte@redhat.com>2012-03-01 15:10:36 +0100
commit6044dfc35b3afb77612cc8a3603e1a0092dfd72a (patch)
tree0b5f4777f56c146263e5fbf7afa3e2c613b4b63c /gtk/gtkbitmaskprivateimpl.h
parent27eb83a4101adb608949cebebe59c307627d1e9e (diff)
downloadgtk+-6044dfc35b3afb77612cc8a3603e1a0092dfd72a.tar.gz
bitmask: Don't allocate memory for small bitmasks
Code taken more or less verbatim from CoglBitmask.
Diffstat (limited to 'gtk/gtkbitmaskprivateimpl.h')
-rw-r--r--gtk/gtkbitmaskprivateimpl.h72
1 files changed, 62 insertions, 10 deletions
diff --git a/gtk/gtkbitmaskprivateimpl.h b/gtk/gtkbitmaskprivateimpl.h
index 365d438e3d..e678a81d44 100644
--- a/gtk/gtkbitmaskprivateimpl.h
+++ b/gtk/gtkbitmaskprivateimpl.h
@@ -23,25 +23,33 @@
static inline GtkBitmask *
_gtk_bitmask_new (void)
{
- return _gtk_allocated_bitmask_new ();
+ return _gtk_bitmask_from_bits (0);
}
static inline GtkBitmask *
_gtk_bitmask_copy (const GtkBitmask *mask)
{
- return _gtk_allocated_bitmask_copy (mask);
+ if (_gtk_bitmask_is_allocated (mask))
+ return _gtk_allocated_bitmask_copy (mask);
+ else
+ return (GtkBitmask *) mask;
}
static inline void
_gtk_bitmask_free (GtkBitmask *mask)
{
- return _gtk_allocated_bitmask_free (mask);
+ if (_gtk_bitmask_is_allocated (mask))
+ return _gtk_allocated_bitmask_free (mask);
}
static inline char *
_gtk_bitmask_to_string (const GtkBitmask *mask)
{
- return _gtk_allocated_bitmask_to_string (mask);
+ GString *string;
+
+ string = g_string_new (NULL);
+ _gtk_allocated_bitmask_print (mask, string);
+ return g_string_free (string, FALSE);
}
static inline void
@@ -62,7 +70,12 @@ static inline GtkBitmask *
_gtk_bitmask_union (GtkBitmask *mask,
const GtkBitmask *other)
{
- return _gtk_allocated_bitmask_union (mask, other);
+ if (_gtk_bitmask_is_allocated (mask) ||
+ _gtk_bitmask_is_allocated (other))
+ return _gtk_allocated_bitmask_union (mask, other);
+ else
+ return _gtk_bitmask_from_bits (_gtk_bitmask_to_bits (mask)
+ | _gtk_bitmask_to_bits (other));
}
static inline GtkBitmask *
@@ -76,7 +89,12 @@ static inline gboolean
_gtk_bitmask_get (const GtkBitmask *mask,
guint index_)
{
- return _gtk_allocated_bitmask_get (mask, index_);
+ if (_gtk_bitmask_is_allocated (mask))
+ return _gtk_allocated_bitmask_get (mask, index_);
+ else
+ return index_ < GTK_BITMASK_N_DIRECT_BITS
+ ? !!(_gtk_bitmask_to_bits (mask) & (((size_t) 1) << index_))
+ : FALSE;
}
static inline GtkBitmask *
@@ -84,7 +102,22 @@ _gtk_bitmask_set (GtkBitmask *mask,
guint index_,
gboolean value)
{
- return _gtk_allocated_bitmask_set (mask, index_, value);
+ if (_gtk_bitmask_is_allocated (mask) ||
+ (index_ >= GTK_BITMASK_N_DIRECT_BITS && value))
+ return _gtk_allocated_bitmask_set (mask, index_, value);
+ else if (index_ < GTK_BITMASK_N_DIRECT_BITS)
+ {
+ gsize bits = _gtk_bitmask_to_bits (mask);
+
+ if (value)
+ bits |= ((size_t) 1) << index_;
+ else
+ bits &= ~(((size_t) 1) << index_);
+
+ return _gtk_bitmask_from_bits (bits);
+ }
+ else
+ return mask;
}
static inline GtkBitmask *
@@ -92,19 +125,34 @@ _gtk_bitmask_invert_range (GtkBitmask *mask,
guint start,
guint end)
{
- return _gtk_allocated_bitmask_invert_range (mask, start, end);
+ if (_gtk_bitmask_is_allocated (mask) ||
+ (end > GTK_BITMASK_N_DIRECT_BITS))
+ return _gtk_allocated_bitmask_invert_range (mask, start, end);
+ else
+ {
+ size_t invert = (((size_t) 1) << end) - (((size_t) 1) << start);
+
+ return _gtk_bitmask_from_bits (_gtk_bitmask_to_bits (mask) ^ invert);
+ }
}
static inline gboolean
_gtk_bitmask_is_empty (const GtkBitmask *mask)
{
- return _gtk_allocated_bitmask_is_empty (mask);
+ return mask == _gtk_bitmask_from_bits (0);
}
static inline gboolean
_gtk_bitmask_equals (const GtkBitmask *mask,
const GtkBitmask *other)
{
+ if (mask == other)
+ return TRUE;
+
+ if (!_gtk_bitmask_is_allocated (mask) ||
+ !_gtk_bitmask_is_allocated (other))
+ return FALSE;
+
return _gtk_allocated_bitmask_equals (mask, other);
}
@@ -112,5 +160,9 @@ static inline gboolean
_gtk_bitmask_intersects (const GtkBitmask *mask,
const GtkBitmask *other)
{
- return _gtk_allocated_bitmask_intersects (mask, other);
+ if (!_gtk_bitmask_is_allocated (mask) ||
+ !_gtk_bitmask_is_allocated (other))
+ return _gtk_allocated_bitmask_intersects (mask, other);
+ else
+ return _gtk_bitmask_to_bits (mask) & _gtk_bitmask_to_bits (other);
}