xref: /reactos/sdk/lib/ucrt/stdlib/lsearch.cpp (revision a6a07059)
1 //
2 // lsearch.cpp
3 //
4 //      Copyright (c) Microsoft Corporation. All rights reserved.
5 //
6 // Defines _lsearch(), which performs a linear search over an array, appending
7 // the key to the end of the array if it is not found.
8 //
9 #include <corecrt_internal.h>
10 #include <memory.h>
11 #include <search.h>
12 
13 #ifdef _M_CEE
14     #define __fileDECL __clrcall
15 #else
16     #define __fileDECL __cdecl
17 #endif
18 
19 
20 
21 #ifdef __USE_CONTEXT
22     #define __COMPARE(context, p1, p2) (*compare)(context, p1, p2)
23 #else
24     #define __COMPARE(context, p1, p2) (*compare)(p1, p2)
25 #endif
26 
27 
28 // Performs a linear search over the array, looking for the value 'key' in an
29 // array of 'num' elements of 'width' bytes in size.  Returns a pointer to the
30 // matching element if found.  Otherwise, adds a new element to the end of the
31 // array, copied from 'key'.
32 //
33 // Parameters:
34 //  * key:   The key for which to search
35 //  * base:  A pointer to the initial element of the array to be searched
36 //  * num:   A pointer to an integer containing the number of elements in the
37 //           array.  If a new element is appended, this function increments the
38 //           pointed-to value.
39 //  * width: The size of each element, in bytes.
40 //  * comp:  Pointer to a function returning analog of strcmp for strings, but
41 //           supplied by the caller for comparing the array elements.  It
42 //           accepts two pointers to elements; returns negative if 1 < 2;
43 //           zero if 1 == 2, and positive if 1 > 2.
44 #ifndef _M_CEE
45 extern "C"
46 #endif
47 #ifdef __USE_CONTEXT
48 void* __fileDECL _lsearch_s(
49     void const*   const key,
50     void*         const base,
51     unsigned int* const num,
52     size_t        const width,
53     int (__fileDECL* compare)(void*, void const*, void const*),
54     void*         const context
55     )
56 #else // __USE_CONTEXT
57 void* __fileDECL _lsearch(
58     const void*   const key,
59     void*         const base,
60     unsigned int* const num,
61     unsigned int  const width,
62     int (__fileDECL* compare)(void const*, void const*)
63     )
64 #endif // __USE_CONTEXT
65 {
66     _VALIDATE_RETURN(key != nullptr, EINVAL, nullptr);
67     _VALIDATE_RETURN(num != nullptr, EINVAL, nullptr);
68     _VALIDATE_RETURN(base != nullptr, EINVAL, nullptr);
69     _VALIDATE_RETURN(width > 0, EINVAL, nullptr);
70     _VALIDATE_RETURN(compare != nullptr, EINVAL, nullptr);
71 
72     char* const first = static_cast<char*>(base);
73     char* const last  = first + *num * width;
74 
75     // Reentrancy diligence: Save (and unset) global-state mode to the stack before making callout to 'compare'
76     __crt_state_management::scoped_global_state_reset saved_state;
77 
78     for (char* p = first; p != last; p += width)
79     {
80         if (__COMPARE(context, key, p) == 0)
81         {
82             return p;
83         }
84     }
85 
86     memcpy(last, key, width);
87     ++(*num);
88     return last;
89 }
90 
91 #undef __COMPARE
92