h'igure 5.;}: U M L d ia g ra m d e p ic tin g th e class h ie ra rc h ie s fo r a s im p le m e m o ry a llo c a to r ( liu ffe r M a n a g e r ) and its e xte n d e d ve rsion u sin g th e s tra te g y p a tte r n .
5 . 3 . 3 A d a p t a t i o n u s in g R e f l e c t i o n
In th e re fle c tiv e ve rs io n , a d a p ta tio n is m ade a v a ila b le by r e ify in g in v o c a tio n s on th e o rig in a l b u ffe r m a n a g e r class and by ])ro v id in g a m e ta -in te rfa c e (a so -called e x te n s io n protoco l)
th a t a llo w s to re b in d th e code o f th e a llo c a te /re le a s e o p e ra tio n s . T h e code fo r th e new s tra te g ie s can fo r e x a m p le be jH o v id e d in th e fo rm o f a d y n a m ic lin k lib r a r y ( D L L ) .
I'he ste|)s fo r im p le m e n tin g th e d y n a m ic a lly a d a p ta b le b u ffe r m a n a g e r are o u tlin e d b elow . A s in th e p re v io u s e x a m p le , \v q s t a r t w ith a s im p le p ro to c o l d e fin itio n in o rd e r to re ify
s t r u c t u r a l i nf o r ma t io n a.bout classes. Behavioural reflection can t he n always be added l at er in s ub p ro to co l s . In a second ste[), we declare an extension protocol t h a t provides t h e code for swi tchi ng to a new st rat egy. T h e pur p o se of an ext ensi on prot ocol is to e n c a p s ul a t e a n d s e p a r a t e nieta-level code from t he ac tua l MOP i mp l e me n t a t i o ns , allowing t h e s a m e ext ensi on protocol t o be reused for multiple, c omp at i bl e MOPs.
p r o t o c o l T y p e l n f o { s h a r e d : r e i f y C l a s s : MClass; r e i f y A t t r i b u t e : M A t t r i b u t e ; r e i f y M e t h o d : M M e t h o d ; 1 ■ ; > c l a s s Hole = = > Fypeinfo c l a s s l i uf f e r Ma n a g e r = = > T y p e l n fo c l a s s . \ d a i ) t a t i o n P r o t o c o l { p u b l i c : v o i d c h a n g e P o l i c y ( M O b j e c t *bu fma n , c h a r ^ s t r a t e g y );
Whe n a client binds t o a buffer m a n a g e r obj ec t , it is provided with t h e s t r a t e g y originally enij^loyed by t h e buffer m a n a g e r class. As long as t he client does not re quest a different s t r at egy, invocat ion is not reified im])lying t h a t t he s t a n d a r d C++ invocation mecha ni sm is used.
W hen a d a p t a t i o n is triggered by t h e application, invocation is reified allowing t o di ve r t t he call to t he new i mp le me n t a t i o n. New s tr a t egi es can be provided on t he fly by subclassing t he original buffer m a n a g e r class, redefining t he al loca te /r el ea se m e t h o d s and by compiling t he code into a DLL.
'Lhe meta-level code for rebinding t he i m| )l ement at ion of t he al lo c a te /r el ea se m e t h o d s per for ms t h e following tasks:
1. open a DLL as specified by t he s t r a t e g y p a r a m e t e r ;
2. rel)ind t h e code of t he a.llocate/release m et ho d s. 'Lhis is done by modifying the m e t h o d - m e t a o b j e c t s of t he buffer m a n a g e r class: each m e t h o d m e t a o b j e c t c ont ai ns
a f u n c t i o n p o i n t e r t o t h e m e t h o d whi ch can be r ep l ac e d t o p o i n t t o t h e n e w code. 3. reify i n v o c a t i o n for t h e client: in o r d e r t o d i v e r t all f u t u r e i n v o c a t i o n s t o t h e i m p o r t e d
c o d e , i n v o c a t i o n on t h e cl i ent si de h a s t o be reified. R e if yi n g i n v o c a t i o n s i m p l y i nvol ves c r e a t i n g a n i n v o c a t i o n m e t a o b j e c t a n d i n s e r t i n g it t o t h e buffer m a n a g e r ’s me ta -l e ve l c o n f i g u r a t i o n . R u n - t i m e c he c ks e n s u r e t h a t all f u r t h e r i n v o c a t i o n s ar e r e d i r e c t e d t o t h e me t a - l e v e l . I ' i g u r e 5.4 d e p i c t s t h e logical view of t h e a d a p t a t i o n m e c h a n i s m . It is w o r t h m e n t i o n i n g t h a t t h e or i gi na l i n t e r f a c e o f t h e buffer m a n a g e r class h a s n o t b e e n a l t e r e d , t h e a d d i t i o n a l f u n c t i o n a l i t y t o su]>port a d a p t a t i o n is c o m p l e t e l y e n c a p s u l a t e d in t h e e x t e n s i o n p r o t o c o l a n d is o r t h o g o n a l t o t h e base-level a p p l i c a t i o n . a b c ^ re ease a llo ca te / re le a se C lent M etalevel s h a r e d o b je c t file Baselevel f ' i g u r e 5.4: C o n c e p t u a l view of t h e a d a p t a t i o n m e c h a n i s m us i ng reflection: cl ie nt s c o m m u n i c a t e w i t h t h e buffer m a n a g e r t h r o u g h t h e a l l o c a t e / r e l e a s e i nt er fa c e. M e t h o d m e t a o b j e c t s r e p r e s e n t t h e i m p l e m e n t a t i o n o f t h e s e o p e r a t i o n s a n d c a n be r e pl a ce d w i t h c o d e loaded f r om a DL L.
5 .3 .4
S t a t e Transfer
A g en e r a l | )r obl em a ri s i ng o u t o f t h e a d a p t a t i o n o f s o f t w a r e c o m p o n e n t s h a s t o d e a l wi t h a pos s i bl e t r a n s f e r o f t h e i n t e r n a l s t a t e o f t h e c o m p o n e n t . In o u r e x a m p l e , s w i t c h i n g t o a new s t r a t e g y m a y r eq ui r e t h e list o f free m e m o r y bl ocks t o be a r r a n g e d in a different o r d e r , for e x a m p l e a c c o r d i n g t o t heir size. T h i s c an o n l y lie d o n e w i t h a k n o w l e d g e of t h e im|)leineiitat,ion d e t a i l s o f t he bul f er m a n a g e r .I ' h e i m p l e me n t o r of a new s t r a t e g y is therefore required t o provide a callback function t hat p er f o r ms t he t ra n sf er of s ta t e . Since a new s t r a t e g y is provided by subcl assi ng from t lie original buffer m a n a g e r class, we can a ss ume t h a t t h e im])lementor of t he new s t r a t e g y lias sufficient knowledge of how t h e list of free blocks is represented because t he code t h a t im| )l ement s t he al lo ca te /r el ea se m e t h o d s relies on t heir internal re pr e s en t a t i o n anyway. By using s om e s or t of n a m i n g convention for t he callback f unction, it is possible t o link t he new m e t h o d t o t h e meta-level re pr e s en ta ti on of t he buffer m a n a g e r an d t o invoke it when a d a p t a t i o n has been triggered.