#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);
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;
{
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
#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
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;
#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;
/*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,
#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
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);
#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>
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])];
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]];
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)];
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];
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);
#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>
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)
{
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;
}
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;
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) {
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) {
#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>
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;
#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;
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) */
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) */
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;
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) */
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);
/* 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 */
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 */
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;
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.
*/
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;
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);
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;
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);
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;
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;
/* 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));
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;
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 */
#include "mini.h"
#include "ir-emit.h"
+#include "mono/utils/bsearch.h"
/*
General notes on SIMD intrinsics
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;
atomic.h \
atomic.c \
mono-hwcap.h \
- mono-hwcap.c
+ mono-hwcap.c \
+ bsearch.h \
+ bsearch.c
arch_sources =
--- /dev/null
+/*
+ * 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;
+}
--- /dev/null
+#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
<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
#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;
{
FnPtr *ptr;
- ptr = bsearch (name, functions, NFUNCTIONS, sizeof (FnPtr),
+ ptr = mono_binary_search (name, functions, NFUNCTIONS, sizeof (FnPtr),
compare_names);
if (ptr == NULL) {
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) {