1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Mesa 3-D graphics library
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Copyright (C) 2009 Chia-I Wu <olv@0xlab.org>
5bf215546Sopenharmony_ci *
6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
8bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
9bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
11bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
12bf215546Sopenharmony_ci *
13bf215546Sopenharmony_ci * The above copyright notice and this permission notice shall be included
14bf215546Sopenharmony_ci * in all copies or substantial portions of the Software.
15bf215546Sopenharmony_ci *
16bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22bf215546Sopenharmony_ci * DEALINGS IN THE SOFTWARE.
23bf215546Sopenharmony_ci */
24bf215546Sopenharmony_ci
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ci/**
27bf215546Sopenharmony_ci * \file remap.c
28bf215546Sopenharmony_ci * Remap table management.
29bf215546Sopenharmony_ci *
30bf215546Sopenharmony_ci * Entries in the dispatch table are either static or dynamic.  The
31bf215546Sopenharmony_ci * dispatch table is shared by mesa core and glapi.  When they are
32bf215546Sopenharmony_ci * built separately, it is possible that a static entry in mesa core
33bf215546Sopenharmony_ci * is dynamic, or assigned a different static offset, in glapi.  The
34bf215546Sopenharmony_ci * remap table is in charge of mapping a static entry in mesa core to
35bf215546Sopenharmony_ci * a dynamic entry, or the corresponding static entry, in glapi.
36bf215546Sopenharmony_ci */
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_ci#include <stdbool.h>
39bf215546Sopenharmony_ci#include <string.h>
40bf215546Sopenharmony_ci#include "remap.h"
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_ci#include "glapi/glapi.h"
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_ci#define MAX_ENTRY_POINTS 16
45bf215546Sopenharmony_ci
46bf215546Sopenharmony_ci#define need_MESA_remap_table
47bf215546Sopenharmony_ci#include "main/remap_helper.h"
48bf215546Sopenharmony_ci#include "errors.h"
49bf215546Sopenharmony_ci
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ci/* this is global for quick access */
52bf215546Sopenharmony_ciint driDispatchRemapTable[driDispatchRemapTable_size];
53bf215546Sopenharmony_ci
54bf215546Sopenharmony_ci
55bf215546Sopenharmony_ci/**
56bf215546Sopenharmony_ci * Map a function by its spec.  The function will be added to glapi,
57bf215546Sopenharmony_ci * and the dispatch offset will be returned.
58bf215546Sopenharmony_ci *
59bf215546Sopenharmony_ci * \param spec a '\0'-separated string array specifying a function.
60bf215546Sopenharmony_ci *        It begins with the parameter signature of the function,
61bf215546Sopenharmony_ci *        followed by the names of the entry points.  An empty entry
62bf215546Sopenharmony_ci *        point name terminates the array.
63bf215546Sopenharmony_ci *
64bf215546Sopenharmony_ci * \return the offset of the (re-)mapped function in the dispatch
65bf215546Sopenharmony_ci *         table, or -1.
66bf215546Sopenharmony_ci */
67bf215546Sopenharmony_cistatic int
68bf215546Sopenharmony_cimap_function_spec(const char *spec)
69bf215546Sopenharmony_ci{
70bf215546Sopenharmony_ci   const char *signature;
71bf215546Sopenharmony_ci   const char *names[MAX_ENTRY_POINTS + 1];
72bf215546Sopenharmony_ci   int num_names = 0;
73bf215546Sopenharmony_ci
74bf215546Sopenharmony_ci   if (!spec)
75bf215546Sopenharmony_ci      return -1;
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_ci   signature = spec;
78bf215546Sopenharmony_ci   spec += strlen(spec) + 1;
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_ci   /* spec is terminated by an empty string */
81bf215546Sopenharmony_ci   while (*spec) {
82bf215546Sopenharmony_ci      names[num_names] = spec;
83bf215546Sopenharmony_ci      num_names++;
84bf215546Sopenharmony_ci      if (num_names >= MAX_ENTRY_POINTS)
85bf215546Sopenharmony_ci         break;
86bf215546Sopenharmony_ci      spec += strlen(spec) + 1;
87bf215546Sopenharmony_ci   }
88bf215546Sopenharmony_ci   if (!num_names)
89bf215546Sopenharmony_ci      return -1;
90bf215546Sopenharmony_ci
91bf215546Sopenharmony_ci   names[num_names] = NULL;
92bf215546Sopenharmony_ci
93bf215546Sopenharmony_ci   /* add the entry points to the dispatch table */
94bf215546Sopenharmony_ci   return _glapi_add_dispatch(names, signature);
95bf215546Sopenharmony_ci}
96bf215546Sopenharmony_ci
97bf215546Sopenharmony_ci
98bf215546Sopenharmony_ci/**
99bf215546Sopenharmony_ci * Initialize the remap table.  This is called in one_time_init().
100bf215546Sopenharmony_ci * The remap table needs to be initialized before calling the
101bf215546Sopenharmony_ci * CALL/GET/SET macros defined in main/dispatch.h.
102bf215546Sopenharmony_ci */
103bf215546Sopenharmony_civoid
104bf215546Sopenharmony_ci_mesa_init_remap_table(void)
105bf215546Sopenharmony_ci{
106bf215546Sopenharmony_ci   static bool initialized = false;
107bf215546Sopenharmony_ci   GLint i;
108bf215546Sopenharmony_ci
109bf215546Sopenharmony_ci   if (initialized)
110bf215546Sopenharmony_ci      return;
111bf215546Sopenharmony_ci   initialized = true;
112bf215546Sopenharmony_ci
113bf215546Sopenharmony_ci   /* initialize the MESA_remap_table_functions table */
114bf215546Sopenharmony_ci   for (i = 0; i < driDispatchRemapTable_size; i++) {
115bf215546Sopenharmony_ci      int offset;
116bf215546Sopenharmony_ci      const char *spec;
117bf215546Sopenharmony_ci
118bf215546Sopenharmony_ci      /* sanity check */
119bf215546Sopenharmony_ci      assert(i == MESA_remap_table_functions[i].remap_index);
120bf215546Sopenharmony_ci      spec = _mesa_function_pool + MESA_remap_table_functions[i].pool_index;
121bf215546Sopenharmony_ci
122bf215546Sopenharmony_ci      offset = map_function_spec(spec);
123bf215546Sopenharmony_ci      /* store the dispatch offset in the MESA_remap_table_functions table */
124bf215546Sopenharmony_ci      driDispatchRemapTable[i] = offset;
125bf215546Sopenharmony_ci      if (offset < 0) {
126bf215546Sopenharmony_ci         const char *name = spec + strlen(spec) + 1;
127bf215546Sopenharmony_ci         _mesa_warning(NULL, "failed to remap %s", name);
128bf215546Sopenharmony_ci      }
129bf215546Sopenharmony_ci   }
130bf215546Sopenharmony_ci}
131