8 ms·
The simplest allocator ever is this: void *malloc(int size) { static long nextAddress = BASE_ADDRESS; void *ptr = (void *)nextAddress;
by a-priori 13y ago
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.
- deleted 13y ago[deleted]
- ricree 13y agoThere's a reason he said toy.
- __david__ 13y agoIsn't that effectively sbrk()?
- IgorPartola 13y agosbrk() 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.
- alecdbrooks 13y agoThe 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.
- mnbvcxza 13y agoOr we could just assume that 'he' means 'he or she'.
- alecdbrooks 13y agoIt 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.
- sambeau 13y agoYou're better-off using "they".
- derekp7 13y agoI'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?
- ojii 13y ago"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.
- a-priori 13y agoYes, 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.
- __david__ 13y ago> 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.
- IgorPartola 13y agoYou probably want to align the memory you return as well instead of having size be an arbitrary value.
- a-priori 13y agoYou'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.
- gizmo686 13y agoWhat advantage does aligning it have, given that you are already satisfied with using an allocater that cannot free memory.
- ArbitraryLimits 13y agoNone 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.
- cpeterso 13y agodjb'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); }
- a-priori 13y agoCool, 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.