| |
| Posted by Michele in reply to Wu Yongwei | PermalinkReply |
|
Michele
Posted in reply to Wu Yongwei
| also i'm having problems on dmc 8.3.5n with placement delete and new/delete resolution:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
//the allocator must know the size of chunks from address!!
class iallocator
{
public:
virtual void* alloc(size_t s)
{
void* tmp = malloc(s);
printf("allocator::alloc(%d) return:%x\n",s,tmp);
return tmp;
}
virtual void free(void* p)
{
printf("allocator::free(addr:%x)\n",p);
::free(p);
}
virtual void* reserve(size_t s)
{
return NULL;
}
};
iallocator globalallocator;
#define GLOBAL_ALLOCATOR globalallocator
void* operator new(size_t s)
{
printf("::new(%d)\n",s);
return NULL;
}
void operator delete(void* p)
{
printf("::delete()\n");
}
class base
{
public:
base()
{
//NOP
}
~base()
{
//NOP
}
inline static void* New(size_t s,iallocator &allo = GLOBAL_ALLOCATOR)
{
void** pchunk = (void**) allo.alloc(s+sizeof(void*));
pchunk[0] = (void*)&allo;
return &pchunk[1];
}
inline static void Delete(void* p,iallocator &allo)
{
allo.free(&((void**)p)[-1]);
}
inline static void Delete(void* p)
{
iallocator* allo = (iallocator*)((void**)p)[-1];
allo->free(&((void**)p)[-1]);
}
void* operator new(size_t s)
{
printf("base::new(size:%d)\n",s);
return New(s);
}
void operator delete(void* p,size_t s)
{
printf("base::delete(addr:%x)\n",p);
Delete(p);
}
//also this new operator should give a compiler error because the
correspondig delete is undeclarable
//and if exception are enabled the compiler need the corresponding delete in
case of exception in ctor
void* operator new(size_t s,iallocator &allo)
{
printf("base::new(size:%d,allocator:%x)\n",s,&allo);
return New(s,allo);
}
/* compiler error because no placement delete supported
void operator delete(void* p,size_t s,iallocator &allo)
{
printf("base::delete(addr:%x,allocator:%x)\n",p,&allo);
Delete(p,allo);
}
*/
void* operator new[](size_t s)
{
printf("base::new[](size:%d)\n",s);
return New(s);
}
void operator delete[](void* p,size_t s)
{
printf("base::delete[](addr:%x)\n",p);
Delete(p);
}
// same for the operator new[]
void* operator new[](size_t s,iallocator &allo)
{
printf("base::new[](size:%d,allocator:%x)\n",s,&allo);
return New(s,allo);
}
/* compiler error because no placement delete[] supported
void operator delete[](void* p,iallocator &allo)
{
printf("base::delete[](addr:%x,allocator:%x)\n",p,&allo);
Delete(p,allo);
}
*/
};
//************************************************************************** ****
class a : public base
{
private:
int _a;
public:
a()
{
printf("a::ctor()\n");
//throw 1;
}
a(int p)
{
printf("a::ctor(%d)\n",p);
_a = 666;
if(p)
throw 1;
}
~a()
{
printf("a::dtor()\n");
}
};
int main(int argc, char *argv[])
{
a* poa;
try
{
poa = new a(1);
delete poa;
}
catch(int ignore)
{}
printf("\n");
poa = new a(0);
delete poa;
printf("\n");
try
{
poa = new(globalallocator) a(1);
delete poa;
}
catch(int ignore)
{}
printf("\n");
poa = new(globalallocator) a(0);
delete poa;
printf("\n");
try
{
poa = new a[3];
delete[] poa;
}
catch(int ignore)
{}
printf("\n");
try
{
poa = new(globalallocator) a[3];
delete[] poa;
}
catch(int ignore)
{}
getchar();
return 0;
}
compiled with: dmc -Ae
also the output have somethig strage:
OUTPUT:
base::new(size:4)
allocator::alloc(8) return:8303d0
a::ctor(1)
base::delete(addr:8303d4) //expected call to placement delete
allocator::free(addr:8303d0)
base::new(size:4)
allocator::alloc(8) return:8303d0
a::ctor(0)
a::dtor()
base::delete(addr:8303d4)
allocator::free(addr:8303d0)
base::new(size:4,allocator:40bf2c)
allocator::alloc(8) return:8303d0
a::ctor(1)
base::delete(addr:8303d4)
allocator::free(addr:8303d0)
base::new(size:4,allocator:40bf2c)
allocator::alloc(8) return:8303d0
a::ctor(0)
a::dtor()
base::delete(addr:8303d4)
allocator::free(addr:8303d0)
a::ctor() //???? new is't called? after the first ctor i'll expect
base::new[] as in the next section
a::ctor()
a::ctor()
a::dtor()
a::dtor()
a::dtor()
::delete() //called global delete??? i think should be: base::delete[]
base::new[](size:16,allocator:40bf2c)
allocator::alloc(20) return:830da8
a::ctor()
a::ctor()
a::ctor()
a::dtor()
a::dtor()
a::dtor()
::delete() //called global delete??? i think should be: placement
base::delete[]
|