malloc() free()
malloc() free()
Does anyone have or know of a portable implementation of malloc?
Ive tried using dlmalloc, but with no sucess. I could give someone the OS source if they could implement it for me. Any docs if someone knows of them would be a last resort, if I had to write one myself.
Ive tried using dlmalloc, but with no sucess. I could give someone the OS source if they could implement it for me. Any docs if someone knows of them would be a last resort, if I had to write one myself.
- Combuster
- Member
- Posts: 9301
- Joined: Wed Oct 18, 2006 3:45 am
- Libera.chat IRC: [com]buster
- Location: On the balcony, where I can actually keep 1½m distance
- Contact:
Re:malloc() free()
there's an implementation here, no idea if you can make it work with your os though:
http://sourceforge.net/projects/pdclib/
http://sourceforge.net/projects/pdclib/
Re:malloc() free()
try to see the implementation of MM here... its quite nice...
http://www.codezen.org/viridis/index.php
http://www.codezen.org/viridis/index.php
Re:malloc() free()
heres my version of what AR gave me a long time ago ;D :
Code: Select all
byte_t *heap;
byte_t *HeapCurEnd, *max_heap;
struct header *freelist;
void set_memory()
{
heap = (byte_t *)0x100000;
max_heap = heap + (extmem * 1024);
freelist = NULL;
}
void *malloc(size_t sz)
{
if(!sz)
return NULL;
--sz;
struct header *current = freelist;
struct header *previous = freelist;
while(current)
{
if(current->size > sz)
{
previous->next = current->next;
if(current == freelist)
freelist = current->next;
current->next = NULL;
return (void*) ++current;
}
previous = current;
current = current->next;
}
++sz;
if(heap + sz + sizeof(struct header) > max_heap)
return NULL;
current = (struct header*)heap;
current->sig = HEAPSIG;
current->size = sz;
current->next = NULL;
heap += sz + sizeof(struct header);
return (void*) ++current;
}
void free(void *ptr)
{
struct header *hh = (struct header*)ptr;
--hh;
if(hh->sig != HEAPSIG)
return;
if(freelist == NULL)
freelist = hh;
else
{
hh->next = freelist->next;
freelist->next = hh;
}
}
Re:malloc() free()
Thanks! couple of questions:
what is byte_t declared as?
NULL is defined as 0x00 right?
"struct header *freelist" << What is "header"
what is byte_t declared as?
NULL is defined as 0x00 right?
"struct header *freelist" << What is "header"
Re:malloc() free()
This one is designed to be portable and it works well enough to run Firefox, java, etc. You only need to implement 4 functions to port it to your OS and it's documented:
http://www.djm.co.za/liballoc/
http://www.djm.co.za/liballoc/
Re:malloc() free()
durand: thans, but i think ill stick to GLneo's code. Ive got everything but "header" done.
Re:malloc() free()
Code: Select all
typedef unsigned long size_t;
typedef unsigned char byte_t;
#define HEAPSIG 0xDEADBEEF
struct header
{
unsigned int sig;
size_t size;
struct header *next;
};
Re:malloc() free()
PDCLib (see signature) does have a preliminary implementation in v0.4. It's not really efficient, realloc() is buggy in 0.4, and free() doesn't return memory to the system, but it works, and it will be improved (along with the rest of the library) without you having to worry about anything. You get a C standard library for free to go along with it.
Every good solution is obvious once you've found it.
Re:malloc() free()
I dunno I found Durands easier to implement / more flexible in terms to the design of an OS.
Re:malloc() free()
I accept all kinds of criticism on my malloc() implementation, after all it's just a placeholder right now. You could e.g. claim that it's not thread-safe.
But you named about the only point I can't really fathom, as the current OS glue necessary for PDCLib malloc() / free() amounts to one function, which is effectively identical to what you'd have to do for liballoc_alloc() and liballoc_free()...
But I don't really complain, advertising pre-alpha code was a bit overbold anyway. But to quote an US gouvernor: I'll be back. ;D
Edit: Get me right - if there's a thing to be improved in PDCLib, for heaven's sake tell me about it, I'm writing it mainly for you folks here. I just don't see how the malloc-OS-interface could be made yet easier...
Edit 2: And I completely missed Combuster's post...
But you named about the only point I can't really fathom, as the current OS glue necessary for PDCLib malloc() / free() amounts to one function, which is effectively identical to what you'd have to do for liballoc_alloc() and liballoc_free()...
But I don't really complain, advertising pre-alpha code was a bit overbold anyway. But to quote an US gouvernor: I'll be back. ;D
Edit: Get me right - if there's a thing to be improved in PDCLib, for heaven's sake tell me about it, I'm writing it mainly for you folks here. I just don't see how the malloc-OS-interface could be made yet easier...
Edit 2: And I completely missed Combuster's post...
Every good solution is obvious once you've found it.
- AndrewAPrice
- Member
- Posts: 2309
- Joined: Mon Jun 05, 2006 11:00 pm
- Location: USA (and Australia)
I know this is 6 months after the last post in this thread, but I have a problem with GLneo's implementation and some questions.
When trying to compile the above code, along with the provided headers in gcc, I receive the following errors;
The bottom two errors refer to this line:
With my questions, the function set_memory() has a variable extmem, which I'm guessing is the amount of extended memory on the system, therefore, if I change the function to set_memory(size_t extmem) I should be able to call it from my main function during kernel initialization. My question is, how do I get the avaliable extended memory to pass into the function?
To do this I read I have to get the multiboot information from GRUB. My kernel entry code is:
and my main.c code is:
Will this send the multiboot information to my main function? The structures are defined in http://www.gnu.org/software/grub/manual ... oot.h.html. Then from my main function, would I be able to call set_memory(mbd->mem_upper) to send the amount of extended memory to the set_memory function in GLneo's malloc/free functions?
My last question is: do I have to take in consideration the size of my kernel when I call set_memory(mbd->mem_upper)? Say my kernel is 12kb, should I call set_memory(mbd->mem_upper - 0x3000)?
Thanks.
When trying to compile the above code, along with the provided headers in gcc, I receive the following errors;
Code: Select all
memory.c: In function 'malloc':
memory.c:48: warning: initialization from incompatible pointer type
memory.c:49: warning: initialization from incompatible pointer type
memory.c:55: warning: comparison of distinct pointer types lacks a cast
memory.c:56: warning: assignment from incompatible pointer type
memory.c: In function 'free':
memory.c:83: warning: assignment from incompatible pointer type
memory.c:86: error: dereferencing pointer to incomplete type
memory.c:87: error: dereferencing pointer to incomplete type
Code: Select all
hh->next = freelist->next;
freelist->next = hh;
To do this I read I have to get the multiboot information from GRUB. My kernel entry code is:
Code: Select all
[BITS 32]
global start
start:
mov esp, _sys_stack ; This points the stack to our new stack area
jmp stublet
..... ; lots of code goes here
stublet:
extern _main
call _main
.... ; more code goes here
Code: Select all
#include <multiboot.h>
... /* lots of code goes here */
int main(multiboot_info_t* mbd, unsigned int magic)
{
... /* more code goes here*/
My last question is: do I have to take in consideration the size of my kernel when I call set_memory(mbd->mem_upper)? Say my kernel is 12kb, should I call set_memory(mbd->mem_upper - 0x3000)?
Thanks.