Use new atomis with allocated_heap.
[mono.git] / mono / metadata / sgen-memory-governor.c
1 /*
2  * sgen-cardtable.c: Card table implementation for sgen
3  *
4  * Author:
5  *      Rodrigo Kumpera (rkumpera@novell.com)
6  *
7  * SGen is licensed under the terms of the MIT X11 license
8  *
9  * Copyright 2001-2003 Ximian, Inc
10  * Copyright 2003-2010 Novell, Inc.
11  * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
12  * 
13  * Permission is hereby granted, free of charge, to any person obtaining
14  * a copy of this software and associated documentation files (the
15  * "Software"), to deal in the Software without restriction, including
16  * without limitation the rights to use, copy, modify, merge, publish,
17  * distribute, sublicense, and/or sell copies of the Software, and to
18  * permit persons to whom the Software is furnished to do so, subject to
19  * the following conditions:
20  * 
21  * The above copyright notice and this permission notice shall be
22  * included in all copies or substantial portions of the Software.
23  * 
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31  */
32
33 #include "config.h"
34 #ifdef HAVE_SGEN_GC
35
36 #include "metadata/sgen-gc.h"
37 #include "metadata/sgen-memory-governor.h"
38 #include "utils/mono-counters.h"
39
40 #define MIN_MINOR_COLLECTION_ALLOWANCE  ((mword)(DEFAULT_NURSERY_SIZE * SGEN_MIN_ALLOWANCE_NURSERY_SIZE_RATIO))
41
42 /*heap limits*/
43 static mword max_heap_size = ((mword)0)- ((mword)1);
44 static mword soft_heap_limit = ((mword)0) - ((mword)1);
45 static mword allocated_heap;
46
47
48 mword
49 sgen_memgov_available_free_space (void)
50 {
51         return max_heap_size - MIN (allocated_heap, max_heap_size);
52 }
53
54 mword
55 sgen_memgov_min_allowance (void)
56 {
57         return MIN_MINOR_COLLECTION_ALLOWANCE;
58 }
59
60 mword
61 sgen_memgov_adjust_allowance (mword allowance_estimate, mword new_heap_size)
62 {
63         if (new_heap_size + allowance_estimate > soft_heap_limit) {
64                 if (new_heap_size > soft_heap_limit)
65                         return MIN_MINOR_COLLECTION_ALLOWANCE;
66                 else
67                         return MAX (soft_heap_limit - new_heap_size, MIN_MINOR_COLLECTION_ALLOWANCE);
68         }
69         return allowance_estimate;
70 }
71
72 void
73 sgen_memgov_release_space (mword size, int space)
74 {
75         SGEN_ATOMIC_ADD_P (allocated_heap, -size);
76 }
77
78 gboolean
79 sgen_memgov_try_alloc_space (mword size, int space)
80 {
81         if (sgen_memgov_available_free_space () < size)
82                 return FALSE;
83
84         SGEN_ATOMIC_ADD_P (allocated_heap, size);
85         mono_runtime_resource_check_limit (MONO_RESOURCE_GC_HEAP, allocated_heap);
86         return TRUE;
87 }
88
89 void
90 sgen_memgov_init (glong max_heap, glong soft_limit)
91 {
92         if (soft_limit)
93                 soft_heap_limit = soft_limit;
94
95         if (max_heap == 0)
96                 return;
97
98         if (max_heap < soft_limit) {
99                 fprintf (stderr, "max-heap-size must be at least as large as soft-heap-limit.\n");
100                 exit (1);
101         }
102
103         if (max_heap < sgen_nursery_size * 4) {
104                 fprintf (stderr, "max-heap-size must be at least 4 times larger than nursery size.\n");
105                 exit (1);
106         }
107         max_heap_size = max_heap - sgen_nursery_size;
108 }
109
110 #endif