Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Re: Using speci...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 5 of 8 Topic 45810 of 47006
Post > Topic >>

Re: Using special allocator interfaces...

by "Chris Thomasson" <cristom@[EMAIL PROTECTED] > May 8, 2008 at 07:30 AM

"Alf P. Steinbach" <alfps@[EMAIL PROTECTED]
> wrote in message 
news:xeOdnY77pZL6mr7VnZ2dnUVZ_gSdnZ2d@[EMAIL PROTECTED]
>* Chris Thomasson:
>> "Alf P. Steinbach" <alfps@[EMAIL PROTECTED]
> wrote in message 
>> news:cI6dncePT72PYr_VnZ2dnUVZ_tHinZ2d@[EMAIL PROTECTED]
>>> * Chris Thomasson:
>>>> Does this program produce any undefined-behavior:
>>>>
>>>> http://pastebin.com/m5eb3c81a
>>>>
>>>> ?
>>>
>>> Yes,
>>>
>>>   *pregion = (Region*)0x12348765
>>>
>>> is formally UB.
>>
>> Is that the only thing you can find?
>
> If you want a more thorough discussion, post the code, and explain what 
> you're concerned about.  Then others will probably jump on it.  It's The

> Way(TM).

Okay:


/**********************************************************************/
#include <cstdio>
#include <cstddef>
#include <cassert>
#include <new>



// Imagine there is a "special" allocator interface...
// I will just use ::operator new/delete for place holders
namespace Allocator {
  struct Region;

  static void* MemRequest(
   Region** pregion,
   std::size_t size
  ) {
    *pregion = (Region*)0x12348765;
    return ::operator new(size);
  }

  static void MemRelease(
   Region* region,
   void* ptr,
   std::size_t size
  ) {
    assert(region == (Region*)0x12348765);
    ::operator delete(ptr);
  }
}




// Can I use the above allocator interface like this:
template<typename T>
struct AllocatorBlock {
  unsigned char mBuffer[sizeof(T)];
  Allocator::Region* mRegion;

  static void* MemRequest() {
    Allocator::Region* region;
    AllocatorBlock* const block = (AllocatorBlock*)
      Allocator::MemRequest(&region, sizeof(T));
    block->mRegion = region;
    return block->mBuffer;
  }

  static void MemRelease(void* ptr) {
    AllocatorBlock* const block = (AllocatorBlock*)ptr;
    Allocator::MemRelease(block->mRegion, ptr, sizeof(T));
  }
};


struct MyObject {
  int const State;
  MyObject(int state) : State(state) {}

  // special per-object new/delete overload...
  void* operator new(std::size_t size) {
    return AllocatorBlock<MyObject>::MemRequest();
  }

  void operator delete(void* ptr) {
    AllocatorBlock<MyObject>::MemRelease(ptr);
  }
};




#define OBJECTS 10

int main() {
  int i;
  MyObject* objs[OBJECTS] = { NULL };

  for (i = 0; i < OBJECTS; ++i) {
    objs[i] = new MyObject(i + 1);
    std::printf("(%p/%d) - Create\n", (void*)objs[i], objs[i]->State);
  }

  std::puts("-----------------------------------------");

  for (i = 0; i < OBJECTS; ++i) {
    std::printf("(%p/%d) - Destroy\n", (void*)objs[i], objs[i]->State);
    delete objs[i];
  }


  std::puts("\n\n\n__________________________________\
_________________________\nPress <ENTER> to exit...");
  std::getchar();
  return 0;
}


/**********************************************************************/
 




 8 Posts in Topic:
Using special allocator interfaces...
"Chris Thomasson&quo  2008-05-08 06:20:47 
Re: Using special allocator interfaces...
"Alf P. Steinbach&qu  2008-05-08 15:24:33 
Re: Using special allocator interfaces...
"Chris Thomasson&quo  2008-05-08 06:44:06 
Re: Using special allocator interfaces...
"Alf P. Steinbach&qu  2008-05-08 16:00:06 
Re: Using special allocator interfaces...
"Chris Thomasson&quo  2008-05-08 07:30:35 
Re: Using special allocator interfaces...
"Chris Thomasson&quo  2008-05-08 22:03:09 
Re: Using special allocator interfaces...
"Chris Thomasson&quo  2008-05-09 15:34:04 
Re: Using special allocator interfaces...
Daniel Pitts <newsgrou  2008-05-08 08:12:00 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Jul 24 2:06:58 CDT 2008.