This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC 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: Big-endian Gcc on Intel IA32


Linus Torvalds wrote:
> 
> I actually think that it might be equally powerful to just have a way of
> "tainting" certain pointers, and disallowing their use at compile-time
> unless the recipient claims to accept the specific form of "tainting".
> This is, in fact, more-or-less what the "const" qualifier does, but it
> might be useful to allow user-defined "taints".

You can do this already in C++. (Of course I realise this isn't much
help for a large C application like the kernel.)

  // Andrei Alexandrescu's static assertion template
  // (this is in everybody's library by now)
  template <bool Pred> struct static_assert;
  template <> struct static_assert<true> {};

  // This is valid only if there are no bits in Src that aren't in Dst
  template <unsigned Src, unsigned Dst> struct convert_check:
    static_assert<((Src | Dst) == Dst)> {};

  // Template for qualified types
  // Each bit in Mask represents a type qualifier
  template <typename T, unsigned Mask> class qualified {
    public:
      qualified(T t = T()):
        value_(t) {}
      operator T() const {
        static_assert<(Mask == 0)>();
        return value_;
      };
      template <typename T2, unsigned Mask2>
        qualified(qualified<T2, Mask2> src):
          value_(src.value_) {
            convert_check<Mask2, Mask>();
          }
      template <unsigned Mask2> friend qualified<T, Mask2>
        qualified_cast(qualified src) {
          return qualified<T, Mask2>(src.value_);
        }
    private:
      T value_;
  };

  // Define a couple of type qualifiers
  const unsigned magic(1);
  const unsigned tainted(2);

  // Examples of use
  int main() {
    int i(42);                             // Plain int
    qualified<int, magic> m;               // Magic int
    qualified<int, (magic | tainted)> mt;  // Magic, tainted int
    mt = i;                                // OK, can add qualifiers
    // m = mt;                             // This won't compile
    m = qualified_cast<magic>(mt);         // But this will
    return 0;
  }


-- 
Ross Smith ...................................... Auckland, New Zealand
r-smith@ihug.co.nz ......................... http://storm.net.nz/~ross/
  "We need a new cosmology. New gods. New sacraments. Another drink."
                                                       -- Patti Smith


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