Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2008-05

Issue with TBits.OpenBit

7 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Sergei Gorelkin

Hello,

The current TBits.OpenBit function differs from Delphi. In Delphi, it is a read-only operation: if all allocated bits are set, it returns value equal to Size. In FPC, it expands the storage and modifies the Size property, so the result never exceeds (Size-1). This difference causes the following code to fail:

var
   index: integer;
begin
   index := Bits.OpenBit;
   if index = Bits.Size then
     AllocateSomeResourceElsewhere();  // In FPC, this is never executed
   Bits[index] := True;                // Bits get expanded here
end;

I tried to fix that myself, but TBits code looked next to chaos for me. For example, the Clear method checks bit index and sets FBSize to (bit+1), but SetOn (which is supposed to be just complementary) does not check the index and sets FBSize to (bit) withoud adding 1; setting FBSize and FSize is scattered over several methods. So I decided to improve that, too. SetOn and Clear functionality merged into SetBit, and FBSize and FSize are changed only in SetSize.

The resulting patch is attached.

Some questions still remain, however:

1) Typing: TBitArray (classesh.inc line 308) is declared as array of Cardinal. It means that on 64-bit platforms it will consist of QWord's. Since number of bits in each element is hardcoded to 32, it looks like half of the storage will be wasted (although TBits will operate correctly). How should it be fixed: by changing Cardinal to LongWord, or by using platform-dependent values for BITSHIFT and MASK?

2) Raising 'Out of memory' error in TBits.SetSize is probably redundant because ReallocMem will do it itself?

3) Is the TBits.Size limit of 65536 necessary? I guess that it comes from Delphi, where it is caused by assembler instructions being used (BT/BTS and alike). But FPC implementation has no such limitation, so the limit looks very artificial...

Best regards,
Sergei

Reply by email

Vincent Snijders
to Sergei Gorelkin
Sergei Gorelkin wrote
1) Typing: TBitArray (classesh.inc line 308) is declared as array of Cardinal. It means that on 64-bit platforms it will consist of QWord's.

No, cardinal is 32 bits on a 64 bits platform too.

Vincent

Reply by email

Graeme Geldenhuys
to Vincent Snijders
Vincent Snijders wrote 5 lines
Sergei Gorelkin schreef: > 1) Typing: TBitArray (classesh.inc line 308) is declared as array of > Cardinal. It means that on 64-bit platforms it will consist of QWord's. No, cardinal is 32 bits on a 64 bits platform too.

Is it???
Free Pascal documentation - Chapter 3 (Types) in table 3.2 says the following.

Cardinal: either word (2 bytes), longword (4 bytes) or qword (8 bytes).

Regards,
- Graeme -

_______________________________________________________ fpGUI - a cross-platform GUI toolkit using Free Pascal http://opensoft.homeip.net/fpgui/

Reply by email

Michael Van Canneyt
to Graeme Geldenhuys
Graeme Geldenhuys wrote 11 lines
Vincent Snijders wrote: > Sergei Gorelkin schreef: > > 1) Typing: TBitArray (classesh.inc line 308) is declared as array of > > Cardinal. It means that on 64-bit platforms it will consist of QWord's. > > No, cardinal is 32 bits on a 64 bits platform too. Is it??? Free Pascal documentation - Chapter 3 (Types) in table 3.2 says the following. Cardinal: either word (2 bytes), longword (4 bytes) or qword (8 bytes).

Unfortunately, that was wrong. It's been corrected.

Michael.

Reply by email

Sergei Gorelkin
to Michael Van Canneyt
Michael Van Canneyt wrote 15 lines
On Fri, 23 May 2008, Graeme Geldenhuys wrote: > Vincent Snijders wrote: >> Sergei Gorelkin schreef: >>> 1) Typing: TBitArray (classesh.inc line 308) is declared as array of >>> Cardinal. It means that on 64-bit platforms it will consist of QWord's. >> No, cardinal is 32 bits on a 64 bits platform too. > Is it??? > Free Pascal documentation - Chapter 3 (Types) in table 3.2 says the following. > > Cardinal: either word (2 bytes), longword (4 bytes) or qword (8 bytes). Unfortunately, that was wrong. It's been corrected.

Ok, then this one is a false alarm.

Sergei

Reply by email

Felipe Monteiro de Carvalho
to Michael Van Canneyt: Unfortunately, that was wrong. It's been corrected. Michael.
Michael Van Canneyt wrote
Unfortunately, that was wrong. It's been corrected.

It would be nice if the on-line docs were updated:

http://www.freepascal.org/docs-html/ref/refsu5.html#x25-240003.1.1

What are the not-fixed-size types then? Is Integer always 4 bytes? Isn't it 2 bytes in TP mode or something?

What is the not-fixed-size type for Unsigner Integer? PtrUInt?

Felipe Monteiro de Carvalho

Reply by email

Michael Van Canneyt
to Felipe Monteiro de Carvalho
Felipe Monteiro de Carvalho wrote 12 lines
On Fri, May 23, 2008 at 11:40 AM, Michael Van Canneyt wrote: > Unfortunately, that was wrong. It's been corrected. It would be nice if the on-line docs were updated: http://www.freepascal.org/docs-html/ref/refsu5.html#x25-240003.1.1 What are the not-fixed-size types then? Is Integer always 4 bytes? Isn't it 2 bytes in TP mode or something? What is the not-fixed-size type for Unsigner Integer? PtrUInt?

It depends on what you want. If you want an integer with the size of a pointer, then the answer is yes.

Michael.

Reply by email