1<html><head><meta http-equiv="Content-Type" content="text/html; charset=ANSI_X3.4-1968"><title>Chapter&#160;11.&#160;Buffer and Memory Management</title><meta name="generator" content="DocBook XSL Stylesheets V1.78.1"><link rel="home" href="index.html" title="Writing an ALSA Driver"><link rel="up" href="index.html" title="Writing an ALSA Driver"><link rel="prev" href="misc-devices-IEC958.html" title="IEC958 (S/PDIF)"><link rel="next" href="buffer-and-memory-external-hardware.html" title="External Hardware Buffers"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter&#160;11.&#160;Buffer and Memory Management</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="misc-devices-IEC958.html">Prev</a>&#160;</td><th width="60%" align="center">&#160;</th><td width="20%" align="right">&#160;<a accesskey="n" href="buffer-and-memory-external-hardware.html">Next</a></td></tr></table><hr></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="buffer-and-memory"></a>Chapter&#160;11.&#160;Buffer and Memory Management</h1></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl class="toc"><dt><span class="section"><a href="buffer-and-memory.html#buffer-and-memory-buffer-types">Buffer Types</a></span></dt><dt><span class="section"><a href="buffer-and-memory-external-hardware.html">External Hardware Buffers</a></span></dt><dt><span class="section"><a href="buffer-and-memory-non-contiguous.html">Non-Contiguous Buffers</a></span></dt><dt><span class="section"><a href="buffer-and-memory-vmalloced.html">Vmalloc'ed Buffers</a></span></dt></dl></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="buffer-and-memory-buffer-types"></a>Buffer Types</h2></div></div></div><p>
2        ALSA provides several different buffer allocation functions
3      depending on the bus and the architecture. All these have a
4      consistent API. The allocation of physically-contiguous pages is
5      done via 
6      <code class="function">snd_malloc_xxx_pages()</code> function, where xxx
7      is the bus type. 
8      </p><p>
9        The allocation of pages with fallback is
10      <code class="function">snd_malloc_xxx_pages_fallback()</code>. This
11      function tries to allocate the specified pages but if the pages
12      are not available, it tries to reduce the page sizes until
13      enough space is found.
14      </p><p>
15      The release the pages, call
16      <code class="function">snd_free_xxx_pages()</code> function. 
17      </p><p>
18      Usually, ALSA drivers try to allocate and reserve
19       a large contiguous physical space
20       at the time the module is loaded for the later use.
21       This is called <span class="quote">&#8220;<span class="quote">pre-allocation</span>&#8221;</span>.
22       As already written, you can call the following function at 
23       pcm instance construction time (in the case of PCI bus). 
24
25        </p><div class="informalexample"><pre class="programlisting">
26
27  snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
28                                        snd_dma_pci_data(pci), size, max);
29
30          </pre></div><p>
31
32        where <em class="parameter"><code>size</code></em> is the byte size to be
33      pre-allocated and the <em class="parameter"><code>max</code></em> is the maximum
34      size to be changed via the <code class="filename">prealloc</code> proc file.
35      The allocator will try to get an area as large as possible
36      within the given size. 
37      </p><p>
38      The second argument (type) and the third argument (device pointer)
39      are dependent on the bus.
40      In the case of the ISA bus, pass <code class="function">snd_dma_isa_data()</code>
41      as the third argument with <code class="constant">SNDRV_DMA_TYPE_DEV</code> type.
42      For the continuous buffer unrelated to the bus can be pre-allocated
43      with <code class="constant">SNDRV_DMA_TYPE_CONTINUOUS</code> type and the
44      <code class="function">snd_dma_continuous_data(GFP_KERNEL)</code> device pointer,
45      where <code class="constant">GFP_KERNEL</code> is the kernel allocation flag to
46      use.
47      For the PCI scatter-gather buffers, use
48      <code class="constant">SNDRV_DMA_TYPE_DEV_SG</code> with
49      <code class="function">snd_dma_pci_data(pci)</code>
50      (see the 
51          <a class="link" href="buffer-and-memory-non-contiguous.html" title="Non-Contiguous Buffers"><em class="citetitle">Non-Contiguous Buffers
52          </em></a> section).
53      </p><p>
54        Once the buffer is pre-allocated, you can use the
55        allocator in the <em class="structfield"><code>hw_params</code></em> callback: 
56
57        </p><div class="informalexample"><pre class="programlisting">
58
59  snd_pcm_lib_malloc_pages(substream, size);
60
61          </pre></div><p>
62
63        Note that you have to pre-allocate to use this function.
64      </p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="misc-devices-IEC958.html">Prev</a>&#160;</td><td width="20%" align="center">&#160;</td><td width="40%" align="right">&#160;<a accesskey="n" href="buffer-and-memory-external-hardware.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">IEC958 (S/PDIF)&#160;</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">&#160;External Hardware Buffers</td></tr></table></div></body></html>
65