Recent gcc adds a warning, -Wclass-memaccess, that warns if you use memset or 
similar on a non-trivial type -- in other words, a type that requires something 
be done to initialize instances of it (set fields, call a user-defined 
constructor, init a vptr, etc.).  The reason is that initialize-by-memset is 
fragile: if your class ever gains initialization, memset will either overwrite 
that initialization or falsely assure you that no further initialization is 
necessary.  Sad!

Mozilla code probably doesn't do this much *directly*, but unfortunately 
PodZero and PodArrayZero do.

Fortunately, C++11 lets you use class member-initializers (especially 
brace-initializers) to cleanly zero things.[0]  If that doesn't cut it, 
<algorithm>'s std::fill and std::fill_n are type-safe alternatives that 
optimizing compilers will eat for breakfast.

Please don't add new uses of Pod{,Array}Zero, and start removing existing uses 
where possible.  It's generally not too tricky; I fixed all issues of this sort 
building the JS engine last week, and the only serious quirks I hit were for 
fairly unusual SpiderMonkey needs.  I'll probably rename these to 
DeprecatedPod{,Array}Zero to particularly discourage their use, until they can 
be removed.

See 
<http://whereswalden.com/2018/05/21/psa-stop-using-mozillapodzero-and-mozillapodarrayzero/>
 for more information.

Thanks!

Jeff

0. The only exception are bit-fields.  The best solution for bit-fields is to 
dump them in a class, brace-initialize the whole thing to zeroes, then (if 
needed) overwrite any customized fields in the enclosing class's constructor.  
See <https://hg.mozilla.org/mozilla-central/rev/7c45180cea08> for an example.
_______________________________________________
dev-platform mailing list
dev-platform@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-platform

Reply via email to