<?xml version="1.0" encoding="ANSI_X3.4-1968" standalone="no"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=ANSI_X3.4-1968" /><title>Chapter 11. Buffer and Memory Management</title><meta name="generator" content="DocBook XSL Stylesheets V1.75.2" /><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="ch10s03.html" title="IEC958 (S/PDIF)" /><link rel="next" href="ch11s02.html" title="External Hardware Buffers" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter 11. Buffer and Memory Management</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch10s03.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="ch11s02.html">Next</a></td></tr></table><hr /></div><div class="chapter" title="Chapter 11. Buffer and Memory Management"><div class="titlepage"><div><div><h2 class="title"><a id="buffer-and-memory"></a>Chapter 11. Buffer and Memory Management</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="section"><a href="ch11.html#buffer-and-memory-buffer-types">Buffer Types</a></span></dt><dt><span class="section"><a href="ch11s02.html">External Hardware Buffers</a></span></dt><dt><span class="section"><a href="ch11s03.html">Non-Contiguous Buffers</a></span></dt><dt><span class="section"><a href="ch11s04.html">Vmalloc'ed Buffers</a></span></dt></dl></div><div class="section" title="Buffer Types"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="buffer-and-memory-buffer-types"></a>Buffer Types</h2></div></div></div><p> ALSA provides several different buffer allocation functions depending on the bus and the architecture. All these have a consistent API. The allocation of physically-contiguous pages is done via <code class="function">snd_malloc_xxx_pages()</code> function, where xxx is the bus type. </p><p> The allocation of pages with fallback is <code class="function">snd_malloc_xxx_pages_fallback()</code>. This function tries to allocate the specified pages but if the pages are not available, it tries to reduce the page sizes until enough space is found. </p><p> The release the pages, call <code class="function">snd_free_xxx_pages()</code> function. </p><p> Usually, ALSA drivers try to allocate and reserve a large contiguous physical space at the time the module is loaded for the later use. This is called <span class="quote">“<span class="quote">pre-allocation</span>”</span>. As already written, you can call the following function at pcm instance construction time (in the case of PCI bus). </p><div class="informalexample"><pre class="programlisting"> snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci), size, max); </pre></div><p> where <em class="parameter"><code>size</code></em> is the byte size to be pre-allocated and the <em class="parameter"><code>max</code></em> is the maximum size to be changed via the <code class="filename">prealloc</code> proc file. The allocator will try to get an area as large as possible within the given size. </p><p> The second argument (type) and the third argument (device pointer) are dependent on the bus. In the case of the ISA bus, pass <code class="function">snd_dma_isa_data()</code> as the third argument with <code class="constant">SNDRV_DMA_TYPE_DEV</code> type. For the continuous buffer unrelated to the bus can be pre-allocated with <code class="constant">SNDRV_DMA_TYPE_CONTINUOUS</code> type and the <code class="function">snd_dma_continuous_data(GFP_KERNEL)</code> device pointer, where <code class="constant">GFP_KERNEL</code> is the kernel allocation flag to use. For the PCI scatter-gather buffers, use <code class="constant">SNDRV_DMA_TYPE_DEV_SG</code> with <code class="function">snd_dma_pci_data(pci)</code> (see the <a class="link" href="ch11s03.html" title="Non-Contiguous Buffers"><em class="citetitle">Non-Contiguous Buffers </em></a> section). </p><p> Once the buffer is pre-allocated, you can use the allocator in the <em class="structfield"><code>hw_params</code></em> callback: </p><div class="informalexample"><pre class="programlisting"> snd_pcm_lib_malloc_pages(substream, size); </pre></div><p> Note that you have to pre-allocate to use this function. </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch10s03.html">Prev</a> </td><td width="20%" align="center"> </td><td width="40%" align="right"> <a accesskey="n" href="ch11s02.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">IEC958 (S/PDIF) </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> External Hardware Buffers</td></tr></table></div></body></html>