Skip to content

Commit

Permalink
Merge pull request #1063 from Qwinci/lfs64-additions
Browse files Browse the repository at this point in the history
Lfs64 additions, hsearch family of functions and icmp6 define
  • Loading branch information
qookei authored May 27, 2024
2 parents 45a3926 + e88c79c commit f1b7b83
Show file tree
Hide file tree
Showing 22 changed files with 283 additions and 2 deletions.
2 changes: 2 additions & 0 deletions abis/linux/fcntl.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@

#define F_GETLK 5
#define F_SETLK 6
#define F_SETLK64 F_SETLK
#define F_SETLKW 7
#define F_SETLKW64 F_SETLKW

#define F_SETOWN_EX 15
#define F_GETOWN_EX 16
Expand Down
2 changes: 2 additions & 0 deletions abis/mlibc/fcntl.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@
#define F_SETFL 6
#define F_GETLK 7
#define F_SETLK 8
#define F_SETLK64 F_SETLK
#define F_SETLKW 9
#define F_SETLKW64 F_SETLKW
#define F_GETOWN 10
#define F_SETOWN 11

Expand Down
2 changes: 2 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ internal_sources = [
'options/internal/generic/strings.cpp',
'options/internal/generic/ubsan.cpp',
'options/internal/generic/threads.cpp',
'options/internal/generic/search.cpp',
'options/internal/gcc/stack_protector.cpp',
'options/internal/gcc/guard-abi.cpp',
'options/internal/gcc/initfini.cpp',
Expand Down Expand Up @@ -391,6 +392,7 @@ if not no_headers
'options/internal/include/bits/cpu_set.h',
'options/internal/include/bits/threads.h',
'options/internal/include/bits/winsize.h',
'options/internal/include/bits/search.h',
subdir: 'bits'
)
endif
Expand Down
14 changes: 14 additions & 0 deletions options/glibc/generic/search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <bits/glibc/glibc_search.h>
#include <mlibc/search.hpp>

int hcreate_r(size_t num_entries, hsearch_data *htab) {
return mlibc::hcreate_r(num_entries, htab);
}

void hdestroy_r(hsearch_data *htab) {
mlibc::hdestroy_r(htab);
}

int hsearch_r(ENTRY item, ACTION action, ENTRY **ret, hsearch_data *htab) {
return mlibc::hsearch_r(item, action, ret, htab);
}
22 changes: 22 additions & 0 deletions options/glibc/include/bits/glibc/glibc_search.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef _GLIBC_SEARCH_H
#define _GLIBC_SEARCH_H

#ifdef __cplusplus
extern "C" {
#endif

#include <bits/search.h>

#ifndef __MLIBC_ABI_ONLY

int hcreate_r(size_t num_entries, struct hsearch_data *htab);
void hdestroy_r(struct hsearch_data *htab);
int hsearch_r(ENTRY item, ACTION action, ENTRY **ret, struct hsearch_data *htab);

#endif

#ifdef __cplusplus
}
#endif

#endif /* _GLIBC_SEARCH_H */
2 changes: 2 additions & 0 deletions options/glibc/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ libc_sources += files(
'generic/glibc-assert.cpp',
'generic/malloc.cpp',
'generic/sys-io.cpp',
'generic/search.cpp'
)

if not no_headers
Expand Down Expand Up @@ -84,6 +85,7 @@ if not no_headers
'include/bits/glibc/glibc_assert.h',
'include/bits/glibc/glibc_malloc.h',
'include/bits/glibc/glibc_icmp6.h',
'include/bits/glibc/glibc_search.h',
subdir: 'bits/glibc'
)
endif
Expand Down
98 changes: 98 additions & 0 deletions options/internal/generic/search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#include <mlibc/search.hpp>
#include <frg/string.hpp>
#include <stdlib.h>
#include <stdint.h>
#include <errno.h>

struct _ENTRY {
ENTRY entry;
bool used;
};

namespace mlibc {

int hcreate_r(size_t num_entries, struct hsearch_data *htab) {
if(!htab) {
errno = EINVAL;
return 0;
}

htab->table = static_cast<_ENTRY*>(calloc(num_entries, sizeof(_ENTRY)));
if(!htab->table) {
errno = ENOMEM;
return 0;
}
htab->filled = 0;
htab->size = num_entries;
return 1;
}

void hdestroy_r(struct hsearch_data *htab) {
if(!htab) {
errno = EINVAL;
return;
}
free(htab->table);
htab->table = nullptr;
htab->size = 0;
htab->filled = 0;
}


int hsearch_r(ENTRY item, ACTION action, ENTRY **ret, struct hsearch_data *htab) {
auto key = frg::string_view{item.key};
auto hash = frg::hash<frg::string_view>{}(key);

size_t bucket_index = hash % htab->size;
size_t start = bucket_index;
while(true) {
auto &bucket = htab->table[bucket_index];

if(bucket.used) {
if(bucket.entry.key == key) {
*ret = &bucket.entry;
return 1;
}
} else if(action == FIND) {
errno = ESRCH;
*ret = nullptr;
return 0;
}

bucket_index = (bucket_index + 1) % htab->size;

if(bucket_index == start) {
if(action == FIND) {
errno = ESRCH;
*ret = nullptr;
return 0;
} else {
break;
}
}
}

// insert a new entry.
if(htab->size == htab->filled) {
errno = ENOMEM;
return 0;
}
++htab->filled;

bucket_index = start;
while(true) {
auto &bucket = htab->table[bucket_index];
if(!bucket.used) {
bucket.used = true;
bucket.entry = item;
*ret = &bucket.entry;
break;
}

bucket_index = (bucket_index + 1) % htab->size;
}

return 1;
}

} // namespace mlibc
24 changes: 24 additions & 0 deletions options/internal/include/bits/search.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef _MLIBC_INTERNAL_SEARCH_H
#define _MLIBC_INTERNAL_SEARCH_H

#include <bits/size_t.h>

typedef enum {
FIND,
ENTER
} ACTION;

typedef struct entry {
char *key;
void *data;
} ENTRY;

struct _ENTRY;

struct hsearch_data {
struct _ENTRY *table;
unsigned int size;
unsigned int filled;
};

#endif /* _MLIBC_INTERNAL_SEARCH_H */
14 changes: 14 additions & 0 deletions options/internal/include/mlibc/search.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef MLIBC_SEARCH
#define MLIBC_SEARCH

#include <bits/search.h>

namespace mlibc {

int hcreate_r(size_t num_entries, struct hsearch_data *htab);
void hdestroy_r(struct hsearch_data *htab);
int hsearch_r(ENTRY item, ACTION action, ENTRY **ret, struct hsearch_data *htab);

} // namespace mlibc

#endif // MLIBC_SEARCH
2 changes: 2 additions & 0 deletions options/linux/generic/sys-statfs-stubs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ int fstatfs(int fd, struct statfs *buf) {
return 0;
}

[[gnu::alias("fstatfs")]] int fstatfs64(int, struct statfs64 *);

1 change: 1 addition & 0 deletions options/linux/include/sys/statfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ extern "C" {

int statfs(const char *, struct statfs *);
int fstatfs(int, struct statfs *);
int fstatfs64(int, struct statfs64 *);

#endif /* !__MLIBC_ABI_ONLY */

Expand Down
21 changes: 21 additions & 0 deletions options/posix/generic/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <stddef.h>
#include <new>
#include <mlibc/allocator.hpp>
#include <mlibc/search.hpp>
#include <frg/stack.hpp>
#include <stdlib.h>

Expand Down Expand Up @@ -162,3 +163,23 @@ void *lfind(const void *key, const void *base, size_t *nelp,
__ensure(!"Not implemented");
__builtin_unreachable();
}

namespace {
hsearch_data globalTable {};
}

int hcreate(size_t num_entries) {
return mlibc::hcreate_r(num_entries, &globalTable);
}

void hdestroy(void) {
mlibc::hdestroy_r(&globalTable);
}

ENTRY *hsearch(ENTRY item, ACTION action) {
ENTRY *ret;
if(mlibc::hsearch_r(item, action, &ret, &globalTable) == 0) {
return nullptr;
}
return ret;
}
2 changes: 2 additions & 0 deletions options/posix/generic/sys-file-stubs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ int flock(int fd, int opt) {
return 0;
}

[[gnu::alias("flock")]] int flock64(int fd, int opt);

4 changes: 3 additions & 1 deletion options/posix/generic/sys-statvfs-stubs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ int statvfs(const char *path, struct statvfs *out) {
return 0;
}

[[gnu::alias("statvfs")]] int statvfs64(const char *path, struct statvfs *out);
[[gnu::alias("statvfs")]] int statvfs64(const char *path, struct statvfs64 *out);

int fstatvfs(int fd, struct statvfs *out) {
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_fstatvfs, -1);
Expand All @@ -24,3 +24,5 @@ int fstatvfs(int fd, struct statvfs *out) {
return 0;
}

[[gnu::alias("fstatvfs")]] int fstatvfs64(int, struct statvfs64 *);

4 changes: 4 additions & 0 deletions options/posix/generic/unistd-stubs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,8 @@ ssize_t pread(int fd, void *buf, size_t n, off_t off) {
return num_read;
}

[[gnu::alias("pread")]] ssize_t pread64(int fd, void *buf, size_t n, off_t off);

ssize_t pwrite(int fd, const void *buf, size_t n, off_t off) {
ssize_t num_written;

Expand All @@ -549,6 +551,8 @@ ssize_t pwrite(int fd, const void *buf, size_t n, off_t off) {
return num_written;
}

[[gnu::alias("pwrite")]] ssize_t pwrite64(int fd, const void *buf, size_t n, off_t off);

ssize_t readlink(const char *__restrict path, char *__restrict buffer, size_t max_size) {
ssize_t length;
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_readlink, -1);
Expand Down
11 changes: 11 additions & 0 deletions options/posix/include/fcntl.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ extern "C" {

#define O_NDELAY O_NONBLOCK

/* WARNING: keep `flock` and `flock64` in sync or bad things will happen! */

struct flock {
short l_type;
short l_whence;
Expand All @@ -25,12 +27,21 @@ struct flock {
pid_t l_pid;
};

struct flock64 {
short l_type;
short l_whence;
off_t l_start;
off_t l_len;
pid_t l_pid;
};

#ifndef __MLIBC_ABI_ONLY

int creat(const char *, mode_t);
int fallocate(int fd, int mode, off_t offset, off_t len);
int fcntl(int fd, int command, ...);
int open(const char *path, int flags, ...);
int open64(const char *path, int flags, ...);
int openat(int, const char *, int, ...);
int posix_fadvise(int, off_t, off_t, int);
int posix_fallocate(int, off_t, off_t);
Expand Down
1 change: 1 addition & 0 deletions options/posix/include/netinet/icmp6.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ extern "C" {
#define ICMP6_FILTER_BLOCKOTHERS 3
#define ICMP6_FILTER_PASSONLY 4
#define ICMP6_ECHO_REQUEST 128
#define ICMP6_ECHO_REPLY 129

struct icmp6_filter {
uint32_t icmp6_filt[8];
Expand Down
10 changes: 10 additions & 0 deletions options/posix/include/search.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#define _SEARCH_H

#include <stddef.h>
#include <bits/search.h>
#include <mlibc-config.h>

#ifdef __cplusplus
extern "C" {
Expand All @@ -15,6 +17,10 @@ typedef enum {
leaf
} VISIT;

#if __MLIBC_GLIBC_OPTION && defined(_GNU_SOURCE)
#include <bits/glibc/glibc_search.h>
#endif

#ifndef __MLIBC_ABI_ONLY

void *tsearch(const void *, void **, int(*compar)(const void *, const void *));
Expand All @@ -28,6 +34,10 @@ void *lsearch(const void *key, void *base, size_t *nelp, size_t width,
void *lfind(const void *key, const void *base, size_t *nelp,
size_t width, int (*compar)(const void *, const void *));

int hcreate(size_t num_entries);
void hdestroy(void);
ENTRY *hsearch(ENTRY item, ACTION action);

#endif /* !__MLIBC_ABI_ONLY */

#ifdef __cplusplus
Expand Down
1 change: 1 addition & 0 deletions options/posix/include/sys/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ extern "C" {
#ifndef __MLIBC_ABI_ONLY

int flock(int, int);
int flock64(int, int);

#endif /* !__MLIBC_ABI_ONLY */

Expand Down
Loading

0 comments on commit f1b7b83

Please sign in to comment.