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 > Programming Threads > Re: sup****ting...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 17 of 28 Topic 4066 of 4146
Post > Topic >>

Re: sup****ting header-less blocks in memory allocators...

by Michel Decima <michel.decima@[EMAIL PROTECTED] > Oct 3, 2008 at 01:56 PM

Dmitriy V'jukov a écrit :
> On Oct 2, 10:50 pm, "Chris M. Thomasson" <n...@[EMAIL PROTECTED]
> wrote:
> 
>> Please correct me if I am wrong, but it seems as if this particular
>> allocator interface would "need" to know what type it was dealing with
in
>> advance; correct?
> 
> Yes,
> 
>> In other words, it probably could not be used to overload
>> global operator new/delete right?
> 
> Yes.
> 
>> Or, is `derived_t' dealing with internal
>> allocator structures. Where am I going wrong here Dmitriy? You know, I
was
>> thinking about creating something special for C++. But, I am a C
programmer,
>> and felt the need to sup****t my language of choice. The cool thing
about C++
>> is that you can do exactly what you did. Also, you can overload class
local
>> delete operator and have it automatically return the size of the
dynamic
>> allocation be it single object or array. If the user allocated an
array, you
>> simply divide this size of object size, and bam you have array size.
> 
> It's not always true. For example:
> 
> template<typename derived_t>
> struct object_t
> {
>   static void* operator new (size_t sz)
>   {
>     return malloc(sizeof(derived_t));
>   }
> };
> 
> struct derived1 : object_t<derived1>
> {
>   int x1;
> };
> 
> struct derived2 : derived1
> {
>   int x2;
> };
> 
> derived2* d2 = new derived2 (); // CRASH! Allocate memory only for
> derived1, derived2 will not fit into that memory

But if you use the 'sz' parameter of operator new to call malloc(),
then you will allocate enough memory for derived2.
And you can overload the class-local delete operator with a second
size_t parameter, you will known the size of the object to deallocate:

#include <stdlib.h>
#include <stdio.h>

void* alloc_8()  { printf( "alloc_8\n")  ; return malloc( 8 ) ; }
void* alloc_16() { printf( "alloc_16\n") ; return malloc( 16 ) ; }
void free_8( void* ptr )  { printf( "free_8\n" );  ::free( ptr ) ; }
void free_16( void* ptr ) { printf( "free_16\n" ); ::free( ptr ) ; }

template<typename derived_t>
struct object_t
{
     // derived_t is not required to be most derived type

     static void* operator new ( size_t sz )
     {
         // resolution at runtime
         if ( sz <= 8 )
             return alloc_8() ;
         else if ( sz <= 16 )
             return alloc_16() ;
         else
             return ::malloc( sz ) ;
     }

     static void operator delete ( void* ptr, size_t sz )
     {
         // resolution at runtime
         if ( sz <= 8 )
             return free_8( ptr ) ;
         else if ( sz <= 16 )
             return free_16( ptr ) ;
         else
             ::free( ptr ) ;
     }
} ;

struct A  : object_t< A >
{
     virtual ~A() {}
     long x ;
} ;

struct B : A
{
     long y ;
} ;

int main()
{
     printf( "sizeof(A)=%lu\n", sizeof( A ) ) ;
     A* a = new A ;
     delete a ;

     printf( "sizeof(B)=%lu\n", sizeof( B ) ) ;
     B* b = new B ;
     delete b ;
}
 




 28 Posts in Topic:
supporting header-less blocks in memory allocators...
"Chris M. Thomasson&  2008-10-02 07:28:39 
Re: supporting header-less blocks in memory allocators...
"Chris M. Thomasson&  2008-10-02 07:30:14 
Re: supporting header-less blocks in memory allocators...
Eric Sosman <Eric.Sosm  2008-10-02 10:55:36 
Re: supporting header-less blocks in memory allocators...
"Chris M. Thomasson&  2008-10-02 10:17:04 
Re: supporting header-less blocks in memory allocators...
"Chris M. Thomasson&  2008-10-02 14:47:48 
Re: supporting header-less blocks in memory allocators...
"Dmitriy V'jukov&quo  2008-10-02 08:41:31 
Re: supporting header-less blocks in memory allocators...
"Chris M. Thomasson&  2008-10-02 09:29:16 
Re: supporting header-less blocks in memory allocators...
"Dmitriy V'jukov&quo  2008-10-02 11:00:02 
Re: supporting header-less blocks in memory allocators...
"Chris M. Thomasson&  2008-10-02 11:50:57 
Re: supporting header-less blocks in memory allocators...
"Chris M. Thomasson&  2008-10-02 11:55:02 
Re: supporting header-less blocks in memory allocators...
"Chris M. Thomasson&  2008-10-02 12:20:25 
Re: supporting header-less blocks in memory allocators...
"Chris M. Thomasson&  2008-10-02 12:30:51 
Re: supporting header-less blocks in memory allocators...
"Dmitriy V'jukov&quo  2008-10-02 12:00:46 
Re: supporting header-less blocks in memory allocators...
"Chris M. Thomasson&  2008-10-02 12:30:09 
Re: supporting header-less blocks in memory allocators...
"Chris M. Thomasson&  2008-10-02 12:38:26 
Re: supporting header-less blocks in memory allocators...
"Chris M. Thomasson&  2008-10-02 12:48:45 
Re: supporting header-less blocks in memory allocators...
Michel Decima <michel.  2008-10-03 13:56:34 
Re: supporting header-less blocks in memory allocators...
"Dmitriy V'jukov&quo  2008-10-02 12:07:41 
Re: supporting header-less blocks in memory allocators...
"Chris M. Thomasson&  2008-10-03 22:41:41 
Re: supporting header-less blocks in memory allocators...
"Dmitriy V'jukov&quo  2008-10-02 12:24:01 
Re: supporting header-less blocks in memory allocators...
"Chris M. Thomasson&  2008-10-02 12:32:18 
Re: supporting header-less blocks in memory allocators...
"Chris M. Thomasson&  2008-10-02 12:35:16 
Re: supporting header-less blocks in memory allocators...
"Dmitriy V'jukov&quo  2008-10-03 03:40:04 
Re: supporting header-less blocks in memory allocators...
"Dmitriy V'jukov&quo  2008-10-03 03:42:50 
Re: supporting header-less blocks in memory allocators...
"Dmitriy V'jukov&quo  2008-10-03 05:52:20 
Re: supporting header-less blocks in memory allocators...
Michel Decima <michel.  2008-10-03 15:28:13 
Re: supporting header-less blocks in memory allocators...
"Chris M. Thomasson&  2008-10-03 22:19:59 
Re: supporting header-less blocks in memory allocators...
"Dmitriy V'jukov&quo  2008-10-06 00:02:32 

Post A Reply:
  Go here to Signup

AddThis Feed Button


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

Contact
tan12V112 Sat Nov 22 9:51:08 CST 2008.