1*433d6423SLionel Sambuc /*- 2*433d6423SLionel Sambuc * Copyright (c) 2006 Thomas Friebel <tf13@os.inf.tu-dresden.de> 3*433d6423SLionel Sambuc * Copyright (c) 2006 Christian Helmuth <ch12@os.inf.tu-dresden.de> 4*433d6423SLionel Sambuc * Copyright (c) 2010 Dirk Vogt <dvogt@few.vu.nl>. 5*433d6423SLionel Sambuc * All rights reserved. 6*433d6423SLionel Sambuc * 7*433d6423SLionel Sambuc * Redistribution and use in source and binary forms, with or without 8*433d6423SLionel Sambuc * modification, are permitted provided that the following conditions 9*433d6423SLionel Sambuc * are met: 10*433d6423SLionel Sambuc * 11*433d6423SLionel Sambuc * 1. Redistributions of source code must retain the above copyright 12*433d6423SLionel Sambuc * notice, this list of conditions and the following disclaimer. 13*433d6423SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright 14*433d6423SLionel Sambuc * notice, this list of conditions and the following disclaimer in 15*433d6423SLionel Sambuc * the documentation and/or other materials provided with the 16*433d6423SLionel Sambuc * distribution. 17*433d6423SLionel Sambuc * 18*433d6423SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19*433d6423SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20*433d6423SLionel Sambuc * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 21*433d6423SLionel Sambuc * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 22*433d6423SLionel Sambuc * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 23*433d6423SLionel Sambuc * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 24*433d6423SLionel Sambuc * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25*433d6423SLionel Sambuc * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 26*433d6423SLionel Sambuc * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 27*433d6423SLionel Sambuc * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 28*433d6423SLionel Sambuc * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29*433d6423SLionel Sambuc * SUCH DAMAGE. 30*433d6423SLionel Sambuc */ 31*433d6423SLionel Sambuc #ifndef _DDEKIT_MEMORY_H 32*433d6423SLionel Sambuc #define _DDEKIT_MEMORY_H 33*433d6423SLionel Sambuc 34*433d6423SLionel Sambuc #include <ddekit/ddekit.h> 35*433d6423SLionel Sambuc 36*433d6423SLionel Sambuc /******************* 37*433d6423SLionel Sambuc ** Slab facility ** 38*433d6423SLionel Sambuc *******************/ 39*433d6423SLionel Sambuc 40*433d6423SLionel Sambuc struct ddekit_slab; 41*433d6423SLionel Sambuc 42*433d6423SLionel Sambuc /* Store user pointer in slab cache */ 43*433d6423SLionel Sambuc void ddekit_slab_set_data(struct ddekit_slab * slab, void *data); 44*433d6423SLionel Sambuc 45*433d6423SLionel Sambuc /* Read user pointer from slab cache */ 46*433d6423SLionel Sambuc void *ddekit_slab_get_data(struct ddekit_slab * slab); 47*433d6423SLionel Sambuc 48*433d6423SLionel Sambuc /* Allocate slab in slab cache */ 49*433d6423SLionel Sambuc void *ddekit_slab_alloc(struct ddekit_slab * slab); 50*433d6423SLionel Sambuc 51*433d6423SLionel Sambuc /* Allocate slab in slab cache */ 52*433d6423SLionel Sambuc void ddekit_slab_free(struct ddekit_slab * slab, void *objp); 53*433d6423SLionel Sambuc 54*433d6423SLionel Sambuc /* 55*433d6423SLionel Sambuc * Setup page cache for all slabs 56*433d6423SLionel Sambuc * 57*433d6423SLionel Sambuc * pages: maximal number of memory pages 58*433d6423SLionel Sambuc * 59*433d6423SLionel Sambuc * If 'pages' is too low, memory pages may be given back to the memory server 60*433d6423SLionel Sambuc * (dm_phys) and just to be allocated again later. This hits performance (but 61*433d6423SLionel Sambuc * saves memory). Increase 'pages' to avoid this thrashing-like effect. 62*433d6423SLionel Sambuc * 63*433d6423SLionel Sambuc * If the maximal number of unused pages is exceeded, subsequent deallocation 64*433d6423SLionel Sambuc * will be freed at the memory server. This page cache caches pages from all 65*433d6423SLionel Sambuc * slabs. 66*433d6423SLionel Sambuc */ 67*433d6423SLionel Sambuc void ddekit_slab_setup_page_cache(unsigned pages); 68*433d6423SLionel Sambuc 69*433d6423SLionel Sambuc /* 70*433d6423SLionel Sambuc * Destroy slab cache 71*433d6423SLionel Sambuc * 72*433d6423SLionel Sambuc * slab: pointer to slab cache structure 73*433d6423SLionel Sambuc */ 74*433d6423SLionel Sambuc void ddekit_slab_destroy(struct ddekit_slab * slab); 75*433d6423SLionel Sambuc 76*433d6423SLionel Sambuc /** 77*433d6423SLionel Sambuc * Initialize slab cache 78*433d6423SLionel Sambuc * 79*433d6423SLionel Sambuc * \param size size of cache objects 80*433d6423SLionel Sambuc * \param contiguous make this slab use physically contiguous memory 81*433d6423SLionel Sambuc * 82*433d6423SLionel Sambuc * \return pointer to new slab cache or 0 on error 83*433d6423SLionel Sambuc */ 84*433d6423SLionel Sambuc struct ddekit_slab * ddekit_slab_init(unsigned size, int contiguous); 85*433d6423SLionel Sambuc 86*433d6423SLionel Sambuc 87*433d6423SLionel Sambuc /********************** 88*433d6423SLionel Sambuc ** Memory allocator ** 89*433d6423SLionel Sambuc **********************/ 90*433d6423SLionel Sambuc 91*433d6423SLionel Sambuc /* 92*433d6423SLionel Sambuc * Allocate large memory block 93*433d6423SLionel Sambuc * 94*433d6423SLionel Sambuc * \param size block size 95*433d6423SLionel Sambuc * \return pointer to new memory block 96*433d6423SLionel Sambuc * 97*433d6423SLionel Sambuc * Allocations via this allocator may be slow (because memory servers are 98*433d6423SLionel Sambuc * involved) and should be used only for large (i.e., > page size) blocks. If 99*433d6423SLionel Sambuc * allocations/deallocations are relatively dynamic this may not be what you 100*433d6423SLionel Sambuc * want. 101*433d6423SLionel Sambuc * 102*433d6423SLionel Sambuc * Allocated blocks have valid virt->phys mappings and are physically 103*433d6423SLionel Sambuc * contiguous. 104*433d6423SLionel Sambuc */ 105*433d6423SLionel Sambuc void *ddekit_large_malloc(int size); 106*433d6423SLionel Sambuc 107*433d6423SLionel Sambuc /** 108*433d6423SLionel Sambuc * Free large memory block 109*433d6423SLionel Sambuc * 110*433d6423SLionel Sambuc * \param p pointer to memory block 111*433d6423SLionel Sambuc */ 112*433d6423SLionel Sambuc void ddekit_large_free(void *p); 113*433d6423SLionel Sambuc 114*433d6423SLionel Sambuc /** FIXME 115*433d6423SLionel Sambuc * contig_malloc() is the lowest-level allocator interface one could implement. 116*433d6423SLionel Sambuc * we should consider to provide vmalloc() too. */ 117*433d6423SLionel Sambuc void *ddekit_contig_malloc(unsigned long size, unsigned long low, 118*433d6423SLionel Sambuc unsigned long high, unsigned long alignment, unsigned long boundary); 119*433d6423SLionel Sambuc 120*433d6423SLionel Sambuc 121*433d6423SLionel Sambuc /***************************** 122*433d6423SLionel Sambuc ** Simple memory allocator ** 123*433d6423SLionel Sambuc *****************************/ 124*433d6423SLionel Sambuc 125*433d6423SLionel Sambuc /** 126*433d6423SLionel Sambuc * Allocate memory block via simple allocator 127*433d6423SLionel Sambuc * 128*433d6423SLionel Sambuc * \param size block size 129*433d6423SLionel Sambuc * \return pointer to new memory block 130*433d6423SLionel Sambuc * 131*433d6423SLionel Sambuc * The blocks allocated via this allocator CANNOT be used for DMA or other 132*433d6423SLionel Sambuc * device operations, i.e., there exists no virt->phys mapping. 133*433d6423SLionel Sambuc */ 134*433d6423SLionel Sambuc void *ddekit_simple_malloc(unsigned size); 135*433d6423SLionel Sambuc 136*433d6423SLionel Sambuc /** 137*433d6423SLionel Sambuc * Free memory block via simple allocator 138*433d6423SLionel Sambuc * 139*433d6423SLionel Sambuc * \param p pointer to memory block 140*433d6423SLionel Sambuc */ 141*433d6423SLionel Sambuc void ddekit_simple_free(void *p); 142*433d6423SLionel Sambuc 143*433d6423SLionel Sambuc #endif 144