05.05.2013 Views

Programming PHP

Programming PHP

Programming PHP

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

SWF_DIR=$i<br />

AC_MSG_RESULT(found in $i)<br />

fi<br />

done<br />

fi<br />

if test -z "$SWF_DIR"; then<br />

AC_MSG_RESULT(not found)<br />

AC_MSG_ERROR(Please reinstall the libswf distribution - swf.h should<br />

be /include and libswf.a should be in /lib)<br />

fi<br />

<strong>PHP</strong>_ADD_INCLUDE($SWF_DIR/include)<br />

<strong>PHP</strong>_SUBST(SWF_SHARED_LIBADD)<br />

<strong>PHP</strong>_ADD_LIBRARY_WITH_PATH(swf, $SWF_DIR/lib, SWF_SHARED_LIBADD)<br />

AC_DEFINE(HAVE_SWF,1,[ ])<br />

<strong>PHP</strong>_EXTENSION(swf, $ext_shared)<br />

fi<br />

The AC_MSG_CHECKING( ) macro is used to make configure print a message about what<br />

it’s checking for. When we’ve found the include files, we add them to <strong>PHP</strong>’s standard<br />

include search path with the <strong>PHP</strong>_ADD_INCLUDE( ) macro. When we find the SWF<br />

shared libraries, we add them to the library search path and ensure that we link them<br />

into the final binary through the <strong>PHP</strong>_ADD_LIBRARY_WITH_PATH( ) macro. Things can<br />

get a lot more complex than this once you start worrying about different versions of<br />

libraries and different platforms. For a very complex example, see the GD library’s<br />

config.m4 in ext/gd/config.m4.<br />

Memory Management<br />

In C, you always have to worry about memory management. This still holds true<br />

when writing <strong>PHP</strong> extensions in C, but the extension API provides you with a safety<br />

net and some helpful debugging facilities if you use the API’s memory-management<br />

wrapper functions (you are strongly encouraged to do so). The wrapper functions are:<br />

emalloc( )<br />

efree( )<br />

estrdup( )<br />

estrndup( )<br />

ecalloc( )<br />

erealloc( )<br />

These work exactly like the native C counterparts after which they are named.<br />

One of the features you get by using emalloc( ) is a safety net for memory leaks. If<br />

you emalloc( ) something and forget to efree( ) it, <strong>PHP</strong> prints a leak warning like<br />

this if you are running in debug mode (enabled by compiling <strong>PHP</strong> with the --enabledebug<br />

switch):<br />

foo.c(123) : Freeing 0x0821E5FC (20 bytes), script=foo.php<br />

Last leak repeated 1 time<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2002 O’Reilly & Associates, Inc. All rights reserved.<br />

Memory Management | 329

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!