altos/stmf0: Use double buffering for ChaosKey
[fw/altos] / src / scheme / tiny-test / ao_scheme_test.c
1 /*
2  * Copyright © 2016 Keith Packard <keithp@keithp.com>
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, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  */
14
15 #include "ao_scheme.h"
16 #include <stdio.h>
17
18 static char save_file[] = "scheme.image";
19
20 int
21 ao_scheme_os_save(void)
22 {
23         FILE    *save = fopen(save_file, "w");
24
25         if (!save) {
26                 perror(save_file);
27                 return 0;
28         }
29         fwrite(ao_scheme_pool, 1, AO_SCHEME_POOL_TOTAL, save);
30         fclose(save);
31         return 1;
32 }
33
34 int
35 ao_scheme_os_restore_save(struct ao_scheme_os_save *save, int offset)
36 {
37         FILE    *restore = fopen(save_file, "r");
38         size_t  ret;
39
40         if (!restore) {
41                 perror(save_file);
42                 return 0;
43         }
44         fseek(restore, offset, SEEK_SET);
45         ret = fread(save, sizeof (struct ao_scheme_os_save), 1, restore);
46         fclose(restore);
47         if (ret != 1)
48                 return 0;
49         return 1;
50 }
51
52 int
53 ao_scheme_os_restore(void)
54 {
55         FILE    *restore = fopen(save_file, "r");
56         size_t  ret;
57
58         if (!restore) {
59                 perror(save_file);
60                 return 0;
61         }
62         ret = fread(ao_scheme_pool, 1, AO_SCHEME_POOL_TOTAL, restore);
63         fclose(restore);
64         if (ret != AO_SCHEME_POOL_TOTAL)
65                 return 0;
66         return 1;
67 }
68
69 int
70 main (int argc, char **argv)
71 {
72         (void) argc;
73
74         while (*++argv) {
75                 FILE *in = fopen(*argv, "r");
76                 if (!in) {
77                         perror(*argv);
78                         exit(1);
79                 }
80                 ao_scheme_read_eval_print(in, stdout, false);
81                 fclose(in);
82         }
83         ao_scheme_read_eval_print(stdin, stdout, true);
84
85 #ifdef DBG_MEM_STATS
86         printf ("collects: full: %lu incremental %lu\n",
87                 ao_scheme_collects[AO_SCHEME_COLLECT_FULL],
88                 ao_scheme_collects[AO_SCHEME_COLLECT_INCREMENTAL]);
89
90         printf ("freed: full %lu incremental %lu\n",
91                 ao_scheme_freed[AO_SCHEME_COLLECT_FULL],
92                 ao_scheme_freed[AO_SCHEME_COLLECT_INCREMENTAL]);
93
94         printf("loops: full %lu incremental %lu\n",
95                 ao_scheme_loops[AO_SCHEME_COLLECT_FULL],
96                 ao_scheme_loops[AO_SCHEME_COLLECT_INCREMENTAL]);
97
98         printf("loops per collect: full %f incremental %f\n",
99                (double) ao_scheme_loops[AO_SCHEME_COLLECT_FULL] /
100                (double) ao_scheme_collects[AO_SCHEME_COLLECT_FULL],
101                (double) ao_scheme_loops[AO_SCHEME_COLLECT_INCREMENTAL] /
102                (double) ao_scheme_collects[AO_SCHEME_COLLECT_INCREMENTAL]);
103
104         printf("freed per collect: full %f incremental %f\n",
105                (double) ao_scheme_freed[AO_SCHEME_COLLECT_FULL] /
106                (double) ao_scheme_collects[AO_SCHEME_COLLECT_FULL],
107                (double) ao_scheme_freed[AO_SCHEME_COLLECT_INCREMENTAL] /
108                (double) ao_scheme_collects[AO_SCHEME_COLLECT_INCREMENTAL]);
109
110         printf("freed per loop: full %f incremental %f\n",
111                (double) ao_scheme_freed[AO_SCHEME_COLLECT_FULL] /
112                (double) ao_scheme_loops[AO_SCHEME_COLLECT_FULL],
113                (double) ao_scheme_freed[AO_SCHEME_COLLECT_INCREMENTAL] /
114                (double) ao_scheme_loops[AO_SCHEME_COLLECT_INCREMENTAL]);
115 #endif
116 }