new free_contig() and changes to make drivers use it; so now we
[minix.git] / lib / syslib / alloc_util.c
blob217d1a7939d111a0eb963e486ac1a12a2aa89ae6
2 #include "syslib.h"
4 #include <stdlib.h>
5 #include <errno.h>
6 #include <sys/mman.h>
7 #include <minix/sysutil.h>
9 int sys_umap_data_fb(endpoint_t ep, vir_bytes buf, vir_bytes len, phys_bytes *phys)
11 int r;
13 if((r=sys_umap(ep, VM_D, buf, len, phys)) != OK) {
14 if(r != EINVAL)
15 return r;
16 r = sys_umap(ep, D, buf, len, phys);
20 return r;
24 void *alloc_contig(size_t len, int flags, phys_bytes *phys)
26 int r;
27 vir_bytes buf;
28 int mmapflags = MAP_PREALLOC|MAP_CONTIG|MAP_ANON;
30 if(flags & AC_LOWER16M)
31 mmapflags |= MAP_LOWER16M;
32 if(flags & AC_LOWER1M)
33 mmapflags |= MAP_LOWER1M;
34 if(flags & AC_ALIGN64K)
35 mmapflags |= MAP_ALIGN64K;
37 /* First try to get memory with mmap. This is gauranteed
38 * to be page-aligned, and we can tell VM it has to be
39 * pre-allocated and contiguous.
41 errno = 0;
42 buf = (vir_bytes) mmap(0, len, PROT_READ|PROT_WRITE, mmapflags, -1, 0);
44 /* If that failed, maybe we're not running in paged mode.
45 * If that's the case, ENXIO will be returned.
46 * Memory returned with malloc() will be preallocated and
47 * contiguous, so fallback on that, and ask for a little extra
48 * so we can page align it ourselves.
50 if(buf == (vir_bytes) MAP_FAILED) {
51 u32_t align = 0;
52 if(errno != (_SIGN ENXIO)) {
53 return NULL;
55 if(flags & AC_ALIGN4K)
56 align = 4*1024;
57 if(flags & AC_ALIGN64K)
58 align = 64*1024;
59 if(len + align < len)
60 return NULL;
61 len += align;
62 if(!(buf = (vir_bytes) malloc(len))) {
63 return NULL;
65 if(align)
66 buf += align - (buf % align);
69 /* Get physical address, if requested. */
70 if(phys != NULL && sys_umap_data_fb(SELF, buf, len, phys) != OK)
71 panic("alloc_contig.c", "sys_umap_data_fb failed", NO_NUM);
73 return (void *) buf;
76 int free_contig(void *addr, size_t len)
78 return munmap(addr, len);