Bug 4468

Summary: Use _mesa wrappers for libc functions
Product: Mesa Reporter: George - <fufutos610>
Component: Mesa coreAssignee: mesa-dev
Status: RESOLVED FIXED QA Contact:
Severity: minor    
Priority: high    
Version: git   
Hardware: x86 (IA32)   
OS: Linux (All)   
Whiteboard:
i915 platform: i915 features:
Attachments: _mesa wrappers for libc functions
use _mesa wrappers for libc
minor update: x86-64 similar to x86
update: wrap remaing calls
Move system headers to imports.h
Misc build fixes
drop the headers move

Description George - 2005-09-15 09:00:51 UTC
The attached patch uses the corresponding _mesa wrappers for libc functions.
It also adds a wrapper for memcmp(). 

There are a dozen more libc functions used by mesa without a wrapper. Does
mesa want to wrap these calls as well, similar to memcmp() ? The goal is to
have all these "external" symbols in imports.o only.

List of unwrapped functions:

         abort
         atof
         bsearch
         exit
         exp
         fclose
         fflush
         ffs
         fopen
         fprintf
         sprintf
         sscanf
         strchr
         strlen
Comment 1 George - 2005-09-15 09:01:52 UTC
Created attachment 3290 [details] [review]
_mesa wrappers for libc functions
Comment 2 Nicholas Miell 2005-09-15 16:58:54 UTC
Wrapping libc functions prevents the compiler from recognizing that they won't
return (abort, exit, etc.), typechecking their arguments (fprintf, sprintf,
etc.) or inlining them when it would be beneficial (sinf, memcpy, strlen, etc.)

If anything, Mesa's existing wrappers should be removed in favor of using the C
library functions directly.

http://gcc.gnu.org/onlinedocs/gcc-4.0.1/gcc/Other-Builtins.html is a list of the
C library functions that gcc will optimize, other compilers have similar lists.
Comment 3 Brian Paul 2005-09-16 11:17:47 UTC
I've checked in the patch.  Thanks.

The wrappers are needed in order to build Mesa as an XFree86/X.org server module.

For the functions which can be compiler built-ins (and aren't especially
performance sensitive) there are preprocessor/macro wrappers (such as FABSF)
which shouldn't prevent using the built-ins.
Comment 4 George - 2005-09-16 15:25:02 UTC
Reopening to submit another patch:

* Fix a few more calls:

	sprintf	use wrapper
	strlen	use wrapper
	strchr	replace with strstr, is this ok ?
	atof	replace with strtod, is this ok ?
	log	add define
	exp	add define
	ffs	add wrapper
	bsearch	add wrapper
	
* Fix some libm calls which didn't use their defines

* Remove two fprintf( stderr, ...)

	common_x86.c: changed to _mesa_debug
	t_vp_build.c: changed to _mesa_problem

* Revert the change from vsprintf to _mesa_sprintf in slang_compile.c
	sorry for this


Remaining calls:

	texenvprogram.o:
	         U xf86exit
	t_vp_build.o:
	         U xf86exit
	t_vertex_generic.o:
	         U xf86abort
	slang_compile.o:
	         U xf86exit
	         U xf86vsprintf
	slang_execute.o:
	         U xf86fclose
	         U xf86fflush
	         U xf86fopen
	         U xf86fprintf
	xm_api.o:
	         U xf86sscanf
Comment 5 George - 2005-09-16 15:27:39 UTC
Created attachment 3301 [details] [review]
use _mesa wrappers for libc
Comment 6 George - 2005-09-16 20:02:35 UTC
Created attachment 3306 [details] [review]
minor update: x86-64 similar to x86
Comment 7 George - 2005-09-18 08:52:51 UTC
Created attachment 3315 [details] [review]
update: wrap remaing calls

This is the last update for using wrappers. With this patch, the mesa core 
(libmesa.a) no longer has "external" symbols (when DEBUG is undefined) !

The patch fixes the remaining calls, as follows:

	fclose
	fflush
	fopen
	fprintf 	guard with DEBUG_XXX
	vsprintf	add wrapper
	exit		add wrapper
	abort		replace with exit(1)

It also moves some stuff out of glheader.h:

	move ffs stuff to imports.c

	move glcore.h to mtypes.h, imports.h
		glcore.h really doesn't belong in glheader.h,
		it defines the GLimports/GLexports mesa types

	move wgl stuff to dglwgl.h
		this is a *wild* guess based on grep,
		I just wanted to move this stuff out

Btw, after the viewport changes indirect rendering segfaults 
in _swrast_clear_depth_buffer, if this helps ...
Comment 8 Brian Paul 2005-09-19 13:14:21 UTC
I've checked in your latest patch.  Thanks.

I'll look into the _swrast_clear_depth_buffer() problem next.  That should
really be a separate bug report...
Comment 9 Michel Dänzer 2005-09-19 13:18:45 UTC
(In reply to comment #8)
> 
> I'll look into the _swrast_clear_depth_buffer() problem next.  That should
> really be a separate bug report...

It is, bug 4087.
Comment 10 George - 2005-09-22 04:09:50 UTC
Created attachment 3363 [details] [review]
Move system headers to imports.h

This patch moves system headers to imports.h

*	imports.h is a more appropriate place
*	remove FLT_MIN: it is in float.h which is included in all cases
*	remove _mesa_log2: does not exist
*	fix includes in s_tcc.c
*	fix includes in gen_matypes.c: this is stand-alone
*	fix includes in glapi/

Moreover in glheader.h, I replaced inttypes.h with stdint.h for DRI 
platforms. Both linux-dri and freebsd-dri configs use -std=c99, thus 
they should have this. I did this because including inttypes.h (from 
glheader.h) before stdlib.h (from imports.h) resulted in __SIZE_TYPE__ 
being undefined for drm.h. Using IN_DRI_DRIVER instead of platform 
checks allows makedepend to get this right as well.
Comment 11 George - 2005-09-22 04:11:44 UTC
Created attachment 3364 [details] [review]
Misc build fixes


*	src/mesa/Makefile: fix for depend (matypes.h) and osmesa target
*	src/mesa/drivers/dri/Makefile.template: cleanup after use of libdrm
*	math/m_debug, x86/: isolate the math debug code with DEBUG_MATH, 
	RUN_DEBUG_BENCHMARK was actually implied by DEBUG (now DEBUG_MATH) 
	in m_debug_util.h
Comment 12 Brian Paul 2005-10-07 10:20:26 UTC
I've checked in the DEBUG_MATH changes, but not the other Makefile changes. 
src/mesa/Makefile can't have any 'ifeq/endif' lines because they're only
suppoted in GNU-make.  src/mesa/Makefile needs needs to work with conventional
make on other systems.
Comment 13 George - 2005-10-12 05:42:32 UTC
Created attachment 3547 [details] [review]
drop the headers move
Comment 14 Brian Paul 2005-10-12 12:48:14 UTC
I fixed the #includes in s_tcc.c and removed _mesa_log2() from imports.h

I left in the FLT_MIN definition for now just to be safe.  I'd have to test with
XFree86/X.org to see whether or not it's still needed.
Comment 15 George - 2006-01-26 15:22:32 UTC
FLT_MIN will probably go away after the accelerated indirect work, closing.
Comment 16 Adam Jackson 2009-08-24 12:23:26 UTC
Mass version move, cvs -> git

Use of freedesktop.org services, including Bugzilla, is subject to our Code of Conduct. How we collect and use information is described in our Privacy Policy.