C++ and RTTI

Hi,

I use C++ for own driver.

There is some problem with MSVC.NET run-time library on link stage: all
RTTI-functions are included in user-mode library and that library cannot be
linked with drivers.
How I can use RTTI advantage in kernel projects?


Serge Rumyantsev, SE
Protek
e-mail: xxxxx@protek.com
www: http://www.protek.com

Just out of curiosity, what do you need RTTI for in a driver?

Chuck

----- Original Message -----
From: “Rumyantsev Sergey”
To: “Windows System Software Developers Interest List”

Sent: Wednesday, August 27, 2003 1:16 PM
Subject: [ntdev] C++ and RTTI

> Hi,
>
> I use C++ for own driver.
>
> There is some problem with MSVC.NET run-time library on link stage:
all
> RTTI-functions are included in user-mode library and that library
cannot be
> linked with drivers.
> How I can use RTTI advantage in kernel projects?
>
> ____________________________
> Serge Rumyantsev, SE
> Protek
> e-mail: xxxxx@protek.com
> www: http://www.protek.com

Hi,

One way to enable RTTI support in the drivers is to add USE_RTTI=1 to you
“sources” file.

Regards,
Gennady Mayko.

I’m guessing that he needs it to know the run time type of an object :slight_smile:

While from a purist standpoint this is indicative of a design flaw, it is
such a common problem that it is part of the language standard. There is no
support for this or other C++ runtime features in the kernel. So you have to
reverse engineer your own.

=====================
Mark Roddy
Hollis Technology Solutions
www.hollistech.com
xxxxx@hollistech.com

-----Original Message-----
From: Chuck Batson [mailto:xxxxx@cbatson.com]
Sent: Wednesday, August 27, 2003 2:23 AM
To: Windows System Software Developers Interest List
Subject: [ntdev] Re: C++ and RTTI

Just out of curiosity, what do you need RTTI for in a driver?

Chuck

----- Original Message -----
From: “Rumyantsev Sergey”
To: “Windows System Software Developers Interest List”
Sent: Wednesday, August 27, 2003 1:16 PM
Subject: [ntdev] C++ and RTTI

> Hi,
>
> I use C++ for own driver.
>
> There is some problem with MSVC.NET run-time library on link stage:
all
> RTTI-functions are included in user-mode library and that library
cannot be
> linked with drivers.
> How I can use RTTI advantage in kernel projects?
>
> ____________________________
> Serge Rumyantsev, SE
> Protek
> e-mail: xxxxx@protek.com
> www: http://www.protek.com


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@stratus.com To
unsubscribe send a blank email to xxxxx@lists.osr.com

Hi,

Thanks to everybody…

Just out of curiosity, what do you need RTTI for in a driver?

I’m in need of RTTI support for getting real base address of
allocated memory block as shown below (I’m realising own smart pointer
class with reference counter and in trouble with supporting and comparing
pointers list):

class A
{
public:
virtual ~A () {};
int m_intValue;
};

class B
{
public:
virtual ~B () {};
long m_longValue;
};

class C : public A, public B
{
public:
virtual ~C () {};
char m_charValue;
};

void f ()
{
C* c = new C ();
A* a = c;
B* b = c;

// b != c, b != a

void* _b = dynamic_cast (b);

// _b == c, _b == a

delete c;
}

____________________________
Serge Rumyantsev, SE
Protek
e-mail: xxxxx@protek.com
www: http://www.protek.com

Not to rehash a dozen years of language wars on this issue, but in my
opnion if you’re ever in a situation where you need to do this, it is by
bad design. Give us the details of your smart pointer implementation and
we’ll design around the problem. :slight_smile:

Rumyantsev Sergey wrote:

Hi,

Thanks to everybody…

>Just out of curiosity, what do you need RTTI for in a driver?

I’m in need of RTTI support for getting real base address of
allocated memory block as shown below (I’m realising own smart pointer
class with reference counter and in trouble with supporting and comparing
pointers list):

class A
{
public:
virtual ~A () {};
int m_intValue;
};

class B
{
public:
virtual ~B () {};
long m_longValue;
};

class C : public A, public B
{
public:
virtual ~C () {};
char m_charValue;
};

void f ()
{
C* c = new C ();
A* a = c;
B* b = c;

// b != c, b != a

void* _b = dynamic_cast (b);
>
> // _b == c, _b == a
>
> delete c;
> }
>
> ____________________________
> Serge Rumyantsev, SE
> Protek
> e-mail: xxxxx@protek.com
> www: http://www.protek.com
>
>
>


Nick Ryan (MVP for DDK)

2Federico Bianchi:

SR> http://www.acc.umu.se/~bosse/libcpp.zip

thanks.

2Nick Ryan:

SR> Not to rehash a dozen years of language wars on this issue, but in my
SR> opnion if you’re ever in a situation where you need to do this, it is by

SR> bad design.

may be…

SR> Give us the details of your smart pointer implementation and we’ll
design around the problem. :slight_smile:

for ex.:

typedef reference_count unsigned;
typedef std::map objects_and_references;

objects_and_references the_map;

template
class smart_ptr;
{
public:
smart_ptr (T* t = 0) : t_(t) { ++the_map[dynamic_cast(t_)]; }
~smart_ptr()
{
if(!–the_map[dynamic_cast(t_)])
{
the_map.erase(dynamic_cast(t_));
delete t_;
}
}
// other methods
};

without dynamic_cast this technique is not working for multi-inherited
objects.

____________________________
Serge Rumyantsev, SE
Protek
e-mail: xxxxx@protek.com
www: http://www.protek.com

That’s just horrible!

-htfv

----- Original Message -----
From: “Rumyantsev Sergey”
To: “Windows System Software Developers Interest List”
Sent: Friday, August 29, 2003 12:35 PM
Subject: [ntdev] Re: C++ and RTTI

> 2Federico Bianchi:
>
> SR> http://www.acc.umu.se/~bosse/libcpp.zip
>
> thanks.
>
> 2Nick Ryan:
>
> SR> Not to rehash a dozen years of language wars on this issue, but in my
> SR> opnion if you’re ever in a situation where you need to do this, it is
by
>
> SR> bad design.
>
> may be…
>
> SR> Give us the details of your smart pointer implementation and we’ll
> design around the problem. :slight_smile:
>
> for ex.:
>
> typedef reference_count unsigned;
> typedef std::map objects_and_references;
>
> objects_and_references the_map;
>
> template
> class smart_ptr;
> {
> public:
> smart_ptr (T* t = 0) : t_(t) { ++the_map[dynamic_cast(t_)]; }
> ~smart_ptr()
> {
> if(!–the_map[dynamic_cast(t_)])
> {
> the_map.erase(dynamic_cast(t_));
> delete t_;
> }
> }
> // other methods
> };
>
> without dynamic_cast this technique is not working for multi-inherited
> objects.
>
> ____________________________
> Serge Rumyantsev, SE
> Protek
> e-mail: xxxxx@protek.com
> www: http://www.protek.com
>
>
>
> —
> Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: xxxxx@vba.com.by
> To unsubscribe send a blank email to xxxxx@lists.osr.com
>
>

I just can’t calm down. Man, when drivers are written like that, the world
will go down to hell. Look at the Boost C++ library (http://www.boost.org).
It has 5 (!) smart pointer classes and every one of them is much-much
smarter than yours.

-htfv

----- Original Message -----
From: “Alexey Logachyov”
To: “Windows System Software Developers Interest List”
Sent: Friday, August 29, 2003 1:26 PM
Subject: [ntdev] Re: C++ and RTTI

> That’s just horrible!
>
> -htfv
>
>
> ----- Original Message -----
> From: “Rumyantsev Sergey”
> To: “Windows System Software Developers Interest List”

> Sent: Friday, August 29, 2003 12:35 PM
> Subject: [ntdev] Re: C++ and RTTI
>
>
> > 2Federico Bianchi:
> >
> > SR> http://www.acc.umu.se/~bosse/libcpp.zip
> >
> > thanks.
> >
> > 2Nick Ryan:
> >
> > SR> Not to rehash a dozen years of language wars on this issue, but in
my
> > SR> opnion if you’re ever in a situation where you need to do this, it
is
> by
> >
> > SR> bad design.
> >
> > may be…
> >
> > SR> Give us the details of your smart pointer implementation and we’ll
> > design around the problem. :slight_smile:
> >
> > for ex.:
> >
> > typedef reference_count unsigned;
> > typedef std::map objects_and_references;
> >
> > objects_and_references the_map;
> >
> > template
> > class smart_ptr;
> > {
> > public:
> > smart_ptr (T* t = 0) : t_(t) { ++the_map[dynamic_cast(t_)]; }
> > ~smart_ptr()
> > {
> > if(!–the_map[dynamic_cast(t_)])
> > {
> > the_map.erase(dynamic_cast(t_));
> > delete t_;
> > }
> > }
> > // other methods
> > };
> >
> > without dynamic_cast this technique is not working for multi-inherited
> > objects.
> >
> > ____________________________
> > Serge Rumyantsev, SE
> > Protek
> > e-mail: xxxxx@protek.com
> > www: http://www.protek.com
> >
> >
> >
> > —
> > Questions? First check the Kernel Driver FAQ at
> http://www.osronline.com/article.cfm?id=256
> >
> > You are currently subscribed to ntdev as: xxxxx@vba.com.by
> > To unsubscribe send a blank email to xxxxx@lists.osr.com
> >
> >
>
>
>
>
> —
> Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: xxxxx@vba.com.by
> To unsubscribe send a blank email to xxxxx@lists.osr.com
>
>

All discussions of beauty aside, I don’t think this does what you think
anyway - given:

class A {};
class B {};
class C : public A, public B {};

C * pC;
B *pB = pC;

I don’t think

dynamic_cast(pB)

Will actually return (void )pC which I think is what you said you expect…

I’m actually surprised this doesn’t cause an exception to be thrown (but
maybe it just quietly converts pB to a void * using the built-in conversion.

By the way - you cant use RTTI in the kernel anyway since it throws
exceptions which, as we all know, don’t work in the kernel… Sure you could
implement some bastardized RTTI that doesn’t throw yourself but don’t expect
to use the standard support routines…

/simgr

-----Original Message-----
From: Rumyantsev Sergey [mailto:xxxxx@protek.com]
Sent: Friday, August 29, 2003 5:35 AM
To: Windows System Software Developers Interest List
Subject: [ntdev] Re: C++ and RTTI

2Federico Bianchi:

SR> http://www.acc.umu.se/~bosse/libcpp.zip

thanks.

2Nick Ryan:

SR> Not to rehash a dozen years of language wars on this issue, but in my
SR> opnion if you’re ever in a situation where you need to do this, it is by

SR> bad design.

may be…

SR> Give us the details of your smart pointer implementation and we’ll
design around the problem. :slight_smile:

for ex.:

typedef reference_count unsigned;
typedef std::map objects_and_references;

objects_and_references the_map;

template
class smart_ptr;
{
public:
smart_ptr (T
t = 0) : t_(t) { ++the_map[dynamic_cast(t_)]; }
~smart_ptr()
{
if(!–the_map[dynamic_cast(t_)])
{
the_map.erase(dynamic_cast(t_));
delete t_;
}
}
// other methods
};

without dynamic_cast this technique is not working for multi-inherited
objects.

____________________________
Serge Rumyantsev, SE
Protek
e-mail: xxxxx@protek.com
www: http://www.protek.com


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@stratus.com
To unsubscribe send a blank email to xxxxx@lists.osr.com

Think twice about smart pointers and STL containers, they will
invariably destroy the underlying pointer b/c of the way object types
are handled. Scott Meyers goes into great detail about this in his book
Effective STL.

http://www.amazon.com/exec/obidos/tg/detail/-/0201749629/qid=1062179900/
sr=8-3/ref=sr_8_3/102-8294978-4806541?v=glance&s=books&n=507846

You should definitely read it before using any such STL construct

D

This posting is provided “AS IS” with no warranties, and confers no
rights

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Rumyantsev Sergey
Sent: Friday, August 29, 2003 2:35 AM
To: Windows System Software Developers Interest List
Subject: [ntdev] Re: C++ and RTTI

2Federico Bianchi:

SR> http://www.acc.umu.se/~bosse/libcpp.zip

thanks.

2Nick Ryan:

SR> Not to rehash a dozen years of language wars on this issue, but in
my
SR> opnion if you’re ever in a situation where you need to do this, it
is by

SR> bad design.

may be…

SR> Give us the details of your smart pointer implementation and we’ll
design around the problem. :slight_smile:

for ex.:

typedef reference_count unsigned;
typedef std::map objects_and_references;

objects_and_references the_map;

template
class smart_ptr;
{
public:
smart_ptr (T* t = 0) : t_(t) { ++the_map[dynamic_cast(t_)]; }
~smart_ptr()
{
if(!–the_map[dynamic_cast(t_)])
{
the_map.erase(dynamic_cast(t_));
delete t_;
}
}
// other methods
};

without dynamic_cast this technique is not working for multi-inherited
objects.

____________________________
Serge Rumyantsev, SE
Protek
e-mail: xxxxx@protek.com
www: http://www.protek.com


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@windows.microsoft.com
To unsubscribe send a blank email to xxxxx@lists.osr.com

That’s definitely an excellent book for kernel-mode programmer to read
before sleep. As far as I remember, this book covers only smart pointer
class of the STL library (while mentioning other libraries, including
Boost). But OP tries to construct an ever smarter pointer of his own, one
with reference counting. And while there are dozens of implementations that
can be found on the net, I suggest to use one that comes with the Boost C++
library. Boost is a very active project an you can always count that the
code found in the library is tested by a lot of people and bugs, if any
found, are fixed very fast.

-htfv

----- Original Message -----
From: “Doron Holan”
To: “Windows System Software Developers Interest List”
Sent: Friday, August 29, 2003 8:59 PM
Subject: [ntdev] Re: C++ and RTTI

Think twice about smart pointers and STL containers, they will
invariably destroy the underlying pointer b/c of the way object types
are handled. Scott Meyers goes into great detail about this in his book
Effective STL.

http://www.amazon.com/exec/obidos/tg/detail/-/0201749629/qid=1062179900/
sr=8-3/ref=sr_8_3/102-8294978-4806541?v=glance&s=books&n=507846

You should definitely read it before using any such STL construct

D

This posting is provided “AS IS” with no warranties, and confers no
rights

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Rumyantsev Sergey
Sent: Friday, August 29, 2003 2:35 AM
To: Windows System Software Developers Interest List
Subject: [ntdev] Re: C++ and RTTI

2Federico Bianchi:

SR> http://www.acc.umu.se/~bosse/libcpp.zip

thanks.

2Nick Ryan:

SR> Not to rehash a dozen years of language wars on this issue, but in
my
SR> opnion if you’re ever in a situation where you need to do this, it
is by

SR> bad design.

may be…

SR> Give us the details of your smart pointer implementation and we’ll
design around the problem. :slight_smile:

for ex.:

typedef reference_count unsigned;
typedef std::map objects_and_references;

objects_and_references the_map;

template
class smart_ptr;
{
public:
smart_ptr (T* t = 0) : t_(t) { ++the_map[dynamic_cast(t_)]; }
~smart_ptr()
{
if(!–the_map[dynamic_cast(t_)])
{
the_map.erase(dynamic_cast(t_));
delete t_;
}
}
// other methods
};

without dynamic_cast this technique is not working for multi-inherited
objects.

____________________________
Serge Rumyantsev, SE
Protek
e-mail: xxxxx@protek.com
www: http://www.protek.com


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@windows.microsoft.com
To unsubscribe send a blank email to xxxxx@lists.osr.com


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@vba.com.by
To unsubscribe send a blank email to xxxxx@lists.osr.com

Hi,

thanks to everybody, again…

this problem is solved by this way
http://www.acc.umu.se/~bosse/libcpp.zip

ps

2GrahamSimon

I don’t think
dynamic_cast(pB)
>Will actually return (void )pC which I
>think is what you said you expect…

You can try mentioned sample in debugger :wink:

I was in need of retrieving base address of given object, but
there is some problem occured:
(void
) pC != (void*) pB // (or pC != pB)
but dynamic casting like this
dynamic_cast(pC) == dynamic_cast(pB)
solves my problem.

2AlexeyLogachyov

>I just can’t calm down. Man, when drivers are written like that,
>the world will go down to hell.

this is flame, and nothing more.

>Look at the Boost C++ library (http://www.boost.org).
>It has 5 (!) smart pointer classes and every one of
>them is much-much smarter than yours.

thanks… I’ve seen it before.
Mentioned smart_ptr code is only sample for RTTI usage demonstration.
My question is academical question only.

____________________________
Serge Rumyantsev, SE
Protek
e-mail: xxxxx@protek.com
www: http://www.protek.com

> for ex.:

typedef reference_count unsigned;

Well, maybe

typedef unsigned reference_count;

is more correct? Also why not use the MS’s type names like ULONG - easier to
type and read? The Windows driver code is not portable anyway.

typedef std::map objects_and_references;

This is very, very bad design. Objects must keep the reference count in
themselves, not in some side map.

Something like:

class CRefCountObject
{
public:
CRefCountObject() { m_RefCount = 1; }
VOID AddRef() { InterlockedIncrement(&m_RefCount); }
VOID Release() { if( InterlockedDecrement(&m_RefCount) == 0 ) delete
this; }
virtual ~CRefCountObject() {}
private:
LONG m_RefCount;
};

class CMyObject : virtual public CRefCountObject
{


This is multiple-inheritance-safe and faster, if you will have 10.000 or more
objects - map lookup each addref can consume time.

Also read the ATL source for a similar reference counting implementation. Read
COMDEF.H (can be wrong with the actual name - the MSVC’s header where
_com_ptr_t is declared) for “smart pointer” implementation.

Note that your original code is also not concurrency safe. The STL map has no
synchronization in it.

Also abusing the C++ features is bad. It consumes the limited amount of the
human attention for implementation details and extremely bad syntax full of
visual noise (dynamic_cast<> is a sample which deserves a monument). This
distracts the developer from the real work which is done by this code. Read any
MS’s DDK sample, they are clear and stupid. Now imagine the same code written
in C++ with heavy use of STL, dynamic_cast<>, RTTI and multiple inheritance.

Use C++ features wisely, and remember - writing unreadable and unmaintainable
code in C++ is by far easier then in C. Do not end with a situation when your
successor will need to throw away your code and replace it with a C code of his
own.

Also RTTI is very, very rarely useful, and provocates the atrocious non-OO codi
ng styles by breaking polymorphism. I personally consider the inclusion of this
feature to the language itself as a Bad Thing.

If you really need RTTI - why not use the macro-based implementation like MFC’s
IMPLEMENT_DYNAMIC? You will have the control over the class object layout, and
you have no such control if the feature is language-included.

What does your driver do? I can hardly imagine the need of multiple inheritance
in a driver.

Also note that some parts of STL rely on exceptions, and supporting C++
exceptions in kernel mode code is a major problem. C++ code with unwind
semantics enabled creates an internal __SEH_Rec structure in a stack frame of
each function, and the stack size in the kernel is 12KB.

Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
xxxxx@storagecraft.com
http://www.storagecraft.com

> I just can’t calm down. Man, when drivers are written like that, the world

will go down to hell. Look at the Boost C++ library (http://www.boost.org).

Why? We saw people asking questions about “Please help! My driver BSODs!”
without providing a call stack, we saw people trying to import the user32
routine from the driver, we saw people trying to show UI from drivers, and so
on. The bad C++ coding practice is a minor issue comparing to all of this :slight_smile:

The impact on product quality is also noticeable. For instance, the drivers for
Mustek Bearpaw scanner have a bug in power management path. After going
hibernate and back with a scanner attached, I see the system thread consuming
100% of CPU.

The nVidia Detonator bugs were also renowned once - around 2000.

Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
xxxxx@storagecraft.com
http://www.storagecraft.com

That’s what I talking about. When everyone starts pushing every piece of
code he can find without any kind of understanding of how things are
working, things get bad. Reference counting is not a concept that is
difficult to understand. It was discussed hundreds of thousands of millions
times. There are sample written in great number, lying on the net for free.
But OP ignores all that facts. He has an awful design which he tries to
compensate with another bad design, ignoring all kinds of advises given
here. Look at his last post: he solved his problems by linkin his code with
RTTI library. God! I know that it is not good to criticize people like, that
we should stay tolerant trying to explain a person where’s his mistake. But
I sometimes can’t see such postings without all kinds of curses.

-htfv

----- Original Message -----
From: “Maxim S. Shatskih”
To: “Windows System Software Developers Interest List”
Sent: Wednesday, September 03, 2003 1:46 AM
Subject: [ntdev] Re: C++ and RTTI

> > I just can’t calm down. Man, when drivers are written like that, the
world
> > will go down to hell. Look at the Boost C++ library
(http://www.boost.org).
>
> Why? We saw people asking questions about “Please help! My driver BSODs!”
> without providing a call stack, we saw people trying to import the user32
> routine from the driver, we saw people trying to show UI from drivers, and
so
> on. The bad C++ coding practice is a minor issue comparing to all of this
:slight_smile:
>
> The impact on product quality is also noticeable. For instance, the
drivers for
> Mustek Bearpaw scanner have a bug in power management path. After going
> hibernate and back with a scanner attached, I see the system thread
consuming
> 100% of CPU.
>
> The nVidia Detonator bugs were also renowned once - around 2000.
>
> Maxim Shatskih, Windows DDK MVP
> StorageCraft Corporation
> xxxxx@storagecraft.com
> http://www.storagecraft.com
>
>
> —
> Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: xxxxx@vba.com.by
> To unsubscribe send a blank email to xxxxx@lists.osr.com
>
>

Sometimes you don’t have access to the class source code. In that case you
have to allocate reference counter somewhere. The best way is to make the
smart pointer class do that as long as smart pointer controls object
lifetime and reference counter lives as long as controlled object does. No
need in using map, no need in using dynamic_cast. The overhead is only
allocating reference counter on object creation and freeing reference
counter when it falls to zero.

-htfv

----- Original Message -----
From: “Maxim S. Shatskih”
To: “Windows System Software Developers Interest List”
Sent: Wednesday, September 03, 2003 1:35 AM
Subject: [ntdev] Re: C++ and RTTI

> > for ex.:
> >
> > typedef reference_count unsigned;
>
> Well, maybe
>
> typedef unsigned reference_count;
>
> is more correct? Also why not use the MS’s type names like ULONG - easier
to
> type and read? The Windows driver code is not portable anyway.
>
> > typedef std::map objects_and_references;
>
> This is very, very bad design. Objects must keep the reference count in
> themselves, not in some side map.
>
> Something like:
>
> class CRefCountObject
> {
> public:
> CRefCountObject() { m_RefCount = 1; }
> VOID AddRef() { InterlockedIncrement(&m_RefCount); }
> VOID Release() { if( InterlockedDecrement(&m_RefCount) == 0 ) delete
> this; }
> virtual ~CRefCountObject() {}
> private:
> LONG m_RefCount;
> };
>
> class CMyObject : virtual public CRefCountObject
> {
> …
>
> This is multiple-inheritance-safe and faster, if you will have 10.000 or
more
> objects - map lookup each addref can consume time.
>
> Also read the ATL source for a similar reference counting implementation.
Read
> COMDEF.H (can be wrong with the actual name - the MSVC’s header where
> _com_ptr_t is declared) for “smart pointer” implementation.
>
> Note that your original code is also not concurrency safe. The STL map has
no
> synchronization in it.
>
> Also abusing the C++ features is bad. It consumes the limited amount of
the
> human attention for implementation details and extremely bad syntax full
of
> visual noise (dynamic_cast<> is a sample which deserves a monument). This
> distracts the developer from the real work which is done by this code.
Read any
> MS’s DDK sample, they are clear and stupid. Now imagine the same code
written
> in C++ with heavy use of STL, dynamic_cast<>, RTTI and multiple
inheritance.
>
> Use C++ features wisely, and remember - writing unreadable and
unmaintainable
> code in C++ is by far easier then in C. Do not end with a situation when
your
> successor will need to throw away your code and replace it with a C code
of his
> own.
>
> Also RTTI is very, very rarely useful, and provocates the atrocious non-OO
codi
> ng styles by breaking polymorphism. I personally consider the inclusion of
this
> feature to the language itself as a Bad Thing.
>
> If you really need RTTI - why not use the macro-based implementation like
MFC’s
> IMPLEMENT_DYNAMIC? You will have the control over the class object layout,
and
> you have no such control if the feature is language-included.
>
> What does your driver do? I can hardly imagine the need of multiple
inheritance
> in a driver.
>
> Also note that some parts of STL rely on exceptions, and supporting C++
> exceptions in kernel mode code is a major problem. C++ code with unwind
> semantics enabled creates an internal __SEH_Rec structure in a stack frame
of
> each function, and the stack size in the kernel is 12KB.
>
> Maxim Shatskih, Windows DDK MVP
> StorageCraft Corporation
> xxxxx@storagecraft.com
> http://www.storagecraft.com
>
>
> —
> Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: xxxxx@vba.com.by
> To unsubscribe send a blank email to xxxxx@lists.osr.com
>
>

I agree with you… it would be nice if people we ranted at about bad
design
would tell us what product/company they are working on/for so we can
run away quickly from it/them.

-Jeff

-----Original Message-----
From: Alexey Logachyov [mailto:xxxxx@vba.com.by]
Sent: Thursday, September 04, 2003 7:46 AM
To: Windows System Software Developers Interest List
Subject: [ntdev] Re: C++ and RTTI

Sometimes you don’t have access to the class source code. In that case you
have to allocate reference counter somewhere. The best way is to make the
smart pointer class do that as long as smart pointer controls object
lifetime and reference counter lives as long as controlled object does. No
need in using map, no need in using dynamic_cast. The overhead is only
allocating reference counter on object creation and freeing reference
counter when it falls to zero.

-htfv

----- Original Message -----
From: “Maxim S. Shatskih”
To: “Windows System Software Developers Interest List”
Sent: Wednesday, September 03, 2003 1:35 AM
Subject: [ntdev] Re: C++ and RTTI

> > for ex.:
> >
> > typedef reference_count unsigned;
>
> Well, maybe
>
> typedef unsigned reference_count;
>
> is more correct? Also why not use the MS’s type names like ULONG - easier
to
> type and read? The Windows driver code is not portable anyway.
>
> > typedef std::map objects_and_references;
>
> This is very, very bad design. Objects must keep the reference count in
> themselves, not in some side map.
>
> Something like:
>
> class CRefCountObject
> {
> public:
> CRefCountObject() { m_RefCount = 1; }
> VOID AddRef() { InterlockedIncrement(&m_RefCount); }
> VOID Release() { if( InterlockedDecrement(&m_RefCount) == 0 ) delete
> this; }
> virtual ~CRefCountObject() {}
> private:
> LONG m_RefCount;
> };
>
> class CMyObject : virtual public CRefCountObject
> {
> …
>
> This is multiple-inheritance-safe and faster, if you will have 10.000 or
more
> objects - map lookup each addref can consume time.
>
> Also read the ATL source for a similar reference counting implementation.
Read
> COMDEF.H (can be wrong with the actual name - the MSVC’s header where
> _com_ptr_t is declared) for “smart pointer” implementation.
>
> Note that your original code is also not concurrency safe. The STL map has
no
> synchronization in it.
>
> Also abusing the C++ features is bad. It consumes the limited amount of
the
> human attention for implementation details and extremely bad syntax full
of
> visual noise (dynamic_cast<> is a sample which deserves a monument). This
> distracts the developer from the real work which is done by this code.
Read any
> MS’s DDK sample, they are clear and stupid. Now imagine the same code
written
> in C++ with heavy use of STL, dynamic_cast<>, RTTI and multiple
inheritance.
>
> Use C++ features wisely, and remember - writing unreadable and
unmaintainable
> code in C++ is by far easier then in C. Do not end with a situation when
your
> successor will need to throw away your code and replace it with a C code
of his
> own.
>
> Also RTTI is very, very rarely useful, and provocates the atrocious non-OO
codi
> ng styles by breaking polymorphism. I personally consider the inclusion of
this
> feature to the language itself as a Bad Thing.
>
> If you really need RTTI - why not use the macro-based implementation like
MFC’s
> IMPLEMENT_DYNAMIC? You will have the control over the class object layout,
and
> you have no such control if the feature is language-included.
>
> What does your driver do? I can hardly imagine the need of multiple
inheritance
> in a driver.
>
> Also note that some parts of STL rely on exceptions, and supporting C++
> exceptions in kernel mode code is a major problem. C++ code with unwind
> semantics enabled creates an internal __SEH_Rec structure in a stack frame
of
> each function, and the stack size in the kernel is 12KB.
>
> Maxim Shatskih, Windows DDK MVP
> StorageCraft Corporation
> xxxxx@storagecraft.com
> http://www.storagecraft.com
>
>
> —
> Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: xxxxx@vba.com.by
> To unsubscribe send a blank email to xxxxx@lists.osr.com
>
>


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@concord.com
To unsubscribe send a blank email to xxxxx@lists.osr.com


This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.
This footnote also confirms that this email message has been swept by
the latest virus scan software available for the presence of computer
viruses.

I had that thought, too. But very often people on this list do driver work
just for their pleasure thus other people’s time is often wasted for those
who perhaps would never become a driver programmer (or won’t become a
programmer at all). Yet, I hope that they will learn things from the list
before they will get their hands to any of real products.

-htfv

----- Original Message -----
From: “Curless, Jeffrey”
To: “Windows System Software Developers Interest List”
Sent: Thursday, September 04, 2003 4:05 PM
Subject: [ntdev] Re: C++ and RTTI

> I agree with you… it would be nice if people we ranted at about bad
> design
> would tell us what product/company they are working on/for so we can
> run away quickly from it/them.
>
> -Jeff
>
> -----Original Message-----
> From: Alexey Logachyov [mailto:xxxxx@vba.com.by]
> Sent: Thursday, September 04, 2003 7:46 AM
> To: Windows System Software Developers Interest List
> Subject: [ntdev] Re: C++ and RTTI
>
>
> Sometimes you don’t have access to the class source code. In that case you
> have to allocate reference counter somewhere. The best way is to make the
> smart pointer class do that as long as smart pointer controls object
> lifetime and reference counter lives as long as controlled object does. No
> need in using map, no need in using dynamic_cast. The overhead is only
> allocating reference counter on object creation and freeing reference
> counter when it falls to zero.
>
> -htfv
>
>
>
> ----- Original Message -----
> From: “Maxim S. Shatskih”
> To: “Windows System Software Developers Interest List”

> Sent: Wednesday, September 03, 2003 1:35 AM
> Subject: [ntdev] Re: C++ and RTTI
>
>
> > > for ex.:
> > >
> > > typedef reference_count unsigned;
> >
> > Well, maybe
> >
> > typedef unsigned reference_count;
> >
> > is more correct? Also why not use the MS’s type names like ULONG -
easier
> to
> > type and read? The Windows driver code is not portable anyway.
> >
> > > typedef std::map objects_and_references;
> >
> > This is very, very bad design. Objects must keep the reference count in
> > themselves, not in some side map.
> >
> > Something like:
> >
> > class CRefCountObject
> > {
> > public:
> > CRefCountObject() { m_RefCount = 1; }
> > VOID AddRef() { InterlockedIncrement(&m_RefCount); }
> > VOID Release() { if( InterlockedDecrement(&m_RefCount) == 0 ) delete
> > this; }
> > virtual ~CRefCountObject() {}
> > private:
> > LONG m_RefCount;
> > };
> >
> > class CMyObject : virtual public CRefCountObject
> > {
> > …
> >
> > This is multiple-inheritance-safe and faster, if you will have 10.000 or
> more
> > objects - map lookup each addref can consume time.
> >
> > Also read the ATL source for a similar reference counting
implementation.
> Read
> > COMDEF.H (can be wrong with the actual name - the MSVC’s header where
> > _com_ptr_t is declared) for “smart pointer” implementation.
> >
> > Note that your original code is also not concurrency safe. The STL map
has
> no
> > synchronization in it.
> >
> > Also abusing the C++ features is bad. It consumes the limited amount of
> the
> > human attention for implementation details and extremely bad syntax full
> of
> > visual noise (dynamic_cast<> is a sample which deserves a monument).
This
> > distracts the developer from the real work which is done by this code.
> Read any
> > MS’s DDK sample, they are clear and stupid. Now imagine the same code
> written
> > in C++ with heavy use of STL, dynamic_cast<>, RTTI and multiple
> inheritance.
> >
> > Use C++ features wisely, and remember - writing unreadable and
> unmaintainable
> > code in C++ is by far easier then in C. Do not end with a situation when
> your
> > successor will need to throw away your code and replace it with a C code
> of his
> > own.
> >
> > Also RTTI is very, very rarely useful, and provocates the atrocious
non-OO
> codi
> > ng styles by breaking polymorphism. I personally consider the inclusion
of
> this
> > feature to the language itself as a Bad Thing.
> >
> > If you really need RTTI - why not use the macro-based implementation
like
> MFC’s
> > IMPLEMENT_DYNAMIC? You will have the control over the class object
layout,
> and
> > you have no such control if the feature is language-included.
> >
> > What does your driver do? I can hardly imagine the need of multiple
> inheritance
> > in a driver.
> >
> > Also note that some parts of STL rely on exceptions, and supporting C++
> > exceptions in kernel mode code is a major problem. C++ code with unwind
> > semantics enabled creates an internal __SEH_Rec structure in a stack
frame
> of
> > each function, and the stack size in the kernel is 12KB.
> >
> > Maxim Shatskih, Windows DDK MVP
> > StorageCraft Corporation
> > xxxxx@storagecraft.com
> > http://www.storagecraft.com
> >
> >
> > —
> > Questions? First check the Kernel Driver FAQ at
> http://www.osronline.com/article.cfm?id=256
> >
> > You are currently subscribed to ntdev as: xxxxx@vba.com.by
> > To unsubscribe send a blank email to xxxxx@lists.osr.com
> >
> >
>
>
>
> —
> Questions? First check the Kernel Driver FAQ at
> http://www.osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: xxxxx@concord.com
> To unsubscribe send a blank email to xxxxx@lists.osr.com
>
>
>
> This email and any files transmitted with it are confidential and
> intended solely for the use of the individual or entity to whom they
> are addressed. If you have received this email in error please notify
> the system manager.
> This footnote also confirms that this email message has been swept by
> the latest virus scan software available for the presence of computer
> viruses.
>

>
>
> —
> Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: xxxxx@vba.com.by
> To unsubscribe send a blank email to xxxxx@lists.osr.com
>
>