stub.c revision b8e80941
1848b8605Smrg/*
2848b8605Smrg * Mesa 3-D graphics library
3848b8605Smrg *
4848b8605Smrg * Copyright (C) 2010 LunarG Inc.
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 * Authors:
25848b8605Smrg *    Chia-I Wu <olv@lunarg.com>
26848b8605Smrg */
27848b8605Smrg
28848b8605Smrg#include <stdlib.h>
29848b8605Smrg#include <string.h>
30848b8605Smrg#include <assert.h>
31b8e80941Smrg#include "c11/threads.h"
32848b8605Smrg
33b8e80941Smrg#include "util/macros.h"
34848b8605Smrg#include "u_current.h"
35848b8605Smrg#include "entry.h"
36848b8605Smrg#include "stub.h"
37848b8605Smrg#include "table.h"
38848b8605Smrg
39848b8605Smrg
40848b8605Smrgstruct mapi_stub {
41848b8605Smrg   const void *name;
42848b8605Smrg   int slot;
43848b8605Smrg   mapi_func addr;
44848b8605Smrg};
45848b8605Smrg
46848b8605Smrg/* define public_string_pool and public_stubs */
47848b8605Smrg#define MAPI_TMP_PUBLIC_STUBS
48848b8605Smrg#include "mapi_tmp.h"
49848b8605Smrg
50848b8605Smrgstatic struct mapi_stub dynamic_stubs[MAPI_TABLE_NUM_DYNAMIC];
51848b8605Smrgstatic int num_dynamic_stubs;
52848b8605Smrgstatic int next_dynamic_slot = MAPI_TABLE_NUM_STATIC;
53848b8605Smrg
54848b8605Smrgvoid
55848b8605Smrgstub_init_once(void)
56848b8605Smrg{
57b8e80941Smrg   static once_flag flag = ONCE_FLAG_INIT;
58b8e80941Smrg   call_once(&flag, entry_patch_public);
59848b8605Smrg}
60848b8605Smrg
61848b8605Smrgstatic int
62848b8605Smrgstub_compare(const void *key, const void *elem)
63848b8605Smrg{
64848b8605Smrg   const char *name = (const char *) key;
65848b8605Smrg   const struct mapi_stub *stub = (const struct mapi_stub *) elem;
66848b8605Smrg   const char *stub_name;
67848b8605Smrg
68848b8605Smrg   stub_name = &public_string_pool[(unsigned long) stub->name];
69848b8605Smrg
70848b8605Smrg   return strcmp(name, stub_name);
71848b8605Smrg}
72848b8605Smrg
73848b8605Smrg/**
74848b8605Smrg * Return the public stub with the given name.
75848b8605Smrg */
76848b8605Smrgconst struct mapi_stub *
77848b8605Smrgstub_find_public(const char *name)
78848b8605Smrg{
79848b8605Smrg   return (const struct mapi_stub *) bsearch(name, public_stubs,
80848b8605Smrg         ARRAY_SIZE(public_stubs), sizeof(public_stubs[0]), stub_compare);
81848b8605Smrg}
82848b8605Smrg
83848b8605Smrg/**
84848b8605Smrg * Add a dynamic stub.
85848b8605Smrg */
86848b8605Smrgstatic struct mapi_stub *
87848b8605Smrgstub_add_dynamic(const char *name)
88848b8605Smrg{
89848b8605Smrg   struct mapi_stub *stub;
90848b8605Smrg   int idx;
91848b8605Smrg
92848b8605Smrg   idx = num_dynamic_stubs;
93848b8605Smrg   /* minus 1 to make sure we can never reach the last slot */
94848b8605Smrg   if (idx >= MAPI_TABLE_NUM_DYNAMIC - 1)
95848b8605Smrg      return NULL;
96848b8605Smrg
97848b8605Smrg   stub = &dynamic_stubs[idx];
98848b8605Smrg
99848b8605Smrg   /* dispatch to the last slot, which is reserved for no-op */
100848b8605Smrg   stub->addr = entry_generate(
101848b8605Smrg         MAPI_TABLE_NUM_STATIC + MAPI_TABLE_NUM_DYNAMIC - 1);
102848b8605Smrg   if (!stub->addr)
103848b8605Smrg      return NULL;
104848b8605Smrg
105b8e80941Smrg   stub->name = (const void *) strdup(name);
106848b8605Smrg   /* to be fixed later */
107848b8605Smrg   stub->slot = -1;
108848b8605Smrg
109848b8605Smrg   num_dynamic_stubs = idx + 1;
110848b8605Smrg
111848b8605Smrg   return stub;
112848b8605Smrg}
113848b8605Smrg
114848b8605Smrg/**
115848b8605Smrg * Return the dynamic stub with the given name.  If no such stub exists and
116848b8605Smrg * generate is true, a new stub is generated.
117848b8605Smrg */
118848b8605Smrgstruct mapi_stub *
119848b8605Smrgstub_find_dynamic(const char *name, int generate)
120848b8605Smrg{
121848b8605Smrg   static mtx_t dynamic_mutex = _MTX_INITIALIZER_NP;
122848b8605Smrg   struct mapi_stub *stub = NULL;
123848b8605Smrg   int count, i;
124848b8605Smrg
125848b8605Smrg   mtx_lock(&dynamic_mutex);
126848b8605Smrg
127848b8605Smrg   if (generate)
128848b8605Smrg      assert(!stub_find_public(name));
129848b8605Smrg
130848b8605Smrg   count = num_dynamic_stubs;
131848b8605Smrg   for (i = 0; i < count; i++) {
132848b8605Smrg      if (strcmp(name, (const char *) dynamic_stubs[i].name) == 0) {
133848b8605Smrg         stub = &dynamic_stubs[i];
134848b8605Smrg         break;
135848b8605Smrg      }
136848b8605Smrg   }
137848b8605Smrg
138848b8605Smrg   /* generate a dynamic stub */
139848b8605Smrg   if (generate && !stub)
140848b8605Smrg         stub = stub_add_dynamic(name);
141848b8605Smrg
142848b8605Smrg   mtx_unlock(&dynamic_mutex);
143848b8605Smrg
144848b8605Smrg   return stub;
145848b8605Smrg}
146848b8605Smrg
147848b8605Smrgstatic const struct mapi_stub *
148848b8605Smrgsearch_table_by_slot(const struct mapi_stub *table, size_t num_entries,
149848b8605Smrg                     int slot)
150848b8605Smrg{
151848b8605Smrg   size_t i;
152848b8605Smrg   for (i = 0; i < num_entries; ++i) {
153848b8605Smrg      if (table[i].slot == slot)
154848b8605Smrg         return &table[i];
155848b8605Smrg   }
156848b8605Smrg   return NULL;
157848b8605Smrg}
158848b8605Smrg
159848b8605Smrgconst struct mapi_stub *
160848b8605Smrgstub_find_by_slot(int slot)
161848b8605Smrg{
162848b8605Smrg   const struct mapi_stub *stub =
163848b8605Smrg      search_table_by_slot(public_stubs, ARRAY_SIZE(public_stubs), slot);
164848b8605Smrg   if (stub)
165848b8605Smrg      return stub;
166848b8605Smrg   return search_table_by_slot(dynamic_stubs, num_dynamic_stubs, slot);
167848b8605Smrg}
168848b8605Smrg
169848b8605Smrgvoid
170848b8605Smrgstub_fix_dynamic(struct mapi_stub *stub, const struct mapi_stub *alias)
171848b8605Smrg{
172848b8605Smrg   int slot;
173848b8605Smrg
174848b8605Smrg   if (stub->slot >= 0)
175848b8605Smrg      return;
176848b8605Smrg
177848b8605Smrg   if (alias)
178848b8605Smrg      slot = alias->slot;
179848b8605Smrg   else
180848b8605Smrg      slot = next_dynamic_slot++;
181848b8605Smrg
182848b8605Smrg   entry_patch(stub->addr, slot);
183848b8605Smrg   stub->slot = slot;
184848b8605Smrg}
185848b8605Smrg
186848b8605Smrg/**
187848b8605Smrg * Return the name of a stub.
188848b8605Smrg */
189848b8605Smrgconst char *
190848b8605Smrgstub_get_name(const struct mapi_stub *stub)
191848b8605Smrg{
192848b8605Smrg   const char *name;
193848b8605Smrg
194848b8605Smrg   if (stub >= public_stubs &&
195848b8605Smrg       stub < public_stubs + ARRAY_SIZE(public_stubs))
196848b8605Smrg      name = &public_string_pool[(unsigned long) stub->name];
197848b8605Smrg   else
198848b8605Smrg      name = (const char *) stub->name;
199848b8605Smrg
200848b8605Smrg   return name;
201848b8605Smrg}
202848b8605Smrg
203848b8605Smrg/**
204848b8605Smrg * Return the slot of a stub.
205848b8605Smrg */
206848b8605Smrgint
207848b8605Smrgstub_get_slot(const struct mapi_stub *stub)
208848b8605Smrg{
209848b8605Smrg   return stub->slot;
210848b8605Smrg}
211848b8605Smrg
212848b8605Smrg/**
213848b8605Smrg * Return the address of a stub.
214848b8605Smrg */
215848b8605Smrgmapi_func
216848b8605Smrgstub_get_addr(const struct mapi_stub *stub)
217848b8605Smrg{
218848b8605Smrg   assert(stub->addr || (unsigned int) stub->slot < MAPI_TABLE_NUM_STATIC);
219848b8605Smrg   return (stub->addr) ? stub->addr : entry_get_public(stub->slot);
220848b8605Smrg}
221