normalized doxygen comments
[libfirm] / ir / debug / seqnumbers.c
1 /*
2  * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief      Implements simple sequence numbers for Firm debug info.
23  * @author     Michael Beck
24  * @date       2005
25  * @version    $Id$
26  * @summary
27  *  Sequence numbers for Firm.
28  *
29  *  A sequence number is an unique number representing a filename
30  *  and a line number. The number 0 represents empty information.
31  *  This module is an optional "snap-in" for the Firm debug info.
32  */
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
36
37 #include "set.h"
38 #include "hashptr.h"
39 #include "ident.h"
40 #include "seqnumbers.h"
41
42 /**
43  * A entry in the sequence number table.
44  */
45 struct sn_entry {
46   ident    *filename;  /**< the filename */
47   unsigned lineno;     /**< the line number */
48 };
49
50 static set *seqnos = NULL;
51
52 /** hash a seqno entry */
53 #define HASH(key) (HASH_PTR((key).filename) ^ (key).lineno)
54
55 /**
56  * Compare two seqno entries.
57  */
58 static int seqno_cmp(const void *elt, const void *key, size_t size)
59 {
60   seqno_t e1 = (seqno_t)elt;
61   seqno_t e2 = (seqno_t)key;
62
63   return (e1->filename != e2->filename) | (e1->lineno - e2->lineno);
64 }
65
66 /*
67  * Create a new sequence number from a filename and a line number.
68  */
69 seqno_t firm_seqno_enter(const char *filename, unsigned lineno)
70 {
71   struct sn_entry key;
72
73   key.filename = new_id_from_str(filename);
74   key.lineno   = lineno;
75
76   return set_insert(seqnos, &key, sizeof(key), HASH(key));
77 }
78
79 /*
80  * Create a new sequence number from a filename ident and a line number.
81  */
82 seqno_t firm_seqno_enter_id(ident *filename, unsigned lineno)
83 {
84   struct sn_entry key;
85
86   key.filename = filename;
87   key.lineno   = lineno;
88
89   return set_insert(seqnos, &key, sizeof(key), HASH(key));
90 }
91
92 /**
93  * Retrieve filename and line number form a sequence number
94  */
95 const char *firm_seqno_retrieve(seqno_t seqno, unsigned *lineno)
96 {
97   if (seqnos && seqno) {
98     *lineno = seqno->lineno;
99     return get_id_str(seqno->filename);
100   }
101   *lineno = 0;
102   return NULL;
103 }
104
105 /*
106  * Creates the seqno pool.
107  */
108 void firm_seqno_init(void)
109 {
110   if (seqnos)
111     firm_seqno_term();
112
113   seqnos = new_set(seqno_cmp, 8);
114 }
115
116 /*
117  * Terminates the seqno pool.
118  * Sequence numbers cannot be resolved anymore.
119  */
120 void firm_seqno_term(void)
121 {
122   if (seqnos) {
123     del_set(seqnos);
124     seqnos = NULL;
125   }
126 }