public inbox for glibc-cvs@sourceware.org
help / color / mirror / Atom feed
* [glibc/azanella/bz23960] linux: Simplify opendir buffer allocation
@ 2020-10-02 13:53 Adhemerval Zanella
0 siblings, 0 replies; 6+ messages in thread
From: Adhemerval Zanella @ 2020-10-02 13:53 UTC (permalink / raw)
To: glibc-cvs
https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=c66e52bf76415c26b10b46dbc5660db9b00972e2
commit c66e52bf76415c26b10b46dbc5660db9b00972e2
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date: Sun Apr 12 17:42:35 2020 -0300
linux: Simplify opendir buffer allocation
The fallback allocation is removed, so the possible size constraint
should be analyzed just once; __alloc_dir assumes that 'statp'
argument is non-null, and the max_buffer_size move to close its
used.
Checked on x86_64-linux-gnu and i686-linux-gnu.
Diff:
---
include/dirent.h | 3 ++-
sysdeps/unix/sysv/linux/opendir.c | 52 ++++++++++++++-------------------------
2 files changed, 21 insertions(+), 34 deletions(-)
diff --git a/include/dirent.h b/include/dirent.h
index 2b1cdcf8bd..fdf4c4a2f1 100644
--- a/include/dirent.h
+++ b/include/dirent.h
@@ -48,7 +48,8 @@ extern int __versionsort64 (const struct dirent64 **a,
const struct dirent64 **b)
__attribute_pure__;
extern DIR *__alloc_dir (int fd, bool close_fd, int flags,
- const struct stat64 *statp) attribute_hidden;
+ const struct stat64 *statp)
+ __nonnull (4) attribute_hidden;
extern __typeof (rewinddir) __rewinddir;
extern __typeof (seekdir) __seekdir;
extern __typeof (dirfd) __dirfd;
diff --git a/sysdeps/unix/sysv/linux/opendir.c b/sysdeps/unix/sysv/linux/opendir.c
index e89e09bfc7..4e8d108821 100644
--- a/sysdeps/unix/sysv/linux/opendir.c
+++ b/sysdeps/unix/sysv/linux/opendir.c
@@ -23,12 +23,6 @@
#include <not-cancel.h>
-/* The st_blksize value of the directory is used as a hint for the
- size of the buffer which receives struct dirent values from the
- kernel. st_blksize is limited to MAX_DIR_BUFFER_SIZE, in case the
- file system provides a bogus value. */
-#define MAX_DIR_BUFFER_SIZE 1048576U
-
enum {
opendir_oflags = O_RDONLY|O_NDELAY|O_DIRECTORY|O_LARGEFILE|O_CLOEXEC
};
@@ -100,38 +94,30 @@ __alloc_dir (int fd, bool close_fd, int flags, const struct stat64 *statp)
file descriptor. */
if (!close_fd
&& __glibc_unlikely (__fcntl64_nocancel (fd, F_SETFD, FD_CLOEXEC) < 0))
- goto lose;
-
- const size_t default_allocation = (4 * BUFSIZ < sizeof (struct dirent64)
- ? sizeof (struct dirent64) : 4 * BUFSIZ);
- const size_t small_allocation = (BUFSIZ < sizeof (struct dirent64)
- ? sizeof (struct dirent64) : BUFSIZ);
- size_t allocation = default_allocation;
-#ifdef _STATBUF_ST_BLKSIZE
+ return NULL;
+
+ /* The st_blksize value of the directory is used as a hint for the
+ size of the buffer which receives struct dirent values from the
+ kernel. st_blksize is limited to max_buffer_size, in case the
+ file system provides a bogus value. */
+ enum { max_buffer_size = 1U << 20 };
+
+ const size_t allocation_size = 4 * BUFSIZ;
+ _Static_assert (allocation_size >= sizeof (struct dirent64),
+ "allocation_size < sizeof (struct dirent64)");
+
/* Increase allocation if requested, but not if the value appears to
- be bogus. */
- if (statp != NULL)
- allocation = MIN (MAX ((size_t) statp->st_blksize, default_allocation),
- MAX_DIR_BUFFER_SIZE);
-#endif
+ be bogus. It will be between 32Kb (for blocksizes smaller than BUFSIZ)
+ up to 1Mb. */
+ size_t allocation = MIN (MAX ((size_t) statp->st_blksize, allocation_size),
+ max_buffer_size);
DIR *dirp = (DIR *) malloc (sizeof (DIR) + allocation);
if (dirp == NULL)
{
- allocation = small_allocation;
- dirp = (DIR *) malloc (sizeof (DIR) + allocation);
-
- if (dirp == NULL)
- lose:
- {
- if (close_fd)
- {
- int save_errno = errno;
- __close_nocancel_nostatus (fd);
- __set_errno (save_errno);
- }
- return NULL;
- }
+ if (close_fd)
+ __close_nocancel_nostatus (fd);
+ return NULL;
}
dirp->fd = fd;
^ permalink raw reply [flat|nested] 6+ messages in thread
* [glibc/azanella/bz23960] linux: Simplify opendir buffer allocation
@ 2020-04-17 13:22 Adhemerval Zanella
0 siblings, 0 replies; 6+ messages in thread
From: Adhemerval Zanella @ 2020-04-17 13:22 UTC (permalink / raw)
To: glibc-cvs
https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=94d07bf65fd663881aa6b322c8fb7bec033786d0
commit 94d07bf65fd663881aa6b322c8fb7bec033786d0
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date: Sun Apr 12 17:42:35 2020 -0300
linux: Simplify opendir buffer allocation
THe fallback allocation is removed, so the possible size constraint
should be analized just once; __alloc_dir assumes that 'statp'
argument is non-null, and the max_buffer_size move to close its
used.
Checked on x86_64-linux-gnu and i686-linux-gnu.
Diff:
---
include/dirent.h | 3 ++-
sysdeps/unix/sysv/linux/opendir.c | 52 ++++++++++++++-------------------------
2 files changed, 21 insertions(+), 34 deletions(-)
diff --git a/include/dirent.h b/include/dirent.h
index 2b1cdcf8bd..fdf4c4a2f1 100644
--- a/include/dirent.h
+++ b/include/dirent.h
@@ -48,7 +48,8 @@ extern int __versionsort64 (const struct dirent64 **a,
const struct dirent64 **b)
__attribute_pure__;
extern DIR *__alloc_dir (int fd, bool close_fd, int flags,
- const struct stat64 *statp) attribute_hidden;
+ const struct stat64 *statp)
+ __nonnull (4) attribute_hidden;
extern __typeof (rewinddir) __rewinddir;
extern __typeof (seekdir) __seekdir;
extern __typeof (dirfd) __dirfd;
diff --git a/sysdeps/unix/sysv/linux/opendir.c b/sysdeps/unix/sysv/linux/opendir.c
index c6ab79246c..765c8104b3 100644
--- a/sysdeps/unix/sysv/linux/opendir.c
+++ b/sysdeps/unix/sysv/linux/opendir.c
@@ -23,12 +23,6 @@
#include <not-cancel.h>
-/* The st_blksize value of the directory is used as a hint for the
- size of the buffer which receives struct dirent values from the
- kernel. st_blksize is limited to MAX_DIR_BUFFER_SIZE, in case the
- file system provides a bogus value. */
-#define MAX_DIR_BUFFER_SIZE 1048576U
-
enum {
opendir_oflags = O_RDONLY|O_NDELAY|O_DIRECTORY|O_LARGEFILE|O_CLOEXEC
};
@@ -100,38 +94,30 @@ __alloc_dir (int fd, bool close_fd, int flags, const struct stat64 *statp)
file descriptor. */
if (!close_fd
&& __glibc_unlikely (__fcntl64_nocancel (fd, F_SETFD, FD_CLOEXEC) < 0))
- goto lose;
-
- const size_t default_allocation = (4 * BUFSIZ < sizeof (struct dirent64)
- ? sizeof (struct dirent64) : 4 * BUFSIZ);
- const size_t small_allocation = (BUFSIZ < sizeof (struct dirent64)
- ? sizeof (struct dirent64) : BUFSIZ);
- size_t allocation = default_allocation;
-#ifdef _STATBUF_ST_BLKSIZE
+ return NULL;
+
+ /* The st_blksize value of the directory is used as a hint for the
+ size of the buffer which receives struct dirent values from the
+ kernel. st_blksize is limited to max_buffer_size, in case the
+ file system provides a bogus value. */
+ enum { max_buffer_size = 1U << 20 };
+
+ const size_t allocation_size = 4 * BUFSIZ;
+ _Static_assert (allocation_size >= sizeof (struct dirent64),
+ "allocation_size < sizeof (struct dirent64)");
+
/* Increase allocation if requested, but not if the value appears to
- be bogus. */
- if (statp != NULL)
- allocation = MIN (MAX ((size_t) statp->st_blksize, default_allocation),
- MAX_DIR_BUFFER_SIZE);
-#endif
+ be bogus. It will be between 32Kb (for blocksizes smaller than BUFSIZ)
+ up to 1Mb. */
+ size_t allocation = MIN (MAX ((size_t) statp->st_blksize, allocation_size),
+ max_buffer_size);
DIR *dirp = (DIR *) malloc (sizeof (DIR) + allocation);
if (dirp == NULL)
{
- allocation = small_allocation;
- dirp = (DIR *) malloc (sizeof (DIR) + allocation);
-
- if (dirp == NULL)
- lose:
- {
- if (close_fd)
- {
- int save_errno = errno;
- __close_nocancel_nostatus (fd);
- __set_errno (save_errno);
- }
- return NULL;
- }
+ if (close_fd)
+ __close_nocancel_nostatus (fd);
+ return NULL;
}
dirp->fd = fd;
^ permalink raw reply [flat|nested] 6+ messages in thread
* [glibc/azanella/bz23960] linux: Simplify opendir buffer allocation
@ 2020-04-16 13:20 Adhemerval Zanella
0 siblings, 0 replies; 6+ messages in thread
From: Adhemerval Zanella @ 2020-04-16 13:20 UTC (permalink / raw)
To: glibc-cvs
https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=e84a7ef34dcef0d0671dae5c018d7448b10b017e
commit e84a7ef34dcef0d0671dae5c018d7448b10b017e
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date: Sun Apr 12 17:42:35 2020 -0300
linux: Simplify opendir buffer allocation
THe fallback allocation is removed, so the possible size constraint
should be analized just once; __alloc_dir assumes that 'statp'
argument is non-null, and the max_buffer_size move to close its
used.
Checked on x86_64-linux-gnu and i686-linux-gnu.
Diff:
---
include/dirent.h | 3 ++-
sysdeps/unix/sysv/linux/opendir.c | 52 ++++++++++++++-------------------------
2 files changed, 21 insertions(+), 34 deletions(-)
diff --git a/include/dirent.h b/include/dirent.h
index 2b1cdcf8bd..fdf4c4a2f1 100644
--- a/include/dirent.h
+++ b/include/dirent.h
@@ -48,7 +48,8 @@ extern int __versionsort64 (const struct dirent64 **a,
const struct dirent64 **b)
__attribute_pure__;
extern DIR *__alloc_dir (int fd, bool close_fd, int flags,
- const struct stat64 *statp) attribute_hidden;
+ const struct stat64 *statp)
+ __nonnull (4) attribute_hidden;
extern __typeof (rewinddir) __rewinddir;
extern __typeof (seekdir) __seekdir;
extern __typeof (dirfd) __dirfd;
diff --git a/sysdeps/unix/sysv/linux/opendir.c b/sysdeps/unix/sysv/linux/opendir.c
index c6ab79246c..765c8104b3 100644
--- a/sysdeps/unix/sysv/linux/opendir.c
+++ b/sysdeps/unix/sysv/linux/opendir.c
@@ -23,12 +23,6 @@
#include <not-cancel.h>
-/* The st_blksize value of the directory is used as a hint for the
- size of the buffer which receives struct dirent values from the
- kernel. st_blksize is limited to MAX_DIR_BUFFER_SIZE, in case the
- file system provides a bogus value. */
-#define MAX_DIR_BUFFER_SIZE 1048576U
-
enum {
opendir_oflags = O_RDONLY|O_NDELAY|O_DIRECTORY|O_LARGEFILE|O_CLOEXEC
};
@@ -100,38 +94,30 @@ __alloc_dir (int fd, bool close_fd, int flags, const struct stat64 *statp)
file descriptor. */
if (!close_fd
&& __glibc_unlikely (__fcntl64_nocancel (fd, F_SETFD, FD_CLOEXEC) < 0))
- goto lose;
-
- const size_t default_allocation = (4 * BUFSIZ < sizeof (struct dirent64)
- ? sizeof (struct dirent64) : 4 * BUFSIZ);
- const size_t small_allocation = (BUFSIZ < sizeof (struct dirent64)
- ? sizeof (struct dirent64) : BUFSIZ);
- size_t allocation = default_allocation;
-#ifdef _STATBUF_ST_BLKSIZE
+ return NULL;
+
+ /* The st_blksize value of the directory is used as a hint for the
+ size of the buffer which receives struct dirent values from the
+ kernel. st_blksize is limited to max_buffer_size, in case the
+ file system provides a bogus value. */
+ enum { max_buffer_size = 1U << 20 };
+
+ const size_t allocation_size = 4 * BUFSIZ;
+ _Static_assert (allocation_size >= sizeof (struct dirent64),
+ "allocation_size < sizeof (struct dirent64)");
+
/* Increase allocation if requested, but not if the value appears to
- be bogus. */
- if (statp != NULL)
- allocation = MIN (MAX ((size_t) statp->st_blksize, default_allocation),
- MAX_DIR_BUFFER_SIZE);
-#endif
+ be bogus. It will be between 32Kb (for blocksizes smaller than BUFSIZ)
+ up to 1Mb. */
+ size_t allocation = MIN (MAX ((size_t) statp->st_blksize, allocation_size),
+ max_buffer_size);
DIR *dirp = (DIR *) malloc (sizeof (DIR) + allocation);
if (dirp == NULL)
{
- allocation = small_allocation;
- dirp = (DIR *) malloc (sizeof (DIR) + allocation);
-
- if (dirp == NULL)
- lose:
- {
- if (close_fd)
- {
- int save_errno = errno;
- __close_nocancel_nostatus (fd);
- __set_errno (save_errno);
- }
- return NULL;
- }
+ if (close_fd)
+ __close_nocancel_nostatus (fd);
+ return NULL;
}
dirp->fd = fd;
^ permalink raw reply [flat|nested] 6+ messages in thread
* [glibc/azanella/bz23960] linux: Simplify opendir buffer allocation
@ 2020-04-15 14:14 Adhemerval Zanella
0 siblings, 0 replies; 6+ messages in thread
From: Adhemerval Zanella @ 2020-04-15 14:14 UTC (permalink / raw)
To: glibc-cvs
https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=85030d0a99ac6fa42049ff001485959e125ee264
commit 85030d0a99ac6fa42049ff001485959e125ee264
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date: Sun Apr 12 17:42:35 2020 -0300
linux: Simplify opendir buffer allocation
THe fallback allocation is removed, so the possible size constraint
should be analized just once; __alloc_dir assumes that 'statp'
argument is non-null, and the max_buffer_size move to close its
used.
Checked on x86_64-linux-gnu and i686-linux-gnu.
Diff:
---
include/dirent.h | 3 ++-
sysdeps/unix/sysv/linux/opendir.c | 45 +++++++++++++++------------------------
2 files changed, 19 insertions(+), 29 deletions(-)
diff --git a/include/dirent.h b/include/dirent.h
index 2b1cdcf8bd..fdf4c4a2f1 100644
--- a/include/dirent.h
+++ b/include/dirent.h
@@ -48,7 +48,8 @@ extern int __versionsort64 (const struct dirent64 **a,
const struct dirent64 **b)
__attribute_pure__;
extern DIR *__alloc_dir (int fd, bool close_fd, int flags,
- const struct stat64 *statp) attribute_hidden;
+ const struct stat64 *statp)
+ __nonnull (4) attribute_hidden;
extern __typeof (rewinddir) __rewinddir;
extern __typeof (seekdir) __seekdir;
extern __typeof (dirfd) __dirfd;
diff --git a/sysdeps/unix/sysv/linux/opendir.c b/sysdeps/unix/sysv/linux/opendir.c
index c6ab79246c..5469e2e18f 100644
--- a/sysdeps/unix/sysv/linux/opendir.c
+++ b/sysdeps/unix/sysv/linux/opendir.c
@@ -23,12 +23,6 @@
#include <not-cancel.h>
-/* The st_blksize value of the directory is used as a hint for the
- size of the buffer which receives struct dirent values from the
- kernel. st_blksize is limited to MAX_DIR_BUFFER_SIZE, in case the
- file system provides a bogus value. */
-#define MAX_DIR_BUFFER_SIZE 1048576U
-
enum {
opendir_oflags = O_RDONLY|O_NDELAY|O_DIRECTORY|O_LARGEFILE|O_CLOEXEC
};
@@ -102,34 +96,29 @@ __alloc_dir (int fd, bool close_fd, int flags, const struct stat64 *statp)
&& __glibc_unlikely (__fcntl64_nocancel (fd, F_SETFD, FD_CLOEXEC) < 0))
goto lose;
- const size_t default_allocation = (4 * BUFSIZ < sizeof (struct dirent64)
- ? sizeof (struct dirent64) : 4 * BUFSIZ);
- const size_t small_allocation = (BUFSIZ < sizeof (struct dirent64)
- ? sizeof (struct dirent64) : BUFSIZ);
- size_t allocation = default_allocation;
-#ifdef _STATBUF_ST_BLKSIZE
+ /* The st_blksize value of the directory is used as a hint for the
+ size of the buffer which receives struct dirent values from the
+ kernel. st_blksize is limited to max_buffer_size, in case the
+ file system provides a bogus value. */
+ enum { max_buffer_size = 1U << 20 };
+
+ const size_t allocation_size = 4 * BUFSIZ;
+ _Static_assert (allocation_size >= sizeof (struct dirent64),
+ "allocation_size < sizeof (struct dirent64)");
+
/* Increase allocation if requested, but not if the value appears to
- be bogus. */
- if (statp != NULL)
- allocation = MIN (MAX ((size_t) statp->st_blksize, default_allocation),
- MAX_DIR_BUFFER_SIZE);
-#endif
+ be bogus. It will be between 32Kb (for blocksizes smaller than BUFSIZ)
+ up to 1Mb. */
+ size_t allocation = MIN (MAX ((size_t) statp->st_blksize, allocation_size),
+ max_buffer_size);
DIR *dirp = (DIR *) malloc (sizeof (DIR) + allocation);
if (dirp == NULL)
{
- allocation = small_allocation;
- dirp = (DIR *) malloc (sizeof (DIR) + allocation);
-
- if (dirp == NULL)
- lose:
+ lose:
+ if (close_fd)
{
- if (close_fd)
- {
- int save_errno = errno;
- __close_nocancel_nostatus (fd);
- __set_errno (save_errno);
- }
+ INTERNAL_SYSCALL_CALL (close, fd);
return NULL;
}
}
^ permalink raw reply [flat|nested] 6+ messages in thread
* [glibc/azanella/bz23960] linux: Simplify opendir buffer allocation
@ 2020-04-14 21:09 Adhemerval Zanella
0 siblings, 0 replies; 6+ messages in thread
From: Adhemerval Zanella @ 2020-04-14 21:09 UTC (permalink / raw)
To: glibc-cvs
https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=0a6ff811c0223ba9df78869c27cc22b86e60f767
commit 0a6ff811c0223ba9df78869c27cc22b86e60f767
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date: Sun Apr 12 17:42:35 2020 -0300
linux: Simplify opendir buffer allocation
THe fallback allocation is removed, __alloc_dir assumes that 'statp'
argument is non-null, and the max_buffer_size move to close its
used.
Checked on x86_64-linux-gnu and i686-linux-gnu.
Diff:
---
include/dirent.h | 3 ++-
sysdeps/unix/sysv/linux/opendir.c | 45 +++++++++++++++------------------------
2 files changed, 19 insertions(+), 29 deletions(-)
diff --git a/include/dirent.h b/include/dirent.h
index 2b1cdcf8bd..fdf4c4a2f1 100644
--- a/include/dirent.h
+++ b/include/dirent.h
@@ -48,7 +48,8 @@ extern int __versionsort64 (const struct dirent64 **a,
const struct dirent64 **b)
__attribute_pure__;
extern DIR *__alloc_dir (int fd, bool close_fd, int flags,
- const struct stat64 *statp) attribute_hidden;
+ const struct stat64 *statp)
+ __nonnull (4) attribute_hidden;
extern __typeof (rewinddir) __rewinddir;
extern __typeof (seekdir) __seekdir;
extern __typeof (dirfd) __dirfd;
diff --git a/sysdeps/unix/sysv/linux/opendir.c b/sysdeps/unix/sysv/linux/opendir.c
index c6ab79246c..0f8a1f28c6 100644
--- a/sysdeps/unix/sysv/linux/opendir.c
+++ b/sysdeps/unix/sysv/linux/opendir.c
@@ -23,12 +23,6 @@
#include <not-cancel.h>
-/* The st_blksize value of the directory is used as a hint for the
- size of the buffer which receives struct dirent values from the
- kernel. st_blksize is limited to MAX_DIR_BUFFER_SIZE, in case the
- file system provides a bogus value. */
-#define MAX_DIR_BUFFER_SIZE 1048576U
-
enum {
opendir_oflags = O_RDONLY|O_NDELAY|O_DIRECTORY|O_LARGEFILE|O_CLOEXEC
};
@@ -102,34 +96,29 @@ __alloc_dir (int fd, bool close_fd, int flags, const struct stat64 *statp)
&& __glibc_unlikely (__fcntl64_nocancel (fd, F_SETFD, FD_CLOEXEC) < 0))
goto lose;
- const size_t default_allocation = (4 * BUFSIZ < sizeof (struct dirent64)
- ? sizeof (struct dirent64) : 4 * BUFSIZ);
- const size_t small_allocation = (BUFSIZ < sizeof (struct dirent64)
- ? sizeof (struct dirent64) : BUFSIZ);
- size_t allocation = default_allocation;
-#ifdef _STATBUF_ST_BLKSIZE
+ /* The st_blksize value of the directory is used as a hint for the
+ size of the buffer which receives struct dirent values from the
+ kernel. st_blksize is limited to MAX_DIR_BUFFER_SIZE, in case the
+ file system provides a bogus value. */
+ enum { max_buffer_size = 1U << 20 };
+
+ const size_t allocation_size = 4 * BUFSIZ;
+ _Static_assert (allocation_size >= sizeof (struct dirent64),
+ "allocation_size < sizeof (struct dirent64)");
+
/* Increase allocation if requested, but not if the value appears to
- be bogus. */
- if (statp != NULL)
- allocation = MIN (MAX ((size_t) statp->st_blksize, default_allocation),
- MAX_DIR_BUFFER_SIZE);
-#endif
+ be bogus. It will be between 32Kb (for blocksizes smaller than BUFSIZ)
+ up to 1Mb. */
+ size_t allocation = MIN (MAX ((size_t) statp->st_blksize, allocation_size),
+ max_buffer_size);
DIR *dirp = (DIR *) malloc (sizeof (DIR) + allocation);
if (dirp == NULL)
{
- allocation = small_allocation;
- dirp = (DIR *) malloc (sizeof (DIR) + allocation);
-
- if (dirp == NULL)
- lose:
+ lose:
+ if (close_fd)
{
- if (close_fd)
- {
- int save_errno = errno;
- __close_nocancel_nostatus (fd);
- __set_errno (save_errno);
- }
+ INTERNAL_SYSCALL_CALL (close, fd);
return NULL;
}
}
^ permalink raw reply [flat|nested] 6+ messages in thread
* [glibc/azanella/bz23960] linux: Simplify opendir buffer allocation
@ 2020-04-13 21:28 Adhemerval Zanella
0 siblings, 0 replies; 6+ messages in thread
From: Adhemerval Zanella @ 2020-04-13 21:28 UTC (permalink / raw)
To: glibc-cvs
https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=60467738ef49863d556005925a7fe53489dd16f9
commit 60467738ef49863d556005925a7fe53489dd16f9
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date: Sun Apr 12 17:42:35 2020 -0300
linux: Simplify opendir buffer allocation
Diff:
---
include/dirent.h | 3 ++-
sysdeps/unix/sysv/linux/opendir.c | 45 +++++++++++++++------------------------
2 files changed, 19 insertions(+), 29 deletions(-)
diff --git a/include/dirent.h b/include/dirent.h
index 2b1cdcf8bd..fdf4c4a2f1 100644
--- a/include/dirent.h
+++ b/include/dirent.h
@@ -48,7 +48,8 @@ extern int __versionsort64 (const struct dirent64 **a,
const struct dirent64 **b)
__attribute_pure__;
extern DIR *__alloc_dir (int fd, bool close_fd, int flags,
- const struct stat64 *statp) attribute_hidden;
+ const struct stat64 *statp)
+ __nonnull (4) attribute_hidden;
extern __typeof (rewinddir) __rewinddir;
extern __typeof (seekdir) __seekdir;
extern __typeof (dirfd) __dirfd;
diff --git a/sysdeps/unix/sysv/linux/opendir.c b/sysdeps/unix/sysv/linux/opendir.c
index c6ab79246c..0f8a1f28c6 100644
--- a/sysdeps/unix/sysv/linux/opendir.c
+++ b/sysdeps/unix/sysv/linux/opendir.c
@@ -23,12 +23,6 @@
#include <not-cancel.h>
-/* The st_blksize value of the directory is used as a hint for the
- size of the buffer which receives struct dirent values from the
- kernel. st_blksize is limited to MAX_DIR_BUFFER_SIZE, in case the
- file system provides a bogus value. */
-#define MAX_DIR_BUFFER_SIZE 1048576U
-
enum {
opendir_oflags = O_RDONLY|O_NDELAY|O_DIRECTORY|O_LARGEFILE|O_CLOEXEC
};
@@ -102,34 +96,29 @@ __alloc_dir (int fd, bool close_fd, int flags, const struct stat64 *statp)
&& __glibc_unlikely (__fcntl64_nocancel (fd, F_SETFD, FD_CLOEXEC) < 0))
goto lose;
- const size_t default_allocation = (4 * BUFSIZ < sizeof (struct dirent64)
- ? sizeof (struct dirent64) : 4 * BUFSIZ);
- const size_t small_allocation = (BUFSIZ < sizeof (struct dirent64)
- ? sizeof (struct dirent64) : BUFSIZ);
- size_t allocation = default_allocation;
-#ifdef _STATBUF_ST_BLKSIZE
+ /* The st_blksize value of the directory is used as a hint for the
+ size of the buffer which receives struct dirent values from the
+ kernel. st_blksize is limited to MAX_DIR_BUFFER_SIZE, in case the
+ file system provides a bogus value. */
+ enum { max_buffer_size = 1U << 20 };
+
+ const size_t allocation_size = 4 * BUFSIZ;
+ _Static_assert (allocation_size >= sizeof (struct dirent64),
+ "allocation_size < sizeof (struct dirent64)");
+
/* Increase allocation if requested, but not if the value appears to
- be bogus. */
- if (statp != NULL)
- allocation = MIN (MAX ((size_t) statp->st_blksize, default_allocation),
- MAX_DIR_BUFFER_SIZE);
-#endif
+ be bogus. It will be between 32Kb (for blocksizes smaller than BUFSIZ)
+ up to 1Mb. */
+ size_t allocation = MIN (MAX ((size_t) statp->st_blksize, allocation_size),
+ max_buffer_size);
DIR *dirp = (DIR *) malloc (sizeof (DIR) + allocation);
if (dirp == NULL)
{
- allocation = small_allocation;
- dirp = (DIR *) malloc (sizeof (DIR) + allocation);
-
- if (dirp == NULL)
- lose:
+ lose:
+ if (close_fd)
{
- if (close_fd)
- {
- int save_errno = errno;
- __close_nocancel_nostatus (fd);
- __set_errno (save_errno);
- }
+ INTERNAL_SYSCALL_CALL (close, fd);
return NULL;
}
}
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2020-10-02 13:53 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-02 13:53 [glibc/azanella/bz23960] linux: Simplify opendir buffer allocation Adhemerval Zanella
-- strict thread matches above, loose matches on Subject: below --
2020-04-17 13:22 Adhemerval Zanella
2020-04-16 13:20 Adhemerval Zanella
2020-04-15 14:14 Adhemerval Zanella
2020-04-14 21:09 Adhemerval Zanella
2020-04-13 21:28 Adhemerval Zanella
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).