malloc() implementation

K

Karsten Jung

Hello together,

We have to make our own implementation of malloc() under Linux. Does
anybody can give me a hint where to start?

Our implementation should only work under Linux.

Thanks

Karsten
 
F

Flash Gordon

Karsten said:
Hello together,

We have to make our own implementation of malloc() under Linux. Does
anybody can give me a hint where to start?

Our implementation should only work under Linux.

Read the glibc code (if "pollution" from LGPL code is not a problem) and
ask on a Linux group, remembering that you will have to write your own
free and realloc code, and you have to deal with the issue of whether
other library routines are calling your malloc or the standard one.
Personally I would not bother without a *very* good reason (which might
exist).

You can't write a malloc routine in standard C which might be part of
why it is in the standard library, hence the suggestion of asking in a
Linux group.
 
J

John Devereux

Flash Gordon said:
You can't write a malloc routine in standard C which might be part of
why it is in the standard library, hence the suggestion of asking in a
Linux group.

What you *can* do though is statically allocate a memory buffer, then
allocate out of that with your own "malloc".
 
F

Flash Gordon

John said:
What you *can* do though is statically allocate a memory buffer, then
allocate out of that with your own "malloc".

True, as long as you call is something other than malloc ;-)
It all depends on what the OP actually wants to achieve.
 
F

Flash Gordon

Haider said:
Read C Programming Kerninghan & Ritchie you will get the idea.

Get the idea about what? Please provide context when responding, there
is no guarantee that people can see the post you are replying to.

In any case, last time I looked I'm sure that K&R did not describe how
to implement malloc which is what the subject line of this thread is..
 
R

Richard Heathfield

Flash Gordon said:
Get the idea about what? Please provide context when responding, there
is no guarantee that people can see the post you are replying to.

In any case, last time I looked I'm sure that K&R did not describe how
to implement malloc which is what the subject line of this thread is..

See Chapter 8, specifically section 8.7.
 
R

Richard Tobin

Read C Programming Kerninghan & Ritchie you will get the idea.
[/QUOTE]
Get the idea about what? Please provide context when responding, there
is no guarantee that people can see the post you are replying to.

It's a good answer to almost every on-topic question in comp.lang.c,
so the context doesn't matter :)
In any case, last time I looked I'm sure that K&R did not describe how
to implement malloc which is what the subject line of this thread is..

It describes the implementation of a "rudimentary" storage allocator
as an example in the section on Address Arithmetic (5.4).

-- Richard
 
F

Flash Gordon

Get the idea about what? Please provide context when responding, there
is no guarantee that people can see the post you are replying to.

It's a good answer to almost every on-topic question in comp.lang.c,
so the context doesn't matter :)[/QUOTE]

The standard and the FAQ are better answers ;-)
It describes the implementation of a "rudimentary" storage allocator
as an example in the section on Address Arithmetic (5.4).

My reply was still technically accurate, I was sure of what I stated. It
just happens that I was wrong to be sure of it ;-)
 
C

Clever Monkey

Flash said:
True, as long as you call is something other than malloc ;-)
It all depends on what the OP actually wants to achieve.

Wouldn't interpositioning allow you to define a default global symbol
"malloc" that would essentially overwrite the Standard one?

Not, mind you, something I recommend, but isn't one of the hallmarks of
C that one can do this without constraint, making all code (including
any external libs that are linked in later) use the non-standard function?
 
K

Keith Thompson

Clever Monkey said:
Wouldn't interpositioning allow you to define a default global symbol
"malloc" that would essentially overwrite the Standard one?

If the implementation happens to support it, but such support is not
required by the standard.
 
P

Peter Nilsson

Karsten said:
Hello together,

Who is 'together'?
We have to make our own implementation of malloc() under Linux. Does
anybody can give me a hint where to start?

Oh I don't know... maybe a linux group!!
Our implementation should only work under Linux.

Then why are you posting this to comp.lang.c?

Please try reading FAQs and welcome messages, then try lurking to get
the feel of a group, before you blindly post off topic requests.
 
M

Michael Mair

Peter said:
Who is 'together'?

Consider it a quirk of word-by-word translation; the OP
probably meant "Hello everybody"/"Hi all".

<snip>

Cheers
Michael
 
E

Eric Sosman

Clever said:
Wouldn't interpositioning allow you to define a default global symbol
"malloc" that would essentially overwrite the Standard one?

Not, mind you, something I recommend, but isn't one of the hallmarks of
C that one can do this without constraint, making all code (including
any external libs that are linked in later) use the non-standard function?

"Hallmark of C ..." Fuzzy concept, that. Yet the Rationale
invokes the "Spirit of C," so perhaps we can dither a bit without
risk of too much castigation.

In my own (less than universal) experience, two obstacles to
interposing your own malloc() -- or sqrt(), or whatever -- are
fairly frequently found:

- The Standard library may be incorporated into the program
as an indivisible unit: You get the whole thing, or nothing
at all. This is/was the case on VMS in the Old Days, when
the whole schmear was implemented as one giant monolithic
shared library. VMS' linker would squawk about conflicting
symbol definitions if your home-grown malloc() had the same
name as the malloc() in the library.

- The library itself may use unadvertised knowledge of its own
internals. (It pretty much must do so if things like atexit()
and fflush(NULL) are to work.) For example, imagine an fopen()
that for performance reasons wants to allocate an I/O buffer
on a memory page boundary. It might call a library-private
_malloc_pages() function to get such specially-aligned memory.
Fine so far: You haven't replaced _malloc_pages(), so the
library's own version is used. But what if fopen() then hands
the buffer memory to free()? It's (presumably) your free(),
not the library's free(), that gets called -- and what are
the chances that your free() and the library's free() use
identical data structures?

It's a shame, really, that C doesn't have a mechanism to allow
overriding parts of the library. Because abuses of dynamic memory
are so common, the malloc() suite is perhaps the most mouth-watering
override target, but there are others: A gets() that bleats whenever
called, or a scanf() that complains if given a "%s", even an acos()
that dumps a stack trace if its argument is larger than 1. The C
Standard, though, makes a point of separating the roles of implementor
and user -- and having lived on both sides of that fence, I for one
find the fence something of a comfort.
 
C

CBFalconer

Karsten said:
We have to make our own implementation of malloc() under Linux.
Does anybody can give me a hint where to start?

Our implementation should only work under Linux.

My nmalloc for DJGPP should be close. It basically depends on the
presence of sbrk and some ability to make meaning conversion
between char* and void*. The debuggery (which is not present in
the final version unless enabled) has other dependencies, including
some on the use of gcc.

<http://cbfalconer.home.att.net/download/>

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
C

CBFalconer

Haider said:
Read C Programming Kerninghan & Ritchie you will get the idea.

For what? In general on usenet you should realize that readers may
very well not have convenient access to previous articles in a
thread. That means that your reply articles should include
adequate context, so that they stand by themselves. Google is NOT
usenet, it is only a very poor interface to the real usenet
system. To include proper context when using google, see my sig.
below. Please be sure to read the referenced URLs.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
H

Haider

Flash said:
Get the idea about what? Please provide context when responding, there
is no guarantee that people can see the post you are replying to.

In any case, last time I looked I'm sure that K&R did not describe how
to implement malloc which is what the subject line of this thread is..
--

Hi,
Please go to the following link you will get a copy of K&R in pdf
format
www.oberon2005.ru/paper/kr_c.pdf
then go to the page no 149 to 152 and you will get the idea of
implementation of malloc.
otherwise if you already have a copy of second edition of K&R then it
is in section 7 of chapter 8.
also K&R will give you an Idea about everything.
 
H

Haider

Get the idea about what? Please provide context when responding, there
is no guarantee that people can see the post you are replying to.

It's a good answer to almost every on-topic question in comp.lang.c,
so the context doesn't matter :)[/QUOTE]

well said
for those whose blood group is C K&R is iron suppliment.
It describes the implementation of a "rudimentary" storage allocator
as an example in the section on Address Arithmetic (5.4).

-- Richard
also K&R will give you an Idea about everything.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,183
Messages
2,570,969
Members
47,524
Latest member
ecomwebdesign

Latest Threads

Top