2 * Copyright 2001-2003 Ximian, Inc
3 * Copyright 2003-2010 Novell, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 typedef struct _PinStatAddress PinStatAddress
;
32 struct _PinStatAddress
{
36 PinStatAddress
*right
;
39 typedef struct _ObjectList ObjectList
;
45 static PinStatAddress
*pin_stat_addresses
= NULL
;
46 static size_t pinned_byte_counts
[PIN_TYPE_MAX
];
48 static ObjectList
*pinned_objects
= NULL
;
51 pin_stats_tree_free (PinStatAddress
*node
)
55 pin_stats_tree_free (node
->left
);
56 pin_stats_tree_free (node
->right
);
57 mono_sgen_free_internal_dynamic (node
, sizeof (PinStatAddress
), INTERNAL_MEM_STATISTICS
);
61 pin_stats_reset (void)
64 pin_stats_tree_free (pin_stat_addresses
);
65 pin_stat_addresses
= NULL
;
66 for (i
= 0; i
< PIN_TYPE_MAX
; ++i
)
67 pinned_byte_counts
[i
] = 0;
68 while (pinned_objects
) {
69 ObjectList
*next
= pinned_objects
->next
;
70 mono_sgen_free_internal_dynamic (pinned_objects
, sizeof (ObjectList
), INTERNAL_MEM_STATISTICS
);
71 pinned_objects
= next
;
76 pin_stats_register_address (char *addr
, int pin_type
)
78 PinStatAddress
**node_ptr
= &pin_stat_addresses
;
80 int pin_type_bit
= 1 << pin_type
;
84 if (addr
== node
->addr
) {
85 node
->pin_types
|= pin_type_bit
;
88 if (addr
< node
->addr
)
89 node_ptr
= &node
->left
;
91 node_ptr
= &node
->right
;
94 node
= mono_sgen_alloc_internal_dynamic (sizeof (PinStatAddress
), INTERNAL_MEM_STATISTICS
);
96 node
->pin_types
= pin_type_bit
;
97 node
->left
= node
->right
= NULL
;
103 pin_stats_count_object_from_tree (char *obj
, size_t size
, PinStatAddress
*node
, int *pin_types
)
107 if (node
->addr
>= obj
&& node
->addr
< obj
+ size
) {
109 for (i
= 0; i
< PIN_TYPE_MAX
; ++i
) {
110 int pin_bit
= 1 << i
;
111 if (!(*pin_types
& pin_bit
) && (node
->pin_types
& pin_bit
)) {
112 pinned_byte_counts
[i
] += size
;
113 *pin_types
|= pin_bit
;
117 if (obj
< node
->addr
)
118 pin_stats_count_object_from_tree (obj
, size
, node
->left
, pin_types
);
119 if (obj
+ size
- 1 > node
->addr
)
120 pin_stats_count_object_from_tree (obj
, size
, node
->right
, pin_types
);
124 pin_stats_register_object (char *obj
, size_t size
)
127 ObjectList
*list
= mono_sgen_alloc_internal_dynamic (sizeof (ObjectList
), INTERNAL_MEM_STATISTICS
);
128 pin_stats_count_object_from_tree (obj
, size
, pin_stat_addresses
, &pin_types
);
129 list
->obj
= (MonoObject
*)obj
;
130 list
->next
= pinned_objects
;
131 pinned_objects
= list
;