+ free(shared);
+}
+
+
+/**
+ * gl_shared_state objects are ref counted.
+ * If ptr's refcount goes to zero, free the shared state.
+ */
+void
+_mesa_reference_shared_state(struct gl_context *ctx,
+ struct gl_shared_state **ptr,
+ struct gl_shared_state *state)
+{
+ if (*ptr == state)
+ return;
+
+ if (*ptr) {
+ /* unref old state */
+ struct gl_shared_state *old = *ptr;
+ GLboolean delete;
+
+ mtx_lock(&old->Mutex);
+ assert(old->RefCount >= 1);
+ old->RefCount--;
+ delete = (old->RefCount == 0);
+ mtx_unlock(&old->Mutex);
+
+ if (delete) {
+ free_shared_state(ctx, old);
+ }
+
+ *ptr = NULL;
+ }
+
+ if (state) {
+ /* reference new state */
+ mtx_lock(&state->Mutex);
+ state->RefCount++;
+ *ptr = state;
+ mtx_unlock(&state->Mutex);
+ }