2 * Mesa 3-D graphics library
5 * Copyright (C) 2005-2006 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
26 * \file slang_utility.c
32 #include "slang_utility.h"
35 slang_string_concat (char *dst
, const char *src
)
37 return _mesa_strcpy (dst
+ _mesa_strlen (dst
), src
);
44 slang_string_init (slang_string
*self
)
49 self
->fail
= GL_FALSE
;
53 slang_string_free (slang_string
*self
)
55 if (self
->data
!= NULL
)
56 _mesa_free (self
->data
);
60 slang_string_reset (slang_string
*self
)
63 self
->fail
= GL_FALSE
;
67 grow (slang_string
*self
, GLuint size
)
71 if (size
> self
->capacity
) {
72 /* do not overflow 32-bit range */
73 assert (size
< 0x80000000);
75 self
->data
= (char *) (_mesa_realloc (self
->data
, self
->capacity
, size
* 2));
76 self
->capacity
= size
* 2;
77 if (self
->data
== NULL
) {
87 slang_string_push (slang_string
*self
, const slang_string
*str
)
93 if (grow (self
, self
->length
+ str
->length
)) {
94 _mesa_memcpy (&self
->data
[self
->length
], str
->data
, str
->length
);
95 self
->length
+= str
->length
;
100 slang_string_pushc (slang_string
*self
, const char c
)
102 if (grow (self
, self
->length
+ 1)) {
103 self
->data
[self
->length
] = c
;
109 slang_string_pushs (slang_string
*self
, const char *cstr
, GLuint len
)
111 if (grow (self
, self
->length
+ len
)) {
112 _mesa_memcpy (&self
->data
[self
->length
], cstr
, len
);
118 slang_string_pushi (slang_string
*self
, GLint i
)
122 _mesa_sprintf (buffer
, "%d", i
);
123 slang_string_pushs (self
, buffer
, strlen (buffer
));
127 slang_string_cstr (slang_string
*self
)
129 if (grow (self
, self
->length
+ 1))
130 self
->data
[self
->length
] = '\0';
134 /* slang_atom_pool */
137 slang_atom_pool_construct(slang_atom_pool
* pool
)
141 for (i
= 0; i
< SLANG_ATOM_POOL_SIZE
; i
++)
142 pool
->entries
[i
] = NULL
;
146 slang_atom_pool_destruct (slang_atom_pool
* pool
)
150 for (i
= 0; i
< SLANG_ATOM_POOL_SIZE
; i
++) {
151 slang_atom_entry
* entry
;
153 entry
= pool
->entries
[i
];
154 while (entry
!= NULL
) {
155 slang_atom_entry
*next
;
158 slang_alloc_free(entry
->id
);
159 slang_alloc_free(entry
);
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.
171 slang_atom_pool_atom(slang_atom_pool
* pool
, const char * id
)
175 slang_atom_entry
** entry
;
177 /* Hash a given string to a number in the range [0, ATOM_POOL_SIZE). */
182 hash
= (hash
<< 4) + (GLuint
) (*p
++);
183 g
= hash
& 0xf0000000;
188 hash
%= SLANG_ATOM_POOL_SIZE
;
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
;
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
)));
205 return SLANG_ATOM_NULL
;
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
;
217 * Return the name of a given atom.
220 slang_atom_pool_id(slang_atom_pool
* pool
, slang_atom atom
)
222 return (const char *) (atom
);