diff -r 4f6a2bbdff3f xen/common/xmalloc_tlsf.c --- a/xen/common/xmalloc_tlsf.c Tue Jan 13 15:53:47 2009 +0000 +++ b/xen/common/xmalloc_tlsf.c Wed Jan 14 17:24:26 2009 -0700 @@ -496,6 +496,119 @@ void xmem_pool_free(void *ptr, struct xm spin_unlock(&pool->lock); } +#ifdef __x86_64__ +/* + * Glue for 64-bit-only admalloc(). + */ + +static struct admem_pool *adpool; + +static void *admalloc_pool_get(unsigned long size) +{ + struct page_info *pi; + + ASSERT(size == PAGE_SIZE); + + if ((pi = alloc_domheap_pages(0,0,0)) == NULL) + return NULL; + return mfn_to_virt(page_to_mfn(pi)); +} + +static void admalloc_pool_put(void *p) +{ + free_domheap_pages(mfn_to_page(virt_to_mfn(p)),0); +} + +static void *admalloc_whole_pages(unsigned long size) +{ + struct bhdr *b; + struct page_info *pi; + unsigned int pageorder = get_order_from_bytes(size + BHDR_OVERHEAD); + + pi = alloc_domheap_pages(0,0,0); + if (pi == NULL) + return NULL; + b = mfn_to_virt(page_to_mfn(pi)); + + b->size = (1 << (pageorder + PAGE_SHIFT)); + return (void *)b->ptr.buffer; +} + +static void tlsf_init(void) +{ + INIT_LIST_HEAD(&pool_list_head); + spin_lock_init(&pool_list_lock); + adpool = admem_pool_create( + "admalloc", admalloc_pool_get, admalloc_pool_put, + PAGE_SIZE, 0, PAGE_SIZE); + BUG_ON(!adpool); +} + +/* + * admalloc() + */ + +void *_admalloc(unsigned long size, unsigned long align) +{ + void *p; + u32 pad; + + ASSERT(!in_irq()); + + ASSERT((align & (align - 1)) == 0); + if ( align < MEM_ALIGN ) + align = MEM_ALIGN; + size += align - MEM_ALIGN; + + if ( !adpool ) + tlsf_init(); + + if ( size >= (PAGE_SIZE - (2*BHDR_OVERHEAD)) ) + p = admalloc_whole_pages(size); + else + p = admem_pool_alloc(size, adpool); + + /* Add alignment padding. */ + if ( (pad = -(long)p & (align - 1)) != 0 ) + { + char *q = (char *)p + pad; + struct bhdr *b = (struct bhdr *)(q - BHDR_OVERHEAD); + ASSERT(q > (char *)p); + b->size = pad | 1; + p = q; + } + + ASSERT(((unsigned long)p & (align - 1)) == 0); + return p; +} + +void adfree(void *p) +{ + struct bhdr *b; + + ASSERT(!in_irq()); + + if ( p == NULL ) + return; + + /* Strip alignment padding. */ + b = (struct bhdr *)((char *) p - BHDR_OVERHEAD); + if ( b->size & 1 ) + { + p = (char *)p - (b->size & ~1u); + b = (struct bhdr *)((char *)p - BHDR_OVERHEAD); + ASSERT(!(b->size & 1)); + } + + if ( b->size >= (PAGE_SIZE - (2*BHDR_OVERHEAD)) ) + free_domheap_pages(mfn_to_page(virt_to_mfn(p)), + get_order_from_bytes(b->size)); + else + admem_pool_free(p, adpool); +} + +#else + /* * Glue for xmalloc(). */ @@ -597,3 +710,5 @@ void xfree(void *p) else xmem_pool_free(p, xenpool); } + +#endif diff -r 4f6a2bbdff3f xen/include/xen/xmalloc.h --- a/xen/include/xen/xmalloc.h Tue Jan 13 15:53:47 2009 +0000 +++ b/xen/include/xen/xmalloc.h Wed Jan 14 17:24:26 2009 -0700 @@ -5,6 +5,11 @@ /* * Xen malloc/free-style interface. */ + +#ifdef __x86_64__ +#define _xmalloc _admalloc +#define xfree adfree +#endif /* Allocate space for typed object. */ #define xmalloc(_type) ((_type *)_xmalloc(sizeof(_type), __alignof__(_type)))