Skip to content

Commit 6837728

Browse files
committed
[libc][search] implement posix lsearch function
1 parent efc6d33 commit 6837728

File tree

11 files changed

+180
-1
lines changed

11 files changed

+180
-1
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,7 @@ if(LLVM_LIBC_FULL_BUILD)
943943
libc.src.search.hsearch_r
944944
libc.src.search.insque
945945
libc.src.search.remque
946+
libc.src.search.lsearch
946947

947948
# threads.h entrypoints
948949
libc.src.threads.call_once

libc/config/linux/riscv/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,7 @@ if(LLVM_LIBC_FULL_BUILD)
873873
libc.src.search.hsearch_r
874874
libc.src.search.insque
875875
libc.src.search.remque
876+
libc.src.search.lsearch
876877

877878
# threads.h entrypoints
878879
libc.src.threads.call_once

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,7 @@ if(LLVM_LIBC_FULL_BUILD)
10061006
libc.src.search.hsearch_r
10071007
libc.src.search.insque
10081008
libc.src.search.remque
1009+
libc.src.search.lsearch
10091010

10101011
# threads.h entrypoints
10111012
libc.src.threads.call_once

libc/docs/libc_search.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ hdestroy |check|
4343
hsearch |check|
4444
insque |check|
4545
lfind
46-
lsearch
46+
lsearch |check|
4747
remque |check|
4848
tdelete
4949
tfind

libc/newhdrgen/yaml/search.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ functions:
3636
arguments:
3737
- type: ENTRY
3838
- type: ACTION
39+
- name: lsearch
40+
standards:
41+
- POSIX
42+
return_type: void *
43+
arguments:
44+
- type: void *
45+
- type: void *
46+
- type: size_t *
47+
- type: size_t
48+
- type: int(*compar)(const void *, const void *)
3949
- name: hsearch_r
4050
standards: GNUExtensions
4151
return_type: int

libc/spec/posix.td

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,6 +1618,17 @@ def POSIX : StandardSpec<"POSIX"> {
16181618
ArgSpec<VoidPtr>
16191619
]
16201620
>,
1621+
FunctionSpec<
1622+
"lsearch",
1623+
RetValSpec<VoidPtr>,
1624+
[
1625+
ArgSpec<VoidPtr>,
1626+
ArgSpec<VoidPtr>,
1627+
ArgSpec<SizeTPtr>,
1628+
ArgSpec<SizeTType>,
1629+
// TODO: Unsure how to specify int(*compar)(void *, void *)
1630+
]
1631+
>
16211632
]
16221633
>;
16231634

libc/src/search/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ add_entrypoint_object(
2727
libc.include.search
2828
)
2929

30+
add_entrypoint_object(
31+
lsearch
32+
SRCS
33+
lsearch.cpp
34+
HDRS
35+
lsearch.h
36+
DEPENDS
37+
libc.include.search
38+
libc.src.__support.CPP.cstddef
39+
libc.src.string.memory_utils.inline_memcpy
40+
)
41+
3042
add_entrypoint_object(
3143
hsearch
3244
SRCS

libc/src/search/lsearch.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===-- Implementation of lsearch -------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/search/lsearch.h"
10+
#include "src/__support/CPP/cstddef.h" // cpp::byte
11+
#include "src/__support/common.h"
12+
#include "src/__support/macros/config.h"
13+
#include "src/string/memory_utils/inline_memcpy.h" // inline_memcpy
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
LLVM_LIBC_FUNCTION(void *, lsearch,
17+
(void *key, void *base, size_t *nmemb, size_t size,
18+
int (*compar)(void *, void *))) {
19+
cpp::byte *next = reinterpret_cast<cpp::byte *>(base);
20+
cpp::byte *end = next + (*nmemb * size);
21+
while (next < end) {
22+
if (compar(key, next) == 0) {
23+
return next;
24+
}
25+
next += size;
26+
}
27+
28+
inline_memcpy(next, key, size);
29+
*nmemb += 1;
30+
return next;
31+
}
32+
33+
} // namespace LIBC_NAMESPACE_DECL

libc/src/search/lsearch.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header for lsearch -----------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_SEARCH_LSEARCH_H
10+
#define LLVM_LIBC_SRC_SEARCH_LSEARCH_H
11+
12+
#include "src/__support/macros/config.h"
13+
#include <search.h> // size_t
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
void *lsearch(void *key, void *base, size_t *nmemb, size_t size,
17+
int (*compar)(void *, void *));
18+
} // namespace LIBC_NAMESPACE_DECL
19+
20+
#endif // LLVM_LIBC_SRC_SEARCH_LSEARCH_H

libc/test/src/search/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,14 @@ add_libc_unittest(
2525
libc.src.search.insque
2626
libc.src.search.remque
2727
)
28+
29+
add_libc_unittest(
30+
lsearch_test
31+
SUITE
32+
libc_search_unittests
33+
SRCS
34+
lsearch_test.cpp
35+
DEPENDS
36+
libc.src.search.lsearch
37+
)
38+

0 commit comments

Comments
 (0)