2 * UCW Library -- Allocation of Large Aligned Buffers
4 * (c) 2006--2007 Martin Mares <mj@ucw.cz>
5 * (c) 2007 Pavel Charvat <char@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
17 page_alloc(unsigned int len)
19 ASSERT(!(len & (CPU_PAGE_SIZE-1)));
20 byte *p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
21 if (p == (byte*) MAP_FAILED)
22 die("Cannot mmap %d bytes of memory: %m", len);
27 page_free(void *start, unsigned int len)
29 ASSERT(!(len & (CPU_PAGE_SIZE-1)));
30 ASSERT(!((uintptr_t) start & (CPU_PAGE_SIZE-1)));
35 page_realloc(void *start, unsigned int old_len, unsigned int new_len)
37 void *p = page_alloc(new_len);
38 memcpy(p, start, MIN(old_len, new_len));
39 page_free(start, old_len);
46 return ALIGN_TO(len, (u64)CPU_PAGE_SIZE);
53 #ifndef CPU_64BIT_POINTERS
55 die("big_alloc: Size %ju is too large for a 32-bit machine", (uintmax_t) len);
58 len += 2*CPU_PAGE_SIZE;
60 byte *p = page_alloc(len);
62 mprotect(p, CPU_PAGE_SIZE, PROT_NONE);
63 mprotect(p+len-CPU_PAGE_SIZE, CPU_PAGE_SIZE, PROT_NONE);
70 big_free(void *start, u64 len)
76 len += 2*CPU_PAGE_SIZE;
85 byte *p = big_alloc(123456);