Re: Reference counting

Discussion in 'C Programming' started by jameskuyper, Jun 15, 2011.

  1. jameskuyper

    jameskuyper Guest

    On Tuesday, June 14, 2011 6:00:21 PM UTC-4, Tim Rentsch wrote:
    > James Kuyper <> writes:
    >
    > > On 06/10/2011 02:30 PM, ImpalerCore wrote:

    ....
    > >> #define PTR_HEADER_SIZE (sizeof (int))
    > >>
    > >> void* rc_malloc( size_t size )
    > >> {
    > >> void* mem = NULL;
    > >> void* p = NULL;
    > >>
    > >> p = malloc( size + PTR_HEADER_SIZE );
    > >>
    > >> if ( p )
    > >> {
    > >> /* Set the reference count to 1. */
    > >> *((int*)p) = 1;
    > >>
    > >> mem = (unsigned char*)p + PTR_HEADER_SIZE;
    > >> }
    > >>
    > >> return mem;
    > >> }

    > >
    > > The value stored in 'p' is guaranteed to be correctly aligned for all
    > > types. However, the only thing portably guaranteed about alignment off
    > > the value stored in 'mem' is that it is suitable for 'int', it need not
    > > be suitably aligned for any type with a size greater than 1 that is
    > > different from that of 'int'. [snip]

    >
    > Unless one considers the type 'unsigned int' to be a type
    > different from that of 'int'.


    I was talking about types "with a size ... that is different from that of 'int'". 'unsigned int' is a type with a size that is guaranteed to be the same as that of 'int', so that type is just as safe as 'int' itself.
     
    jameskuyper, Jun 15, 2011
    #1
    1. Advertising

  2. jameskuyper

    Ian Collins Guest

    On 06/16/11 09:42 AM, jameskuyper wrote:

    Please don't use the sociopathic "new" google interface, it ruins threading!

    --
    Ian Collins
     
    Ian Collins, Jun 15, 2011
    #2
    1. Advertising

  3. jameskuyper

    James Kuyper Guest

    On 06/15/2011 06:01 PM, Ian Collins wrote:
    > On 06/16/11 09:42 AM, jameskuyper wrote:
    >
    > Please don't use the sociopathic "new" google interface, it ruins threading!


    Sorry about that. I normally post from my home machine, and use Google
    only for tracking the newsgroups when not at home. I should have been
    more careful about responding.
    --
    James Kuyper
     
    James Kuyper, Jun 16, 2011
    #3
  4. jameskuyper

    Tim Rentsch Guest

    jameskuyper <> writes:

    > On Tuesday, June 14, 2011 6:00:21 PM UTC-4, Tim Rentsch wrote:
    >> James Kuyper <> writes:
    >>
    >> > On 06/10/2011 02:30 PM, ImpalerCore wrote:

    > ...
    >> >> #define PTR_HEADER_SIZE (sizeof (int))
    >> >>
    >> >> void* rc_malloc( size_t size )
    >> >> {
    >> >> void* mem = NULL;
    >> >> void* p = NULL;
    >> >>
    >> >> p = malloc( size + PTR_HEADER_SIZE );
    >> >>
    >> >> if ( p )
    >> >> {
    >> >> /* Set the reference count to 1. */
    >> >> *((int*)p) = 1;
    >> >>
    >> >> mem = (unsigned char*)p + PTR_HEADER_SIZE;
    >> >> }
    >> >>
    >> >> return mem;
    >> >> }
    >> >
    >> > The value stored in 'p' is guaranteed to be correctly aligned for all
    >> > types. However, the only thing portably guaranteed about alignment off
    >> > the value stored in 'mem' is that it is suitable for 'int', it need not
    >> > be suitably aligned for any type with a size greater than 1 that is
    >> > different from that of 'int'. [snip]

    >>
    >> Unless one considers the type 'unsigned int' to be a type
    >> different from that of 'int'.

    >
    > I was talking about types "with a size ... that is different from that
    > of 'int'". 'unsigned int' is a type with a size that is guaranteed to
    > be the same as that of 'int', so that type is just as safe as 'int'
    > itself.


    I see - the unspecified referent of the final "that" allowed
    the earlier statement to be read ambiguously.

    But the assertion about size isn't right either. The pointer
    value in 'mem' is suitably aligned for any type whose size
    evenly divides 'sizeof (int)', or for that matter any type
    whose alignment evenly divides 'sizeof (int)'. The second
    condition is conservatively testable (no false positives,
    only potentially false negatives), and the first is exactly
    testable, using completely portable code.
     
    Tim Rentsch, Jun 19, 2011
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ash
    Replies:
    1
    Views:
    449
    SenderX
    Oct 24, 2003
  2. Kalle Rutanen

    Reference counting in C++

    Kalle Rutanen, May 7, 2005, in forum: C++
    Replies:
    0
    Views:
    590
    Kalle Rutanen
    May 7, 2005
  3. Tony Johansson

    reference counting

    Tony Johansson, May 22, 2005, in forum: C++
    Replies:
    4
    Views:
    385
    Andrew Koenig
    May 23, 2005
  4. mathieu
    Replies:
    8
    Views:
    532
    Juha Nieminen
    Aug 31, 2008
  5. edwardfredriks

    counting up instead of counting down

    edwardfredriks, Sep 6, 2005, in forum: Javascript
    Replies:
    6
    Views:
    241
    Dr John Stockton
    Sep 7, 2005
Loading...

Share This Page