1848b8605Smrg/*
2848b8605Smrg * Mesa 3-D graphics library
3848b8605Smrg *
4848b8605Smrg * Copyright (C) 2009 Chia-I Wu <olv@0xlab.org>
5848b8605Smrg *
6848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a
7848b8605Smrg * copy of this software and associated documentation files (the "Software"),
8848b8605Smrg * to deal in the Software without restriction, including without limitation
9848b8605Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10848b8605Smrg * and/or sell copies of the Software, and to permit persons to whom the
11848b8605Smrg * Software is furnished to do so, subject to the following conditions:
12848b8605Smrg *
13848b8605Smrg * The above copyright notice and this permission notice shall be included
14848b8605Smrg * in all copies or substantial portions of the Software.
15848b8605Smrg *
16848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17848b8605Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18848b8605Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19848b8605Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20848b8605Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21848b8605Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22848b8605Smrg * DEALINGS IN THE SOFTWARE.
23848b8605Smrg */
24848b8605Smrg
25848b8605Smrg
26848b8605Smrg/**
27848b8605Smrg * \file remap.c
28848b8605Smrg * Remap table management.
29848b8605Smrg *
30848b8605Smrg * Entries in the dispatch table are either static or dynamic.  The
31848b8605Smrg * dispatch table is shared by mesa core and glapi.  When they are
32848b8605Smrg * built separately, it is possible that a static entry in mesa core
33848b8605Smrg * is dynamic, or assigned a different static offset, in glapi.  The
34848b8605Smrg * remap table is in charge of mapping a static entry in mesa core to
35848b8605Smrg * a dynamic entry, or the corresponding static entry, in glapi.
36848b8605Smrg */
37848b8605Smrg
38b8e80941Smrg#include <stdbool.h>
39848b8605Smrg#include "remap.h"
40848b8605Smrg#include "imports.h"
41848b8605Smrg#include "glapi/glapi.h"
42848b8605Smrg
43848b8605Smrg#define MAX_ENTRY_POINTS 16
44848b8605Smrg
45848b8605Smrg#define need_MESA_remap_table
46848b8605Smrg#include "main/remap_helper.h"
47b8e80941Smrg#include "errors.h"
48848b8605Smrg
49848b8605Smrg
50848b8605Smrg/* this is global for quick access */
51848b8605Smrgint driDispatchRemapTable[driDispatchRemapTable_size];
52848b8605Smrg
53848b8605Smrg
54848b8605Smrg/**
55848b8605Smrg * Map a function by its spec.  The function will be added to glapi,
56848b8605Smrg * and the dispatch offset will be returned.
57848b8605Smrg *
58848b8605Smrg * \param spec a '\0'-separated string array specifying a function.
59848b8605Smrg *        It begins with the parameter signature of the function,
60848b8605Smrg *        followed by the names of the entry points.  An empty entry
61848b8605Smrg *        point name terminates the array.
62848b8605Smrg *
63848b8605Smrg * \return the offset of the (re-)mapped function in the dispatch
64848b8605Smrg *         table, or -1.
65848b8605Smrg */
66b8e80941Smrgstatic int
67b8e80941Smrgmap_function_spec(const char *spec)
68848b8605Smrg{
69848b8605Smrg   const char *signature;
70848b8605Smrg   const char *names[MAX_ENTRY_POINTS + 1];
71b8e80941Smrg   int num_names = 0;
72848b8605Smrg
73848b8605Smrg   if (!spec)
74848b8605Smrg      return -1;
75848b8605Smrg
76848b8605Smrg   signature = spec;
77848b8605Smrg   spec += strlen(spec) + 1;
78848b8605Smrg
79848b8605Smrg   /* spec is terminated by an empty string */
80848b8605Smrg   while (*spec) {
81848b8605Smrg      names[num_names] = spec;
82848b8605Smrg      num_names++;
83848b8605Smrg      if (num_names >= MAX_ENTRY_POINTS)
84848b8605Smrg         break;
85848b8605Smrg      spec += strlen(spec) + 1;
86848b8605Smrg   }
87848b8605Smrg   if (!num_names)
88848b8605Smrg      return -1;
89848b8605Smrg
90848b8605Smrg   names[num_names] = NULL;
91848b8605Smrg
92848b8605Smrg   /* add the entry points to the dispatch table */
93848b8605Smrg   return _glapi_add_dispatch(names, signature);
94848b8605Smrg}
95848b8605Smrg
96848b8605Smrg
97848b8605Smrg/**
98848b8605Smrg * Initialize the remap table.  This is called in one_time_init().
99848b8605Smrg * The remap table needs to be initialized before calling the
100848b8605Smrg * CALL/GET/SET macros defined in main/dispatch.h.
101848b8605Smrg */
102b8e80941Smrgvoid
103b8e80941Smrg_mesa_init_remap_table(void)
104848b8605Smrg{
105b8e80941Smrg   static bool initialized = false;
106848b8605Smrg   GLint i;
107848b8605Smrg
108848b8605Smrg   if (initialized)
109848b8605Smrg      return;
110b8e80941Smrg   initialized = true;
111848b8605Smrg
112b8e80941Smrg   /* initialize the MESA_remap_table_functions table */
113b8e80941Smrg   for (i = 0; i < driDispatchRemapTable_size; i++) {
114b8e80941Smrg      int offset;
115848b8605Smrg      const char *spec;
116848b8605Smrg
117848b8605Smrg      /* sanity check */
118b8e80941Smrg      assert(i == MESA_remap_table_functions[i].remap_index);
119b8e80941Smrg      spec = _mesa_function_pool + MESA_remap_table_functions[i].pool_index;
120848b8605Smrg
121b8e80941Smrg      offset = map_function_spec(spec);
122b8e80941Smrg      /* store the dispatch offset in the MESA_remap_table_functions table */
123848b8605Smrg      driDispatchRemapTable[i] = offset;
124848b8605Smrg      if (offset < 0) {
125848b8605Smrg         const char *name = spec + strlen(spec) + 1;
126848b8605Smrg         _mesa_warning(NULL, "failed to remap %s", name);
127848b8605Smrg      }
128848b8605Smrg   }
129848b8605Smrg}
130