Skip to content

Commit 01587de

Browse files
committed
Simplify memory manager
1 parent 5a9a08d commit 01587de

40 files changed

+450
-310
lines changed

ctl/is_void.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// -*-mode:c++;indent-tabs-mode:nil;c-basic-offset:4;tab-width:8;coding:utf-8-*-
2+
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
3+
#ifndef CTL_IS_VOID_H_
4+
#define CTL_IS_VOID_H_
5+
#include "integral_constant.h"
6+
#include "remove_cv.h"
7+
8+
namespace ctl {
9+
10+
template<typename>
11+
struct is_void_ : public ctl::false_type
12+
{};
13+
14+
template<>
15+
struct is_void_<void> : public ctl::true_type
16+
{};
17+
18+
template<typename _Tp>
19+
struct is_void : public is_void_<typename ctl::remove_cv<_Tp>::type>::type
20+
{};
21+
22+
} // namespace ctl
23+
24+
#endif // CTL_IS_VOID_H_

ctl/vector.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ class vector
525525
capacity_ = new_capacity;
526526
}
527527

528-
Allocator alloc_;
528+
[[no_unique_address]] Allocator alloc_;
529529
pointer data_;
530530
size_type size_;
531531
size_type capacity_;

examples/greenbean.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <sys/socket.h>
2525
#include <time.h>
2626
#include "libc/mem/leaks.h"
27+
#include "libc/runtime/runtime.h"
2728

2829
/**
2930
* @fileoverview greenbean lightweight threaded web server
@@ -336,10 +337,9 @@ int main(int argc, char *argv[]) {
336337
sigaddset(&block, SIGHUP);
337338
sigaddset(&block, SIGQUIT);
338339
pthread_attr_t attr;
339-
int pagesz = getauxval(AT_PAGESZ);
340340
unassert(!pthread_attr_init(&attr));
341341
unassert(!pthread_attr_setstacksize(&attr, 65536));
342-
unassert(!pthread_attr_setguardsize(&attr, pagesz));
342+
unassert(!pthread_attr_setguardsize(&attr, getpagesize()));
343343
unassert(!pthread_attr_setsigmask_np(&attr, &block));
344344
unassert(!pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, 0));
345345
pthread_t *th = gc(calloc(threads, sizeof(pthread_t)));

libc/calls/madvise.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* @return 0 on success, or -1 w/ errno
3131
* @raise EINVAL if `advice` isn't valid or supported by system
3232
* @raise EINVAL on Linux if addr/length isn't page size aligned with
33-
* respect to `getauxval(AT_PAGESZ)`
33+
* respect to `getpagesize()`
3434
* @raise ENOMEM on Linux if addr/length overlaps unmapped regions
3535
* @see libc/sysv/consts.sh
3636
* @see fadvise()

libc/calls/posix_madvise.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
* @return 0 on success, or errno on error
2626
* @raise EINVAL if `advice` isn't valid or supported by system
2727
* @raise EINVAL on Linux if addr/length isn't page size aligned with
28-
* respect to `getauxval(AT_PAGESZ)`
28+
* respect to `getpagesize()`
2929
* @raise ENOMEM on Linux if addr/length overlaps unmapped regions
3030
* @returnserrno
3131
*/

libc/calls/setrlimit.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "libc/calls/struct/rlimit.internal.h"
2222
#include "libc/calls/syscall-sysv.internal.h"
2323
#include "libc/dce.h"
24+
#include "libc/errno.h"
2425
#include "libc/intrin/describeflags.internal.h"
2526
#include "libc/intrin/strace.internal.h"
2627
#include "libc/macros.internal.h"
@@ -77,6 +78,7 @@
7778
*/
7879
int setrlimit(int resource, const struct rlimit *rlim) {
7980
int rc;
81+
int olde = errno;
8082
if (resource == 127) {
8183
rc = einval();
8284
} else if (!rlim) {
@@ -85,18 +87,16 @@ int setrlimit(int resource, const struct rlimit *rlim) {
8587
rc = _sysret(__syslib->__setrlimit(resource, rlim));
8688
} else if (!IsWindows()) {
8789
rc = sys_setrlimit(resource, rlim);
88-
if (IsXnu() && !rc && resource == RLIMIT_AS) {
89-
// TODO(jart): What's up with XNU and NetBSD?
90-
__virtualmax = rlim->rlim_cur;
91-
}
9290
} else if (resource == RLIMIT_STACK) {
9391
rc = enotsup();
94-
} else if (resource == RLIMIT_AS) {
95-
__virtualmax = rlim->rlim_cur;
96-
rc = 0;
9792
} else {
9893
rc = einval();
9994
}
95+
if (resource == RLIMIT_AS) {
96+
__virtualmax = rlim->rlim_cur;
97+
errno = olde;
98+
rc = 0;
99+
}
100100
STRACE("setrlimit(%s, %s) → %d% m", DescribeRlimitName(resource),
101101
DescribeRlimit(0, rlim), rc);
102102
return rc;

libc/dlopen/dlopen.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ static wontreturn dontinstrument void foreign_helper(void **p) {
303303
static dontinline void elf_exec(const char *file, char **envp) {
304304

305305
// get microprocessor page size
306-
long pagesz = getauxval(AT_PAGESZ);
306+
long pagesz = getpagesize();
307307

308308
// load helper executable into address space
309309
struct Loaded prog;

libc/intrin/getauxval.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
1717
│ PERFORMANCE OF THIS SOFTWARE. │
1818
╚─────────────────────────────────────────────────────────────────────────────*/
19+
#include "libc/dce.h"
1920
#include "libc/errno.h"
2021
#include "libc/intrin/getauxval.internal.h"
22+
#include "libc/nt/struct/systeminfo.h"
23+
#include "libc/nt/systeminfo.h"
2124
#include "libc/runtime/runtime.h"
2225
#include "libc/sysv/consts/auxv.h"
2326

@@ -35,11 +38,17 @@ unsigned long getauxval(unsigned long key) {
3538
x = __getauxval(key);
3639
if (key == AT_PAGESZ) {
3740
if (!x.isfound) {
41+
if (!IsWindows()) {
3842
#ifdef __aarch64__
39-
x.value = 16384;
43+
x.value = 16384;
4044
#else
41-
x.value = 4096;
45+
x.value = 4096;
4246
#endif
47+
} else {
48+
struct NtSystemInfo si;
49+
GetSystemInfo(&si);
50+
x.value = si.dwPageSize;
51+
}
4352
}
4453
x.isfound = true;
4554
}

libc/intrin/getmainstack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ static size_t __get_stack_size(int pagesz, uintptr_t start, uintptr_t top) {
105105
* This function works on every OS except Windows.
106106
*/
107107
struct AddrSize __get_main_stack(void) {
108-
int pagesz = getauxval(AT_PAGESZ);
108+
int pagesz = getpagesize();
109109
uintptr_t start = (uintptr_t)__argv;
110110
uintptr_t top = __get_main_top(pagesz);
111111
uintptr_t bot = top - __get_stack_size(pagesz, start, top);

libc/runtime/getpagesize.c renamed to libc/intrin/getpagesize.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717
│ PERFORMANCE OF THIS SOFTWARE. │
1818
╚─────────────────────────────────────────────────────────────────────────────*/
1919
#include "libc/runtime/runtime.h"
20+
#include "libc/sysv/consts/auxv.h"
2021

2122
/**
2223
* Returns granularity of memory manager.
2324
* @see sysconf(_SC_PAGE_SIZE) which is portable
2425
*/
2526
int getpagesize(void) {
26-
return __granularity();
27+
return getauxval(AT_PAGESZ);
2728
}

0 commit comments

Comments
 (0)