• Re: C and turtles, 80286 protected mode

    From John Levine@21:1/5 to All on Wed Oct 16 01:08:40 2024
    According to MitchAlsup1 <[email protected]>:
    The paragraaph with 3 >'s indicates malloc() cannot be written
    in std. C. It used to be written in std. K&R C. I am not asking
    if it is still in the std libraries, I am asking what happened
    to make it impossible to write malloc() in std. C ?!?

    It's easy enough to write malloc() in standard C if your flavor of the
    standard includes a way to ask the operating system for heap space.
    Back in the old days it was sbrk(). Now it's mmap(MAP_ANON).

    But I would be quite surprised if either of those ever made it into
    any version of standard C.

    --
    Regards,
    John Levine, [email protected], Primary Perpetrator of "The Internet for Dummies",
    Please consider the environment before reading this e-mail. https://jl.ly

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From John Levine@21:1/5 to All on Wed Oct 16 03:09:59 2024
    According to MitchAlsup1 <[email protected]>:
    It's easy enough to write malloc() in standard C if your flavor of the
    standard includes a way to ask the operating system for heap space.
    Back in the old days it was sbrk(). Now it's mmap(MAP_ANON).

    But I would be quite surprised if either of those ever made it into
    any version of standard C.

    sbrk() was in the std. library on PDP-11/70 that I used 1982.

    It was in the library on the PDP-11/45 I used in 1976, too, but
    the C library was whatever was in the C library, a mix of Unix
    system calls and other stuff. The first C standard wasn't
    published until 1985.

    In 1976 I also wrote C code that ran on an 11/05 that controlled some
    early bitmap terminals. It didn't have sbrk() becasue there wasn't an
    operating system underneath.
    --
    Regards,
    John Levine, [email protected], Primary Perpetrator of "The Internet for Dummies",
    Please consider the environment before reading this e-mail. https://jl.ly

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From MitchAlsup1@21:1/5 to John Levine on Wed Oct 16 02:48:39 2024
    On Wed, 16 Oct 2024 1:08:40 +0000, John Levine wrote:

    According to MitchAlsup1 <[email protected]>:
    The paragraaph with 3 >'s indicates malloc() cannot be written
    in std. C. It used to be written in std. K&R C. I am not asking
    if it is still in the std libraries, I am asking what happened
    to make it impossible to write malloc() in std. C ?!?

    It's easy enough to write malloc() in standard C if your flavor of the standard includes a way to ask the operating system for heap space.
    Back in the old days it was sbrk(). Now it's mmap(MAP_ANON).

    But I would be quite surprised if either of those ever made it into
    any version of standard C.

    sbrk() was in the std. library on PDP-11/70 that I used 1982.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David Brown@21:1/5 to John Levine on Wed Oct 16 10:04:23 2024
    On 16/10/2024 03:08, John Levine wrote:
    According to MitchAlsup1 <[email protected]>:
    The paragraaph with 3 >'s indicates malloc() cannot be written
    in std. C. It used to be written in std. K&R C. I am not asking
    if it is still in the std libraries, I am asking what happened
    to make it impossible to write malloc() in std. C ?!?

    It's easy enough to write malloc() in standard C if your flavor of the standard includes a way to ask the operating system for heap space.
    Back in the old days it was sbrk(). Now it's mmap(MAP_ANON).


    Note that these are in no way universal. They might be common on some
    systems, but there are vast numbers of C implementations that are not
    used on POSIX or *nix type systems.

    And while it is, I think, /possible/ to write a malloc() in pure
    portable standard C (plus something like sbrk), it is never done. There
    are always advantages in using implementation-specific features for
    picking good sizes and alignments, handling locks for threading safety,
    using different algorithms with different balances between speed and compactness, and so on.

    But I would be quite surprised if either of those ever made it into
    any version of standard C.


    Indeed.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Scott Lurndal@21:1/5 to Paul A. Clayton on Wed Oct 16 19:41:03 2024
    "Paul A. Clayton" <[email protected]> writes:
    On 10/15/24 9:08 PM, John Levine wrote:
    According to MitchAlsup1 <[email protected]>:
    The paragraaph with 3 >'s indicates malloc() cannot be written
    in std. C. It used to be written in std. K&R C. I am not asking
    if it is still in the std libraries, I am asking what happened
    to make it impossible to write malloc() in std. C ?!?

    It's easy enough to write malloc() in standard C if your flavor of the
    standard includes a way to ask the operating system for heap space.
    Back in the old days it was sbrk(). Now it's mmap(MAP_ANON).

    Another possibility might be for the OS/hardware to support
    allocate on use. This would be similar to an implicit
    mmap(MAP_ANONYMOUS | MAP_FIXED) on a page fault. Obviously, this
    would not be portable, but sbrk() and mmap() are not portable.

    The easiest way to implement malloc in standard C is to
    start with a statically defined allocation region.

    e.g. malloc.c:

    ...
    static unsigned char region[MAX_HEAP_IN_BYTES];

    void *
    malloc(size_t size)
    {
    /* manage heap and return allocation of size bytes */
    }

    Downside is that the application always uses MAX_HEAP_IN_BYTES
    plus the app size in memory; appropriate for standalone apps.
    (traditional definition of standalone - no OS or other
    runtime support).

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David Brown@21:1/5 to Scott Lurndal on Thu Oct 17 16:13:34 2024
    On 16/10/2024 21:41, Scott Lurndal wrote:
    "Paul A. Clayton" <[email protected]> writes:
    On 10/15/24 9:08 PM, John Levine wrote:
    According to MitchAlsup1 <[email protected]>:
    The paragraaph with 3 >'s indicates malloc() cannot be written
    in std. C. It used to be written in std. K&R C. I am not asking
    if it is still in the std libraries, I am asking what happened
    to make it impossible to write malloc() in std. C ?!?

    It's easy enough to write malloc() in standard C if your flavor of the
    standard includes a way to ask the operating system for heap space.
    Back in the old days it was sbrk(). Now it's mmap(MAP_ANON).

    Another possibility might be for the OS/hardware to support
    allocate on use. This would be similar to an implicit
    mmap(MAP_ANONYMOUS | MAP_FIXED) on a page fault. Obviously, this
    would not be portable, but sbrk() and mmap() are not portable.

    The easiest way to implement malloc in standard C is to
    start with a statically defined allocation region.

    e.g. malloc.c:

    ...
    static unsigned char region[MAX_HEAP_IN_BYTES];

    void *
    malloc(size_t size)
    {
    /* manage heap and return allocation of size bytes */
    }

    Downside is that the application always uses MAX_HEAP_IN_BYTES
    plus the app size in memory; appropriate for standalone apps.
    (traditional definition of standalone - no OS or other
    runtime support).


    There are other downsides to this.

    One is that the "region" must be zeroed by the pre-main startup code.
    On some systems, there may be efficient ways to handle this, but on
    small embedded devices it usually means effectively memset'ing the heap
    before main() can start. That can be a significant inconvenience for
    some systems. C has (at the moment) no standard way to say that a
    variable does not need to be initialised, but there are usually implementation-specific ways to handle this.

    The other issue is that returned memory blocks have a type - they are
    arrays of unsigned char - and when you use them for something else, your malloc'ed object aliases part of another normal C object (the array
    "region"). This can cause complications with advanced compiler analysis.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Thomas Koenig@21:1/5 to John Levine on Thu Oct 17 19:49:59 2024
    On 2024-10-16, John Levine <[email protected]> wrote:

    It was in the library on the PDP-11/45 I used in 1976, too, but
    the C library was whatever was in the C library, a mix of Unix
    system calls and other stuff. The first C standard wasn't
    published until 1985.

    Wasn't it 1989?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Scott Lurndal@21:1/5 to Thomas Koenig on Thu Oct 17 21:03:02 2024
    Thomas Koenig <[email protected]> writes:
    On 2024-10-16, John Levine <[email protected]> wrote:

    It was in the library on the PDP-11/45 I used in 1976, too, but
    the C library was whatever was in the C library, a mix of Unix
    system calls and other stuff. The first C standard wasn't
    published until 1985.

    Wasn't it 1989?

    The first draft was released in '85, the X3J11 committee
    was formed in '83.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lawrence D'Oliveiro@21:1/5 to Paul A. Clayton on Sun Oct 20 07:07:06 2024
    On Wed, 16 Oct 2024 15:07:14 -0400, Paul A. Clayton wrote:

    Obviously, this would not be portable, but sbrk() and mmap() are not portable.

    They are portable at the OS level, since they are part of POSIX.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lawrence D'Oliveiro@21:1/5 to Thomas Koenig on Sun Oct 20 07:08:20 2024
    On Thu, 17 Oct 2024 19:49:59 -0000 (UTC), Thomas Koenig wrote:

    On 2024-10-16, John Levine <[email protected]> wrote:

    The first C standard wasn't published until 1985.

    Wasn't it 1989?

    As I recall, the ANSI standard corresponded to the second edition of the
    K&R book.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From George Neuner@21:1/5 to [email protected] on Sun Oct 20 15:49:54 2024
    On Sun, 20 Oct 2024 07:08:20 -0000 (UTC), Lawrence D'Oliveiro
    <[email protected]d> wrote:

    On Thu, 17 Oct 2024 19:49:59 -0000 (UTC), Thomas Koenig wrote:

    On 2024-10-16, John Levine <[email protected]> wrote:

    The first C standard wasn't published until 1985.

    Wasn't it 1989?

    As I recall, the ANSI standard corresponded to the second edition of the
    K&R book.

    I may be mistaken, but my recollection is that '89 ANSI corresponded
    to the Kernighan and Plauger book.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Tim Rentsch@21:1/5 to George Neuner on Mon Oct 21 18:19:02 2024
    George Neuner <[email protected]> writes:

    On Sun, 20 Oct 2024 07:08:20 -0000 (UTC), Lawrence D'Oliveiro <[email protected]d> wrote:

    On Thu, 17 Oct 2024 19:49:59 -0000 (UTC), Thomas Koenig wrote:

    On 2024-10-16, John Levine <[email protected]> wrote:

    The first C standard wasn't published until 1985.

    Wasn't it 1989?

    As I recall, the ANSI standard corresponded to the second edition of the
    K&R book.

    I may be mistaken, but my recollection is that '89 ANSI corresponded
    to the Kernighan and Plauger book.

    The book by Kernighan and Ritchie, The C Programming Language, was
    first published in 1978; the second edition was published in 1988.

    The book by Kernighan and Plauger, Elements of Programming Style,
    was first published in 1974, with a second edition published in 1978.

    The second edition of K&R was written during the time that the C standardization effort (started roughly 1982) was taking place. The
    intent of K&R 2 was to reflect the language specification of the
    anticipated ANSI C standard. My understanding is that K&R 2, because
    it was finished a year or two before the ANSI C standard was finished,
    has some minor differences with respect to C89. (I have not made any
    effort to see whether that is so; I am simply passing along second
    hand information.)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From George Neuner@21:1/5 to [email protected] on Tue Oct 22 17:28:49 2024
    On Mon, 21 Oct 2024 18:19:02 -0700, Tim Rentsch
    <[email protected]> wrote:

    George Neuner <[email protected]> writes:

    On Sun, 20 Oct 2024 07:08:20 -0000 (UTC), Lawrence D'Oliveiro
    <[email protected]d> wrote:


    As I recall, the ANSI standard corresponded to the second edition of the >>> K&R book.

    I may be mistaken, but my recollection is that '89 ANSI corresponded
    to the Kernighan and Plauger book.

    The book by Kernighan and Ritchie, The C Programming Language, was
    first published in 1978; the second edition was published in 1988.

    The book by Kernighan and Plauger, Elements of Programming Style,
    was first published in 1974, with a second edition published in 1978.

    The second edition of K&R was written during the time that the C >standardization effort (started roughly 1982) was taking place. The
    intent of K&R 2 was to reflect the language specification of the
    anticipated ANSI C standard. My understanding is that K&R 2, because
    it was finished a year or two before the ANSI C standard was finished,
    has some minor differences with respect to C89. (I have not made any
    effort to see whether that is so; I am simply passing along second
    hand information.)

    Your information is better than mine 8-)
    Thanks.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)