pkgin_all: script to auto-install all packages
[minix.git] / lib / libsys / alloc_util.c
blob280598e76cd2c0690053eedae521b033f75c3307
2 #include "syslib.h"
4 #include <stdlib.h>
5 #include <errno.h>
6 #include <sys/mman.h>
7 #include <minix/sysutil.h>
9 void *alloc_contig(size_t len, int flags, phys_bytes *phys)
11 vir_bytes buf;
12 int mmapflags = MAP_PREALLOC|MAP_CONTIG|MAP_ANON;
14 if(flags & AC_LOWER16M)
15 mmapflags |= MAP_LOWER16M;
16 if(flags & AC_LOWER1M)
17 mmapflags |= MAP_LOWER1M;
18 if(flags & AC_ALIGN64K)
19 mmapflags |= MAP_ALIGN64K;
21 /* First try to get memory with minix_mmap. This is guaranteed
22 * to be page-aligned, and we can tell VM it has to be
23 * pre-allocated and contiguous.
25 errno = 0;
26 buf = (vir_bytes) minix_mmap(0, len, PROT_READ|PROT_WRITE, mmapflags, -1, 0);
28 /* If that failed, maybe we're not running in paged mode.
29 * If that's the case, ENXIO will be returned.
30 * Memory returned with malloc() will be preallocated and
31 * contiguous, so fallback on that, and ask for a little extra
32 * so we can page align it ourselves.
34 if(buf == (vir_bytes) MAP_FAILED) {
35 u32_t align = 0;
36 if(errno != (_SIGN ENXIO)) {
37 return NULL;
39 if(flags & AC_ALIGN4K)
40 align = 4*1024;
41 if(flags & AC_ALIGN64K)
42 align = 64*1024;
43 if(len + align < len)
44 return NULL;
45 len += align;
46 if(!(buf = (vir_bytes) malloc(len))) {
47 return NULL;
49 if(align)
50 buf += align - (buf % align);
53 /* Get physical address, if requested. */
54 if(phys != NULL && sys_umap(SELF, VM_D, buf, len, phys) != OK)
55 panic("sys_umap_data_fb failed");
57 return (void *) buf;
60 int free_contig(void *addr, size_t len)
62 return minix_munmap(addr, len);