This is the mail archive of the ecos-discuss@sources.redhat.com mailing list for the eCos project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: What's the meaning of those C++ code? Thanks.


On Wed, 2003-04-23 at 15:12, QiangHuang wrote:
> Hi all:
>    I have tried to understand the following C++ code in eCOS but I can't
> find the way out. Could anybody help me on this? Thanks a lot.
> 
> //==========================================================================
> ===============================
> file: kapidata.h
> 
> struct cyg_mutex_t
> {
>     cyg_atomic          locked;         /* true if locked               */
>     cyg_thread          *owner;         /* Current locking thread       */
>     cyg_threadqueue     queue;          /* Queue of waiting threads     */
> 
> #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DYNAMIC
>     cyg_mutex_protocol_t protocol;       /* this mutex's protocol        */
> #endif
> #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_CEILING
>     cyg_priority_t      ceiling;        /* mutex priority ceiling       */
> #endif
> };
> //==========================================================================
> ===============================
> 
> //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> +++++++++++++++++++++++++++++++
> file: kapi.cxx
> 
> inline void *operator new(size_t size, void *ptr)
> {
>     CYG_CHECK_DATA_PTR( ptr, "Bad pointer" );
>     return ptr;
> }
> 
> 
> externC void cyg_mutex_init(
>     cyg_mutex_t        *mutex          /* Mutex to init
> */
> )
> {
>     CYG_ASSERT_SIZES( cyg_mutex_t, Cyg_Mutex );
> 
>     Cyg_Mutex *m = new((void *)mutex) Cyg_Mutex;
> 
>     m=m;
> }
> //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> +++++++++++++++++++++++++++++++
> 
> so when declares:
> 
> cyg_mutex_t mutex1;
> cyg_mutex_init(&mutex1);
> 
> what's the meaning of  "(1). Cyg_Mutex *m = new((void *)mutex) Cyg_Mutex;

That's called "placement new". It is in every good book on C++. It tells
the new operator to use the provided memory (in this case mutex) rather
than allocating it on the heap.

> (2). m=m;"  here? Thanks a lot.

That's been done to silence a compiler warning. Leave this out and the
compiler will complain about m being an unused variable. The optimiser
will throw it out afterwards.

All they want to achieve here is to call the Cyg_Mutex constructor on
the provided memory. I suppose something like this

externC void cyg_mutex_init(
    cyg_mutex_t        *mutex          /* Mutex to init */
    )
{
    CYG_ASSERT_SIZES( cyg_mutex_t, Cyg_Mutex );

    new((void *)mutex) Cyg_Mutex;
}

would have the same effect. Maybe you get another warning if you do it
like this ...


-- 
----------------------------------------------------------------------
ir. Bob Koninckx
Katholieke Universiteit Leuven
Division Production Engineering,                   tel.  +32 16 322535
Machine Design and Automation                      fax.  +32 16 322987
Celestijnenlaan 300B                  bob dot koninckx at mech dot kuleuven dot ac dot be
B-3001 Leuven Belgium               http://www.mech.kuleuven.ac.be/pma
----------------------------------------------------------------------


-- 
Before posting, please read the FAQ: http://sources.redhat.com/fom/ecos
and search the list archive: http://sources.redhat.com/ml/ecos-discuss


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]