This is the mail archive of the
ecos-discuss@sources.redhat.com
mailing list for the eCos project.
RE: What's the meaning of those C++ code? Thanks.
- From: Bob Koninckx <bob dot koninckx at mech dot kuleuven dot ac dot be>
- To: QiangHuang <jameshq at liverpool dot ac dot uk>
- Cc: ecos-discuss at sources dot redhat dot com
- Date: 23 Apr 2003 16:32:30 +0200
- Subject: RE: [ECOS] What's the meaning of those C++ code? Thanks.
- Organization: KULeuven
- References: <ONEOIJPPOKNBBKJKCGLCEEFMCAAA.jameshq@liv.ac.uk>
On Wed, 2003-04-23 at 16:18, QiangHuang wrote:
> Thank you very much.
>
> Does it mean that: a Cyg_Mutext class object will be created on the
> provided memory ("mutex")? or just the call the contrustor of Cyg_Mutex on
> the provided memory("mutex")?
That's the same thing. Calling a constructor implies that you can look
at the provided memory as an instance of the class the constructor
belongs to.
>
> any different between " Cyg_Mutex *m = new((void *)mutex) Cyg_Mutex; " and "
> Cyg_Mutex *m = new((void *)mutex) Cyg_Mutex( ); "
I don't think so, I am not 100 % sure though. You are aware that these
questions have little to do with eCos and everything with C++, are you ?
>
> it seems the " void cyg_flag_init(){ Cyg_Flag *t = new((void *)flag)
> Cyg_Flag(); t=t; } " (Note: Cyg_Flag( ) (with bracket)) while
> "cyg_mutex_init( ){ Cyg_Mutex *m = new((void *)mutex) Cyg_Mutex;" m=m;} (no
> bracket here) ?
Don't understand what you're asking here?
>
> But how can it be sured to have the same size for : Cyg_Mutex Class and
> struct cyg_mutex_t if a class object is created on the provided memory
> ("mutex")?
> Thanks a lot.
If you use the placement new, its your own responsibility to provide it
with a big enough chunk of memory. Same remark as above. There are
better places to ask this kind of questions than this list.
Bob
>
>
> -----Original Message-----
> From: Bob Koninckx [mailto:bob dot koninckx at mech dot kuleuven dot ac dot be]
> Sent: 23 April 2003 14:26
> To: QiangHuang
> Subject: Re: [ECOS] 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
> ----------------------------------------------------------------------
--
----------------------------------------------------------------------
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