Add a mono_binary_search () function.
authorAlex Rønne Petersen <alexrp@xamarin.com>
Fri, 26 Jul 2013 14:24:50 +0000 (16:24 +0200)
committerAlex Rønne Petersen <alexrp@xamarin.com>
Fri, 26 Jul 2013 14:24:50 +0000 (16:24 +0200)
This is a replacement for bsearch () because some platforms
have bogus asserts in their implementations.

14 files changed:
mono/dis/main.c
mono/io-layer/messages.c
mono/metadata/class.c
mono/metadata/debug-mono-symfile.c
mono/metadata/icall.c
mono/metadata/locales.c
mono/metadata/metadata-verify.c
mono/metadata/metadata.c
mono/mini/simd-intrinsics.c
mono/utils/Makefile.am
mono/utils/bsearch.c [new file with mode: 0644]
mono/utils/bsearch.h [new file with mode: 0644]
msvc/libmonoutils.vcxproj
support/supportw.c

index d4719cac40c0592b98bbef8dcbf4c5dce0af19a1..42d735b0f751ed7780406a0b3448bb0ebac2048d 100644 (file)
@@ -30,6 +30,7 @@
 #include <mono/metadata/loader.h>
 #include <mono/metadata/assembly.h>
 #include <mono/metadata/appdomain.h>
+#include <mono/utils/bsearch.h>
 
 static void     setup_filter          (MonoImage *image);
 static gboolean should_include_type   (int idx);
@@ -1109,7 +1110,7 @@ dis_interfaces (MonoImage *m, guint32 typedef_row, MonoGenericContainer *contain
        loc.col_idx = MONO_INTERFACEIMPL_CLASS;
        loc.idx = typedef_row;
 
-       if (!bsearch (&loc, table->base, table->rows, table->row_size, table_locator))
+       if (!mono_binary_search (&loc, table->base, table->rows, table->row_size, table_locator))
                return;
 
        start = loc.result;
@@ -1680,7 +1681,7 @@ table_includes (TableFilter *tf, int idx)
 {
        if (!tf->count)
                return FALSE;
-       return bsearch (&idx, tf->elems, tf->count, sizeof (int), int_cmp) != NULL;
+       return mono_binary_search (&idx, tf->elems, tf->count, sizeof (int), int_cmp) != NULL;
 }
 
 static gboolean 
index 26a5d4292bddf2fe1dc7e87ba004f3f454cb67e8..a8a7db80ffd6a6b48ce453e11608be748949ee78 100644 (file)
@@ -17,6 +17,7 @@
 #include <mono/io-layer/wapi-private.h>
 #include <mono/io-layer/misc-private.h>
 #include <mono/io-layer/messages.h>
+#include <mono/utils/bsearch.h>
 
 #undef DEBUG
 
@@ -1830,7 +1831,7 @@ find_msg (guint32 id, ErrorDesc *base, int n)
        ErrorDesc d, *result;
        d.id = id;
        
-       result = bsearch (&d, base, n, sizeof (ErrorDesc), msg_compare);
+       result = mono_binary_search (&d, base, n, sizeof (ErrorDesc), msg_compare);
        if (result == NULL)
                return NULL;
        return result->txt;
index 06f589934a0fff09509fad7b9aadaf66ce430506..167e449705a63f596a1819e9f1e535f480354aad 100644 (file)
@@ -42,6 +42,8 @@
 #include <mono/utils/mono-logger-internal.h>
 #include <mono/utils/mono-memory-model.h>
 #include <mono/utils/atomic.h>
+#include <mono/utils/bsearch.h>
+
 MonoStats mono_stats;
 
 gboolean mono_print_vtable = FALSE;
@@ -2692,7 +2694,7 @@ compare_interface_ids (const void *p_key, const void *p_element) {
 /*FIXME verify all callers if they should switch to mono_class_interface_offset_with_variance*/
 int
 mono_class_interface_offset (MonoClass *klass, MonoClass *itf) {
-       MonoClass **result = bsearch (
+       MonoClass **result = mono_binary_search (
                        itf,
                        klass->interfaces_packed,
                        klass->interface_offsets_count,
index d4c1241478fd78613d559e6ded90896ecd3017e9..9832418bec12990296a243288a4958fdba0f801b 100644 (file)
@@ -30,6 +30,7 @@
 #include <mono/metadata/metadata-internals.h>
 #include <mono/metadata/class-internals.h>
 #include <mono/utils/mono-mmap.h>
+#include <mono/utils/bsearch.h>
 
 #include <fcntl.h>
 #ifdef HAVE_UNISTD_H
@@ -731,7 +732,7 @@ mono_debug_symfile_lookup_method (MonoDebugHandle *handle, MonoMethod *method)
        first_ie = (MonoSymbolFileMethodEntry *)
                (symfile->raw_contents + read32(&(symfile->offset_table->_method_table_offset)));
 
-       ie = bsearch (GUINT_TO_POINTER (mono_method_get_token (method)), first_ie,
+       ie = mono_binary_search (GUINT_TO_POINTER (mono_method_get_token (method)), first_ie,
                                   read32(&(symfile->offset_table->_method_count)),
                                   sizeof (MonoSymbolFileMethodEntry), compare_method);
 
index 317f894fc4392f739403411b3d445e2c88946afd..faf8485c528df7b7f1d97d5eae71c59d3f4bfa81 100644 (file)
@@ -84,6 +84,7 @@
 #include <mono/utils/mono-mmap.h>
 #include <mono/utils/mono-io-portability.h>
 #include <mono/utils/mono-digest.h>
+#include <mono/utils/bsearch.h>
 
 #if defined (HOST_WIN32)
 #include <windows.h>
@@ -7947,7 +7948,7 @@ compare_method_imap (const void *key, const void *elem)
 static gpointer
 find_method_icall (const IcallTypeDesc *imap, const char *name)
 {
-       const guint16 *nameslot = bsearch (name, icall_names_idx + imap->first_icall, icall_desc_num_icalls (imap), sizeof (icall_names_idx [0]), compare_method_imap);
+       const guint16 *nameslot = mono_binary_search (name, icall_names_idx + imap->first_icall, icall_desc_num_icalls (imap), sizeof (icall_names_idx [0]), compare_method_imap);
        if (!nameslot)
                return NULL;
        return (gpointer)icall_functions [(nameslot - &icall_names_idx [0])];
@@ -7963,7 +7964,7 @@ compare_class_imap (const void *key, const void *elem)
 static const IcallTypeDesc*
 find_class_icalls (const char *name)
 {
-       const guint16 *nameslot = bsearch (name, icall_type_names_idx, Icall_type_num, sizeof (icall_type_names_idx [0]), compare_class_imap);
+       const guint16 *nameslot = mono_binary_search (name, icall_type_names_idx, Icall_type_num, sizeof (icall_type_names_idx [0]), compare_class_imap);
        if (!nameslot)
                return NULL;
        return &icall_type_descs [nameslot - &icall_type_names_idx [0]];
@@ -7981,7 +7982,7 @@ compare_method_imap (const void *key, const void *elem)
 static gpointer
 find_method_icall (const IcallTypeDesc *imap, const char *name)
 {
-       const char **nameslot = bsearch (name, icall_names + imap->first_icall, icall_desc_num_icalls (imap), sizeof (icall_names [0]), compare_method_imap);
+       const char **nameslot = mono_binary_search (name, icall_names + imap->first_icall, icall_desc_num_icalls (imap), sizeof (icall_names [0]), compare_method_imap);
        if (!nameslot)
                return NULL;
        return (gpointer)icall_functions [(nameslot - icall_names)];
@@ -7997,7 +7998,7 @@ compare_class_imap (const void *key, const void *elem)
 static const IcallTypeDesc*
 find_class_icalls (const char *name)
 {
-       const char **nameslot = bsearch (name, icall_type_names, Icall_type_num, sizeof (icall_type_names [0]), compare_class_imap);
+       const char **nameslot = mono_binary_search (name, icall_type_names, Icall_type_num, sizeof (icall_type_names [0]), compare_class_imap);
        if (!nameslot)
                return NULL;
        return &icall_type_descs [nameslot - icall_type_names];
@@ -8208,7 +8209,7 @@ mono_lookup_icall_symbol (MonoMethod *m)
        func = mono_lookup_internal_call (m);
        if (!func)
                return NULL;
-       slot = bsearch (func, functions_sorted, G_N_ELEMENTS (icall_functions), sizeof (gpointer), func_cmp);
+       slot = mono_binary_search (func, functions_sorted, G_N_ELEMENTS (icall_functions), sizeof (gpointer), func_cmp);
        if (!slot)
                return NULL;
        g_assert (slot);
index 23e63c5e733e20ae7d0b240391d7bff9a16b570c..1b8be8ade966d7f3dfd329fc13158f6fd0cac35a 100644 (file)
@@ -24,6 +24,7 @@
 #include <mono/metadata/locales.h>
 #include <mono/metadata/culture-info.h>
 #include <mono/metadata/culture-info-tables.h>
+#include <mono/utils/bsearch.h>
 
 #ifndef DISABLE_NORMALIZATION
 #include <mono/metadata/normalization-tables.h>
@@ -313,7 +314,7 @@ construct_culture_from_specific_name (MonoCultureInfo *ci, gchar *name)
 
        MONO_ARCH_SAVE_REGS;
 
-       ne = bsearch (name, culture_name_entries, NUM_CULTURE_ENTRIES,
+       ne = mono_binary_search (name, culture_name_entries, NUM_CULTURE_ENTRIES,
                        sizeof (CultureInfoNameEntry), culture_name_locator);
 
        if (ne == NULL)
@@ -332,7 +333,7 @@ culture_info_entry_from_lcid (int lcid)
 {
        const CultureInfoEntry *ci;
 
-       ci = bsearch (&lcid, culture_entries, NUM_CULTURE_ENTRIES, sizeof (CultureInfoEntry), culture_lcid_locator);
+       ci = mono_binary_search (&lcid, culture_entries, NUM_CULTURE_ENTRIES, sizeof (CultureInfoEntry), culture_lcid_locator);
 
        return ci;
 }
@@ -345,7 +346,7 @@ region_info_entry_from_lcid (int lcid)
 
        MONO_ARCH_SAVE_REGS;
 
-       ne = bsearch (&lcid, culture_entries, NUM_CULTURE_ENTRIES, sizeof (CultureInfoEntry), culture_lcid_locator);
+       ne = mono_binary_search (&lcid, culture_entries, NUM_CULTURE_ENTRIES, sizeof (CultureInfoEntry), culture_lcid_locator);
 
        if (ne == NULL)
                return FALSE;
@@ -537,7 +538,7 @@ ves_icall_System_Globalization_CultureInfo_construct_internal_locale_from_name (
        MONO_ARCH_SAVE_REGS;
 
        n = mono_string_to_utf8 (name);
-       ne = bsearch (n, culture_name_entries, NUM_CULTURE_ENTRIES,
+       ne = mono_binary_search (n, culture_name_entries, NUM_CULTURE_ENTRIES,
                        sizeof (CultureInfoNameEntry), culture_name_locator);
 
        if (ne == NULL) {
@@ -591,7 +592,7 @@ ves_icall_System_Globalization_RegionInfo_construct_internal_region_from_name (M
        MONO_ARCH_SAVE_REGS;
 
        n = mono_string_to_utf8 (name);
-       ne = bsearch (n, region_name_entries, NUM_REGION_ENTRIES,
+       ne = mono_binary_search (n, region_name_entries, NUM_REGION_ENTRIES,
                sizeof (RegionInfoNameEntry), region_name_locator);
 
        if (ne == NULL) {
index 94780bb0d5a469511d2ed0cb47b8f35996197f7e..adbe7a516216191a48abc935be7d87026464ed0b 100644 (file)
@@ -24,6 +24,7 @@
 #include <mono/metadata/attrdefs.h>
 #include <mono/utils/strenc.h>
 #include <mono/utils/mono-error-internals.h>
+#include <mono/utils/bsearch.h>
 #include <string.h>
 //#include <signal.h>
 #include <ctype.h>
@@ -1069,7 +1070,7 @@ search_sorted_table (VerifyContext *ctx, int table, int column, guint32 coded_to
        base = tinfo->base;
 
        VERIFIER_DEBUG ( printf ("looking token %x table %d col %d rsize %d roff %d\n", coded_token, table, column, locator.col_size, locator.col_offset) );
-       res = bsearch (&locator, base, tinfo->rows, tinfo->row_size, token_locator);
+       res = mono_binary_search (&locator, base, tinfo->rows, tinfo->row_size, token_locator);
        if (!res)
                return -1;
 
index a8db2e2d8939bb0f7026b100270316850f1137fb..eea4a8cd39213bb6ce18399f61e55a056722f5f0 100644 (file)
@@ -26,7 +26,8 @@
 #include "marshal.h"
 #include "debug-helpers.h"
 #include <mono/utils/mono-error-internals.h>
+#include <mono/utils/bsearch.h>
+
 /* Auxiliary structure used for caching inflated signatures */
 typedef struct {
        MonoMethodSignature *sig;
@@ -3917,7 +3918,7 @@ mono_metadata_typedef_from_field (MonoImage *meta, guint32 index)
        if (meta->uncompressed_metadata)
                loc.idx = search_ptr_table (meta, MONO_TABLE_FIELD_POINTER, loc.idx);
 
-       if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
+       if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
                return 0;
 
        /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
@@ -3948,7 +3949,7 @@ mono_metadata_typedef_from_method (MonoImage *meta, guint32 index)
        if (meta->uncompressed_metadata)
                loc.idx = search_ptr_table (meta, MONO_TABLE_METHOD_POINTER, loc.idx);
 
-       if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
+       if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
                return 0;
 
        /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
@@ -3990,7 +3991,7 @@ mono_metadata_interfaces_from_typedef_full (MonoImage *meta, guint32 index, Mono
        loc.col_idx = MONO_INTERFACEIMPL_CLASS;
        loc.t = tdef;
 
-       if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
+       if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
                return TRUE;
 
        start = loc.result;
@@ -4086,7 +4087,7 @@ mono_metadata_nested_in_typedef (MonoImage *meta, guint32 index)
        loc.col_idx = MONO_NESTED_CLASS_NESTED;
        loc.t = tdef;
 
-       if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
+       if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
                return 0;
 
        /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
@@ -4150,7 +4151,7 @@ mono_metadata_packing_from_typedef (MonoImage *meta, guint32 index, guint32 *pac
        loc.col_idx = MONO_CLASS_LAYOUT_PARENT;
        loc.t = tdef;
 
-       if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
+       if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
                return 0;
 
        mono_metadata_decode_row (tdef, loc.result, cols, MONO_CLASS_LAYOUT_SIZE);
@@ -4187,7 +4188,7 @@ mono_metadata_custom_attrs_from_index (MonoImage *meta, guint32 index)
 
        /* FIXME: Index translation */
 
-       if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
+       if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
                return 0;
 
        /* Find the first entry by searching backwards */
@@ -4220,7 +4221,7 @@ mono_metadata_declsec_from_index (MonoImage *meta, guint32 index)
        loc.col_idx = MONO_DECL_SECURITY_PARENT;
        loc.t = tdef;
 
-       if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, declsec_locator))
+       if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, declsec_locator))
                return -1;
 
        /* Find the first entry by searching backwards */
@@ -4934,7 +4935,7 @@ mono_metadata_field_info_full (MonoImage *meta, guint32 index, guint32 *offset,
                loc.col_idx = MONO_FIELD_LAYOUT_FIELD;
                loc.t = tdef;
 
-               if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
+               if (tdef->base && mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
                        *offset = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_LAYOUT_OFFSET);
                } else {
                        *offset = (guint32)-1;
@@ -4946,7 +4947,7 @@ mono_metadata_field_info_full (MonoImage *meta, guint32 index, guint32 *offset,
                loc.col_idx = MONO_FIELD_RVA_FIELD;
                loc.t = tdef;
                
-               if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
+               if (tdef->base && mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
                        /*
                         * LAMESPEC: There is no signature, no nothing, just the raw data.
                         */
@@ -5007,7 +5008,7 @@ mono_metadata_get_constant_index (MonoImage *meta, guint32 token, guint32 hint)
        if ((hint > 0) && (hint < tdef->rows) && (mono_metadata_decode_row_col (tdef, hint - 1, MONO_CONSTANT_PARENT) == index))
                return hint;
 
-       if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
+       if (tdef->base && mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
                return loc.result + 1;
        }
        return 0;
@@ -5038,7 +5039,7 @@ mono_metadata_events_from_typedef (MonoImage *meta, guint32 index, guint *end_id
        loc.col_idx = MONO_EVENT_MAP_PARENT;
        loc.idx = index + 1;
 
-       if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
+       if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
                return 0;
        
        start = mono_metadata_decode_row_col (tdef, loc.result, MONO_EVENT_MAP_EVENTLIST);
@@ -5080,7 +5081,7 @@ mono_metadata_methods_from_event   (MonoImage *meta, guint32 index, guint *end_i
        loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
        loc.idx = ((index + 1) << MONO_HAS_SEMANTICS_BITS) | MONO_HAS_SEMANTICS_EVENT; /* Method association coded index */
 
-       if (!bsearch (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
+       if (!mono_binary_search (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
                return 0;
 
        start = loc.result;
@@ -5129,7 +5130,7 @@ mono_metadata_properties_from_typedef (MonoImage *meta, guint32 index, guint *en
        loc.col_idx = MONO_PROPERTY_MAP_PARENT;
        loc.idx = index + 1;
 
-       if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
+       if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
                return 0;
        
        start = mono_metadata_decode_row_col (tdef, loc.result, MONO_PROPERTY_MAP_PROPERTY_LIST);
@@ -5171,7 +5172,7 @@ mono_metadata_methods_from_property   (MonoImage *meta, guint32 index, guint *en
        loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
        loc.idx = ((index + 1) << MONO_HAS_SEMANTICS_BITS) | MONO_HAS_SEMANTICS_PROPERTY; /* Method association coded index */
 
-       if (!bsearch (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
+       if (!mono_binary_search (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
                return 0;
 
        start = loc.result;
@@ -5210,7 +5211,7 @@ mono_metadata_implmap_from_method (MonoImage *meta, guint32 method_idx)
        loc.col_idx = MONO_IMPLMAP_MEMBER;
        loc.idx = ((method_idx + 1) << MONO_MEMBERFORWD_BITS) | MONO_MEMBERFORWD_METHODDEF;
 
-       if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
+       if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
                return 0;
 
        return loc.result + 1;
@@ -5594,7 +5595,7 @@ mono_metadata_get_marshal_info (MonoImage *meta, guint32 idx, gboolean is_field)
 
        /* FIXME: Index translation */
 
-       if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
+       if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
                return NULL;
 
        return mono_metadata_blob_heap (meta, mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_MARSHAL_NATIVE_TYPE));
@@ -5647,7 +5648,7 @@ mono_class_get_overrides_full (MonoImage *image, guint32 type_token, MonoMethod
        loc.col_idx = MONO_METHODIMPL_CLASS;
        loc.idx = mono_metadata_token_index (type_token);
 
-       if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
+       if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
                return TRUE;
 
        start = loc.result;
@@ -5787,7 +5788,7 @@ mono_metadata_get_generic_param_row (MonoImage *image, guint32 token, guint32 *o
        loc.col_idx = MONO_GENERICPARAM_OWNER;
        loc.t = tdef;
 
-       if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
+       if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
                return 0;
 
        /* Find the first entry by searching backwards */
index 24334ce1b73326ac705d3b115d5123f7544324de..ff737bbe9347a7b0f5d2fce3e44832386bd9c356 100644 (file)
@@ -12,6 +12,7 @@
 
 #include "mini.h"
 #include "ir-emit.h"
+#include "mono/utils/bsearch.h"
 
 /*
 General notes on SIMD intrinsics
@@ -1477,7 +1478,7 @@ simd_version_name (guint32 version)
 static MonoInst*
 emit_intrinsics (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args, const SimdIntrinsc *intrinsics, guint32 size)
 {
-       const SimdIntrinsc * result = bsearch (cmethod->name, intrinsics, size, sizeof (SimdIntrinsc), &simd_intrinsic_compare_by_name);
+       const SimdIntrinsc * result = mono_binary_search (cmethod->name, intrinsics, size, sizeof (SimdIntrinsc), &simd_intrinsic_compare_by_name);
        if (!result) {
                DEBUG (printf ("function doesn't have a simd intrinsic %s::%s/%d\n", cmethod->klass->name, cmethod->name, fsig->param_count));
                return NULL;
index 8bd47d30abfda76d8cf81f34fb5a1ad56d7448ff..8eefcda5ca5498bc7414f6f58863d1e1e28451b8 100644 (file)
@@ -105,7 +105,9 @@ monoutils_sources = \
        atomic.h        \
        atomic.c        \
        mono-hwcap.h    \
-       mono-hwcap.c
+       mono-hwcap.c    \
+       bsearch.h       \
+       bsearch.c
 
 arch_sources = 
 
diff --git a/mono/utils/bsearch.c b/mono/utils/bsearch.c
new file mode 100644 (file)
index 0000000..47be444
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+ * bsearch () implementation. Needed because some broken platforms
+ * have implementations that have unreasonable, non-standard
+ * requirements (e.g. "key must not be null"). Taken from NetBSD
+ * with some minor modifications.
+ *
+ * Copyright (c) 1990, 1993
+ *     The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include "mono/utils/bsearch.h"
+
+void *
+mono_binary_search (
+       const void *key,
+       const void *array,
+       size_t array_length,
+       size_t member_size,
+       BinarySearchComparer comparer)
+{
+       const char *base = array;
+       size_t lim;
+       int cmp;
+       const void *p;
+
+       for (lim = array_length; lim; lim >>= 1) {
+               p = base + (lim >> 1) * member_size;
+               cmp = (* comparer) (key, p);
+
+               if (!cmp)
+                       return (void *) p;
+               else if (cmp > 0) {
+                       base = (const char *) p + member_size;
+                       lim--;
+               }
+       }
+
+       return NULL;
+}
diff --git a/mono/utils/bsearch.h b/mono/utils/bsearch.h
new file mode 100644 (file)
index 0000000..82137ef
--- /dev/null
@@ -0,0 +1,18 @@
+#ifndef __MONO_BSEARCH_H__
+#define __MONO_BSEARCH_H__
+
+#include <stdlib.h>
+
+#include "mono/utils/mono-compiler.h"
+
+typedef int (* BinarySearchComparer) (const void *key, const void *member);
+
+void *
+mono_binary_search (
+       const void *key,
+       const void *array,
+       size_t array_length,
+       size_t member_size,
+       BinarySearchComparer comparer);
+
+#endif
index 7511509ac8a43bc91a5cf24e5d64165af21b2fe0..4a98808419078f1c076815c55e53613ef62cdb2f 100644 (file)
@@ -65,6 +65,7 @@
     <ClCompile Include="..\mono\utils\atomic.c" />\r
     <ClCompile Include="..\mono\utils\mono-hwcap.c" />\r
     <ClCompile Include="..\mono\utils\mono-hwcap-x86.c" />\r
+    <ClCompile Include="..\mono\utils\bsearch.c" />\r
   </ItemGroup>\r
   <ItemGroup>\r
     <ClInclude Include="..\mono\utils\dlmalloc.h" />\r
     <ClInclude Include="..\mono\utils\atomic.h" />\r
     <ClInclude Include="..\mono\utils\mono-hwcap.h" />\r
     <ClInclude Include="..\mono\utils\mono-hwcap-x86.h" />\r
+    <ClInclude Include="..\mono\utils\bsearch.h" />\r
   </ItemGroup>\r
   <PropertyGroup Label="Globals">\r
     <ProjectGuid>{8FC2B0C8-51AD-49DF-851F-5D01A77A75E4}</ProjectGuid>\r
index 49ed549c5d2f546ffeffd31a2e3b8289fa32af1a..21fb4abca03ec13a14078a10299d4ef9bddd166a 100644 (file)
@@ -19,6 +19,7 @@
 #include "mono/metadata/object.h"
 #include "mono/metadata/tabledefs.h"
 #include "mono/io-layer/wapi.h"
+#include "mono/utils/bsearch.h"
 
 typedef struct {
        const char *fname;
@@ -49,7 +50,7 @@ get_function (const char *name)
 {
        FnPtr *ptr;
 
-       ptr = bsearch (name, functions, NFUNCTIONS, sizeof (FnPtr),
+       ptr = mono_binary_search (name, functions, NFUNCTIONS, sizeof (FnPtr),
                        compare_names);
 
        if (ptr == NULL) {
@@ -67,7 +68,7 @@ supportw_register_delegate (const char *function_name, void *fnptr)
 
        g_return_val_if_fail (function_name && fnptr, FALSE);
 
-       ptr = bsearch (function_name, functions, NFUNCTIONS, sizeof (FnPtr),
+       ptr = mono_binary_search (function_name, functions, NFUNCTIONS, sizeof (FnPtr),
                        compare_names);
 
        if (ptr == NULL) {