-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[libc][search] implement posix lfind
function
#114692
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
66c36e8
[libc][search] implement posix `lfind` function
duncpro 5946873
replace tabs with spaces
duncpro 5b4b7ca
add const qualifiers to parameters to match the posix standard
duncpro e1f3019
introduce__lsearchcompare_t predicate
duncpro 3e3da44
change from subtraction to != in test compar function
duncpro d615035
#include <search.h> -> #include <stddef.h>
duncpro 23d045e
catch overflow when calculating byte_len
duncpro 7cca8d8
never dereference a nullptr
duncpro 2f523e9
clang-format
duncpro 1326922
VoidPtr -> ConstVoidPtr
duncpro 9421214
replace while loop with for loop
duncpro 757e1c9
Remove unnecessary braces
duncpro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
//===-- Definition of type __lsearchcompare_t -----------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_LIBC_TYPES___LSEARCHCOMPARE_T_H | ||
#define LLVM_LIBC_TYPES___LSEARCHCOMPARE_T_H | ||
|
||
typedef int (*__lsearchcompare_t)(const void *, const void *); | ||
|
||
#endif // LLVM_LIBC_TYPES___LSEARCHCOMPARE_T_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
//===-- Implementation of lfind -------------------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "src/search/lfind.h" | ||
#include "src/__support/CPP/cstddef.h" // cpp::byte | ||
#include "src/__support/common.h" | ||
#include "src/__support/macros/config.h" | ||
#include "src/__support/memory_size.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
LLVM_LIBC_FUNCTION(void *, lfind, | ||
(const void *key, const void *base, size_t *nmemb, | ||
size_t size, int (*compar)(const void *, const void *))) { | ||
if (key == nullptr || base == nullptr || nmemb == nullptr || | ||
compar == nullptr) | ||
return nullptr; | ||
|
||
size_t byte_len = 0; | ||
if (internal::mul_overflow(*nmemb, size, &byte_len)) | ||
return nullptr; | ||
|
||
const cpp::byte *next = reinterpret_cast<const cpp::byte *>(base); | ||
const cpp::byte *end = next + byte_len; | ||
for (; next < end; next += size) { | ||
if (compar(key, next) == 0) | ||
return const_cast<cpp::byte *>(next); | ||
} | ||
duncpro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return nullptr; | ||
} | ||
|
||
} // namespace LIBC_NAMESPACE_DECL |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
//===-- Implementation header for lfind -------------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_LIBC_SRC_SEARCH_LFIND_H | ||
#define LLVM_LIBC_SRC_SEARCH_LFIND_H | ||
|
||
#include "src/__support/macros/config.h" | ||
#include <stddef.h> // size_t | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
void *lfind(const void *key, const void *base, size_t *nmemb, size_t size, | ||
int (*compar)(const void *, const void *)); | ||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
#endif // LLVM_LIBC_SRC_SEARCH_LFIND_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
//===-- Unittests for lfind -----------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "src/search/lfind.h" | ||
#include "test/UnitTest/Test.h" | ||
|
||
int compar(const void *a, const void *b) { | ||
return *reinterpret_cast<const int *>(a) != *reinterpret_cast<const int *>(b); | ||
} | ||
|
||
TEST(LlvmLibcLfindTest, SearchHead) { | ||
int list[3] = {1, 2, 3}; | ||
size_t len = 3; | ||
nickdesaulniers marked this conversation as resolved.
Show resolved
Hide resolved
|
||
int key = 1; | ||
void *ret = LIBC_NAMESPACE::lfind(&key, list, &len, sizeof(int), compar); | ||
ASSERT_TRUE(ret == &list[0]); | ||
} | ||
|
||
TEST(LlvmLibcLfindTest, SearchMiddle) { | ||
int list[3] = {1, 2, 3}; | ||
size_t len = 3; | ||
nickdesaulniers marked this conversation as resolved.
Show resolved
Hide resolved
|
||
int key = 2; | ||
void *ret = LIBC_NAMESPACE::lfind(&key, list, &len, sizeof(int), compar); | ||
ASSERT_TRUE(ret == &list[1]); | ||
} | ||
|
||
TEST(LlvmLibcLfindTest, SearchTail) { | ||
int list[3] = {1, 2, 3}; | ||
size_t len = 3; | ||
nickdesaulniers marked this conversation as resolved.
Show resolved
Hide resolved
|
||
int key = 3; | ||
void *ret = LIBC_NAMESPACE::lfind(&key, list, &len, sizeof(int), compar); | ||
ASSERT_TRUE(ret == &list[2]); | ||
} | ||
|
||
TEST(LlvmLibcLfindTest, SearchNonExistent) { | ||
int list[3] = {1, 2, 3}; | ||
size_t len = 3; | ||
nickdesaulniers marked this conversation as resolved.
Show resolved
Hide resolved
|
||
int key = 5; | ||
void *ret = LIBC_NAMESPACE::lfind(&key, list, &len, sizeof(int), compar); | ||
ASSERT_TRUE(ret == nullptr); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.