cleanup specfile for packaging
[profile/ivi/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., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18  *
19  */
20   
21 #include <stdio.h>
22 #include <malloc.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <sys/poll.h>
26 #include <sys/mman.h>
27 #include <sys/shm.h>
28 #include "list.h"
29
30 #ifndef DOC_HIDDEN
31 struct snd_shm_area {
32         struct list_head list;
33         int shmid;
34         void *ptr;
35         int share;
36 };
37 #endif
38
39 static LIST_HEAD(shm_areas);
40
41 /**
42  * \brief Create a shm area record
43  * \param shmid IPC SHM ID
44  * \param ptr the shared area pointer
45  * \return The allocated shm area record, NULL if fail
46  *
47  * Allocates a shared area record with the given SHM ID and pointer.
48  * The record has a reference counter, which is initialized to 1 by this function.
49  */
50 struct snd_shm_area *snd_shm_area_create(int shmid, void *ptr)
51 {
52         struct snd_shm_area *area = malloc(sizeof(*area));
53         if (area) {
54                 area->shmid = shmid;
55                 area->ptr = ptr;
56                 area->share = 1;
57                 list_add_tail(&area->list, &shm_areas);
58         }
59         return area;
60 }
61
62 /**
63  * \brief Increase the reference counter of shm area record
64  * \param area shm area record
65  * \return the shm area record (identical with the argument)
66  *
67  * Increases the reference counter of the given shared area record.
68  */
69 struct snd_shm_area *snd_shm_area_share(struct snd_shm_area *area)
70 {
71         if (area == NULL)
72                 return NULL;
73         area->share++;
74         return area;
75 }
76
77 /**
78  * \brief Release the shared area record
79  * \param area the shared are record
80  * \return 0 if successful, or a negative error code
81  *
82  * Decreases the reference counter of the given shared area record, and
83  * releases the resources automaticall if it reaches to 0.
84  */
85 int snd_shm_area_destroy(struct snd_shm_area *area)
86 {
87         if (area == NULL)
88                 return -ENOENT;
89         if (--area->share)
90                 return 0;
91         list_del(&area->list);
92         shmdt(area->ptr);
93         free(area);
94         return 0;
95 }
96
97 void snd_shm_area_destructor(void) __attribute__ ((destructor));
98
99 void snd_shm_area_destructor(void)
100 {
101         struct list_head *pos;
102         struct snd_shm_area *area;
103
104         list_for_each(pos, &shm_areas) {
105                 area = list_entry(pos, struct snd_shm_area, list);
106                 shmdt(area->ptr);
107         }
108 }