|
Packit Service |
db8eaa |
/*
|
|
Packit Service |
db8eaa |
* IPC SHM area manager
|
|
Packit Service |
db8eaa |
* Copyright (c) 2003 by Jaroslav Kysela <perex@perex.cz>
|
|
Packit Service |
db8eaa |
*
|
|
Packit Service |
db8eaa |
* This library is free software; you can redistribute it and/or modify
|
|
Packit Service |
db8eaa |
* it under the terms of the GNU Lesser General Public License as
|
|
Packit Service |
db8eaa |
* published by the Free Software Foundation; either version 2.1 of
|
|
Packit Service |
db8eaa |
* the License, or (at your option) any later version.
|
|
Packit Service |
db8eaa |
*
|
|
Packit Service |
db8eaa |
* This program is distributed in the hope that it will be useful,
|
|
Packit Service |
db8eaa |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
db8eaa |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit Service |
db8eaa |
* GNU Lesser General Public License for more details.
|
|
Packit Service |
db8eaa |
*
|
|
Packit Service |
db8eaa |
* You should have received a copy of the GNU Lesser General Public
|
|
Packit Service |
db8eaa |
* License along with this library; if not, write to the Free Software
|
|
Packit Service |
db8eaa |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
Packit Service |
db8eaa |
*
|
|
Packit Service |
db8eaa |
*/
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
#include "config.h"
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
/* These funcs are only used by pcm_mmap when sys/shm.h is available. */
|
|
Packit Service |
db8eaa |
#ifdef HAVE_SYS_SHM_H
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
#include <stdio.h>
|
|
Packit Service |
db8eaa |
#include <malloc.h>
|
|
Packit Service |
db8eaa |
#include <string.h>
|
|
Packit Service |
db8eaa |
#include <errno.h>
|
|
Packit Service |
db8eaa |
#include <poll.h>
|
|
Packit Service |
db8eaa |
#include <sys/mman.h>
|
|
Packit Service |
db8eaa |
#include <sys/shm.h>
|
|
Packit Service |
db8eaa |
#include "list.h"
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
#ifndef DOC_HIDDEN
|
|
Packit Service |
db8eaa |
struct snd_shm_area {
|
|
Packit Service |
db8eaa |
struct list_head list;
|
|
Packit Service |
db8eaa |
int shmid;
|
|
Packit Service |
db8eaa |
void *ptr;
|
|
Packit Service |
db8eaa |
int share;
|
|
Packit Service |
db8eaa |
};
|
|
Packit Service |
db8eaa |
#endif
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
static LIST_HEAD(shm_areas);
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
/**
|
|
Packit Service |
db8eaa |
* \brief Create a shm area record
|
|
Packit Service |
db8eaa |
* \param shmid IPC SHM ID
|
|
Packit Service |
db8eaa |
* \param ptr the shared area pointer
|
|
Packit Service |
db8eaa |
* \return The allocated shm area record, NULL if fail
|
|
Packit Service |
db8eaa |
*
|
|
Packit Service |
db8eaa |
* Allocates a shared area record with the given SHM ID and pointer.
|
|
Packit Service |
db8eaa |
* The record has a reference counter, which is initialized to 1 by this function.
|
|
Packit Service |
db8eaa |
*/
|
|
Packit Service |
db8eaa |
struct snd_shm_area *snd_shm_area_create(int shmid, void *ptr)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
struct snd_shm_area *area = malloc(sizeof(*area));
|
|
Packit Service |
db8eaa |
if (area) {
|
|
Packit Service |
db8eaa |
area->shmid = shmid;
|
|
Packit Service |
db8eaa |
area->ptr = ptr;
|
|
Packit Service |
db8eaa |
area->share = 1;
|
|
Packit Service |
db8eaa |
list_add_tail(&area->list, &shm_areas);
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
return area;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
/**
|
|
Packit Service |
db8eaa |
* \brief Increase the reference counter of shm area record
|
|
Packit Service |
db8eaa |
* \param area shm area record
|
|
Packit Service |
db8eaa |
* \return the shm area record (identical with the argument)
|
|
Packit Service |
db8eaa |
*
|
|
Packit Service |
db8eaa |
* Increases the reference counter of the given shared area record.
|
|
Packit Service |
db8eaa |
*/
|
|
Packit Service |
db8eaa |
struct snd_shm_area *snd_shm_area_share(struct snd_shm_area *area)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
if (area == NULL)
|
|
Packit Service |
db8eaa |
return NULL;
|
|
Packit Service |
db8eaa |
area->share++;
|
|
Packit Service |
db8eaa |
return area;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
/**
|
|
Packit Service |
db8eaa |
* \brief Release the shared area record
|
|
Packit Service |
db8eaa |
* \param area the shared are record
|
|
Packit Service |
db8eaa |
* \return 0 if successful, or a negative error code
|
|
Packit Service |
db8eaa |
*
|
|
Packit Service |
db8eaa |
* Decreases the reference counter of the given shared area record, and
|
|
Packit Service |
db8eaa |
* releases the resources automaticall if it reaches to 0.
|
|
Packit Service |
db8eaa |
*/
|
|
Packit Service |
db8eaa |
int snd_shm_area_destroy(struct snd_shm_area *area)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
if (area == NULL)
|
|
Packit Service |
db8eaa |
return -ENOENT;
|
|
Packit Service |
db8eaa |
if (--area->share)
|
|
Packit Service |
db8eaa |
return 0;
|
|
Packit Service |
db8eaa |
list_del(&area->list);
|
|
Packit Service |
db8eaa |
shmdt(area->ptr);
|
|
Packit Service |
db8eaa |
free(area);
|
|
Packit Service |
db8eaa |
return 0;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
void snd_shm_area_destructor(void) __attribute__ ((destructor));
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
void snd_shm_area_destructor(void)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
struct list_head *pos;
|
|
Packit Service |
db8eaa |
struct snd_shm_area *area;
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
list_for_each(pos, &shm_areas) {
|
|
Packit Service |
db8eaa |
area = list_entry(pos, struct snd_shm_area, list);
|
|
Packit Service |
db8eaa |
shmdt(area->ptr);
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
#endif
|