summaryrefslogtreecommitdiff
path: root/src/mapi/mapi/mapi.c
blob: b471c40b1447bbe52e3e45219a3ae2866d30f63b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
 * Mesa 3-D graphics library
 * Version:  7.9
 *
 * Copyright (C) 2010 LunarG Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 * Authors:
 *    Chia-I Wu <olv@lunarg.com>
 */

#include <stdlib.h>
#include <string.h>

#include "u_current.h"
#include "u_thread.h"
#include "mapi.h"
#include "stub.h"
#include "table.h"

/* dynamic stubs will run out before this array */
static const struct mapi_stub *mapi_stub_map[MAPI_TABLE_NUM_SLOTS];
static int mapi_num_stubs;

static const struct mapi_stub *
get_stub(const char *name, const struct mapi_stub *alias)
{
   const struct mapi_stub *stub;

   stub = stub_find_public(name);
   if (!stub) {
      struct mapi_stub *dyn = stub_find_dynamic(name, 1);
      if (dyn) {
         stub_fix_dynamic(dyn, alias);
         stub = dyn;
      }
   }

   return stub;
}

/**
 * Initialize mapi.  spec consists of NULL-separated strings.  The first string
 * denotes the version.  It is followed by variable numbers of entries.  Each
 * entry can have multiple names.  An empty name terminates an entry.  An empty
 * entry terminates the spec.  A spec of two entries, Foo and Bar, is as
 * follows
 *
 *   "1\0"
 *   "Foo\0"
 *   "FooEXT\0"
 *   "\0"
 *   "Bar\0"
 *   "\0"
 */
void
mapi_init(const char *spec)
{
   u_mutex_declare_static(mutex);
   const char *p;
   int ver, count;

   u_mutex_lock(mutex);

   /* already initialized */
   if (mapi_num_stubs) {
      u_mutex_unlock(mutex);
      return;
   }

   count = 0;
   p = spec;

   /* parse version string */
   ver = atoi(p);
   if (ver != 1) {
      u_mutex_unlock(mutex);
      return;
   }
   p += strlen(p) + 1;

   while (*p) {
      const struct mapi_stub *stub;

      stub = get_stub(p, NULL);
      /* out of dynamic entries */
      if (!stub)
         break;
      p += strlen(p) + 1;

      while (*p) {
         get_stub(p, stub);
         p += strlen(p) + 1;
      }

      mapi_stub_map[count++] = stub;
      p++;
   }

   mapi_num_stubs = count;

   u_mutex_unlock(mutex);
}

/**
 * Return the address of an entry.  Optionally generate the entry if it does
 * not exist.
 */
mapi_proc
mapi_get_proc_address(const char *name)
{
   const struct mapi_stub *stub;

   stub = stub_find_public(name);
   if (!stub)
      stub = stub_find_dynamic(name, 0);

   return (stub) ? (mapi_proc) stub_get_addr(stub) : NULL;
}

/**
 * Create a dispatch table.
 */
struct mapi_table *
mapi_table_create(void)
{
   const struct mapi_table *noop = table_get_noop();
   struct mapi_table *tbl;

   tbl = malloc(MAPI_TABLE_SIZE);
   if (tbl)
      memcpy(tbl, noop, MAPI_TABLE_SIZE);

   return tbl;
}

/**
 * Destroy a dispatch table.
 */
void
mapi_table_destroy(struct mapi_table *tbl)
{
   free(tbl);
}

/**
 * Fill a dispatch table.  The order of the procs is determined when mapi_init
 * is called.
 */
void
mapi_table_fill(struct mapi_table *tbl, const mapi_proc *procs)
{
   const struct mapi_table *noop = table_get_noop();
   int i;

   for (i = 0; i < mapi_num_stubs; i++) {
      const struct mapi_stub *stub = mapi_stub_map[i];
      int slot = stub_get_slot(stub);
      mapi_func func = (mapi_func) procs[i];

      if (!func)
         func = table_get_func(noop, slot);
      table_set_func(tbl, slot, func);
   }
}

/**
 * Make a dispatch table current.
 */
void
mapi_table_make_current(const struct mapi_table *tbl)
{
   u_current_set(tbl);
}