Slide 1

Slide 1 text

INSIDE THE SAUSAGE FACTORY A JOURNEY FROM PHP TO C Jeremy Mikola jmikola

Slide 2

Slide 2 text

I work with these guys

Slide 3

Slide 3 text

At this place

Slide 4

Slide 4 text

I've done a bit of this TI-83 Basic Perl PHP JavaScript C++ Delphi Java Scheme C Assembly Ada95 Python

Slide 5

Slide 5 text

But let's talk about PHP C

Slide 6

Slide 6 text

More specifically… Finding C in PHP A crash course in C PHP internals Creating extensions

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

Finding C in PHP Standard IO: f o p e n ( ) , f w r i t e ( ) Strings functions: s p r i n t f ( ) , s t r c h r ( ) Date/Time functions: s t r f t i m e ( ) , t i m e ( ) Extensions modeled on C APIs c u r l , g d , g m p , m y s q l , p c n t l

Slide 9

Slide 9 text

Simple file IO in PHP $ f p = f o p e n ( " e x a m p l e . t x t " , " w " ) ; i f ( $ f p ) { f p u t s ( $ f p , " h e l l o " ) ; f c l o s e ( $ f p ) ; }

Slide 10

Slide 10 text

Simple file IO in C # i n c l u d e < s t d i o . h > i n t m a i n ( ) { F I L E * f p = f o p e n ( " e x a m p l e . t x t " , " w " ) ; i f ( f p ) { f p u t s ( " h e l l o " , f p ) ; f c l o s e ( f p ) ; } r e t u r n 0 ; }

Slide 11

Slide 11 text

Language differences PHP C Source Interpreted Compiled Typing Dynamic Static Scoping Function-level Block-level Memory Automatic Manual

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

A crash course in C Header files and compiling Static and dynamic linking Declarations and data types Stack and heap memory

Slide 14

Slide 14 text

Header files Included at compile time Type declarations Function prototypes Defined constants, macros

Slide 15

Slide 15 text

Compiling C → Preprocessor → Translation Unit Translation Unit → Compiler → Object File Object Files → Linker → Executable

Slide 16

Slide 16 text

Static and dynamic linking Static linking Monolithic binaries No external dependencies Dynamic linking (. s o , . d l l ) Modules loaded at run-time More flexibility, slight overhead

Slide 17

Slide 17 text

Declarations Declare it before you use it Functions, types, variables Block-level scoping Source and header files e x t e r n and s t a t i c

Slide 18

Slide 18 text

Data types Signed and unsigned integers c h a r , s h o r t , i n t , l o n g Floating point numbers f l o a t , d o u b l e Array and s t r u c t aggregates

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

Pointers Memory

Slide 21

Slide 21 text

Pointers are a type i n t a = 3 ; i n t * p t r = N U L L ; Variable Address Value a 0 x 8 1 3 0 0 x 0 0 0 0 0 0 0 3 p t r 0 x 8 1 3 4 0 x 0 0 0 0 0 0 0 0

Slide 22

Slide 22 text

Pointer assignments p t r = & a ; Variable Address Value a 0 x 8 1 3 0 0 x 0 0 0 0 0 0 0 3 p t r 0 x 8 1 3 4 0 x 0 0 0 0 8 1 3 0

Slide 23

Slide 23 text

Dereferencing pointers * p t r = 6 ; Variable Address Value a 0 x 8 1 3 0 0 x 0 0 0 0 0 0 0 6 p t r 0 x 8 1 3 4 0 x 0 0 0 0 8 1 3 0

Slide 24

Slide 24 text

Pointers and arrays i n t a [ 4 ] = { 0 , 0 , 0 , 0 } ; i n t * p t r = a ; a [ 0 ] = 1 ; p t r [ 1 ] = 2 ; * ( a + 2 ) = 3 ; * ( p t r + 3 ) = 4 ; s i z e o f ( a ) ; / * 4 × s i z e o f ( i n t ) * / s i z e o f ( p t r ) ; / * s i z e o f ( i n t * ) * /

Slide 25

Slide 25 text

Stack and heap memory / * S t a c k a l l o c a t i o n * / i n t z e n d _ a t o i ( c o n s t c h a r * s t r , i n t s t r _ l e n ) ; / * S t a c k a l l o c a t i o n * / i n t f o o [ 1 0 ] ; / * H e a p a l l o c a t i o n * / i n t * f o o = m a l l o c ( 1 0 * s i z e o f ( i n t ) ) ;

Slide 26

Slide 26 text

Heap memory needs to be freed i n t * f o o = m a l l o c ( 1 0 * s i z e o f ( i n t ) ) ; i f ( f o o = = N U L L ) { / * R e q u i s i t e e r r o r h a n d l i n g * / } f r e e ( f o o ) ; / * R e t u r n a l l o c a t e d m e m o r y t o t h e h e a p * / f o o = N U L L ; / * A v o i d a c c e s s i n g f r e e d m e m o r y l a t e r * /

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

php-internals

Slide 29

Slide 29 text

php-src

Slide 30

Slide 30 text

PHP core does the heavy lifting Language parsing, compiling Macros for code scaffolding Data structures, utility functions Memory management, persistence Thread safety

Slide 31

Slide 31 text

lxr.php.net

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

A place for everything Core source and syntax in Z e n d / Parser, types, internal API Extensions in e x t / and PECL Server APIs in s a p i / Tests live in t e s t s /

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

PHPT tests Name, code, and expected output Used by both core and extensions Single-purpose and debuggable Documentation on qa.php.net Why not PHPUnit, PHPSpec, etc?

Slide 37

Slide 37 text

An example PHPT test - - T E S T - - s t r r c h r ( ) t e s t s - - F I L E - - < ? p h p v a r _ d u m p ( s t r r c h r ( " " , " " ) ) ; v a r _ d u m p ( s t r r c h r ( " a b c " , " " ) ) ; v a r _ d u m p ( s t r r c h r ( " " , " a b c " ) ) ; v a r _ d u m p ( s t r r c h r ( " a b c " , " a b c " ) ) ; ? > - - E X P E C T F - - b o o l ( f a l s e ) b o o l ( f a l s e ) b o o l ( f a l s e ) s t r i n g ( 3 ) " a b c "

Slide 38

Slide 38 text

It looks like you're giving a presentation on PHP internals. Would you like help? Talk about zvals

Slide 39

Slide 39 text

PHP variables are zvals t y p e d e f u n i o n _ z v a l u e _ v a l u e { l o n g l v a l ; / * l o n g v a l u e * / d o u b l e d v a l ; / * d o u b l e v a l u e * / s t r u c t { / * s t r i n g v a l u e * / c h a r * v a l ; i n t l e n ; } s t r ; H a s h T a b l e * h t ; / * h a s h t a b l e v a l u e * / z e n d _ o b j e c t _ v a l u e o b j ; / * o b j e c t v a l u e * / } z v a l u e _ v a l u e ; t y p e d e f s t r u c t _ z v a l _ s t r u c t { z v a l u e _ v a l u e v a l u e ; / * v a l u e * / z e n d _ u i n t r e f c o u n t _ _ g c ; / * r e f e r e n c e c o u n t * / z e n d _ u c h a r t y p e ; / * a c t i v e t y p e * / z e n d _ u c h a r i s _ r e f _ _ g c ; / * i s a r e f e r e n c e ? * / } z v a l ;

Slide 40

Slide 40 text

Macros for accessing zval data z v a l z v a l * z v a l * * Z _ L V A L Z _ L V A L _ P Z _ L V A L _ P P Z _ B V A L Z _ B V A L _ P Z _ B V A L _ P P Z _ D V A L Z _ D V A L _ P Z _ D V A L _ P P Z _ S T R V A L Z _ S T R V A L _ P Z _ S T R V A L _ P P Z _ S T R L E N Z _ S T R L E N _ P Z _ S T R L E N _ P P Z _ A R R V A L Z _ A R R V A L _ P Z _ A R R V A L _ P P Z _ O B J V A L Z _ O B J V A L _ P Z _ O B J V A L _ P P Z _ T Y P E Z _ T Y P E _ P Z _ T Y P E _ P P

Slide 41

Slide 41 text

Macros for defining methods P H P _ M E T H O D ( M o n g o C u r s o r , l i m i t ) { l o n g l ; m o n g o _ c u r s o r * c u r s o r ; c u r s o r = ( m o n g o _ c u r s o r * ) z e n d _ o b j e c t _ s t o r e _ g e t _ o b j e c t ( g e t T h i s ( ) T S R M L S _ C C ) ; i f ( z e n d _ p a r s e _ p a r a m e t e r s ( Z E N D _ N U M _ A R G S ( ) T S R M L S _ C C , " l " , & l ) = = F A I L U R E ) { r e t u r n ; } p h p _ m o n g o _ c u r s o r _ s e t _ l i m i t ( c u r s o r , l ) ; R E T V A L _ Z V A L ( g e t T h i s ( ) , 1 , 0 ) ; }

Slide 42

Slide 42 text

Our method after pre-processing v o i d z i m _ M o n g o C u r s o r _ l i m i t ( i n t h t , z v a l * r e t u r n _ v a l u e , z v a l * * r e t u r n _ v a l u e _ p t r , z v a l * t h i s _ p t r , i n t r e t u r n _ v a l u e _ u s e d ) { l o n g l ; m o n g o _ c u r s o r * c u r s o r ; c u r s o r = ( m o n g o _ c u r s o r * ) z e n d _ o b j e c t _ s t o r e _ g e t _ o b j e c t ( t h i s _ p t r ) ; i f ( z e n d _ p a r s e _ p a r a m e t e r s ( h t , " l " , & l ) = = F A I L U R E ) { r e t u r n ; } p h p _ m o n g o _ c u r s o r _ s e t _ l i m i t ( c u r s o r , l ) ; z e n d _ u c h a r i s _ r e f = z v a l _ i s r e f _ p ( r e t u r n _ v a l u e ) ; z e n d _ u i n t r e f c o u n t = z v a l _ r e f c o u n t _ p ( r e t u r n _ v a l u e ) ; r e t u r n _ v a l u e - > v a l u e = t h i s _ p t r - > v a l u e ; ( * r e t u r n _ v a l u e ) . t y p e = ( * t h i s _ p t r ) . t y p e ; z v a l _ c o p y _ c t o r ( r e t u r n _ v a l u e ) ; z v a l _ s e t _ i s r e f _ t o _ p ( r e t u r n _ v a l u e , i s _ r e f ) ; z v a l _ s e t _ r e f c o u n t _ p ( r e t u r n _ v a l u e , r e f c o u n t ) ; }

Slide 43

Slide 43 text

Macros for… Memory allocation Working with hash tables Comparisons, casts, operations Defining class/function entries Other macros # d e f i n e Z _ T Y P E ( z v a l ) ( z v a l ) . t y p e # d e f i n e Z _ T Y P E _ P ( z v a l _ p ) Z _ T Y P E ( * z v a l _ p ) # d e f i n e Z _ T Y P E _ P P ( z v a l _ p p ) Z _ T Y P E ( * * z v a l _ p p )

Slide 44

Slide 44 text

MACROS MACROS EVERYWHERE

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

Ingredients for an extension Autoconf, p h p i z e , core libs c o n f i g . m 4 and c o n f i g . w 3 2 p h p _ m y e x t . c and p h p _ m y e x t . h and test coverage Coding standards Windows? Difficult but . not impossible

Slide 47

Slide 47 text

The recipe $ p h p i z e C o n f i g u r i n g f o r : P H P A p i V e r s i o n : 2 0 1 0 0 4 1 2 Z e n d M o d u l e A p i N o : 2 0 1 0 0 5 2 5 Z e n d E x t e n s i o n A p i N o : 2 2 0 1 0 0 5 2 5 $

Slide 48

Slide 48 text

The recipe $ . / c o n f i g u r e c h e c k i n g f o r g r e p t h a t h a n d l e s l o n g l i n e s a n d - e . . . / b i n / g r e p c h e c k i n g f o r e g r e p . . . / b i n / g r e p - E c h e c k i n g f o r a s e d t h a t d o e s n o t t r u n c a t e o u t p u t . . . / b i n / s e d c h e c k i n g f o r c c . . . c c c h e c k i n g w h e t h e r t h e C c o m p i l e r w o r k s . . . y e s . . . c h e c k i n g w h e t h e r t o b u i l d s t a t i c l i b r a r i e s . . . n o c o n f i g u r e : c r e a t i n g . / c o n f i g . s t a t u s c o n f i g . s t a t u s : c r e a t i n g c o n f i g . h c o n f i g . s t a t u s : e x e c u t i n g l i b t o o l c o m m a n d s $

Slide 49

Slide 49 text

The recipe $ m a k e . . . B u i l d c o m p l e t e . D o n ' t f o r g e t t o r u n ' m a k e t e s t ' . $

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

Recommended reading Developer blogs , , Kristina Chodorow's (2011) Marcus Börger's (2009) Zend Developer Zone articles (2009) Sara Golemon's (2005) PHP Internals Book Nikita Popov Sara Golemon Anthony Ferrara tutorial series conference talks Wrapping C++ Classes in a PHP Extension tutorial series

Slide 52

Slide 52 text

Perhaps the most informative slide in this entire presentation wiki.php.net/internals/references

Slide 53

Slide 53 text

THANKS! Questions?

Slide 54

Slide 54 text

Image Credits http://pbfcomics.com/153/ http://www.flickr.com/photos/phuson/4311168776/ http://www.esourceengine.com/downloads/minecraft/minecraft-library.png http://en.wikipedia.org/wiki/File:The_C_Programming_Language_cover.svg http://xkcd.com/138/ http://en.wikipedia.org/wiki/File:Pointers.svg http://www.flickr.com/photos/abmann/5286312715/ http://gifsoup.com/view/33383/kangaroo-boxing.html http://breakingbadamc.tumblr.com/post/36457804409/lets-cook http://xkcd.com/303/