Easy 64-bit malloc implementation
Posted: Tue Nov 10, 2015 3:27 am
Hi all,
I'm currently working on getting more functionality to my user space. So far I've managed to code the kernel without any need for dynamic allocation (except for the way easier fixed-size dynamic allocation, of course), but right now I'd like to add a working malloc to user-space. I do have one I implemented in about half an hour, but it is incapable of releasing memory and it's poor - but it was only to run some tests.
However, I can't be bothered to implement a good/mediocre memory manager myself. I want to focus on other aspects of the programming for now and revisit it for improvement later.
Hence: I want a simple to implement, ready made malloc implementation.
Of course, I started out implementing liballoc (having to ignore loads of compiler warnings). It resulted in some page faults as a result of casting a pointer to unsigned integer back to pointer... But my OS is 64-bits. So, the address was truncated, bad memory was written to, and the whole thing came down to a halt.
I tried fixing it by changing the casts to uintptr_t, as they should have been in the first place. It seemed to allocate memory properly, but I did 20 malloc(123) calls, and this ended up in 40 calls to liballoc_alloc(16), which can't be right.
So, liballoc seems no good for 64-bits (perhaps this is worth mentioning on the Wiki - it doesn't seem to be mentioned anywhere else).
DLMalloc seems far from easy to implement, as do the others mentioned on the Wiki (http://wiki.osdev.org/Memory_Allocation). Perhaps I'm wrong here, but it does seem to require a lot of interfacing, rather than the 4 functions of liballoc.
So, what is the easiest malloc implementation to implement that is at least mediocre in quality? As stated, it's only temporarily, so it doesn't need to be great.
Also, if I have a choice between sbrk, morecore or mmap, what would you suggest? sbrk seems to be... outdated, especially when using paging. Mmap seems to be hard to fully implement, and my goal is not any form of POSIX/Linux compliance, so mmap feels like an overkill, at least for now. That leaves morecore, but I can't find any resources on what it's even supposed to do.
Hence also if you suggest morecore: could you point me to its specifications?
Thanks in advance!
I'm currently working on getting more functionality to my user space. So far I've managed to code the kernel without any need for dynamic allocation (except for the way easier fixed-size dynamic allocation, of course), but right now I'd like to add a working malloc to user-space. I do have one I implemented in about half an hour, but it is incapable of releasing memory and it's poor - but it was only to run some tests.
However, I can't be bothered to implement a good/mediocre memory manager myself. I want to focus on other aspects of the programming for now and revisit it for improvement later.
Hence: I want a simple to implement, ready made malloc implementation.
Of course, I started out implementing liballoc (having to ignore loads of compiler warnings). It resulted in some page faults as a result of casting a pointer to unsigned integer back to pointer... But my OS is 64-bits. So, the address was truncated, bad memory was written to, and the whole thing came down to a halt.
I tried fixing it by changing the casts to uintptr_t, as they should have been in the first place. It seemed to allocate memory properly, but I did 20 malloc(123) calls, and this ended up in 40 calls to liballoc_alloc(16), which can't be right.
So, liballoc seems no good for 64-bits (perhaps this is worth mentioning on the Wiki - it doesn't seem to be mentioned anywhere else).
DLMalloc seems far from easy to implement, as do the others mentioned on the Wiki (http://wiki.osdev.org/Memory_Allocation). Perhaps I'm wrong here, but it does seem to require a lot of interfacing, rather than the 4 functions of liballoc.
So, what is the easiest malloc implementation to implement that is at least mediocre in quality? As stated, it's only temporarily, so it doesn't need to be great.
Also, if I have a choice between sbrk, morecore or mmap, what would you suggest? sbrk seems to be... outdated, especially when using paging. Mmap seems to be hard to fully implement, and my goal is not any form of POSIX/Linux compliance, so mmap feels like an overkill, at least for now. That leaves morecore, but I can't find any resources on what it's even supposed to do.
Hence also if you suggest morecore: could you point me to its specifications?
Thanks in advance!