Recent

Author Topic: [CLOSED] Clarification of tdynarray's refcount and high usage  (Read 938 times)

lagprogramming

  • Sr. Member
  • ****
  • Posts: 407
The file rtl/inc/dynarr.inc has
Code: Pascal  [Select][+][-]
  1. type
  2.    { don't add new fields, the size is used }
  3.    { to calculate memory requirements       }
  4.    pdynarray = ^tdynarray;
  5.    { removed packed here as
  6.      1) both fields have typically the same size (2, 4 or 8 bytes), if this is not the case, packed
  7.         should be used only for this architecture
  8.      2) the memory blocks are sufficiently well aligned
  9.      3) in particular 64 bit CPUs which require natural alignment suffer from
  10.         the packed as it causes each field access being split in 8 single loads and appropriate shift operations
  11.    }
  12.    tdynarray = { packed } record
  13.       refcount : ptrint;
  14.       high : tdynarrayindex;
  15.    end;
   
   1/2 I've tried to understand the usage of refcount, but the Pascal code made the mission difficult. The questions were related to negative refcount values like: is refcount allowed to be negative, can refcount become negative in any way, and so on. Looking at the code, my conclusion was that refcount's minimum value should be zero. When it's zero, something went wrong somewhere and an exception has to be raised. For normal usage a minimum value of 1 is needed.
   Many times developers use signed integers although those variables are expected to always use unsigned integers. It's this experience with other Pascal reviewed code that made me think twice about tdynarray's refcount.
   If refcount should never be negative, I ask you why refcount is ptrint instead of ptruint. If it was ptruint, programmers would have understood that refcount is designed to store values greater or equal to zero. No more questions about negative values, the code would have been easier to understand.
   
   2/2 tdynarrayindex(tdynarray.high) points to a signed integer, too. I wonder if tdynarray.high is allowed to store negative values. The -1 value of high is the result of function fpc_dynarray_high, not the value stored at tdynarray.high.
Code: Pascal  [Select][+][-]
  1. function fpc_dynarray_high(p : pointer) : tdynarrayindex;[Public,Alias:'FPC_DYNARRAY_HIGH']; compilerproc;
  2.   begin
  3.      if assigned(p) then
  4.        fpc_dynarray_high:=pdynarray(p-sizeof(tdynarray))^.high
  5.      else
  6.        fpc_dynarray_high:=-1;
  7.   end;
« Last Edit: June 30, 2023, 01:22:03 pm by lagprogramming »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11944
  • FPC developer.
Re: Clarification of tdynarray's refcount and high usage
« Reply #1 on: June 13, 2023, 10:59:51 am »
The base type is signed in pascal. Mixing unsigned with signed integers might lead to unnecessary type promotion. Any subtraction might lead to a out of bounds value (runtime errors!) for unsigned types.

So that's why the default is signed unless there are extremely good reasons not to.

ASerge

  • Hero Member
  • *****
  • Posts: 2337
Re: Clarification of tdynarray's refcount and high usage
« Reply #2 on: June 13, 2023, 05:29:21 pm »
Also refcount is negative for constants.

 

TinyPortal © 2005-2018