the 'double align' and union are intended to make sure the 'unlocker' buffer is aligned (at least to an 8-byte boundary). it should be good enough for most platforms. more info here: https://www.securecoding.cert.org/confluence/display/cplusplus/MEM45-CPP.+Provide+properly+aligned+pointers+to+placement+new
2012/3/12 avasilev <[email protected]> > Yes, using the placement new operator was my initial idea. But as far as > I investigated this technique, it is not very safe, because of alignment > issues. I am curious about the role of the 'double align' member and the > union. Is this to prevent a zero-sized array in case sizeof(Unlocker) == 0 ? > Thanks > Alex > 12 март 2012, понеделник 16:20:15 UTC+2, Shezan Baig написа: > >> you could conditionally construct it like: >> >> class ConditionalUnlocker { >> typedef v8::Unlocker RealUnlocker; >> >> union { >> char unlocker[sizeof(RealUnlocker)]**; >> double align; >> }; >> bool istrue; >> >> public: >> ConditionalUnlocker(bool condition) >> : istrue(condition) >> { >> if (istrue) { >> new (unlocker) RealUnlocker(); // construct it within the >> stack-allocated 'unlocker' >> } >> } >> >> ~ConditonalUnlocker() >> { >> if (istrue) { >> RealUnlocker& real = *(RealUnlocker*)unlocker; >> real.~RealUnlocker(); // destruct it >> } >> } >> }; >> >> >> (no guarantees being made about the portability of this class etc) >> >> >> >> 2012/3/12 avasilev <[email protected]> >> >>> Hi Stephan, >>> Thanks >>> Unfortunately this will do the job only if the flag is known at compile >>> time. What I need is to be able to switch this at runtime. >>> Thanks for the response. >>> Best regards >>> Alex >>> >>> 12 март 2012, понеделник 14:25:56 UTC+2, Stephan Beal написа: >>> >>>> On Mon, Mar 12, 2012 at 12:37 PM, avasilev <[email protected]>wrote: >>>> >>>>> Unlocker classes? My use case is a conditional auto-unlocker object, >>>>> that may or may not unlock v8, depending on a global setting, so I >>>>> >>>> >>>> Here's a template which does that: >>>> >>>> /** >>>> A sentry class which instantiates a v8::Unlocker >>>> if the boolean value is true or is a no-op if it is false. >>>> */ >>>> template <bool> struct V8Unlocker {}; >>>> >>>> /** >>>> Equivalent to v8::Unlocker. >>>> */ >>>> template <> >>>> struct V8Unlocker<true> : v8::Unlocker >>>> { >>>> }; >>>> >>>> -- >>>> ----- stephan beal >>>> http://wanderinghorse.net/**home**/stephan/<http://wanderinghorse.net/home/stephan/> >>>> http://gplus.to/sgbeal >>>> >>>> >>> 12 март 2012, понеделник 14:25:56 UTC+2, Stephan Beal написа: >>> >>>> On Mon, Mar 12, 2012 at 12:37 PM, avasilev <[email protected]>wrote: >>>> >>>>> Unlocker classes? My use case is a conditional auto-unlocker object, >>>>> that may or may not unlock v8, depending on a global setting, so I >>>>> >>>> >>>> Here's a template which does that: >>>> >>>> /** >>>> A sentry class which instantiates a v8::Unlocker >>>> if the boolean value is true or is a no-op if it is false. >>>> */ >>>> template <bool> struct V8Unlocker {}; >>>> >>>> /** >>>> Equivalent to v8::Unlocker. >>>> */ >>>> template <> >>>> struct V8Unlocker<true> : v8::Unlocker >>>> { >>>> }; >>>> >>>> -- >>>> ----- stephan beal >>>> http://wanderinghorse.net/**home**/stephan/<http://wanderinghorse.net/home/stephan/> >>>> http://gplus.to/sgbeal >>>> >>>> >>> 12 март 2012, понеделник 14:25:56 UTC+2, Stephan Beal написа: >>> >>>> On Mon, Mar 12, 2012 at 12:37 PM, avasilev <[email protected]>wrote: >>>> >>>>> Unlocker classes? My use case is a conditional auto-unlocker object, >>>>> that may or may not unlock v8, depending on a global setting, so I >>>>> >>>> >>>> Here's a template which does that: >>>> >>>> /** >>>> A sentry class which instantiates a v8::Unlocker >>>> if the boolean value is true or is a no-op if it is false. >>>> */ >>>> template <bool> struct V8Unlocker {}; >>>> >>>> /** >>>> Equivalent to v8::Unlocker. >>>> */ >>>> template <> >>>> struct V8Unlocker<true> : v8::Unlocker >>>> { >>>> }; >>>> >>>> -- >>>> ----- stephan beal >>>> http://wanderinghorse.net/**home**/stephan/<http://wanderinghorse.net/home/stephan/> >>>> http://gplus.to/sgbeal >>>> >>>> >>> 12 март 2012, понеделник 14:25:56 UTC+2, Stephan Beal написа: >>> >>>> On Mon, Mar 12, 2012 at 12:37 PM, avasilev <[email protected]>wrote: >>>> >>>>> Unlocker classes? My use case is a conditional auto-unlocker object, >>>>> that may or may not unlock v8, depending on a global setting, so I >>>>> >>>> >>>> Here's a template which does that: >>>> >>>> /** >>>> A sentry class which instantiates a v8::Unlocker >>>> if the boolean value is true or is a no-op if it is false. >>>> */ >>>> template <bool> struct V8Unlocker {}; >>>> >>>> /** >>>> Equivalent to v8::Unlocker. >>>> */ >>>> template <> >>>> struct V8Unlocker<true> : v8::Unlocker >>>> { >>>> }; >>>> >>>> -- >>>> ----- stephan beal >>>> http://wanderinghorse.net/**home**/stephan/<http://wanderinghorse.net/home/stephan/> >>>> http://gplus.to/sgbeal >>>> >>>> >>> 12 март 2012, понеделник 14:25:56 UTC+2, Stephan Beal написа: >>> >>>> On Mon, Mar 12, 2012 at 12:37 PM, avasilev <[email protected]>wrote: >>>> >>>>> Unlocker classes? My use case is a conditional auto-unlocker object, >>>>> that may or may not unlock v8, depending on a global setting, so I >>>>> >>>> >>>> Here's a template which does that: >>>> >>>> /** >>>> A sentry class which instantiates a v8::Unlocker >>>> if the boolean value is true or is a no-op if it is false. >>>> */ >>>> template <bool> struct V8Unlocker {}; >>>> >>>> /** >>>> Equivalent to v8::Unlocker. >>>> */ >>>> template <> >>>> struct V8Unlocker<true> : v8::Unlocker >>>> { >>>> }; >>>> >>>> -- >>>> ----- stephan beal >>>> http://wanderinghorse.net/**home**/stephan/<http://wanderinghorse.net/home/stephan/> >>>> http://gplus.to/sgbeal >>>> >>>> -- >>> v8-users mailing list >>> [email protected] >>> http://groups.google.com/**group/v8-users<http://groups.google.com/group/v8-users> >>> >> >> -- > v8-users mailing list > [email protected] > http://groups.google.com/group/v8-users > -- v8-users mailing list [email protected] http://groups.google.com/group/v8-users
