[libgee] Conditionally assume in Gee.ArrayList and Gee.LinkedList



commit 08a45821599c8040948d938c3fcf50b334b40719
Author: Maciej Piechotka <uzytkownik2 gmail com>
Date:   Sat Jun 21 20:16:04 2014 +0200

    Conditionally assume in Gee.ArrayList and Gee.LinkedList

 gee/Makefile.am     |    1 +
 gee/arraylist.vala  |   16 +++++++++++++---
 gee/linkedlist.vala |    8 +++++++-
 utils/assume.h      |   33 +++++++++++++++++++++++++++++++++
 utils/geeutils.vapi |    4 ++++
 5 files changed, 58 insertions(+), 4 deletions(-)
---
diff --git a/gee/Makefile.am b/gee/Makefile.am
index a988d22..f7ab9cd 100644
--- a/gee/Makefile.am
+++ b/gee/Makefile.am
@@ -81,6 +81,7 @@ libgee_0_8_la_VALASOURCES = \
 
 libgee_0_8_la_SOURCES = \
        $(libgee_0_8_la_VALASOURCES:.vala=.c) \
+       ../utils/assume.h \
        ../utils/async.h \
        ../utils/free.h \
        ../utils/misc.h \
diff --git a/gee/arraylist.vala b/gee/arraylist.vala
index 83c7e72..2943162 100644
--- a/gee/arraylist.vala
+++ b/gee/arraylist.vala
@@ -24,7 +24,7 @@
  *     Didier 'Ptitjes Villevalois <ptitjes free fr>
  */
 
-using GLib;
+using Gee.Utils.Assume;
 
 /**
  * Resizable array implementation of the { link List} interface.
@@ -281,9 +281,15 @@ public class Gee.ArrayList<G> : AbstractBidirList<G> {
        }
 
        private void shift (int start, int delta) {
+#if !DISABLE_INTERNAL_ASSERTS
                assert (start >= 0);
                assert (start <= _size);
                assert (start >= -delta);
+#else
+               assume (start >= 0);
+               assume (start <= _size);
+               assume (start >= -delta);
+#endif
 
                _items.move (start, start + delta, _size - start);
 
@@ -291,7 +297,11 @@ public class Gee.ArrayList<G> : AbstractBidirList<G> {
        }
 
        private void grow_if_needed (int new_count) {
+#if !DISABLE_INTERNAL_ASSERTS
                assert (new_count >= 0);
+#else
+               assume (new_count >= 0);
+#endif
 
                int minimum_size = _size + new_count;
                if (minimum_size > _items.length) {
@@ -418,13 +428,13 @@ public class Gee.ArrayList<G> : AbstractBidirList<G> {
                        assert (_index < _list._size);
                        return _index;
                }
-               
+
                public bool read_only {
                        get {
                                return false;
                        }
                }
-               
+
                public bool valid {
                        get {
                                return _index >= 0 && _index < _list._size && ! _removed;
diff --git a/gee/linkedlist.vala b/gee/linkedlist.vala
index 40251ba..061426d 100644
--- a/gee/linkedlist.vala
+++ b/gee/linkedlist.vala
@@ -24,6 +24,8 @@
  *     Didier 'Ptitjes Villevalois <ptitjes free fr>
  */
 
+using Gee.Utils.Assume;
+
 /**
  * Doubly-linked list implementation of the { link List} interface.
  *
@@ -112,7 +114,7 @@ public class Gee.LinkedList<G> : AbstractBidirList<G>, Queue<G>, Deque<G> {
        public override int size {
                get { return this._size; }
        }
-       
+
        /**
         * { inheritDoc}
         */
@@ -183,7 +185,9 @@ public class Gee.LinkedList<G> : AbstractBidirList<G>, Queue<G>, Deque<G> {
                assert (index < this._size);
 
                unowned Node<G>? n = this._get_node_at (index);
+#if !DISABLE_INTERNAL_ASSERTS
                assert (n != null);
+#endif
                return n.data;
        }
 
@@ -253,7 +257,9 @@ public class Gee.LinkedList<G> : AbstractBidirList<G>, Queue<G>, Deque<G> {
                assert (index < this._size);
 
                unowned Node<G>? n = this._get_node_at (index);
+#if !DISABLE_INTERNAL_ASSERTS
                assert (n != null);
+#endif
                G element = n.data;
                this._remove_node (n);
                return element;
diff --git a/utils/assume.h b/utils/assume.h
new file mode 100644
index 0000000..35dff51
--- /dev/null
+++ b/utils/assume.h
@@ -0,0 +1,33 @@
+/* assume.h
+ *
+ * Copyright (C) 2013  Maciej Piechotka
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
+ *
+ * Author:
+ *     Maciej Piechotka <uzytkownik2 gmail com>
+ */
+#ifndef GEE_UTILS_ASSUME
+#define GEE_UTILS_ASSUME
+
+#if __GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__ >= 40500
+#define gee_utils_assume(cond) do { if (!(cond)) __builtin_unreachable(); } while (0)
+#elif defined(_MSC_VER)
+#define gee_utils_assume(cond) __assume(cond)
+#else
+#define gee_utils_assume(cond) NULL
+#endif
+
+#endif
diff --git a/utils/geeutils.vapi b/utils/geeutils.vapi
index b25a79e..7b1472d 100644
--- a/utils/geeutils.vapi
+++ b/utils/geeutils.vapi
@@ -1,5 +1,9 @@
 namespace Gee {
        namespace Utils {
+               namespace Assume {
+                       [CCode (cheader_filename = "assume.h", cname = "gee_utils_assume")]
+                       public void assume(bool cond);
+               }
                namespace Async {
                        [CCode (cheader_filename = "async.h")]
                        public async void yield_and_unlock (GLib.Mutex mutex);


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