2 using System
.Runtime
.InteropServices
;
4 // in this test we spend
5 // 30% of the time locking
6 // 10 % allocating the handles
9 static GCHandle
[] handle_array
;
11 static int count
= 4 * 400000; /* multiple of handle types */
14 static void build_array () {
16 handle_array
= new GCHandle
[count
];
18 for (i
= 0; i
< count
; ++i
) {
19 GCHandleType t
= (GCHandleType
) (i
& 3);
20 handle_array
[i
] = GCHandle
.Alloc (i
, t
);
23 static void get_stats (){
28 int normal_reclaimed
= 0;
29 for (i
= 0; i
< count
; ++i
) {
30 GCHandleType t
= (GCHandleType
) (i
& 3);
31 if (handle_array
[i
].IsAllocated
)
35 o
= handle_array
[i
].Target
;
40 Console
.WriteLine ("obj at {0} inconsistent: {1}", i
, val
);
42 if (t
== GCHandleType
.Normal
|| t
== GCHandleType
.Pinned
) {
47 Console
.WriteLine ("allocated: {0}, has target: {1}, normal reclaimed: {2}", is_allocated
, has_target
, normal_reclaimed
);
50 static void free_some (int d
) {
53 for (i
= 0; i
< count
; ++i
) {
55 if (handle_array
[i
].IsAllocated
) {
56 handle_array
[i
].Free ();
61 Console
.WriteLine ("freed: {0}", freed
);
64 static void alloc_many () {
65 int small_count
= count
/ 2;
66 GCHandle
[] more
= new GCHandle
[small_count
];
68 for (i
= 0; i
< small_count
; ++i
) {
69 GCHandleType t
= (GCHandleType
) (i
& 3);
70 more
[i
] = GCHandle
.Alloc (i
, t
);
72 for (i
= 0; i
< small_count
; ++i
) {
75 Console
.WriteLine ("alloc many: {0}", small_count
);
78 static void Main (string[] args
) {
80 count
= 4 * int.Parse (args
[0]);
82 loops
= int.Parse (args
[1]);
84 for (int j
= 0; j
< loops
; ++j
) {
89 static void do_one () {
90 Console
.WriteLine ("start");
94 Console
.WriteLine ("after collect");
97 Console
.WriteLine ("after free(10)");
100 Console
.WriteLine ("after free(4)");
103 Console
.WriteLine ("after collect");
105 for (int i
= 0; i
< 10; ++i
)
107 Console
.WriteLine ("after alloc_many");
110 Console
.WriteLine ("after free all");
113 Console
.WriteLine ("after collect");