0

We want to overload placement new operator just to verify that used memory size is enough for the given class. We know this size. The construction is more or less in this way:

template <size_t MAXSIZE>
class PlacementNewTest {
public:
  void* operator new (size_t size, void* where)
  {
     if (size > MAXSIZE) {
        throw bad_alloc();
     }
     return where;
  }
};

Let say used in such simplified context:

char buffer[200];

class A : public PlacementNewTest<sizeof buffer> {
public:
   char a[100];  
};

class B : public A {
public:
   char b[200];  
};


int main() {
   A* a = new (buffer) A; // OK
   a->~A();
   B* b = new (buffer) B; // throwed bad_alloc
   b->~B();
}

During testing phase I have this PlacementNewTest<> class used, but in the release code I consider to remove it. Do you think, basing on your experience, how much this will cost our performance, not to remove this extra test class? Is this only cost of this verification if (size > MAXSIZE)? In other words, what is performance penalty for such redefinition:

class PlacementNewNOP {
public:
  void* operator new (size_t size, void* where)
  {
     return where;
  }
};

Maybe it is not important in this question - but: This is, and must be, C++03. We cannot upgrade to C++11. And boost is not an option too, just C++03.

PiotrNycz
  • 23,099
  • 7
  • 66
  • 112
  • 1
    While you're at it, you should check the alignment: `assert(reinterpret_cast(where) % alignof(A) == 0)` etc. – Kerrek SB Aug 02 '12 at 13:13
  • @KerrekSB How the hell did I miss that :-/ I'm usually the alignment police :-D – Šimon Tóth Aug 02 '12 at 13:13
  • "Premature optimization is the root of all evil (or at least most of it) in programming". Dont think of performance penalties until you face them. And when you face them, weigh possible improvement against the effort – Roman Saveljev Aug 02 '12 at 13:14
  • @KerrekSB - can you explain why testing alignment is important in this very case? And, do you know replacement for `allignof` which is C++11 feature, I have to use g++3.x.x (C++03 std). – PiotrNycz Aug 02 '12 at 13:31
  • @PiotrNycz Because the alignment requirements of `char` are less stringent than other types. Btw. `alignof()` should be also a gcc extensions (not sure when they added that, but it should be a looong time ago). – Šimon Tóth Aug 02 '12 at 13:34
  • @PiotrNycz: In a crunch you can substitute `sizeof` for `alignof`, I suppose; it may be overshooting, but it should be correct most of the time. – Kerrek SB Aug 02 '12 at 14:19

1 Answers1

1

There shouldn't be any overhead apart from the comparison, unless you are using virtual methods, the binding is static.

Of course there is the exception overhead, but since that is something that shouldn't happen, you should be safe to ignore it.

Šimon Tóth
  • 35,456
  • 20
  • 106
  • 151
  • Can you say why do you think so? I mean, can you explain how g++ compiler internally implements such placement new operator overloading? – PiotrNycz Aug 02 '12 at 13:30
  • @PiotrNycz Because overloading is resolved during compilation, not during run-time. During run-time it will be a simple function call. Again, all methods (unless virtual) are statically bound. – Šimon Tóth Aug 02 '12 at 13:32
  • So, it will be overhead of "simple function call" or no overhead in comparison to case without placement new operator overloading? – PiotrNycz Aug 02 '12 at 13:58
  • @PiotrNycz You will have one call in both cases. – Šimon Tóth Aug 02 '12 at 17:29