Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The simplest allocator ever is this:

    void *malloc(int size) {
        static long nextAddress = BASE_ADDRESS;
        void *ptr = (void *)nextAddress;
        nextAddress += size;
        return ptr;
    }

    void free(void *ptr) {
        return;
    }
    
Using that will last you a long time in writing a toy operating system kernel. Of course, it will eventually run out of memory pages, but that shouldn't happen until your system has been running for a long time.


djb's qmail does almost exactly this! It works surprisingly well for short-lived forked processes.

  typedef union { char irrelevant[ALIGNMENT]; double d; } aligned;
  static aligned realspace[SPACE / ALIGNMENT];
  #define space ((char *) realspace)
  static unsigned int avail = SPACE; /* multiple of ALIGNMENT; 0<=avail<=SPACE */

  char *alloc(n)
  unsigned int n;
  {
    n = ALIGNMENT + n - (n & (ALIGNMENT - 1)); /* XXX: could overflow */
    if (n <= avail) {
      avail -= n;
      return space + avail;
    }
    return malloc(n);
  }

  void alloc_free(x)
  char *x;
  {
    if (x >= space && x < space + SPACE) {
      return; /* XXX: assuming that pointers are flat */
    }
    free(x);
  }


Cool, didn't know that! It's basically a degenerate pooled memory allocator. That one is a bit more clever in that it falls through to a proper malloc when it runs out of space. That's assuming, of course, that you have a proper malloc to fall through too.

Mine will eventually wrap around and clobber whatever's at 0x0. It assumes you have a very liberal page allocator in the background to handle page faults for whatever addresses you decide to point to and automatically wire them up to RAM.


You probably want to align the memory you return as well instead of having size be an arbitrary value.


You're right, it should word-align the addresses, but I didn't want to complicate things. This was just supposed to be a 'minimum viable allocator' to shove into a kernel temporarily then move onto other more interesting things.


What advantage does aligning it have, given that you are already satisfied with using an allocater that cannot free memory.


None on Intel chips, but on virtual any other chip you'd get SIGBUS by referencing e.g. an N-bit datum not alinged on N/8-byte boundaries.


[deleted]


There's a reason he said toy.


Isn't that effectively sbrk()?


sbrk() does allocate memory of the size you want. Instead it allocates memory in chunks of the size of the system page. Thus, if you want to allocate a few bytes, you will get something like 4 KB instead, which is wasteful. Also, sbrk() is a syscall, so it's not available in OP's use case: he has not implemented it.


The author of the post is actually a "she," not a "he." I hate to be pedantic, but I think it's important to not assume authors of technical posts are men.


First, that is my bad. I read the contents of the post, not the title of the blog. Also, I do use "he" as "he or she", because of the influence of both the American culture and the Russian language in which the idea that the male gender is the default is very strong.

Second, no I should not assume. Women and tech have a complex enough relationship as is, and I fully support making our industry more inclusive

Third, you should not assume either. A name is not enough to determine someone's sex or gender. It is then safest to use "they" or even ze or zhe [1], though obviously very few people do. I also hate to be pedantic :).

[1] http://en.m.wikipedia.org/wiki/Gender-specific_and_gender-ne...


I actually did not assume based on her name. (There's nothing worse than a correction that is itself wrong.) I tried to find a biography of her in the third person, but could not find one. From her About page, I found out she was a member of Pyladies, which describers itself as an all-woman group. At that point, I felt I did my due diligence.

It really is an important point, not pedantry, to not assume based on names because many cultures have names that sound like the other gender to American ears (and vice versa, no doubt).


Very nice work. I will have to bookmark pyladies as a resource for friend, etc. Thanks for pointing all this out/bringing it up.


Or we could just assume that 'he' means 'he or she'.


It sometimes does, but that's not really relevant when you're talking about a specific person, whose name is on the top of the page in question.


You're better-off using "they".


I've been seeing a lot of "singular they" lately, even though it goes against what I was taught many years ago in school. One question though: would you yous "they is", or "they are" if you are referring to a singular person?


Languages develop. I was told never to use "due to" instead of "because". Told never to use "quote" instead of "quotation". And, I was told that "horrific" was not a word.

All of those things are no longer true, based on my casual reading of the New York Times.

(Note - I was also told to use "He" as the generic case for singular person. Outside of english 11/12, I've never used anything other than "they" - and have never had anyone comment on it. "They" as the singular person form is fine, and avoids the gender confusion.)


Indeed, singular "they" has been used in English for over four hundred years.


In german we have something like this too.

If you know someone rather well, you say

"Wie geht es dir" - "How are you?"

but if you don't know him, you say

"Wie geht es ihnen?", which literally means "How are they?"

Also, back in the old days, you would talk to the nobility in plural.

"Wie geht es euch?", there is no equivalent in English I guess, since you is plural and singular, "How are you?"


"they are". not a native speaker but we were thaught "they" as the proper way to refer to a single person of unknown gender when I studied for my CPE certificate.


Yes, it is. It's called the "singular 'they'", but some native English speakers were taught that it's incorrect. The controversy over it seems to be an American thing, that at some point in the 19th century someone wrote in a style guide that it's improper grammar, and the meme stuck.

But it's perfectly correct: English writers have been using both the singular 'they' continuously for hundreds of years (14th century?). It also has equivalents in other west European languages (e.g. the German neuter gender and the French ils for a group of people of indeterminate gender), so it's not an English invention either.


"are"

And it's totally fine "They are" has been used in the way for hundreds of years.


Ah, I just found it on Wikipedia, in the article on singluar they...

"Though semantically singular or ambiguous, singular they remains morphologically and syntactically plural (e.g. it still takes plural forms of verbs)."

Thanks.


> Instead it allocates memory in chunks of the size of the system page.

It can do that, it's not required to.

> Also, sbrk() is a syscall, so it's not available in OP's use case: he has not implemented it.

Yes, they have, they just called it malloc() :-). Which was really my (admittedly fairly terse) point.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: