ReactOS 0.4.16-dev-959-g2ec3a19
bsearch.cpp File Reference
#include <corecrt_internal.h>
#include <search.h>
Include dependency graph for bsearch.cpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define __fileDECL   __cdecl
 
#define __COMPARE(context, p1, p2)   (*compare)(p1, p2)
 

Functions

_CRT_SECURITYSAFECRITICAL_ATTRIBUTE void *__fileDECL bsearch (void const *const key, void const *const base, size_t num, size_t const width, int(__fileDECL *const compare)(void const *, void const *))
 

Macro Definition Documentation

◆ __COMPARE

#define __COMPARE (   context,
  p1,
  p2 
)    (*compare)(p1, p2)

Definition at line 47 of file bsearch.cpp.

◆ __fileDECL

#define __fileDECL   __cdecl

Definition at line 14 of file bsearch.cpp.

Function Documentation

◆ bsearch()

Definition at line 65 of file bsearch.cpp.

73{
74 _VALIDATE_RETURN(base != nullptr || num == 0, EINVAL, nullptr);
75 _VALIDATE_RETURN(width > 0, EINVAL, nullptr);
76 _VALIDATE_RETURN(compare != nullptr, EINVAL, nullptr);
77
78 char const* lo = reinterpret_cast<char const*>(base);
79 char const* hi = reinterpret_cast<char const*>(base) + (num - 1) * width;
80
81 // Reentrancy diligence: Save (and unset) global-state mode to the stack before making callout to 'compare'
82 __crt_state_management::scoped_global_state_reset saved_state;
83
84 // We allow a nullptr key here because it breaks some older code and because
85 // we do not dereference this ourselves so we can't be sure that it's a
86 // problem for the comparison function
87
88 while (lo <= hi)
89 {
90 size_t const half = num / 2;
91 if (half != 0)
92 {
93 char const* const mid = lo + (num & 1 ? half : (half - 1)) * width;
94
95 int const result = __COMPARE(context, key, mid);
96 if (result == 0)
97 {
98 return const_cast<void*>(static_cast<void const*>(mid));
99 }
100 else if (result < 0)
101 {
102 hi = mid - width;
103 num = num & 1 ? half : half - 1;
104 }
105 else
106 {
107 lo = mid + width;
108 num = half;
109 }
110 }
111 else if (num != 0)
112 {
113 return __COMPARE(context, key, lo)
114 ? nullptr
115 : const_cast<void*>(static_cast<void const*>(lo));
116 }
117 else
118 {
119 break;
120 }
121 }
122
123 return nullptr;
124}
#define EINVAL
Definition: acclib.h:90
#define __COMPARE(context, p1, p2)
Definition: bsearch.cpp:47
#define _VALIDATE_RETURN(expr, errorcode, retexpr)
return nullptr
Definition: expand.cpp:78
GLint GLint GLsizei width
Definition: gl.h:1546
GLuint64EXT * result
Definition: glext.h:11304
GLuint GLuint num
Definition: glext.h:9618
Definition: bug.cpp:8
Definition: http.c:7252
Definition: copy.c:22
#define const
Definition: zconf.h:233