1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * \file glapi_getproc.c
27 *
28 * Code for implementing glXGetProcAddress(), etc.
29 * This was originally in glapi.c but refactored out.
30 */
31
32
33 #include <assert.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include "glapi/glapi_priv.h"
37 #include "glapitable.h"
38
39
40 /**********************************************************************
41 * Static function management.
42 */
43
44
45 #if !defined(DISPATCH_FUNCTION_SIZE)
46 # define NEED_FUNCTION_POINTER
47 #endif
48 #include "glprocs.h"
49
50
51 /**
52 * Search the table of static entrypoint functions for the named function
53 * and return the corresponding glprocs_table_t entry.
54 */
55 static const glprocs_table_t *
get_static_proc(const char * n)56 get_static_proc(const char *n)
57 {
58 GLuint i;
59 for (i = 0; static_functions[i].Name_offset >= 0; i++) {
60 const char *testName = gl_string_table + static_functions[i].Name_offset;
61 if (strcmp(testName, n) == 0)
62 {
63 return &static_functions[i];
64 }
65 }
66 return NULL;
67 }
68
69
70 /**
71 * Return dispatch table offset of the named static (built-in) function.
72 * Return -1 if function not found.
73 */
74 static GLint
get_static_proc_offset(const char * funcName)75 get_static_proc_offset(const char *funcName)
76 {
77 const glprocs_table_t *const f = get_static_proc(funcName);
78 if (f == NULL) {
79 return -1;
80 }
81
82 return f->Offset;
83 }
84
85
86 /**********************************************************************
87 * Extension function management.
88 */
89
90 /**
91 * Initializes the glapi relocs table, and returns the offset of the given
92 * function in the dispatch table.
93 */
94 int
_glapi_add_dispatch(const char * funcName)95 _glapi_add_dispatch(const char *funcName)
96 {
97 init_glapi_relocs_once();
98
99 return get_static_proc_offset(funcName);
100 }
101
102 /**
103 * Return offset of entrypoint for named function within dispatch table.
104 */
105 GLint
_glapi_get_proc_offset(const char * funcName)106 _glapi_get_proc_offset(const char *funcName)
107 {
108 /* search static functions */
109 return get_static_proc_offset(funcName);
110 }
111
112
113
114 /**
115 * Return dispatch function address for the named static (built-in) function.
116 * Return NULL if function not found.
117 */
118 _glapi_proc
_glapi_get_proc_address(const char * funcName)119 _glapi_get_proc_address(const char *funcName)
120 {
121 init_glapi_relocs_once();
122
123 if (!funcName || funcName[0] != 'g' || funcName[1] != 'l')
124 return NULL;
125
126 const glprocs_table_t *const f = get_static_proc(funcName);
127 if (f == NULL) {
128 return NULL;
129 }
130
131 #if defined(DISPATCH_FUNCTION_SIZE) && defined(GLX_INDIRECT_RENDERING)
132 return (f->Address == NULL)
133 ? get_entrypoint_address(f->Offset)
134 : f->Address;
135 #elif defined(DISPATCH_FUNCTION_SIZE)
136 return get_entrypoint_address(f->Offset);
137 #else
138 return f->Address;
139 #endif
140 }
141
142
143
144 /**
145 * Return the name of the function at the given dispatch offset.
146 * This is only intended for debugging.
147 */
148 const char *
_glapi_get_proc_name(GLuint offset)149 _glapi_get_proc_name(GLuint offset)
150 {
151 GLuint i;
152 for (i = 0; static_functions[i].Name_offset >= 0; i++) {
153 if (static_functions[i].Offset == offset) {
154 return gl_string_table + static_functions[i].Name_offset;
155 }
156 }
157 return NULL;
158 }
159
160
161
162 /**********************************************************************
163 * GL API table functions.
164 */
165
166
167 /**
168 * Return size of dispatch table struct as number of functions (or
169 * slots).
170 */
171 GLuint
_glapi_get_dispatch_table_size(void)172 _glapi_get_dispatch_table_size(void)
173 {
174 return sizeof(struct _glapi_table) / sizeof(void *);
175 }
176