]> icculus.org git repositories - icculus/xz.git/blob - src/xz/hardware.c
Updated comments to match renamed files.
[icculus/xz.git] / src / xz / hardware.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       hardware.c
4 /// \brief      Detection of available hardware resources
5 //
6 //  Author:     Lasse Collin
7 //
8 //  This file has been put into the public domain.
9 //  You can do whatever you want with this file.
10 //
11 ///////////////////////////////////////////////////////////////////////////////
12
13 #include "private.h"
14 #include "physmem.h"
15 #include "cpucores.h"
16
17
18 /// Maximum number of free *coder* threads. This can be set with
19 /// the --threads=NUM command line option.
20 static uint32_t threadlimit;
21
22 /// Memory usage limit
23 static uint64_t memlimit;
24
25
26 extern void
27 hardware_threadlimit_set(uint32_t new_threadlimit)
28 {
29         if (new_threadlimit == 0) {
30                 // The default is the number of available CPU cores.
31                 threadlimit = cpucores();
32                 if (threadlimit == 0)
33                         threadlimit = 1;
34         } else {
35                 threadlimit = new_threadlimit;
36         }
37
38         return;
39 }
40
41
42 extern uint32_t
43 hardware_threadlimit_get(void)
44 {
45         return threadlimit;
46 }
47
48
49 extern void
50 hardware_memlimit_set(uint64_t new_memlimit)
51 {
52         if (new_memlimit == 0) {
53                 // The default is 40 % of total installed physical RAM.
54                 hardware_memlimit_set_percentage(40);
55         } else {
56                 memlimit = new_memlimit;
57         }
58
59         return;
60 }
61
62
63 extern void
64 hardware_memlimit_set_percentage(uint32_t percentage)
65 {
66         assert(percentage > 0);
67         assert(percentage <= 100);
68
69         uint64_t mem = physmem();
70
71         // If we cannot determine the amount of RAM, assume 32 MiB. Maybe
72         // even that is too much on some systems. But on most systems it's
73         // far too little, and can be annoying.
74         if (mem == 0)
75                 mem = UINT64_C(32) * 1024 * 1024;
76
77         memlimit = percentage * mem / 100;
78         return;
79 }
80
81
82 extern uint64_t
83 hardware_memlimit_get(void)
84 {
85         return memlimit;
86 }
87
88
89 extern void
90 hardware_init(void)
91 {
92         hardware_memlimit_set(0);
93         hardware_threadlimit_set(0);
94         return;
95 }