News for hackers. Stuff that matters.
Re:On programming without malloc by sliverstorm on Wednesday December 04, 2013 @01:53PM (#6849164)
Considering how much memory modern machines have, and considering she is running no other programs, I would be so tempted to write malloc like this just for grins:

    int malloc() {
      return rand() % TOTAL_MEMORY;
    }
Re:On programming without malloc by munificent on Wednesday December 04, 2013 @02:34PM (#6849494)
You jest, but you'd actually get surprisingly far just doing:

    void* malloc(size_t size) {
      static void* everything = 0;
      void* result = everything;
      everything += size;
      return result;
    }
When people are hacking together VMs for garbage collected languages, the first allocator often looks like this until they get around to actually collecting the garbage.
Re:On programming without malloc by apgwoz on Wednesday December 04, 2013 @02:37PM (#6849511)
You can get `free` pretty simply too: https://news.ycombinator.com/item?id=6849471
Re:On programming without malloc by stephen_g on Thursday December 05, 2013 @07:41AM (#6854103)
Depending on how your memory is mapped, everything should probably be an address higher than the kernel (you can get your linker script to generate the right address). Sometimes the kernel is at the top of the address space though so you could start your heap at the bottom.
Re:On programming without malloc by jvns on Wednesday December 04, 2013 @02:05PM (#6849262)
oh man that is the kind of bad idea that I love. I'm definitely going to try implementing malloc that way and report on the results =D
Re:On programming without malloc by MichaelGG on Wednesday December 04, 2013 @02:37PM (#6849515)
If that's legal, then I'm confused over the problem. Why not just start with a single address and increase by the amount requested each time? It pushes any difficulty off to the implementation of free, right?
Re:On programming without malloc by sliverstorm on Wednesday December 04, 2013 @02:55PM (#6849676)
You can do that. Of course you have to revamp it when you need to add 'free', but you can do it.

It's just much less pernicious to do it that way, than my method, which means it's less fun :)

Re:On programming without malloc by MichaelGG on Wednesday December 04, 2013 @08:06PM (#6851889)
OK thanks for the info. I thought it was hard because he was in some mode that required him to setup pagetables and service faults and all that.

So it's just hard because writing a decent malloc/free is involved, algorithmically.

Re:On programming without malloc by mercurial on Thursday December 05, 2013 @05:05AM (#6853548)
Indeed, your method is a kind of malloc_roullette, depending on how much memory you allocate to the VM you run your kernel on.
Re:On programming without malloc by TheLoneWolfling on Wednesday December 04, 2013 @02:18PM (#6849359)
Minor qualm: is rand defined?
Re:On programming without malloc by apgwoz on Wednesday December 04, 2013 @02:35PM (#6849501)
Well, rand can be defined as a Linear Congruential Generator (https://en.wikipedia.org/wiki/Linear_congruential_generator), or some other well known algorithm. LCG's don't require anything that the machine doesn't already do (mod, addition and multiplication)
Re:On programming without malloc by rca on Wednesday December 04, 2013 @02:49PM (#6849626)
You could use rdrand if you're on (a recent enough) x86. You just lose some portability. Probably not a major issue at this point.