new varnames in slang_operation_insert()
[mesa.git] / src / mesa / shader / slang / slang_utility.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.6
4 *
5 * Copyright (C) 2005-2006 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * \file slang_utility.c
27 * slang utilities
28 * \author Michal Krol
29 */
30
31 #include "imports.h"
32 #include "slang_utility.h"
33
34 char *
35 slang_string_concat (char *dst, const char *src)
36 {
37 return _mesa_strcpy (dst + _mesa_strlen (dst), src);
38 }
39
40
41 /* slang_string */
42
43 GLvoid
44 slang_string_init (slang_string *self)
45 {
46 self->data = NULL;
47 self->capacity = 0;
48 self->length = 0;
49 self->fail = GL_FALSE;
50 }
51
52 GLvoid
53 slang_string_free (slang_string *self)
54 {
55 if (self->data != NULL)
56 _mesa_free (self->data);
57 }
58
59 GLvoid
60 slang_string_reset (slang_string *self)
61 {
62 self->length = 0;
63 self->fail = GL_FALSE;
64 }
65
66 static GLboolean
67 grow (slang_string *self, GLuint size)
68 {
69 if (self->fail)
70 return GL_FALSE;
71 if (size > self->capacity) {
72 /* do not overflow 32-bit range */
73 assert (size < 0x80000000);
74
75 self->data = (char *) (_mesa_realloc (self->data, self->capacity, size * 2));
76 self->capacity = size * 2;
77 if (self->data == NULL) {
78 self->capacity = 0;
79 self->fail = GL_TRUE;
80 return GL_FALSE;
81 }
82 }
83 return GL_TRUE;
84 }
85
86 GLvoid
87 slang_string_push (slang_string *self, const slang_string *str)
88 {
89 if (str->fail) {
90 self->fail = GL_TRUE;
91 return;
92 }
93 if (grow (self, self->length + str->length)) {
94 _mesa_memcpy (&self->data[self->length], str->data, str->length);
95 self->length += str->length;
96 }
97 }
98
99 GLvoid
100 slang_string_pushc (slang_string *self, const char c)
101 {
102 if (grow (self, self->length + 1)) {
103 self->data[self->length] = c;
104 self->length++;
105 }
106 }
107
108 GLvoid
109 slang_string_pushs (slang_string *self, const char *cstr, GLuint len)
110 {
111 if (grow (self, self->length + len)) {
112 _mesa_memcpy (&self->data[self->length], cstr, len);
113 self->length += len;
114 }
115 }
116
117 GLvoid
118 slang_string_pushi (slang_string *self, GLint i)
119 {
120 char buffer[12];
121
122 _mesa_sprintf (buffer, "%d", i);
123 slang_string_pushs (self, buffer, strlen (buffer));
124 }
125
126 const char *
127 slang_string_cstr (slang_string *self)
128 {
129 if (grow (self, self->length + 1))
130 self->data[self->length] = '\0';
131 return self->data;
132 }
133
134 /* slang_atom_pool */
135
136 void
137 slang_atom_pool_construct(slang_atom_pool * pool)
138 {
139 GLuint i;
140
141 for (i = 0; i < SLANG_ATOM_POOL_SIZE; i++)
142 pool->entries[i] = NULL;
143 }
144
145 void
146 slang_atom_pool_destruct (slang_atom_pool * pool)
147 {
148 GLuint i;
149
150 for (i = 0; i < SLANG_ATOM_POOL_SIZE; i++) {
151 slang_atom_entry * entry;
152
153 entry = pool->entries[i];
154 while (entry != NULL) {
155 slang_atom_entry *next;
156
157 next = entry->next;
158 slang_alloc_free(entry->id);
159 slang_alloc_free(entry);
160 entry = next;
161 }
162 }
163 }
164
165 /*
166 * Search the atom pool for an atom with a given name.
167 * If atom is not found, create and add it to the pool.
168 * Returns ATOM_NULL if the atom was not found and the function failed to create a new atom.
169 */
170 slang_atom
171 slang_atom_pool_atom(slang_atom_pool * pool, const char * id)
172 {
173 GLuint hash;
174 const char * p = id;
175 slang_atom_entry ** entry;
176
177 /* Hash a given string to a number in the range [0, ATOM_POOL_SIZE). */
178 hash = 0;
179 while (*p != '\0') {
180 GLuint g;
181
182 hash = (hash << 4) + (GLuint) (*p++);
183 g = hash & 0xf0000000;
184 if (g != 0)
185 hash ^= g >> 24;
186 hash &= ~g;
187 }
188 hash %= SLANG_ATOM_POOL_SIZE;
189
190 /* Now the hash points to a linked list of atoms with names that have the same hash value.
191 * Search the linked list for a given name. */
192 entry = &pool->entries[hash];
193 while (*entry != NULL) {
194 /* If the same, return the associated atom. */
195 if (slang_string_compare((**entry).id, id) == 0)
196 return (slang_atom) (**entry).id;
197 /* Grab the next atom in the linked list. */
198 entry = &(**entry).next;
199 }
200
201 /* Okay, we have not found an atom. Create a new entry for it.
202 * Note that the <entry> points to the last entry's <next> field. */
203 *entry = (slang_atom_entry *) (slang_alloc_malloc(sizeof(slang_atom_entry)));
204 if (*entry == NULL)
205 return SLANG_ATOM_NULL;
206
207 /* Initialize a new entry. Because we'll need the actual name of the atom, we use the pointer
208 * to this string as an actual atom's value. */
209 (**entry).next = NULL;
210 (**entry).id = slang_string_duplicate(id);
211 if ((**entry).id == NULL)
212 return SLANG_ATOM_NULL;
213 return (slang_atom) (**entry).id;
214 }
215
216 /**
217 * Return the name of a given atom.
218 */
219 const char *
220 slang_atom_pool_id(slang_atom_pool * pool, slang_atom atom)
221 {
222 return (const char *) (atom);
223 }