diff options
author | nthnluu <nate1299@me.com> | 2024-01-28 21:20:27 -0500 |
---|---|---|
committer | nthnluu <nate1299@me.com> | 2024-01-28 21:20:27 -0500 |
commit | c63f340d90800895f007de64b7d2d14624263331 (patch) | |
tree | 2c0849fa597dd6da831c8707b6f2603403778d7b /user/lib/ld-weenix/ldalloc.c |
Created student weenix repository
Diffstat (limited to 'user/lib/ld-weenix/ldalloc.c')
-rw-r--r-- | user/lib/ld-weenix/ldalloc.c | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/user/lib/ld-weenix/ldalloc.c b/user/lib/ld-weenix/ldalloc.c new file mode 100644 index 0000000..c5e79e7 --- /dev/null +++ b/user/lib/ld-weenix/ldalloc.c @@ -0,0 +1,66 @@ +/* + * File: ldalloc.c + * Date: 28 March 1998 + * Acct: David Powell (dep) + * Desc: simple allocation routines + */ + +#include "ldalloc.h" +#include "ldutil.h" +#include <stdio.h> +#include <stdlib.h> +#include <sys/mman.h> +#include <unistd.h> + +static unsigned long start; +static unsigned long pos; +static unsigned long amount; + +/* This function initializes the simple memory allocator. We basically + * allocate a specified number of pages to use as scratch memory for + * the linker itself. No deallocation functionality is provided; the + * amount of memory used should be small, and is usually needed for the + * duration of the program's execution, anyway. + * + * All this function does is mmap the specified number of pages of + * /dev/zero to provide the memory for our little memory-wading-pool. */ + +void _ldainit(unsigned long pagesize, unsigned long pages) +{ + amount = pagesize * pages; + pos = 0; + + start = (unsigned long)mmap(NULL, amount, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANON, -1, 0); + if (start == (unsigned long)MAP_FAILED) + { + fprintf(stderr, "ld-weenix: panic - unable to map /dev/zero\n"); + exit(1); + } +} + +/* This function allocates a block of memory of the specified size from + * our memory pool. The memory is word-aligned, and cannot be freed. */ + +void *_ldalloc(unsigned long size) +{ + unsigned long next; + + if (size & 3) + { + size = (size & ~3) + 4; + } + + if (pos + size > amount) + { + fprintf(stderr, + "ld.so.1: panic - unable to allocate %lu bytes (_ldalloc)\n", + size); + exit(1); + } + + next = start + pos; + pos += size; + + return (void *)next; +} |