xref: /minix/minix/lib/libsys/alloc_util.c (revision 433d6423)
1 
2 #include "syslib.h"
3 
4 #include <stdlib.h>
5 #include <errno.h>
6 #include <sys/mman.h>
7 #include <minix/sysutil.h>
8 
9 void *alloc_contig(size_t len, int flags, phys_bytes *phys)
10 {
11 	vir_bytes buf;
12 	int mmapflags = MAP_PREALLOC|MAP_CONTIG|MAP_ANON;
13 
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_ALIGNMENT_64KB;
20 
21 	/* First try to get memory with mmap. This is guaranteed
22 	 * to be page-aligned, and we can tell VM it has to be
23 	 * pre-allocated and contiguous.
24 	 */
25 	errno = 0;
26 	buf = (vir_bytes) mmap(0, len, PROT_READ|PROT_WRITE, mmapflags, -1, 0);
27 
28 	if(buf == (vir_bytes) MAP_FAILED) {
29 		return NULL;
30 	}
31 
32 	/* Get physical address, if requested. */
33         if(phys != NULL && sys_umap(SELF, VM_D, buf, len, phys) != OK)
34 		panic("sys_umap_data_fb failed");
35 
36 	return (void *) buf;
37 }
38 
39 int free_contig(void *addr, size_t len)
40 {
41 	return munmap(addr, len);
42 }
43 
44