| 1 |
/* |
| 2 |
* Copyright (c) 2011 by Hewlett-Packard Company. All rights reserved. |
| 3 |
* |
| 4 |
* THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED |
| 5 |
* OR IMPLIED. ANY USE IS AT YOUR OWN RISK. |
| 6 |
* |
| 7 |
* Permission is hereby granted to use or copy this program |
| 8 |
* for any purpose, provided the above notices are retained on all copies. |
| 9 |
* Permission to modify the code and to distribute modified code is granted, |
| 10 |
* provided the above notices are retained, and a notice that the code was |
| 11 |
* modified is included with the above copyright notice. |
| 12 |
* |
| 13 |
*/ |
| 14 |
|
| 15 |
#include <stdlib.h> |
| 16 |
#include <stdio.h> |
| 17 |
#include <string.h> |
| 18 |
|
| 19 |
#ifdef HAVE_CONFIG_H |
| 20 |
# include "config.h" |
| 21 |
#endif |
| 22 |
|
| 23 |
#include "gc_disclaim.h" |
| 24 |
|
| 25 |
#define NOT_GCBUILD |
| 26 |
#include "private/gc_priv.h" |
| 27 |
|
| 28 |
#undef rand |
| 29 |
static GC_RAND_STATE_T seed; |
| 30 |
#define rand() GC_RAND_NEXT(&seed) |
| 31 |
|
| 32 |
#define my_assert(e) \ |
| 33 |
if (!(e)) { \ |
| 34 |
fprintf(stderr, "Assertion failure, line %d: " #e "\n", __LINE__); \ |
| 35 |
exit(-1); \ |
| 36 |
} |
| 37 |
|
| 38 |
static int free_count = 0; |
| 39 |
|
| 40 |
struct testobj_s { |
| 41 |
struct testobj_s *keep_link; |
| 42 |
int i; |
| 43 |
}; |
| 44 |
|
| 45 |
typedef struct testobj_s *testobj_t; |
| 46 |
|
| 47 |
void GC_CALLBACK testobj_finalize(void *obj, void *carg) |
| 48 |
{ |
| 49 |
++*(int *)carg; |
| 50 |
my_assert(((testobj_t)obj)->i == 109); |
| 51 |
((testobj_t)obj)->i = 110; |
| 52 |
} |
| 53 |
|
| 54 |
static const struct GC_finalizer_closure fclos = { |
| 55 |
testobj_finalize, |
| 56 |
&free_count |
| 57 |
}; |
| 58 |
|
| 59 |
testobj_t testobj_new(int model) |
| 60 |
{ |
| 61 |
testobj_t obj; |
| 62 |
switch (model) { |
| 63 |
# ifndef GC_NO_FINALIZATION |
| 64 |
case 0: |
| 65 |
obj = (struct testobj_s *)GC_malloc(sizeof(struct testobj_s)); |
| 66 |
if (obj != NULL) |
| 67 |
GC_register_finalizer_no_order(obj, testobj_finalize, |
| 68 |
&free_count, NULL, NULL); |
| 69 |
break; |
| 70 |
# endif |
| 71 |
case 1: |
| 72 |
obj = (testobj_t)GC_finalized_malloc(sizeof(struct testobj_s), |
| 73 |
&fclos); |
| 74 |
break; |
| 75 |
case 2: |
| 76 |
obj = (struct testobj_s *)GC_malloc(sizeof(struct testobj_s)); |
| 77 |
break; |
| 78 |
default: |
| 79 |
exit(-1); |
| 80 |
} |
| 81 |
if (obj == NULL) { |
| 82 |
fprintf(stderr, "Out of memory!\n"); |
| 83 |
exit(3); |
| 84 |
} |
| 85 |
my_assert(obj->i == 0 && obj->keep_link == NULL); |
| 86 |
obj->i = 109; |
| 87 |
return obj; |
| 88 |
} |
| 89 |
|
| 90 |
#define ALLOC_CNT (4*1024*1024) |
| 91 |
#define KEEP_CNT (32*1024) |
| 92 |
|
| 93 |
static char const *model_str[3] = { |
| 94 |
"regular finalization", |
| 95 |
"finalize on reclaim", |
| 96 |
"no finalization" |
| 97 |
}; |
| 98 |
|
| 99 |
int main(int argc, char **argv) |
| 100 |
{ |
| 101 |
int i; |
| 102 |
int model, model_min, model_max; |
| 103 |
testobj_t *keep_arr; |
| 104 |
|
| 105 |
GC_INIT(); |
| 106 |
GC_init_finalized_malloc(); |
| 107 |
if (argc == 2 && strcmp(argv[1], "--help") == 0) { |
| 108 |
fprintf(stderr, |
| 109 |
"Usage: %s [FINALIZATION_MODEL]\n" |
| 110 |
"\t0 -- original finalization\n" |
| 111 |
"\t1 -- finalization on reclaim\n" |
| 112 |
"\t2 -- no finalization\n", argv[0]); |
| 113 |
return 1; |
| 114 |
} |
| 115 |
if (argc == 2) { |
| 116 |
model_min = model_max = (int)COVERT_DATAFLOW(atoi(argv[1])); |
| 117 |
if (model_min < 0 || model_max > 2) |
| 118 |
exit(2); |
| 119 |
} else { |
| 120 |
# ifndef GC_NO_FINALIZATION |
| 121 |
model_min = 0; |
| 122 |
# else |
| 123 |
model_min = 1; |
| 124 |
# endif |
| 125 |
model_max = 2; |
| 126 |
} |
| 127 |
if (GC_get_find_leak()) |
| 128 |
printf("This test program is not designed for leak detection mode\n"); |
| 129 |
|
| 130 |
keep_arr = (testobj_t *)GC_malloc(sizeof(void *) * KEEP_CNT); |
| 131 |
if (NULL == keep_arr) { |
| 132 |
fprintf(stderr, "Out of memory!\n"); |
| 133 |
exit(3); |
| 134 |
} |
| 135 |
|
| 136 |
printf("\t\t\tfin. ratio time/s time/fin.\n"); |
| 137 |
for (model = model_min; model <= model_max; ++model) { |
| 138 |
double t = 0.0; |
| 139 |
# ifndef NO_CLOCK |
| 140 |
CLOCK_TYPE tI, tF; |
| 141 |
|
| 142 |
GET_TIME(tI); |
| 143 |
# endif |
| 144 |
free_count = 0; |
| 145 |
for (i = 0; i < ALLOC_CNT; ++i) { |
| 146 |
int k = rand() % KEEP_CNT; |
| 147 |
keep_arr[k] = testobj_new(model); |
| 148 |
} |
| 149 |
GC_gcollect(); |
| 150 |
# ifndef NO_CLOCK |
| 151 |
GET_TIME(tF); |
| 152 |
t = MS_TIME_DIFF(tF, tI)*1e-3; |
| 153 |
# endif |
| 154 |
|
| 155 |
if (model < 2 && free_count > 0) |
| 156 |
printf("%20s: %12.4f %12g %12g\n", model_str[model], |
| 157 |
free_count/(double)ALLOC_CNT, t, t/free_count); |
| 158 |
else |
| 159 |
printf("%20s: %12.4f %12g %12s\n", |
| 160 |
model_str[model], 0.0, t, "N/A"); |
| 161 |
} |
| 162 |
return 0; |
| 163 |
} |