OSDN Git Service

configure: check if eaccess() is available
[android-x86/external-alsa-lib.git] / src / shmarea.c
1 /*
2  *  IPC SHM area manager
3  *  Copyright (c) 2003 by Jaroslav Kysela <perex@perex.cz>
4  *
5  *   This library is free software; you can redistribute it and/or modify
6  *   it under the terms of the GNU Lesser General Public License as
7  *   published by the Free Software Foundation; either version 2.1 of
8  *   the License, or (at your option) any later version.
9  *
10  *   This program is distributed in the hope that it will be useful,
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *   GNU Lesser General Public License for more details.
14  *
15  *   You should have received a copy of the GNU Lesser General Public
16  *   License along with this library; if not, write to the Free Software
17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  */
20
21 #include "config.h"
22
23 /* These funcs are only used by pcm_mmap when sys/shm.h is available. */
24 #ifdef HAVE_SYS_SHM_H
25
26 #include <stdio.h>
27 #include <malloc.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <poll.h>
31 #include <sys/mman.h>
32 #include <sys/shm.h>
33 #include "list.h"
34
35 #ifndef DOC_HIDDEN
36 struct snd_shm_area {
37         struct list_head list;
38         int shmid;
39         void *ptr;
40         int share;
41 };
42 #endif
43
44 static LIST_HEAD(shm_areas);
45
46 /**
47  * \brief Create a shm area record
48  * \param shmid IPC SHM ID
49  * \param ptr the shared area pointer
50  * \return The allocated shm area record, NULL if fail
51  *
52  * Allocates a shared area record with the given SHM ID and pointer.
53  * The record has a reference counter, which is initialized to 1 by this function.
54  */
55 struct snd_shm_area *snd_shm_area_create(int shmid, void *ptr)
56 {
57         struct snd_shm_area *area = malloc(sizeof(*area));
58         if (area) {
59                 area->shmid = shmid;
60                 area->ptr = ptr;
61                 area->share = 1;
62                 list_add_tail(&area->list, &shm_areas);
63         }
64         return area;
65 }
66
67 /**
68  * \brief Increase the reference counter of shm area record
69  * \param area shm area record
70  * \return the shm area record (identical with the argument)
71  *
72  * Increases the reference counter of the given shared area record.
73  */
74 struct snd_shm_area *snd_shm_area_share(struct snd_shm_area *area)
75 {
76         if (area == NULL)
77                 return NULL;
78         area->share++;
79         return area;
80 }
81
82 /**
83  * \brief Release the shared area record
84  * \param area the shared are record
85  * \return 0 if successful, or a negative error code
86  *
87  * Decreases the reference counter of the given shared area record, and
88  * releases the resources automaticall if it reaches to 0.
89  */
90 int snd_shm_area_destroy(struct snd_shm_area *area)
91 {
92         if (area == NULL)
93                 return -ENOENT;
94         if (--area->share)
95                 return 0;
96         list_del(&area->list);
97         shmdt(area->ptr);
98         free(area);
99         return 0;
100 }
101
102 void snd_shm_area_destructor(void) __attribute__ ((destructor));
103
104 void snd_shm_area_destructor(void)
105 {
106         struct list_head *pos;
107         struct snd_shm_area *area;
108
109         list_for_each(pos, &shm_areas) {
110                 area = list_entry(pos, struct snd_shm_area, list);
111                 shmdt(area->ptr);
112         }
113 }
114
115 #endif