Quoting Thorsten Haude <[EMAIL PROTECTED]>:

> Hi,
>
> I just stumbled over this function in source/interpret.c:
>
> /* Allocate a new string buffer of length chars */
> char *AllocString(int length)
> {
>     char *mem;
>
>     mem = XtMalloc(length + sizeof(char *) + 1);
>     *((char **)mem) = AllocatedStrings;
>     AllocatedStrings = mem;
> #ifdef TRACK_GARBAGE_LEAKS
>     ++numAllocatedStrings;
> #endif
>     return mem + sizeof(char *) + 1;
> }
>
> I have a number of questions about and/or issues with this functions.
>
> - XtMalloc is never tested to be successful. It does not return NULL
>   on errors, but calls XtErrorMsg(), which simply exits NEdit. We
>   have more than 500 calls to XtMalloc; should we cover this by an
>   error handler?
> - It mallocs (length + sizeof(char *) + 1), which looks like a typo.
>   Should this really be (length * sizeof(char *) + 1)?
> - Also, it returns (mem + sizeof(char *) + 1), which is (char* +
>   size_t + int). What is this supposed to bring about?
> - Lastly, it promises in the comment:
>
>     Length does not include the terminating null ** character, so to
>     allocate space for a string of strlen == n, you must ** use
>     AllocString(n+1).
>
>   So what is the + 1 about?

This is the simple garbage management overhead for strings. In brief, all
macro strings are linked in a list, and have a flag byte. When a macro
completes, all global macro variables are scanned and the strings have their
flag set. Then the full list is scanned and any strings without the flag set
are deleted.

The snippet of code you gave actually shows the new buffer being added to the
linked list, AllocatedStrings.

Tony
-- 
NEdit Develop mailing list - [email protected]
http://www.nedit.org/mailman/listinfo/develop

Reply via email to