mesa: use _mesa_HashFindFreeKeys for GL functions

This allows to implement name reuse if we want to.

Reviewed-by: Marek Olšák <marek.olsak@amd.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/6600>
diff --git a/src/mesa/main/arbprogram.c b/src/mesa/main/arbprogram.c
index b8e3aad..2e99aa4 100644
--- a/src/mesa/main/arbprogram.c
+++ b/src/mesa/main/arbprogram.c
@@ -217,7 +217,6 @@
 void GLAPIENTRY
 _mesa_GenProgramsARB(GLsizei n, GLuint *ids)
 {
-   GLuint first;
    GLuint i;
    GET_CURRENT_CONTEXT(ctx);
 
@@ -231,20 +230,15 @@
 
    _mesa_HashLockMutex(ctx->Shared->Programs);
 
-   first = _mesa_HashFindFreeKeyBlock(ctx->Shared->Programs, n);
+   _mesa_HashFindFreeKeys(ctx->Shared->Programs, ids, n);
 
    /* Insert pointer to dummy program as placeholder */
    for (i = 0; i < (GLuint) n; i++) {
-      _mesa_HashInsertLocked(ctx->Shared->Programs, first + i,
+      _mesa_HashInsertLocked(ctx->Shared->Programs, ids[i],
                              &_mesa_DummyProgram, true);
    }
 
    _mesa_HashUnlockMutex(ctx->Shared->Programs);
-
-   /* Return the program names */
-   for (i = 0; i < (GLuint) n; i++) {
-      ids[i] = first + i;
-   }
 }
 
 
diff --git a/src/mesa/main/arrayobj.c b/src/mesa/main/arrayobj.c
index 6ac122d..2d1b14b 100644
--- a/src/mesa/main/arrayobj.c
+++ b/src/mesa/main/arrayobj.c
@@ -1110,13 +1110,12 @@
 gen_vertex_arrays(struct gl_context *ctx, GLsizei n, GLuint *arrays,
                   bool create, const char *func)
 {
-   GLuint first;
    GLint i;
 
    if (!arrays)
       return;
 
-   first = _mesa_HashFindFreeKeyBlock(ctx->Array.Objects, n);
+   _mesa_HashFindFreeKeys(ctx->Array.Objects, arrays, n);
 
    /* For the sake of simplicity we create the array objects in both
     * the Gen* and Create* cases.  The only difference is the value of
@@ -1124,16 +1123,14 @@
     */
    for (i = 0; i < n; i++) {
       struct gl_vertex_array_object *obj;
-      GLuint name = first + i;
 
-      obj = _mesa_new_vao(ctx, name);
+      obj = _mesa_new_vao(ctx, arrays[i]);
       if (!obj) {
          _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
          return;
       }
       obj->EverBound = create;
       _mesa_HashInsertLocked(ctx->Array.Objects, obj->Name, obj, true);
-      arrays[i] = first + i;
    }
 }
 
diff --git a/src/mesa/main/bufferobj.c b/src/mesa/main/bufferobj.c
index 67eb9a4..50ff2d0 100644
--- a/src/mesa/main/bufferobj.c
+++ b/src/mesa/main/bufferobj.c
@@ -1621,7 +1621,6 @@
 static void
 create_buffers(struct gl_context *ctx, GLsizei n, GLuint *buffers, bool dsa)
 {
-   GLuint first;
    struct gl_buffer_object *buf;
 
    if (!buffers)
@@ -1632,14 +1631,13 @@
     */
    _mesa_HashLockMutex(ctx->Shared->BufferObjects);
 
-   first = _mesa_HashFindFreeKeyBlock(ctx->Shared->BufferObjects, n);
+   _mesa_HashFindFreeKeys(ctx->Shared->BufferObjects, buffers, n);
 
    /* Insert the ID and pointer into the hash table. If non-DSA, insert a
     * DummyBufferObject.  Otherwise, create a new buffer object and insert
     * it.
     */
    for (int i = 0; i < n; i++) {
-      buffers[i] = first + i;
       if (dsa) {
          assert(ctx->Driver.NewBufferObject);
          buf = ctx->Driver.NewBufferObject(ctx, buffers[i]);
diff --git a/src/mesa/main/externalobjects.c b/src/mesa/main/externalobjects.c
index c00e23d..6a76648 100644
--- a/src/mesa/main/externalobjects.c
+++ b/src/mesa/main/externalobjects.c
@@ -164,13 +164,10 @@
       return;
 
    _mesa_HashLockMutex(ctx->Shared->MemoryObjects);
-   GLuint first = _mesa_HashFindFreeKeyBlock(ctx->Shared->MemoryObjects, n);
-   if (first) {
+   if (_mesa_HashFindFreeKeys(ctx->Shared->MemoryObjects, memoryObjects, n)) {
       for (GLsizei i = 0; i < n; i++) {
          struct gl_memory_object *memObj;
 
-         memoryObjects[i] = first + i;
-
          /* allocate memory object */
          memObj = ctx->Driver.NewMemoryObject(ctx, memoryObjects[i]);
          if (!memObj) {
@@ -602,10 +599,8 @@
       return;
 
    _mesa_HashLockMutex(ctx->Shared->SemaphoreObjects);
-   GLuint first = _mesa_HashFindFreeKeyBlock(ctx->Shared->SemaphoreObjects, n);
-   if (first) {
+   if (_mesa_HashFindFreeKeys(ctx->Shared->SemaphoreObjects, semaphores, n)) {
       for (GLsizei i = 0; i < n; i++) {
-         semaphores[i] = first + i;
          _mesa_HashInsertLocked(ctx->Shared->SemaphoreObjects,
                                 semaphores[i], &DummySemaphoreObject, true);
       }
diff --git a/src/mesa/main/fbobject.c b/src/mesa/main/fbobject.c
index f4a0e8a..54d2a08 100644
--- a/src/mesa/main/fbobject.c
+++ b/src/mesa/main/fbobject.c
@@ -2026,7 +2026,6 @@
                       bool dsa)
 {
    const char *func = dsa ? "glCreateRenderbuffers" : "glGenRenderbuffers";
-   GLuint first;
    GLint i;
 
    if (!renderbuffers)
@@ -2034,17 +2033,14 @@
 
    _mesa_HashLockMutex(ctx->Shared->RenderBuffers);
 
-   first = _mesa_HashFindFreeKeyBlock(ctx->Shared->RenderBuffers, n);
+   _mesa_HashFindFreeKeys(ctx->Shared->RenderBuffers, renderbuffers, n);
 
    for (i = 0; i < n; i++) {
-      GLuint name = first + i;
-      renderbuffers[i] = name;
-
       if (dsa) {
-         allocate_renderbuffer_locked(ctx, name, true, func);
+         allocate_renderbuffer_locked(ctx, renderbuffers[i], true, func);
       } else {
          /* insert a dummy renderbuffer into the hash table */
-         _mesa_HashInsertLocked(ctx->Shared->RenderBuffers, name,
+         _mesa_HashInsertLocked(ctx->Shared->RenderBuffers, renderbuffers[i],
                                 &DummyRenderbuffer, true);
       }
    }
@@ -3211,7 +3207,6 @@
 create_framebuffers(GLsizei n, GLuint *framebuffers, bool dsa)
 {
    GET_CURRENT_CONTEXT(ctx);
-   GLuint first;
    GLint i;
    struct gl_framebuffer *fb;
 
@@ -3227,12 +3222,9 @@
 
    _mesa_HashLockMutex(ctx->Shared->FrameBuffers);
 
-   first = _mesa_HashFindFreeKeyBlock(ctx->Shared->FrameBuffers, n);
+   _mesa_HashFindFreeKeys(ctx->Shared->FrameBuffers, framebuffers, n);
 
    for (i = 0; i < n; i++) {
-      GLuint name = first + i;
-      framebuffers[i] = name;
-
       if (dsa) {
          fb = ctx->Driver.NewFramebuffer(ctx, framebuffers[i]);
          if (!fb) {
@@ -3244,7 +3236,8 @@
       else
          fb = &DummyFramebuffer;
 
-      _mesa_HashInsertLocked(ctx->Shared->FrameBuffers, name, fb, true);
+      _mesa_HashInsertLocked(ctx->Shared->FrameBuffers, framebuffers[i],
+                             fb, true);
    }
 
    _mesa_HashUnlockMutex(ctx->Shared->FrameBuffers);
diff --git a/src/mesa/main/performance_monitor.c b/src/mesa/main/performance_monitor.c
index d485aca..d7ab836 100644
--- a/src/mesa/main/performance_monitor.c
+++ b/src/mesa/main/performance_monitor.c
@@ -338,7 +338,6 @@
 void GLAPIENTRY
 _mesa_GenPerfMonitorsAMD(GLsizei n, GLuint *monitors)
 {
-   GLuint first;
    GET_CURRENT_CONTEXT(ctx);
 
    if (MESA_VERBOSE & VERBOSE_API)
@@ -354,21 +353,16 @@
    if (monitors == NULL)
       return;
 
-   /* We don't actually need them to be contiguous, but this is what
-    * the rest of Mesa does, so we may as well.
-    */
-   first = _mesa_HashFindFreeKeyBlock(ctx->PerfMonitor.Monitors, n);
-   if (first) {
+   if (_mesa_HashFindFreeKeys(ctx->PerfMonitor.Monitors, monitors, n)) {
       GLsizei i;
       for (i = 0; i < n; i++) {
          struct gl_perf_monitor_object *m =
-            new_performance_monitor(ctx, first + i);
+            new_performance_monitor(ctx, monitors[i]);
          if (!m) {
             _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenPerfMonitorsAMD");
             return;
          }
-         monitors[i] = first + i;
-         _mesa_HashInsert(ctx->PerfMonitor.Monitors, first + i, m, true);
+         _mesa_HashInsert(ctx->PerfMonitor.Monitors, monitors[i], m, true);
       }
    } else {
       _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenPerfMonitorsAMD");
diff --git a/src/mesa/main/pipelineobj.c b/src/mesa/main/pipelineobj.c
index 5bbd886..3ec5cdb 100644
--- a/src/mesa/main/pipelineobj.c
+++ b/src/mesa/main/pipelineobj.c
@@ -596,19 +596,17 @@
                          bool dsa)
 {
    const char *func = dsa ? "glCreateProgramPipelines" : "glGenProgramPipelines";
-   GLuint first;
    GLint i;
 
    if (!pipelines)
       return;
 
-   first = _mesa_HashFindFreeKeyBlock(ctx->Pipeline.Objects, n);
+   _mesa_HashFindFreeKeys(ctx->Pipeline.Objects, pipelines, n);
 
    for (i = 0; i < n; i++) {
       struct gl_pipeline_object *obj;
-      GLuint name = first + i;
 
-      obj = _mesa_new_pipeline_object(ctx, name);
+      obj = _mesa_new_pipeline_object(ctx, pipelines[i]);
       if (!obj) {
          _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
          return;
@@ -620,7 +618,6 @@
       }
 
       save_pipeline_object(ctx, obj);
-      pipelines[i] = first + i;
    }
 }
 
diff --git a/src/mesa/main/queryobj.c b/src/mesa/main/queryobj.c
index 8c66808..7696721 100644
--- a/src/mesa/main/queryobj.c
+++ b/src/mesa/main/queryobj.c
@@ -262,7 +262,6 @@
                bool dsa)
 {
    const char *func = dsa ? "glGenQueries" : "glCreateQueries";
-   GLuint first;
 
    if (MESA_VERBOSE & VERBOSE_API)
       _mesa_debug(ctx, "%s(%d)\n", func, n);
@@ -272,12 +271,11 @@
       return;
    }
 
-   first = _mesa_HashFindFreeKeyBlock(ctx->Query.QueryObjects, n);
-   if (first) {
+   if (_mesa_HashFindFreeKeys(ctx->Query.QueryObjects, ids, n)) {
       GLsizei i;
       for (i = 0; i < n; i++) {
          struct gl_query_object *q
-            = ctx->Driver.NewQueryObject(ctx, first + i);
+            = ctx->Driver.NewQueryObject(ctx, ids[i]);
          if (!q) {
             _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
             return;
@@ -286,8 +284,7 @@
             q->Target = target;
             q->EverBound = GL_TRUE;
          }
-         ids[i] = first + i;
-         _mesa_HashInsertLocked(ctx->Query.QueryObjects, first + i, q, true);
+         _mesa_HashInsertLocked(ctx->Query.QueryObjects, ids[i], q, true);
       }
    }
 }
diff --git a/src/mesa/main/samplerobj.c b/src/mesa/main/samplerobj.c
index 9fd857b..bc58a5c 100644
--- a/src/mesa/main/samplerobj.c
+++ b/src/mesa/main/samplerobj.c
@@ -157,7 +157,6 @@
 create_samplers(struct gl_context *ctx, GLsizei count, GLuint *samplers,
                 const char *caller)
 {
-   GLuint first;
    GLint i;
 
    if (!samplers)
@@ -165,22 +164,21 @@
 
    _mesa_HashLockMutex(ctx->Shared->SamplerObjects);
 
-   first = _mesa_HashFindFreeKeyBlock(ctx->Shared->SamplerObjects, count);
+   _mesa_HashFindFreeKeys(ctx->Shared->SamplerObjects, samplers, count);
 
    /* Insert the ID and pointer to new sampler object into hash table */
    for (i = 0; i < count; i++) {
       struct gl_sampler_object *sampObj;
-      GLuint name = first + i;
 
-      sampObj = ctx->Driver.NewSamplerObject(ctx, name);
+      sampObj = ctx->Driver.NewSamplerObject(ctx, samplers[i]);
       if (!sampObj) {
          _mesa_HashUnlockMutex(ctx->Shared->SamplerObjects);
          _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", caller);
          return;
       }
 
-      _mesa_HashInsertLocked(ctx->Shared->SamplerObjects, name, sampObj, true);
-      samplers[i] = name;
+      _mesa_HashInsertLocked(ctx->Shared->SamplerObjects, samplers[i],
+                             sampObj, true);
    }
 
    _mesa_HashUnlockMutex(ctx->Shared->SamplerObjects);
diff --git a/src/mesa/main/texobj.c b/src/mesa/main/texobj.c
index a81838e..1d057b8 100644
--- a/src/mesa/main/texobj.c
+++ b/src/mesa/main/texobj.c
@@ -1212,7 +1212,6 @@
 create_textures(struct gl_context *ctx, GLenum target,
                 GLsizei n, GLuint *textures, const char *caller)
 {
-   GLuint first;
    GLint i;
 
    if (!textures)
@@ -1223,13 +1222,12 @@
     */
    _mesa_HashLockMutex(ctx->Shared->TexObjects);
 
-   first = _mesa_HashFindFreeKeyBlock(ctx->Shared->TexObjects, n);
+   _mesa_HashFindFreeKeys(ctx->Shared->TexObjects, textures, n);
 
    /* Allocate new, empty texture objects */
    for (i = 0; i < n; i++) {
       struct gl_texture_object *texObj;
-      GLuint name = first + i;
-      texObj = ctx->Driver.NewTextureObject(ctx, name, target);
+      texObj = ctx->Driver.NewTextureObject(ctx, textures[i], target);
       if (!texObj) {
          _mesa_HashUnlockMutex(ctx->Shared->TexObjects);
          _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", caller);
@@ -1238,8 +1236,6 @@
 
       /* insert into hash table */
       _mesa_HashInsertLocked(ctx->Shared->TexObjects, texObj->Name, texObj, true);
-
-      textures[i] = name;
    }
 
    _mesa_HashUnlockMutex(ctx->Shared->TexObjects);
@@ -1277,7 +1273,7 @@
  *
  * \sa glGenTextures(), glCreateTextures().
  *
- * Calls _mesa_HashFindFreeKeyBlock() to find a block of free texture
+ * Calls _mesa_HashFindFreeKeys() to find a block of free texture
  * IDs which are stored in \p textures.  Corresponding empty texture
  * objects are also generated.
  */
@@ -1305,7 +1301,7 @@
  *
  * \sa glCreateTextures(), glGenTextures().
  *
- * Calls _mesa_HashFindFreeKeyBlock() to find a block of free texture
+ * Calls _mesa_HashFindFreeKeys() to find a block of free texture
  * IDs which are stored in \p textures.  Corresponding empty texture
  * objects are also generated.
  */
diff --git a/src/mesa/main/transformfeedback.c b/src/mesa/main/transformfeedback.c
index b3b7f1e..6965635 100644
--- a/src/mesa/main/transformfeedback.c
+++ b/src/mesa/main/transformfeedback.c
@@ -1054,7 +1054,6 @@
 create_transform_feedbacks(struct gl_context *ctx, GLsizei n, GLuint *ids,
                            bool dsa)
 {
-   GLuint first;
    const char* func;
 
    if (dsa)
@@ -1070,19 +1069,16 @@
    if (!ids)
       return;
 
-   /* we don't need contiguous IDs, but this might be faster */
-   first = _mesa_HashFindFreeKeyBlock(ctx->TransformFeedback.Objects, n);
-   if (first) {
+   if (_mesa_HashFindFreeKeys(ctx->TransformFeedback.Objects, ids, n)) {
       GLsizei i;
       for (i = 0; i < n; i++) {
          struct gl_transform_feedback_object *obj
-            = ctx->Driver.NewTransformFeedback(ctx, first + i);
+            = ctx->Driver.NewTransformFeedback(ctx, ids[i]);
          if (!obj) {
             _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
             return;
          }
-         ids[i] = first + i;
-         _mesa_HashInsertLocked(ctx->TransformFeedback.Objects, first + i,
+         _mesa_HashInsertLocked(ctx->TransformFeedback.Objects, ids[i],
                                 obj, true);
          if (dsa) {
             /* this is normally done at bind time in the non-dsa case */