sparc: split Mulh node into SMulh and UMulh
[libfirm] / support / libfirmprof / instrument.c
1 /**
2  * Helper code to output instrumentation results
3  *  @author Matthias Braun, Steven Schaefer
4  */
5 #include <stdio.h>
6 #include <string.h>
7 #include <stdlib.h>
8
9 /* Prevent the compiler from mangling the name of this function. */
10 void __init_firmprof(const char*, unsigned int*, size_t)
11      asm("__init_firmprof");
12
13 typedef struct _profile_counter_t {
14         const char *filename;
15         unsigned   *counters;
16         unsigned    len;
17         struct _profile_counter_t *next;
18 } profile_counter_t;
19
20 static profile_counter_t *counters = NULL;
21
22 static void write_profiles(void)
23 {
24         profile_counter_t *counter = counters;
25         while(counter != NULL) {
26                 profile_counter_t *next = counter->next;
27                 FILE *f = fopen(counter->filename, "wb");
28                 if (f == NULL) {
29                         perror("Warning: couldn't open file for writing profiling data");
30                 } else {
31                         fputs("firmprof", f);
32                         fwrite(counter->counters, counter->len * sizeof(unsigned), 1, f);
33                         fclose(f);
34                 }
35                 free(counter);
36                 counter = next;
37         }
38 }
39
40 /**
41  * Register a new profile counter. This is called by separate constructors
42  * for each translation unit. Incidentally, referring to this function as
43  * "__init_firmprof" is perfectly linker friendly.
44  */
45 void __init_firmprof(const char *filename,
46                       unsigned int *counts, size_t len)
47 {
48         static int initialized = 0;
49         profile_counter_t *counter;
50
51         if (!initialized) {
52                 initialized = 1;
53                 atexit(write_profiles);
54         }
55
56         counter = (profile_counter_t*) malloc(sizeof(*counter));
57         if (counter == NULL)
58                 return;
59
60         counter->filename = filename;
61         counter->counters = counts;
62         counter->next     = counters;
63         counter->len      = len;
64
65         counters = counter;
66 }