a very simple malloc question 
Author Message
 a very simple malloc question

Hello all,

I'm a fairly green programmer.  Recently, i was asked in an interview
to implement malloc. Not a rocket science version, but just something.
 So, i fumbled thru a few ideas like keeping a free & used list.

Does anyone know a link to, or can offer a basic implementation of
malloc? I'd really like to learn from this experience.

Thanks!
--



Mon, 11 Apr 2005 06:07:29 GMT  
 a very simple malloc question

Quote:

>Does anyone know a link to, or can offer a basic implementation of
>malloc? I'd really like to learn from this experience.

If you've read and appreciated the malloc in K&R, I recommend a look
at BSD's PHK malloc. It's only about 1000 lines long (about a quarter
of which is introductory rubric) and very straightforward compared to,
say, glibc's malloc, but it performs very well.

http://www.freebsd.org/cgi/cvsweb.cgi/~checkout~/src/lib/libc/stdlib/...

Tony.
--

BAILEY: NORTHWEST VEERING NORTHEAST 5 TO 7. SHOWERS. GOOD.
--



Tue, 12 Apr 2005 09:18:30 GMT  
 a very simple malloc question


[ ... ]

Quote:
> Does anyone know a link to, or can offer a basic implementation of
> malloc? I'd really like to learn from this experience.

Here's a very simple one I wrote years ago.  It is intended strictly
for demonstration, not serious use, and lacks a number of things a
"real" malloc needs.

First of all, to keep things portable, this requires that you
initialize it with the address of a block of memory from which it
will do allocations.  A real version of malloc will normally allocate
memory from the OS using some non-standard function (e.g. sbrk,
HeapAlloc).

Second, this makes no attempt at coalescing freed blocks.  For real
use, that's a crucial addition.  The way things are right now, your
heap is likely to be fragmented beyond usability in a VERY short time
(it makes no real attempt at preventing

Finally (again for portability) this makes no attempt at ensuring
that the blocks it returns are aligned for any possible use -- in
fact, on hardware that has alignment requirements, you can almost
depend upon it to do bad things.  Fixing this in a non-portable
fashion borders on trivial, but finding alignment requirements
portably is a whole different story.

#include <stddef.h>

typedef struct node {
    size_t size;
    struct node *next;

Quote:
} node;

node *free_list;

static void *split_end(node *block, size_t new_size) {
    size_t difference = block->size - new_size;

    node *temp = (node *)((char *)block + difference);
    temp->size = new_size;
    block->size = difference;
    return (void *)((size_t *)temp+1);

Quote:
}

static void *split_begin(node *block, size_t new_size) {
    size_t difference = block->size-new_size;
    node *temp = (node *)((char *)block + new_size);
    temp->size = difference;
    temp->next = free_list;
    free_list = temp;
    return block;

Quote:
}

void b_init(void *block, size_t block_size) {
    ((node *)block)->size = block_size - sizeof(node);
    ((node *)block)->next = NULL;
    free_list = block;

Quote:
}

void b_free(void *block) {
    node *b = (node *)((size_t *)block -1);

    b->next = free_list;
    free_list = b;

Quote:
}

void *b_malloc(size_t size) {
    node *temp, **ptr;
    size_t larger = size+sizeof(node);
    size += sizeof(size_t);

    for ( ptr = &free_list;
        NULL != ptr;
        ptr = &((*ptr)->next))
    {
        if ((*ptr)->size >= size) {
            if ( (*ptr)->size <= larger) {
                temp = (*ptr);
                (*ptr) = (*ptr)->next;
                return (void *)((size_t *)temp + 1);
            }
            else
                return split_end(*ptr, size);      
        }
    }
    return NULL;

Quote:
}

void *b_realloc(void *block, size_t new_size) {
    node *b = (node *)((char *)block - sizeof(size_t));
    char *temp;
    size_t i, size;

    if ( new_size == 0) {
        b_free(block);
        return NULL;
    }

    new_size += sizeof(size_t);

    size = b->size;
    if ( new_size <size)
        size = new_size;

    size -= sizeof(size_t);

    if ( b->size >= new_size+sizeof(node *) )
        return split_begin(b, new_size);

    if ( b->size >= new_size)
        return b;

    temp = b_malloc(new_size);
    if ( NULL == temp)
        return NULL;

    for ( i=0; i<size;i++)
        temp[i] = ((char *)block)[i];
    b_free(block);
    return temp;

Quote:
}

#ifdef TEST
#define num 10

int main(void) {

    int i;
    char block[4096];
    char *temp[num];
    char *big;

    b_init(block, sizeof(block));

    big = b_malloc(100);

    for (i=0; i<num; i++)
        temp[i] = b_malloc(10);

    for (i=0; i<num; i++)
        b_free(temp[i]);

    b_realloc(big, 200);
    b_realloc(big, 10);
    b_realloc(big, 0);

    return 0;

Quote:
}

#endif

--
    Later,
    Jerry.

The universe is a figment of its own imagination.
--



Tue, 12 Apr 2005 09:18:35 GMT  
 a very simple malloc question

Quote:

> Hello all,

> I'm a fairly green programmer.  Recently, i was asked in an interview
> to implement malloc. Not a rocket science version, but just something.
>  So, i fumbled thru a few ideas like keeping a free & used list.

> Does anyone know a link to, or can offer a basic implementation of
> malloc? I'd really like to learn from this experience.

> Thanks!

Hi, Marcus,

There's a fairly basic malloc() implementation given in Kernighan
and Ritchie's _The C Programming Language_, 2nd Ed.  You might want
to look at that as an example, since it includes some explanation
as well as code.

Cheers,
-M

--
Michael J. Fromberger             | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA
--



Tue, 12 Apr 2005 09:19:02 GMT  
 a very simple malloc question
Quote:

> Hello all,

> I'm a fairly green programmer.  Recently, i was asked in an interview
> to implement malloc. Not a rocket science version, but just something.
>  So, i fumbled thru a few ideas like keeping a free & used list.

> Does anyone know a link to, or can offer a basic implementation of
> malloc? I'd really like to learn from this experience.

Not exactly malloc, but a didatic description of an allocator can be
found in K&R section 8.7.

HTH

--
Cesar Rabak
GNU/Linux User 52247.
Get counted: http://counter.li.org/
--



Tue, 12 Apr 2005 09:19:05 GMT  
 a very simple malloc question
Quote:

> Does anyone know a link to, or can offer a basic

 > implementation of malloc?

There's a typical one in K&R 2nd Edition.

.
--



Tue, 12 Apr 2005 09:19:07 GMT  
 a very simple malloc question
Marcus wrote

Quote:
>Hello all,

> I'm a fairly green programmer.  Recently, i was asked in an interview
> to implement malloc. Not a rocket science version, but just something.
>  So, i fumbled thru a few ideas like keeping a free & used list.

> Does anyone know a link to, or can offer a basic implementation of
> malloc? I'd really like to learn from this experience.

> Thanks!

You could try ftp://g.oswego.edu/pub/misc/malloc.c

It looks to me to be a "rocket science version", but you could
probably pick out what you need and learn from it.  It is very well
documented.
Hope this helps!
--



Tue, 12 Apr 2005 09:19:10 GMT  
 a very simple malloc question

Quote:

> I'm a fairly green programmer.  Recently, i was asked in an interview
> to implement malloc. Not a rocket science version, but just something.
>  So, i fumbled thru a few ideas like keeping a free & used list.

> Does anyone know a link to, or can offer a basic implementation of
> malloc? I'd really like to learn from this experience.

You can find a complete implementation on my site, download
directory, as nmalloc.zip.  It is intended for the DJGPP system.

--

   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!
--



Tue, 12 Apr 2005 09:19:13 GMT  
 a very simple malloc question


Quote:
>Hello all,

>I'm a fairly green programmer.  Recently, i was asked in an interview
>to implement malloc. Not a rocket science version, but just something.
> So, i fumbled thru a few ideas like keeping a free & used list.

>Does anyone know a link to, or can offer a basic implementation of
>malloc? I'd really like to learn from this experience.

Section 8.7 in K&R 1 (original) & 2 (ANSI) cover a simple malloc
implementation. There are ftp/web sites where you can download
the K&R example code.
"The Standard C Library" by P.J.Plauger covers storage allocation
in chapter 13 on stdlib.h, p.371ff. Early versions of these may
still be downloadable from the C/C++ User's Journal web site if
you can find out which month they appeared.

Thanks. Take care, Brian Inglis         Calgary, Alberta, Canada
--

    fake address                use address above to reply




--



Tue, 12 Apr 2005 09:19:15 GMT  
 a very simple malloc question

Quote:

>I'm a fairly green programmer.  Recently, i was asked in an interview
>to implement malloc. Not a rocket science version, but just something.
> So, i fumbled thru a few ideas like keeping a free & used list.

>Does anyone know a link to, or can offer a basic implementation of
>malloc? I'd really like to learn from this experience.

Get a copy of Kernighan & Ritchie's "The C Programming Language",
second edition (K&R2).  There is a discussion of malloc implementation
in one of the later chapters (unless my brain is totally fooling me).

You should have a copy of this book even if you don't want to read
its malloc implementation.

Most of the malloc implementations out there are more robust than
anything I could implement in an interview situation; do a Google
search for "Doug Lea malloc" for one example.  Another industrial-strength
malloc is the one in BSD libc; start at
http://www.openbsd.org/cgi-bin/cvsweb/
malloc.c is
http://www.openbsd.org/cgi-bin/cvsweb/src/lib/libc/stdlib/malloc.c?re...

-andy

-andy
--



Tue, 12 Apr 2005 09:19:35 GMT  
 a very simple malloc question

Quote:

> Hello all,

> I'm a fairly green programmer.  Recently, i was asked in an
> interview to implement malloc. Not a rocket science version,
> but just something. So, i fumbled thru a few ideas like keeping
> a free & used list.

> Does anyone know a link to, or can offer a basic implementation
> of malloc? I'd really like to learn from this experience.

The seminal K&R, "The C Programming Language," contains a simple
implementation for you to study.

--

--



Tue, 12 Apr 2005 09:19:19 GMT  
 a very simple malloc question

Quote:
>I'm a fairly green programmer.  Recently, i was asked in an interview
>to implement malloc. Not a rocket science version, but just something.
> So, i fumbled thru a few ideas like keeping a free & used list.

>Does anyone know a link to, or can offer a basic implementation of
>malloc? I'd really like to learn from this experience.

1. Chapter 8 of K&R2

2. "The Standard C Library", by P. J. Plauger

Dan
--
Dan Pop
DESY Zeuthen, RZ group

--



Tue, 12 Apr 2005 09:19:17 GMT  
 a very simple malloc question

Quote:

> Hello all,

> I'm a fairly green programmer.  Recently, i was asked in an interview
> to implement malloc. Not a rocket science version, but just something.
>  So, i fumbled thru a few ideas like keeping a free & used list.

> Does anyone know a link to, or can offer a basic implementation of
> malloc? I'd really like to learn from this experience.

I tried googling on malloc-source-code, and got this implementation in
about 800 lines of C.  It has all kinds of diagnostic bells and whistles
for debugging, but the underlying implementation of malloc seems to be
fairly straightforward and uncluttered.

http://www.panix.com/~mbh/projects.html

You probably don't want to go to the Gnu site and download their
implementation, but that would be the place to go to find out how it's
done in the real world.  I once pulled out their implementation of
atan2(), and it was quite an eye-opener.
--
Antsy
--



Tue, 12 Apr 2005 09:19:26 GMT  
 
 [ 13 post ] 

 Relevant Pages 

1. simple question about malloc & free

2. simple malloc question in functions

3. SIMPLE malloc & pointer question

4. C question : redefining a new malloc() calling standard malloc()

5. to malloc() or not to malloc(), that is the question

6. a simple simple question...

7. simple simple print question

8. Newbie: CATALOG sample question (SIMPLE question)

9. Malloc 2: The revenge of Malloc

10. malloc vs 'no-malloc' - help

11. malloc crashes - malloc(256) returns 0(!) (_MT)

12. malloc, realloc, free questions

 

 
Powered by phpBB® Forum Software