Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: Reference counting

Reply
Thread Tools

Re: Reference counting

 
 
jameskuyper
Guest
Posts: n/a
 
      06-15-2011
On Tuesday, June 14, 2011 6:00:21 PM UTC-4, Tim Rentsch wrote:
> James Kuyper <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      06-15-2011
On 06/16/11 09:42 AM, jameskuyper wrote:

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

--
Ian Collins
 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      06-16-2011
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
 
Reply With Quote
 
Tim Rentsch
Guest
Posts: n/a
 
      06-19-2011
jameskuyper <(E-Mail Removed)> writes:

> On Tuesday, June 14, 2011 6:00:21 PM UTC-4, Tim Rentsch wrote:
>> James Kuyper <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reference counting and API (const reference vs pointer oriented) mathieu C++ 8 08-31-2008 09:05 AM
counting up instead of counting down edwardfredriks Javascript 6 09-07-2005 03:30 PM
reference counting Tony Johansson C++ 4 05-23-2005 01:28 PM
Reference counting in C++ Kalle Rutanen C++ 0 05-07-2005 12:26 PM
flyweight reference counting ash C++ 1 10-24-2003 10:40 AM



Advertisments