2004-07-16 19:56:36 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
|
|
/***
|
2006-06-19 21:53:48 +00:00
|
|
|
This file is part of PulseAudio.
|
2004-07-16 19:56:36 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is free software; you can redistribute it and/or modify
|
2004-11-14 14:58:54 +00:00
|
|
|
it under the terms of the GNU Lesser General Public License as
|
|
|
|
|
published by the Free Software Foundation; either version 2.1 of the
|
|
|
|
|
License, or (at your option) any later version.
|
2004-07-16 19:56:36 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is distributed in the hope that it will be useful, but
|
2004-07-16 19:56:36 +00:00
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2004-11-14 14:58:54 +00:00
|
|
|
Lesser General Public License for more details.
|
2004-07-16 19:56:36 +00:00
|
|
|
|
2004-11-14 14:58:54 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
2006-06-19 21:53:48 +00:00
|
|
|
License along with PulseAudio; if not, write to the Free Software
|
2004-07-16 19:56:36 +00:00
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
|
USA.
|
|
|
|
|
***/
|
|
|
|
|
|
2004-07-16 19:16:42 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2004-06-15 15:18:33 +00:00
|
|
|
#include <stdio.h>
|
2004-06-08 23:54:24 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulse/xmalloc.h>
|
2006-02-17 12:10:58 +00:00
|
|
|
|
2004-06-08 23:54:24 +00:00
|
|
|
#include "memblock.h"
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
static void stat_add(pa_memblock*m, pa_memblock_stat *s) {
|
2004-08-17 19:37:29 +00:00
|
|
|
assert(m);
|
|
|
|
|
|
2004-11-17 00:05:25 +00:00
|
|
|
if (!s) {
|
|
|
|
|
m->stat = NULL;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2004-08-17 19:37:29 +00:00
|
|
|
m->stat = pa_memblock_stat_ref(s);
|
|
|
|
|
s->total++;
|
|
|
|
|
s->allocated++;
|
|
|
|
|
s->total_size += m->length;
|
|
|
|
|
s->allocated_size += m->length;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
static void stat_remove(pa_memblock *m) {
|
2004-08-17 19:37:29 +00:00
|
|
|
assert(m);
|
|
|
|
|
|
|
|
|
|
if (!m->stat)
|
|
|
|
|
return;
|
2004-06-15 15:18:33 +00:00
|
|
|
|
2004-08-17 19:37:29 +00:00
|
|
|
m->stat->total--;
|
|
|
|
|
m->stat->total_size -= m->length;
|
|
|
|
|
|
|
|
|
|
pa_memblock_stat_unref(m->stat);
|
|
|
|
|
m->stat = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_memblock *pa_memblock_new(size_t length, pa_memblock_stat*s) {
|
|
|
|
|
pa_memblock *b = pa_xmalloc(sizeof(pa_memblock)+length);
|
2004-07-03 23:35:12 +00:00
|
|
|
b->type = PA_MEMBLOCK_APPENDED;
|
2004-06-08 23:54:24 +00:00
|
|
|
b->ref = 1;
|
|
|
|
|
b->length = length;
|
|
|
|
|
b->data = b+1;
|
2004-08-14 20:25:32 +00:00
|
|
|
b->free_cb = NULL;
|
2004-11-17 00:05:25 +00:00
|
|
|
b->read_only = 0;
|
2004-08-17 19:37:29 +00:00
|
|
|
stat_add(b, s);
|
2004-06-08 23:54:24 +00:00
|
|
|
return b;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_memblock *pa_memblock_new_dynamic(void *d, size_t length, pa_memblock_stat*s) {
|
|
|
|
|
pa_memblock *b = pa_xmalloc(sizeof(pa_memblock));
|
2004-11-17 00:05:25 +00:00
|
|
|
b->type = PA_MEMBLOCK_DYNAMIC;
|
2004-06-08 23:54:24 +00:00
|
|
|
b->ref = 1;
|
|
|
|
|
b->length = length;
|
|
|
|
|
b->data = d;
|
2004-08-14 20:25:32 +00:00
|
|
|
b->free_cb = NULL;
|
2004-11-17 00:05:25 +00:00
|
|
|
b->read_only = 0;
|
2004-08-17 19:37:29 +00:00
|
|
|
stat_add(b, s);
|
2004-06-08 23:54:24 +00:00
|
|
|
return b;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_memblock *pa_memblock_new_fixed(void *d, size_t length, int read_only, pa_memblock_stat*s) {
|
|
|
|
|
pa_memblock *b = pa_xmalloc(sizeof(pa_memblock));
|
2004-11-17 00:05:25 +00:00
|
|
|
b->type = PA_MEMBLOCK_FIXED;
|
2004-06-08 23:54:24 +00:00
|
|
|
b->ref = 1;
|
|
|
|
|
b->length = length;
|
|
|
|
|
b->data = d;
|
2004-08-14 20:25:32 +00:00
|
|
|
b->free_cb = NULL;
|
2004-11-17 00:05:25 +00:00
|
|
|
b->read_only = read_only;
|
2004-08-17 19:37:29 +00:00
|
|
|
stat_add(b, s);
|
2004-08-14 20:25:32 +00:00
|
|
|
return b;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_memblock *pa_memblock_new_user(void *d, size_t length, void (*free_cb)(void *p), int read_only, pa_memblock_stat*s) {
|
|
|
|
|
pa_memblock *b;
|
2004-08-14 20:25:32 +00:00
|
|
|
assert(d && length && free_cb);
|
2006-01-11 01:17:39 +00:00
|
|
|
b = pa_xmalloc(sizeof(pa_memblock));
|
2004-08-14 20:25:32 +00:00
|
|
|
b->type = PA_MEMBLOCK_USER;
|
|
|
|
|
b->ref = 1;
|
|
|
|
|
b->length = length;
|
|
|
|
|
b->data = d;
|
|
|
|
|
b->free_cb = free_cb;
|
2004-11-17 00:05:25 +00:00
|
|
|
b->read_only = read_only;
|
2004-08-17 19:37:29 +00:00
|
|
|
stat_add(b, s);
|
2004-06-08 23:54:24 +00:00
|
|
|
return b;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_memblock* pa_memblock_ref(pa_memblock*b) {
|
2006-02-20 04:05:16 +00:00
|
|
|
assert(b);
|
|
|
|
|
assert(b->ref >= 1);
|
|
|
|
|
|
2004-06-08 23:54:24 +00:00
|
|
|
b->ref++;
|
|
|
|
|
return b;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
void pa_memblock_unref(pa_memblock*b) {
|
2006-02-20 04:05:16 +00:00
|
|
|
assert(b);
|
|
|
|
|
assert(b->ref >= 1);
|
2004-06-18 17:12:50 +00:00
|
|
|
|
2004-08-14 20:25:32 +00:00
|
|
|
if ((--(b->ref)) == 0) {
|
2004-08-17 19:37:29 +00:00
|
|
|
stat_remove(b);
|
2004-06-18 17:12:50 +00:00
|
|
|
|
2004-08-14 20:25:32 +00:00
|
|
|
if (b->type == PA_MEMBLOCK_USER) {
|
|
|
|
|
assert(b->free_cb);
|
|
|
|
|
b->free_cb(b->data);
|
|
|
|
|
} else if (b->type == PA_MEMBLOCK_DYNAMIC)
|
|
|
|
|
pa_xfree(b->data);
|
|
|
|
|
|
2004-08-04 16:39:30 +00:00
|
|
|
pa_xfree(b);
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
void pa_memblock_unref_fixed(pa_memblock *b) {
|
2004-08-14 20:25:32 +00:00
|
|
|
assert(b && b->ref >= 1 && b->type == PA_MEMBLOCK_FIXED);
|
2004-06-08 23:54:24 +00:00
|
|
|
|
2004-08-14 20:25:32 +00:00
|
|
|
if (b->ref == 1)
|
2004-07-03 23:35:12 +00:00
|
|
|
pa_memblock_unref(b);
|
2004-08-14 20:25:32 +00:00
|
|
|
else {
|
|
|
|
|
b->data = pa_xmemdup(b->data, b->length);
|
2004-07-10 16:50:22 +00:00
|
|
|
b->type = PA_MEMBLOCK_DYNAMIC;
|
|
|
|
|
b->ref--;
|
2004-06-08 23:54:24 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_memblock_stat* pa_memblock_stat_new(void) {
|
|
|
|
|
pa_memblock_stat *s;
|
2004-08-17 19:37:29 +00:00
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
s = pa_xmalloc(sizeof(pa_memblock_stat));
|
2004-08-17 19:37:29 +00:00
|
|
|
s->ref = 1;
|
|
|
|
|
s->total = s->total_size = s->allocated = s->allocated_size = 0;
|
|
|
|
|
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
void pa_memblock_stat_unref(pa_memblock_stat *s) {
|
2004-08-17 19:37:29 +00:00
|
|
|
assert(s && s->ref >= 1);
|
|
|
|
|
|
|
|
|
|
if (!(--(s->ref))) {
|
|
|
|
|
assert(!s->total);
|
|
|
|
|
pa_xfree(s);
|
|
|
|
|
}
|
2004-07-15 20:12:21 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_memblock_stat * pa_memblock_stat_ref(pa_memblock_stat *s) {
|
2004-08-17 19:37:29 +00:00
|
|
|
assert(s);
|
|
|
|
|
s->ref++;
|
|
|
|
|
return s;
|
2004-07-15 20:12:21 +00:00
|
|
|
}
|