2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
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:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
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.
26 * \file glapi_getproc.c
28 * Code for implementing glXGetProcAddress(), etc.
29 * This was originally in glapi.c but refactored out.
36 #include "glapi/glapi_priv.h"
37 #include "glapitable.h"
40 #define FIRST_DYNAMIC_OFFSET (sizeof(struct _glapi_table) / sizeof(void *))
44 /**********************************************************************
45 * Static function management.
49 #if !defined(DISPATCH_FUNCTION_SIZE)
50 # define NEED_FUNCTION_POINTER
56 * Search the table of static entrypoint functions for the named function
57 * and return the corresponding glprocs_table_t entry.
59 static const glprocs_table_t
*
60 get_static_proc( const char * n
)
63 for (i
= 0; static_functions
[i
].Name_offset
>= 0; i
++) {
64 const char *testName
= gl_string_table
+ static_functions
[i
].Name_offset
;
65 if (strcmp(testName
, n
) == 0)
67 return &static_functions
[i
];
75 * Return dispatch table offset of the named static (built-in) function.
76 * Return -1 if function not found.
79 get_static_proc_offset(const char *funcName
)
81 const glprocs_table_t
* const f
= get_static_proc( funcName
);
92 * Return dispatch function address for the named static (built-in) function.
93 * Return NULL if function not found.
96 get_static_proc_address(const char *funcName
)
98 const glprocs_table_t
* const f
= get_static_proc( funcName
);
103 #if defined(DISPATCH_FUNCTION_SIZE) && defined(GLX_INDIRECT_RENDERING)
104 return (f
->Address
== NULL
)
105 ? get_entrypoint_address(f
->Offset
)
107 #elif defined(DISPATCH_FUNCTION_SIZE)
108 return get_entrypoint_address(f
->Offset
);
117 * Return the name of the function at the given offset in the dispatch
118 * table. For debugging only.
121 get_static_proc_name( GLuint offset
)
124 for (i
= 0; static_functions
[i
].Name_offset
>= 0; i
++) {
125 if (static_functions
[i
].Offset
== offset
) {
126 return gl_string_table
+ static_functions
[i
].Name_offset
;
134 /**********************************************************************
135 * Extension function management.
140 * Track information about a function added to the GL API.
142 struct _glapi_function
{
144 * Name of the function.
150 * Text string that describes the types of the parameters passed to the
151 * named function. Parameter types are converted to characters using the
153 * - 'i' for \c GLint, \c GLuint, and \c GLenum
154 * - 'p' for any pointer type
155 * - 'f' for \c GLfloat and \c GLclampf
156 * - 'd' for \c GLdouble and \c GLclampd
158 const char * parameter_signature
;
162 * Offset in the dispatch table where the pointer to the real function is
163 * located. If the driver has not requested that the named function be
164 * added to the dispatch table, this will have the value ~0.
166 unsigned dispatch_offset
;
170 * Pointer to the dispatch stub for the named function.
173 * The semantic of this field should be changed slightly. Currently, it
174 * is always expected to be non-\c NULL. However, it would be better to
175 * only allocate the entry-point stub when the application requests the
176 * function via \c glXGetProcAddress. This would save memory for all the
177 * functions that the driver exports but that the application never wants
180 _glapi_proc dispatch_stub
;
184 static struct _glapi_function ExtEntryTable
[MAX_EXTENSION_FUNCS
];
185 static GLuint NumExtEntryPoints
= 0;
188 static struct _glapi_function
*
189 get_extension_proc(const char *funcName
)
192 for (i
= 0; i
< NumExtEntryPoints
; i
++) {
193 if (strcmp(ExtEntryTable
[i
].name
, funcName
) == 0) {
194 return & ExtEntryTable
[i
];
202 get_extension_proc_offset(const char *funcName
)
204 const struct _glapi_function
* const f
= get_extension_proc( funcName
);
209 return f
->dispatch_offset
;
214 get_extension_proc_address(const char *funcName
)
216 const struct _glapi_function
* const f
= get_extension_proc( funcName
);
221 return f
->dispatch_stub
;
226 get_extension_proc_name(GLuint offset
)
229 for (i
= 0; i
< NumExtEntryPoints
; i
++) {
230 if (ExtEntryTable
[i
].dispatch_offset
== offset
) {
231 return ExtEntryTable
[i
].name
;
239 * strdup() is actually not a standard ANSI C or POSIX routine.
240 * Irix will not define it if ANSI mode is in effect.
243 str_dup(const char *str
)
246 copy
= malloc(strlen(str
) + 1);
255 * Generate new entrypoint
257 * Use a temporary dispatch offset of ~0 (i.e. -1). Later, when the driver
258 * calls \c _glapi_add_dispatch we'll put in the proper offset. If that
259 * never happens, and the user calls this function, he'll segfault. That's
260 * what you get when you try calling a GL function that doesn't really exist.
262 * \param funcName Name of the function to create an entry-point for.
264 * \sa _glapi_add_entrypoint
267 static struct _glapi_function
*
268 add_function_name( const char * funcName
)
270 struct _glapi_function
* entry
= NULL
;
271 _glapi_proc entrypoint
= NULL
;
272 char * name_dup
= NULL
;
274 if (NumExtEntryPoints
>= MAX_EXTENSION_FUNCS
)
277 if (funcName
== NULL
)
280 name_dup
= str_dup(funcName
);
281 if (name_dup
== NULL
)
284 entrypoint
= generate_entrypoint(~0);
286 if (entrypoint
== NULL
) {
291 entry
= & ExtEntryTable
[NumExtEntryPoints
];
294 entry
->name
= name_dup
;
295 entry
->parameter_signature
= NULL
;
296 entry
->dispatch_offset
= ~0;
297 entry
->dispatch_stub
= entrypoint
;
303 static struct _glapi_function
*
304 set_entry_info( struct _glapi_function
* entry
, const char * signature
, unsigned offset
)
306 char * sig_dup
= NULL
;
308 if (signature
== NULL
)
311 sig_dup
= str_dup(signature
);
315 fill_in_entrypoint_offset(entry
->dispatch_stub
, offset
);
317 entry
->parameter_signature
= sig_dup
;
318 entry
->dispatch_offset
= offset
;
325 * Fill-in the dispatch stub for the named function.
327 * This function is intended to be called by a hardware driver. When called,
328 * a dispatch stub may be created for the function. A pointer to this
329 * dispatch function will be returned by glXGetProcAddress.
331 * \param function_names Array of pointers to function names that should
332 * share a common dispatch offset.
333 * \param parameter_signature String representing the types of the parameters
334 * passed to the named function. Parameter types
335 * are converted to characters using the following
337 * - 'i' for \c GLint, \c GLuint, and \c GLenum
338 * - 'p' for any pointer type
339 * - 'f' for \c GLfloat and \c GLclampf
340 * - 'd' for \c GLdouble and \c GLclampd
343 * The offset in the dispatch table of the named function. A pointer to the
344 * driver's implementation of the named function should be stored at
345 * \c dispatch_table[\c offset]. Return -1 if error/problem.
347 * \sa glXGetProcAddress
350 * This function can only handle up to 8 names at a time. As far as I know,
351 * the maximum number of names ever associated with an existing GL function is
352 * 4 (\c glPointParameterfSGIS, \c glPointParameterfEXT,
353 * \c glPointParameterfARB, and \c glPointParameterf), so this should not be
354 * too painful of a limitation.
357 * Determine whether or not \c parameter_signature should be allowed to be
358 * \c NULL. It doesn't seem like much of a hardship for drivers to have to
359 * pass in an empty string.
362 * Determine if code should be added to reject function names that start with
366 * Add code to compare \c parameter_signature with the parameter signature of
367 * a static function. In order to do that, we need to find a way to \b get
368 * the parameter signature of a static function.
372 _glapi_add_dispatch( const char * const * function_names
,
373 const char * parameter_signature
)
375 static int next_dynamic_offset
= FIRST_DYNAMIC_OFFSET
;
376 const char * const real_sig
= (parameter_signature
!= NULL
)
377 ? parameter_signature
: "";
378 struct _glapi_function
* entry
[8];
379 GLboolean is_static
[8];
383 init_glapi_relocs_once();
385 (void) memset( is_static
, 0, sizeof( is_static
) );
386 (void) memset( entry
, 0, sizeof( entry
) );
388 /* Find the _single_ dispatch offset for all function names that already
389 * exist (and have a dispatch offset).
392 for ( i
= 0 ; function_names
[i
] != NULL
; i
++ ) {
393 const char * funcName
= function_names
[i
];
395 int extension_offset
;
397 if (funcName
[0] != 'g' || funcName
[1] != 'l')
400 /* search built-in functions */
401 static_offset
= get_static_proc_offset(funcName
);
403 if (static_offset
>= 0) {
405 is_static
[i
] = GL_TRUE
;
407 /* FIXME: Make sure the parameter signatures match! How do we get
408 * FIXME: the parameter signature for static functions?
411 if ( (offset
!= ~0) && (static_offset
!= offset
) ) {
415 offset
= static_offset
;
420 /* search added extension functions */
421 entry
[i
] = get_extension_proc(funcName
);
423 if (entry
[i
] != NULL
) {
424 extension_offset
= entry
[i
]->dispatch_offset
;
426 /* The offset may be ~0 if the function name was added by
427 * glXGetProcAddress but never filled in by the driver.
430 if (extension_offset
== ~0) {
434 if (strcmp(real_sig
, entry
[i
]->parameter_signature
) != 0) {
438 if ( (offset
!= ~0) && (extension_offset
!= offset
) ) {
442 offset
= extension_offset
;
446 /* If all function names are either new (or with no dispatch offset),
447 * allocate a new dispatch offset.
451 offset
= next_dynamic_offset
;
452 next_dynamic_offset
++;
455 /* Fill in the dispatch offset for the new function names (and those with
456 * no dispatch offset).
459 for ( i
= 0 ; function_names
[i
] != NULL
; i
++ ) {
464 /* generate entrypoints for new function names */
465 if (entry
[i
] == NULL
) {
466 entry
[i
] = add_function_name( function_names
[i
] );
467 if (entry
[i
] == NULL
) {
468 /* FIXME: Possible memory leak here. */
473 if (entry
[i
]->dispatch_offset
== ~0) {
474 set_entry_info( entry
[i
], real_sig
, offset
);
483 * Return offset of entrypoint for named function within dispatch table.
486 _glapi_get_proc_offset(const char *funcName
)
490 /* search extension functions first */
491 offset
= get_extension_proc_offset(funcName
);
495 /* search static functions */
496 return get_static_proc_offset(funcName
);
502 * Return pointer to the named function. If the function name isn't found
503 * in the name of static functions, try generating a new API entrypoint on
504 * the fly with assembly language.
507 _glapi_get_proc_address(const char *funcName
)
510 struct _glapi_function
* entry
;
512 init_glapi_relocs_once();
514 if (!funcName
|| funcName
[0] != 'g' || funcName
[1] != 'l')
517 /* search extension functions first */
518 func
= get_extension_proc_address(funcName
);
522 /* search static functions */
523 func
= get_static_proc_address(funcName
);
527 /* generate entrypoint, dispatch offset must be filled in by the driver */
528 entry
= add_function_name(funcName
);
532 return entry
->dispatch_stub
;
538 * Return the name of the function at the given dispatch offset.
539 * This is only intended for debugging.
542 _glapi_get_proc_name(GLuint offset
)
546 /* search built-in functions */
547 n
= get_static_proc_name(offset
);
552 /* search added extension functions */
553 return get_extension_proc_name(offset
);
558 /**********************************************************************
559 * GL API table functions.
564 * Return size of dispatch table struct as number of functions (or
568 _glapi_get_dispatch_table_size(void)
571 * The dispatch table size (number of entries) is the size of the
572 * _glapi_table struct plus the number of dynamic entries we can add.
573 * The extra slots can be filled in by DRI drivers that register new
574 * extension functions.
576 return FIRST_DYNAMIC_OFFSET
+ MAX_EXTENSION_FUNCS
;