2 * UCW Library -- Allocation of Large Aligned Buffers
4 * (c) 2006 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);
44 big_round(unsigned int len)
46 return ALIGN_TO(len, CPU_PAGE_SIZE);
50 big_alloc(unsigned int len)
54 len += 2*CPU_PAGE_SIZE;
56 byte *p = page_alloc(len);
58 mprotect(p, CPU_PAGE_SIZE, PROT_NONE);
59 mprotect(p+len-CPU_PAGE_SIZE, CPU_PAGE_SIZE, PROT_NONE);
66 big_free(void *start, unsigned int len)
72 len += 2*CPU_PAGE_SIZE;
81 byte *p = big_alloc(123456);