linux/sound/core/memalloc.c
<<
>>
Prefs
   1/*
   2 *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
   3 *                   Takashi Iwai <tiwai@suse.de>
   4 * 
   5 *  Generic memory allocators
   6 *
   7 *
   8 *   This program is free software; you can redistribute it and/or modify
   9 *   it under the terms of the GNU General Public License as published by
  10 *   the Free Software Foundation; either version 2 of the License, or
  11 *   (at your option) any later version.
  12 *
  13 *   This program is distributed in the hope that it will be useful,
  14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
  15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16 *   GNU General Public License for more details.
  17 *
  18 *   You should have received a copy of the GNU General Public License
  19 *   along with this program; if not, write to the Free Software
  20 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  21 *
  22 */
  23
  24#include <linux/module.h>
  25#include <linux/proc_fs.h>
  26#include <linux/init.h>
  27#include <linux/pci.h>
  28#include <linux/slab.h>
  29#include <linux/mm.h>
  30#include <linux/seq_file.h>
  31#include <asm/uaccess.h>
  32#include <linux/dma-mapping.h>
  33#include <linux/moduleparam.h>
  34#include <linux/mutex.h>
  35#include <sound/memalloc.h>
  36
  37
  38MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>, Jaroslav Kysela <perex@perex.cz>");
  39MODULE_DESCRIPTION("Memory allocator for ALSA system.");
  40MODULE_LICENSE("GPL");
  41
  42
  43/*
  44 */
  45
  46static DEFINE_MUTEX(list_mutex);
  47static LIST_HEAD(mem_list_head);
  48
  49/* buffer preservation list */
  50struct snd_mem_list {
  51        struct snd_dma_buffer buffer;
  52        unsigned int id;
  53        struct list_head list;
  54};
  55
  56/* id for pre-allocated buffers */
  57#define SNDRV_DMA_DEVICE_UNUSED (unsigned int)-1
  58
  59/*
  60 *
  61 *  Generic memory allocators
  62 *
  63 */
  64
  65static long snd_allocated_pages; /* holding the number of allocated pages */
  66
  67static inline void inc_snd_pages(int order)
  68{
  69        snd_allocated_pages += 1 << order;
  70}
  71
  72static inline void dec_snd_pages(int order)
  73{
  74        snd_allocated_pages -= 1 << order;
  75}
  76
  77/**
  78 * snd_malloc_pages - allocate pages with the given size
  79 * @size: the size to allocate in bytes
  80 * @gfp_flags: the allocation conditions, GFP_XXX
  81 *
  82 * Allocates the physically contiguous pages with the given size.
  83 *
  84 * Returns the pointer of the buffer, or NULL if no enoguh memory.
  85 */
  86void *snd_malloc_pages(size_t size, gfp_t gfp_flags)
  87{
  88        int pg;
  89        void *res;
  90
  91        if (WARN_ON(!size))
  92                return NULL;
  93        if (WARN_ON(!gfp_flags))
  94                return NULL;
  95        gfp_flags |= __GFP_COMP;        /* compound page lets parts be mapped */
  96        pg = get_order(size);
  97        if ((res = (void *) __get_free_pages(gfp_flags, pg)) != NULL)
  98                inc_snd_pages(pg);
  99        return res;
 100}
 101
 102/**
 103 * snd_free_pages - release the pages
 104 * @ptr: the buffer pointer to release
 105 * @size: the allocated buffer size
 106 *
 107 * Releases the buffer allocated via snd_malloc_pages().
 108 */
 109void snd_free_pages(void *ptr, size_t size)
 110{
 111        int pg;
 112
 113        if (ptr == NULL)
 114                return;
 115        pg = get_order(size);
 116        dec_snd_pages(pg);
 117        free_pages(igned int)long>(( 100}
<
  59/*
1  21 *
1  Bus-specif> *  Generic memory allocators
1  83 *
1  44 */
1  55
  v21name="L2fdefref="+code=href=CONFIG_HAS="srcodehref="+codeCONFIG_HAS="srme="L55">  55  77 * cohern c "sr of allocated pages */
  L86">  86void *fr3        struct ptr, size_t ptr, void *NULL)
 110{
 111        int pg;
 119        void *res;
 117        gfp_t sref">res;
  23
 113        if (WARN_ON(!NULL)
 114                return NULL;
  96        pg = get_order(size);
 117        gsref">pg =   23
  98   |_flags |= _> 117/a>;        /* compound page lets parts be mapped */
  98   |_flags |= ;        
  98   |_flags |= __Gf/a>;        
 117        ptr, get_order(ptr, ptr, size, gef">size);
 113        if (pg)) != NULL)
  98                inc_snd_pages(pg);
  64
 119        return res;
 100}
<  37
 108 * cohern c "sr of allocated pages */
  L86"> 109void fr3        struct ptr, size_t   98   98">  98                void *NULL)
 110{
  51        int pg;
  23
 113        if (ptr == NULL)
 114         "sref">pg;
  96        pg = get_order(size);
 117        dec_snd_pages(pg);
 117        get_order(ptr, ptr, pg);
 100}
< 108
 101
  62/*
1  63/*
1  14 *  Gemanef"mn cefcomment">/*
1 105/*
1 106
  37
  58
  59/*
1  80 * snd_malloc_page> * Releasesrea aca hmment"ous pages wie;">efcomment">/*
1  61ent"> "srlReleasee;">efcomment">/*
1  Bus-specif> devicer: the befcomment">/*
1 103 he allocateize: the sizefcomment">/*
1 104 * @size: tdataefcomment">/*
1 105/*
1  16 * > *  G/* id foemorun     ommen> * corresp allngefcomment">/*
1  77 more details.
  78
  79 he alloiguous pages with t Thi* @size: tsu"faltfullyill be useful,
  80
  81
 * snd_malloc_pages" class="sent"> * sndef">g1        int _malloc_pages" e;">efe=hr3        struct ptr, size_t   98           1        struct snd_dm  86void *NULL)
 110{
 111        if (WARN_ON(!size))
  98   9  -WARN_ON(!pg;
  97        if (WARN_ON(!size))
  98   9  -WARN_ON(!pg;

  9WARN_ON(!(_malloc_pages" e;">efe="sref">pg = _malloc_pages" e;">efe="sref">pg;
 117        (ptr, pg;
 117        (pg;
  9siguch/a>        if (_malloc_pages" e;">efe=)_mem_list {
  9c_pag>        if ( {
 114   7        (ptr, snd_malloc_pages(  98                           loc_pages(size_t ptr, pg);
  94   7        (pg;
  98   break"sref">pg;
  55  9c_pag>        if ( {
 114   7        (ptr, fr 86void *ptr, (pg);
  92   break"sref">pg;
pg;
  55  9c_pag>        if ( {
  98   ef">ptr, fr 86void *ptr, (pg;
  94   break"sref">pg;
pg;
  9default:em_list {
  98   ef">ptr, fr 86void *( %d\nng">"GPLsrit_order(_malloc_pages" e;">efe=)"sref">pg;
 114   7        (ptr, NULL;
  92   7        (pg;
  98   9  -WARN_ON(!pg;
 11100"> 100}
< 111        (size))
  98   9  -WARN_ON(!pg;
 117        (ptr, pg;
  99  0"sref">pg;
 100}
<  90
  21/*
1  Bus-specif> * snd_fentb2ck malloc_page> * Releasesrea aca hmment"ous pages wie;">oiguoufentb2ckefcomment">/*
1  83ent"> "srlReleasee;">efcomment">/*
1  44 devicer: the befcomment">/*
1 105 he allocateize: the sizefcomment">/*
1  16 * @size: tdataefcomment">/*
1  77/*
1  78 * > *  G/* id foemorun     ommen> * corresp allngefcomment">/*
1  79   Wh wino Lpace is left,   alorun     oreducnt"> * cateiandefcomment">/*
1  80e: the sizeagain   T * cateiactuas th* @size: talost hrefcomment">/*
1  61
  Bus-specif>
  83 he alloiguous pages with t Thi* @size: tsu"faltfullyill be useful,
  44
  85 */
 * snd_fentb2ck_malloc_pages" class="sent"> * snd_fentb2ckef">g1        int _malloc_pages" e;">efe=hr3        struct ptr, size_t   94                    1        struct snd_dm  86void *NULL)
  68{
  91        int pg;
  90
 11while/a>        if ((ptr,  * snd_malloc_pages" class="sent"> * sndef">g>        int _malloc_pages" e;">efe=hr 86void *ptr,  {
  92   7        size_t pg;
  98   7        if (NULL)
 114   L98">  99  >        int pg;
 114   7        if (ptr, NULL)
  98           9  -WARN_ON(!pg;
  94   7        ptr, get_order(size);
  98   7        if (pg)) != NULL)
  98   2   7        pg)) != size);
  98   elsef">size);
 114   2   7        size);
  5100"> 100}
<  51        (size))
  94          -WARN_ON(!pg;
 119  0"sref">pg;
 100}
<  37
  58
  59/*
1  60 * snd_free_pages -* @size: tref">sndcomment">/*
1  61 * Releases the b: threa hmize:r pointer to release
  62 *
  63 * * @size: tref">sfer allocs="sent"> * snd_malloc_pages().
  14 */
 109void  * s_malloc_pages" class="sclass="sref">fr1        struct snd_dm  86void *NULL)
  9siguch/a>        if ((_malloc_pages" e;">efe=)_mem_list {
  9c_pag>        if ( {
  98         struct snd_fr7        ((size);
  98   break"sref">pg;
  55  9c_pag>        if ( {
  98                fr>        if ((ptr, (ptr, ((pg);
  94   break"sref">pg;
pg;
  55  9c_pag>        if ( {
  98                fr>        if (pg);
  98   break"sref">pg;
pg;
 11default:em_list {
  92   7        fr 86void *( %d\nng">"GPLsrit_order((_malloc_pages" e;">efe=)"sref">pg;
  9100"> 100}
< 100}
<  55  55  77 *
  78 * reserved ref">sfmmen> * ges widevicee=comment"> *
  79 * Releases the b: threa hmize:st hre=comment"> *
  60 * Releaseidefcomment">/*
1  21 *
1  Bus-specif> * reserved-Releaselistiand re-uent"3 slastref">sndcomment">/*
1  63
1 in/t"> list   Wh wi> * Releaseilorn>
1, it's removed fromi> * s prolist ndcomment">/*
1  14/*
1 105/*
1 106
ptr, size_t fr1        struct snd_dm  86void *        int NULL)
  68{
  91        struct pg;
  90
 117        if (WARN_ON(!size))
  92   9  0"sref">pg;
  23
  9 86void *(pg;
  9it_order(fr 86void *( {
  98   7        if ((ptr ==  {
  94       a>        if ((snd_d.>        int ptr, (ptr,   98        ! 7        ((snd_d.>        int ((  98   2   3        struct ptr, (  98   L94">  9it_order((( {
 114             86void *ptr, (snd_daem_list {
  92   8   7        if ((ptr, size))
  98   8   L94">  9it_order((ptr,  114   L98">  9ef">ptr, fr 86void * {
 114   L98">  9ef">ptr, (pg;
  98           9  >        int (pg;
  94   100"> 100}
<  9100"> 100}
<  9ef">ptr, (pg;
  99  0"sref">pg;
 100}
< 100}
<  83 *
  44 *
 105 * Releaseze:r serve="comment"> *
  16/*
1  77/*
1  78 * ges wiReleasess a reserved ref">s ndcomment">/*
1  79/*
1  80(< at error more details.
  61
fr1        struct snd_dm  86void *        int NULL)
 111        struct pg;
  55  97        if (WARN_ON(!size))
  94          -WARN_ON(!pg;
 117        ptr, fr1lasofr  86void *(pg;
  91        pg;
  98          -WARN_ON(!pg;
 117        (pg;
  9ef">ptr, (snd_dsre  86void *pg;
  9>        if ((        int pg;
  9 86void *(((pg;
  9it_order((pg;
  99  0"sref">pg;
 100}
<  58
  59
  80ssated pages */
  61
 109void pg;
 111        struct pg;
 111        struct pg;
  55 117        (pg;
 11while/a! 7        (  98         struct ptr, pg;
  98   ef">ptr, ptr, fr 86void *        struct pg;
 114   7        void *pg;
  92   7         * s_malloc_pages" class="sclass="sref">fr&ait_order((snd_d)"sref">pg;
  98                fr 86void * {
 11100"> 100}
<  9it_order((pg;
 100}
<  37
  58
  58
  80
  61 *
  Bus-specif>
  "LICENSE("GPLges */
void *pg;
  55fr1        struct void *  86void *pg;
  9a>(i       struct fsreef">ptr,  * s_malloc_pages" clas* @size: "> * sf">fstiwatiwa/a>        if (pg;
  91        struct pg;
  91        int pg;
 11st b:c charm  86void *s_malloc_pages" e;">se="L[]sre{ "LICENSE("GPLsriLICENSE("GPLsriLICENSE("GPLsriLICENSE("GPL }"sref">pg;
 100}
<  9>        if ((pg;
  9 86void *fr 86void *("GPLssref">pg;
 114   L98      struct fs*eef">ptr, fsrit_order(pg;
  9 86void *pg;
 117        fr 86void *( {
  98                pg;
  98         struct fr 86void *( %s\nng">"GPLssref">pg;
  98   L94">  9                (((s_malloc_pages" e;">se="L[it_order((snd_d.>        int _malloc_pages" e;">efe=])"sref">pg;
 114   7        fr 86void *("GPLssref">pg;
  92   8      (e_pages(i)it_order((snd_d.>        int pg;
  98   8   L94g1((snd_d.>        int size);
 11100"> 100}
<  9it_order((pg;
  99  0"sref">pg;
 100}
<  58
fr1        struct void *        struct void *pg;
 119  >        int fr 86void *ptr, fsref">ptr, pg;
 100}
<  23
  44
  23
fr 86void *ptr, fr 86void *("GPL)sref">pg;
  37
ptr, size_t fr1        struct void *void *fs*eef">ptr, snd_dssref">pg;
  98   2   114   7        size_t ;
="+code=size_t" c>;
=ss="srit_order(fs*eef">ptr, pg;
 11charm 86void *f[128]"sref">pg;
  9charm  86void *f,e  86void *pg;
  23
 117        if (;
="+code=size_t" c>;
=ss="stiwarclasofr>        if (f) -c1)sref">pg;
 114          -WARN_ON(!pg;
  97        if (fr 86void *fsrit_order(snd_dsf">size_t ;
="+code=size_t" c>;
=ss="ref">size))
  94          -WARN_ON(!pg;
 117        f[">size_t ;
="+code=size_t" c>;
=ss="]sreiLICENSE(pg;
pg;
  9 86void *ptr, f"sref">pg;
 117        fsreef">ptr, fr&ait_order(pg;
  97        fs||m  86void *fsrreeLICENSE(size))
  98   9  >        int ;
="+code=size_t" c>;
=ss=""sref">pg;
 117        if (fr 86void *f,eiLICENSE("GPL)srre0)_mem_list {
 114   charm  86void *pg;
  98   7        int size_t ptr, ssat=""sref">pg;
  94   a>(i       struct pg;
  98   7        int pg;
  98   3        struct void *pg;
  90
 114   7void *fsreef">ptr, fr&ait_order(ptr,   92   8void *ptr, fr 86void *f,eif">ptr,   98   8void *fsreef">ptr, fr&ait_order(ptr,  118   8void *ptr, fr 86void *f,eif">ptr,  114   L98"r 86void *fsreef">ptr, fr&ait_order(ptr,   98       r 86void *ptr, fr 86void *f,eif">ptr,   94   L98"r 86void *fsreef">ptr, fr&ait_order(ptr,   98       a>        if (ptr, fr 86void *f,e&ait_order(  98   2        if (  44 {
  98   L98"r 86void *fsreef">ptr, fr&ait_order(ptr,  114       r 86void *ssat="1reef">ptr, fr 86void *f,eif">ptr,   92   8void *ssat="1tiwar4)_mem_list {
  98   8   it_order(fr 86void *("GPL)"sref">pg;
 118   8  >        int ;
="+code=size_t" c>;
=ss=""sref">pg;
 114   100"> 100}
<  98    86void *pg;
  94    86void *pg;
  58
  98         struct pg;
  98   ef">ptr, ptr, pg;
 114   while/a" 86void *ptr, fr 86void *size_t ptr, ptr,   92   8   7        if ( {
  98   8   L94">  97        if (fr 86void * {
 118   8  98   ef">ptr, fr 86void * {
 114   L98"8  98   ef">ptr, fr 86void *("GPLsrit_order((size_t pg;
  98           aaaa>  98   ef">ptr, fr 86void *pg;
  94   L98"""""""""""""""""""""9  >        int ;
="+code=size_t" c>;
=ss=""sref">pg;
  98                   100"> 100}
<  98           100"> 100}
<  98           mmenr 86void *        int ssat=""a>        int  114                   1        struct snd_dm">size_t pg;
  92                   ef">ptr, fr&ait_order(        if (pg;
  98                   7        if ( * s_malloc_pages" class="sound/"> * sf">fr 86void *ptr, fr 86void *pg;
 118   8  98   >                   ef">ptr,  {
 114   L98"8  98   ef">ptr, fr 86void *( * sn(classre%d)\nng">"GPLs ef">ptr, pg;
  98           aaaa>  98   ef">ptr, fr 86void *pg;
  94   L98"""""""""""""""""""""9  >        int ;
="+code=size_t" c>;
=ss=""sref">pg;
  98                   100"> 100}
<  98   2   114 ef">ptr, fr&ait_order(ptr, fr 86void *pg;
  98           100"> 100}
< 114                 struct pg;
  92   100"> 100}
<  98   7         118   8void *        int ssat=""a>        int  114   L98"8  91        struct snd_dm">size_t pg;
  98           aaaa>  9ef">ptr, fr&ait_order(        if (pg;
  94   L98"""""""""""""L44">  44
  78
  79
  98   L94">  9        7        if ( * s_malloc_pages" class="sound/"> * sf">fr 86void *ptr,  114                                           ef">ptr,  {
  92                           ef">ptr, fr 86void *( * sn(classre%d)\nng">"GPLs ef">ptr, pg;
  98                           break"sref">pg;
 118   8  98 100}
< 114   L98             ef">ptr, fr&ait_order(void *size_t pg;
  98           100"> 100}
<  94   100"> 100}
<  9} else17        if (fr 86void *f,eiLICENSE("GPL)srre0)00"> 100}
<  98    44">  44
  98    86void *pg;
 11elsesref">pg;
  92   ef">ptr, fr 86void *("GPL)"sref">pg;
  99  >        int ;
="+code=size_t" c>;
=ss=""sref">pg;
 100}
<  44
  55size_t  {
  9.>        int   92ptr,   9.>        int f L92">  92ptr, f,ges */
  9.>        int f L92">  92ptr, f,ges */
  23
  9.>        int fL92">  92ptr, f,ges */
  9.>        int fL92">  92reef">ptr, f,ges */
  9.>        int   9reef">ptr, f,ges */
pg;
  37
  44
pg;
  80
  61
  Bus-specif>
  23
size_t frL86")sref">pg;
  58
 117        ptr, fr 86void *ptr,   98                      &ait_order(pg;
  99  0"sref">pg;
 100}
< 100}
<>        int size_t frL86")sref">pg;
 11>        int fr 86void *ptr, pg;
  9 86void *pg;
  97        if ( * s_malloc_pages" clas* @size: "> * sf">fstiwae0)00"> 100}
<  98   ef">ptr, fr 86void *("GPLs ef">ptr,  * s_malloc_pages" clas* @size: "> * sf">f)"sref">pg;
 100}
<  90
  90
ptr, fr 86void *f)00"> 100}
<ptr, fr 86void *f)00"> 100}
< 100}
<  55  80
  61
  78
fr 86void * * s_malloc_pages" class="sound/"> * sf">f)"sref">pg;
fr 86void * * s_founba/kef="+code=href=class="sound/"> * s_founba/kf">f)"sref">pg;
fr 86void * * s_malloc_pages" class="sf="+c> * sf">f)"sref">pg;
 100}
<ptr, fr 86void *f)"sref">pg;
ptr, fr 86void *f)"sref">pg;
  55ptr, fr 86void * * s_malloc_pages" clasmound/"> * sf">f)"sref">pg;
ptr, fr 86void * * s_malloc_pages" clasf="+c> * sf">f)"sref">pg;



The original LXR software by th3g> http://a>;rceforge.net/projects/lxr">LXR mailto:lxr@"L4ux.no">lxr@"L4ux.nome=".
lxr."L4ux.no kindly hose: by > http://www.redpill-"L4pro.no">Redpill LL4pro ASme=", provider of LL4ux consultan and oper b:ons servic snsince 1995.