Enable build for FreeBSD (part 1/2)
[ardour.git] / libs / pbd / resource.cc
1 /*
2     Copyright (C) 2011 Tim Mayberry
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #ifdef PLATFORM_WINDOWS
21 #include <stdio.h>
22 #else
23 #include <sys/time.h>
24 #include <sys/resource.h>
25 #endif
26
27 #include "pbd/resource.h"
28
29 namespace PBD {
30
31 bool
32 get_resource_limit (ResourceType resource, ResourceLimit& limit)
33 {
34         if (resource == OpenFiles)
35         {
36 #ifdef PLATFORM_WINDOWS
37                 limit.current_limit = _getmaxstdio();
38                 limit.max_limit = 2048;
39                 return true;
40 #else
41                 struct rlimit rl;
42                 if (getrlimit (RLIMIT_NOFILE, &rl) == 0) {
43                         limit.current_limit = rl.rlim_cur;
44                         limit.max_limit = rl.rlim_max;
45                         return true;
46                 }
47 #endif
48         }
49
50         return false;
51 }
52
53 bool
54 set_resource_limit (ResourceType resource, const ResourceLimit& limit)
55 {
56         if (resource == OpenFiles)
57         {
58 #ifdef PLATFORM_WINDOWS
59                 // no soft and hard limits on windows
60                 rlimit_t new_max = _setmaxstdio(limit.current_limit);
61
62                 if (new_max == limit.current_limit) return true;
63 #else
64                 struct rlimit rl;
65                 rl.rlim_cur = limit.current_limit;
66                 rl.rlim_max = limit.max_limit;
67                 if (setrlimit (RLIMIT_NOFILE, &rl) == 0) {
68                         return true;
69                 }
70
71 #endif
72         }
73
74         return false;
75 }
76
77 } // namespace PBD