/
sysmem.c
109 lines (77 loc) · 2.22 KB
/
sysmem.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* Copyright (C) 2010, Parrot Foundation.
*/
/*
=head1 NAME
config/gen/platform/generic/sysmem.c
=head1 DESCRIPTION
Get system memory information.
=head2 Functions
=over 4
=cut
*/
#include <stdio.h>
#include <unistd.h>
#include "parrot/sysmem.h"
#if defined(PARROT_HAS_HEADER_SYSSYSCTL)
# include <sys/sysctl.h>
#endif
/*
=item C<size_t Parrot_sysmem_amount(PARROT_INTERP)>
Get information about available physical memory.
=cut
*/
/*
* The POSIX name is _SC_PAGESIZE, but apparently some systems
* require _SC_PAGE_SIZE instead. (Specific citations would
* be useful.)
*/
#if !defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE)
# define _SC_PAGESIZE _SC_PAGE_SIZE
#endif
size_t
Parrot_sysmem_amount(PARROT_INTERP)
{
size_t memsize = 0;
#if defined(_SC_PAGESIZE) && defined(_SC_AVPHYS_PAGES)
/* Method 1: sysconf().
* Works on Linux and Solaris, and probably other SVR4-related systems.
* This should really check for #ifdef HAS_SYSCONF, but Configure
* doesn't probe for that yet.
*/
memsize = sysconf(_SC_AVPHYS_PAGES) * sysconf(_SC_PAGESIZE);
#elif defined(PARROT_HAS_HEADER_SYSSYSCTL) && defined(CTL_HW) && defined(HW_PHYSMEM)
/* Method 2: sysctl(). Works on BSD-derived systems. Darwin is
* slightly different, and has its own implementation.
* Configure really should test for HAS_SYSCTL, but, for now, test for
* the header sys/sysctl.h and the appropriate constants.
*/
int err = 0;
char *err_msg;
unsigned long length = sizeof (memsize);
int selection[2] = { CTL_HW, HW_PHYSMEM };
err = sysctl(selection, 2, &memsize, &length, NULL, 0);
if (err) {
err_msg = strerror(err);
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_EXTERNAL_ERROR,
"sysctl failed: %s", err_msg);
}
#else
/* Method 3: Random guess. Simply guess 512 MB. This way, parrot
* will at least build. Arguably, one could also just put an error
* here and instruct the user to fix it manually.
*/
memsize = 512 * 1024 * 1024;
#endif
return memsize;
}
/*
=back
=cut
*/
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4 cinoptions='\:2=2' :
*/