The best way is to download Doug Lea's malloc (dlmalloc), put it in your kernel, and forget about it. dlmalloc is really good, but to write it would be really boring. You're not here to write boring code, are you?
Alternatively, look at a copy of Kernighan & Ritchie. There is a simple, neat malloc/free implementation there.
K&R's scheme is to keep a linked list of free memory blocks. To allocate a block, walk the list til you find a block big enough. If you reach the end of the list (or the list is empty), ask morecore for a block at least as big as you need and start again. Once you've found a block, split it in two. Put the second half back in the free list and return the first half to the caller.
To free a block, walk the free list til you find a block adjacent to the block you're freeing. If found, merge the two blocks into one. If not found, add the block to the end of the list.