[fpc-devel] MemSize argument validity

Felipe Monteiro de Carvalho felipemonteiro.carvalho at gmail.com
Wed Feb 17 12:56:15 CET 2010


I would immediately think that the memory size could either be stored
at a negative offset in the memory block or in a table. Looking at the
standard implementation in FPC it uses a negative offset:

from heap.inc:

function SysMemSize(p: pointer): ptruint;
begin
  result := pmemchunk_fixed(pointer(p)-sizeof(tmemchunk_fixed_hdr))^.size;
  if (result and fixedsizeflag) = 0 then
    begin
      result := result and sizemask;
      dec(result, sizeof(tmemchunk_var_hdr));
    end
  else
    begin
      result := result and fixedsizemask;
      dec(result, sizeof(tmemchunk_fixed_hdr));
    end;
end;

So it's impossible with this implementation to check if the pointer is
valid or not.

I think it's possible for you to write your own memory manager which
stores data about memory blocks in a table instead and is then able to
check if the pointer is valid or not. Some operations would be slower
in this case, but also safer.

-- 
Felipe Monteiro de Carvalho



More information about the fpc-devel mailing list