Re: Voting for FastCode rules on memory reading



> The rule about reading past the end of AnsiStrings were accepted
> because the MM allocated memory for strings up to (but not including)
> the first 4 byte aligned byte after the end of string (including #0
> terminator). We were sure that it was 100% safe to read the entire
> memory allocated for a string.

That depends on the memory manager. The Delphi help only specifies this
behavior only for the default memory manager (topic: Memory
Management). In general it isn't clear from the help file that this is
demanded of a memory manager (topic: TMemoryManager type; this topic is
clearly meant to specify how MM functions should behave in general, and
it does not mention alignment).

The reason that it is safe to read the entire last block is not the
specification of the MM, but the CPU protection mechanism: since an
aligned 32-bit block of data is always in only one page it is entriely
readable if one byte of it is.

AFAICS it would be theoretically both possible and legal to make a MM
that aligns all data on multiples of 7 for example. This could mean
that the last DWORD of the string is also used for something else. It
is still safe however, because the entire DWORD is in the same page.
.



Relevant Pages

  • Re: How to take in a string of any size?
    ... >the contents into the allocated memory. ... nobody said the string started at the beginning of the file. ... >number of calls to realloc() isn't going to be excessive). ...
    (comp.lang.c)
  • Re: A better BinaryConverter function in C++
    ... string ConvertToBinary(string Msg) ... You seem to believe these two lines copy the string into a newly ... allocate a block of memory and store its address in pCMsg pointer. ... and the allocated memory is leaked. ...
    (microsoft.public.vc.language)
  • Re: Fast string operations
    ... Looping: I thought looping over arrays in managed code was "slow" ... array handling and such. ... The problem with TrimHelper is that it always returns a new string instance. ... The customer perceives this as a memory leak. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Discovering variable types...
    ... >- but I suppose MS expect us to use wrappers ... memory allocations for your variables from disk as well. ... >They most certainly are of fixed size, changing the size of a String ... >>me to keep buffer size and current postion right in the memory block. ...
    (comp.lang.pascal.delphi.misc)
  • Re: Optimize
    ... if you use it like me to get 'the next string ptr' in addition. ... | |mov ebx eax;dw aligned strings are faster ... | Would it be enough to touch each 16th bytes in the 64K memory area ... Cache-line size is fixed, my AMD got 64 bytes per line. ...
    (alt.lang.asm)