News for hackers. Stuff that matters.
Re:On programming without malloc by a-priori on Wednesday December 04, 2013 @02:44PM (#6849578)
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.
Re:On programming without malloc by cpeterso on Thursday December 05, 2013 @12:14AM (#6852820)
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);
  }
Re:On programming without malloc by a-priori on Thursday December 05, 2013 @10:02AM (#6854674)
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.

Re:On programming without malloc by IgorPartola on Wednesday December 04, 2013 @05:44PM (#6851005)
You probably want to align the memory you return as well instead of having size be an arbitrary value.
Re:On programming without malloc by a-priori on Wednesday December 04, 2013 @07:01PM (#6851560)
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.
Re:On programming without malloc by gizmo686 on Wednesday December 04, 2013 @06:43PM (#6851433)
What advantage does aligning it have, given that you are already satisfied with using an allocater that cannot free memory.
Re:On programming without malloc by ArbitraryLimits on Wednesday December 04, 2013 @07:19PM (#6851654)
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]
Re:On programming without malloc by ricree on Wednesday December 04, 2013 @05:02PM (#6850640)
There's a reason he said toy.
Re:On programming without malloc by __david__ on Wednesday December 04, 2013 @05:31PM (#6850890)
Isn't that effectively sbrk()?
Re:On programming without malloc by IgorPartola on Wednesday December 04, 2013 @05:43PM (#6850993)
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.
Re:On programming without malloc by alecdbrooks on Wednesday December 04, 2013 @08:31PM (#6852024)
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.
Re:On programming without malloc by IgorPartola on Wednesday December 04, 2013 @11:07PM (#6852633)
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...

Re:On programming without malloc by alecdbrooks on Wednesday December 04, 2013 @11:36PM (#6852732)
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).

Re:On programming without malloc by IgorPartola on Thursday December 05, 2013 @12:17AM (#6852826)
Very nice work. I will have to bookmark pyladies as a resource for friend, etc. Thanks for pointing all this out/bringing it up.
Re:On programming without malloc by mnbvcxza on Wednesday December 04, 2013 @09:27PM (#6852282)
Or we could just assume that 'he' means 'he or she'.
Re:On programming without malloc by alecdbrooks on Wednesday December 04, 2013 @09:37PM (#6852323)
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.
Re:On programming without malloc by sambeau on Wednesday December 04, 2013 @10:17PM (#6852479)
You're better-off using "they".
Re:On programming without malloc by derekp7 on Wednesday December 04, 2013 @10:39PM (#6852535)
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?
Re:On programming without malloc by ghshephard on Wednesday December 04, 2013 @11:59PM (#6852784)
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.)

Re:On programming without malloc by Semiapies on Thursday December 05, 2013 @03:08AM (#6853194)
Indeed, singular "they" has been used in English for over four hundred years.
Re:On programming without malloc by k__ on Thursday December 05, 2013 @05:05AM (#6853549)
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?"

Re:On programming without malloc by ojii on Wednesday December 04, 2013 @11:16PM (#6852665)
"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.
Re:On programming without malloc by a-priori on Thursday December 05, 2013 @02:17PM (#6856486)
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.

Re:On programming without malloc by sambeau on Thursday December 05, 2013 @06:48AM (#6853923)
"are"

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

Re:On programming without malloc by derekp7 on Thursday December 05, 2013 @11:40AM (#6855289)
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.

Re:On programming without malloc by __david__ on Thursday December 05, 2013 @05:55AM (#6853737)
> 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.