]> code.delx.au - pulseaudio/blob - src/pulsecore/memblock.h
modify memory block reference counting to use the new reference counting API
[pulseaudio] / src / pulsecore / memblock.h
1 #ifndef foopulsememblockhfoo
2 #define foopulsememblockhfoo
3
4 /* $Id$ */
5
6 /***
7 This file is part of PulseAudio.
8
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as
11 published by the Free Software Foundation; either version 2.1 of the
12 License, or (at your option) any later version.
13
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 USA.
23 ***/
24
25 #include <sys/types.h>
26 #include <inttypes.h>
27
28 #include <pulsecore/llist.h>
29 #include <pulsecore/refcnt.h>
30
31 /* A pa_memblock is a reference counted memory block. PulseAudio
32 * passed references to pa_memblocks around instead of copying
33 * data. See pa_memchunk for a structure that describes parts of
34 * memory blocks. */
35
36 /* The type of memory this block points to */
37 typedef enum pa_memblock_type {
38 PA_MEMBLOCK_POOL, /* Memory is part of the memory pool */
39 PA_MEMBLOCK_POOL_EXTERNAL, /* Data memory is part of the memory pool but the pa_memblock structure itself not */
40 PA_MEMBLOCK_APPENDED, /* the data is appended to the memory block */
41 PA_MEMBLOCK_USER, /* User supplied memory, to be freed with free_cb */
42 PA_MEMBLOCK_FIXED, /* data is a pointer to fixed memory that needs not to be freed */
43 PA_MEMBLOCK_IMPORTED, /* Memory is imported from another process via shm */
44 PA_MEMBLOCK_TYPE_MAX
45 } pa_memblock_type_t;
46
47 typedef struct pa_memblock pa_memblock;
48 typedef struct pa_mempool pa_mempool;
49 typedef struct pa_mempool_stat pa_mempool_stat;
50 typedef struct pa_memimport_segment pa_memimport_segment;
51 typedef struct pa_memimport pa_memimport;
52 typedef struct pa_memexport pa_memexport;
53
54 typedef void (*pa_memimport_release_cb_t)(pa_memimport *i, uint32_t block_id, void *userdata);
55 typedef void (*pa_memexport_revoke_cb_t)(pa_memexport *e, uint32_t block_id, void *userdata);
56
57 struct pa_memblock {
58 pa_memblock_type_t type;
59 int read_only; /* boolean */
60 PA_REFCNT_DECLARE; /* the reference counter */
61 size_t length;
62 void *data;
63 pa_mempool *pool;
64
65 union {
66 struct {
67 void (*free_cb)(void *p); /* If type == PA_MEMBLOCK_USER this points to a function for freeing this memory block */
68 } user;
69
70 struct {
71 uint32_t id;
72 pa_memimport_segment *segment;
73 } imported;
74 } per_type;
75 };
76
77 struct pa_mempool_stat {
78 unsigned n_allocated;
79 unsigned n_accumulated;
80 unsigned n_imported;
81 unsigned n_exported;
82 size_t allocated_size;
83 size_t accumulated_size;
84 size_t imported_size;
85 size_t exported_size;
86
87 unsigned n_too_large_for_pool;
88 unsigned n_pool_full;
89
90 unsigned n_allocated_by_type[PA_MEMBLOCK_TYPE_MAX];
91 unsigned n_accumulated_by_type[PA_MEMBLOCK_TYPE_MAX];
92 };
93
94 /* Allocate a new memory block of type PA_MEMBLOCK_MEMPOOL or PA_MEMBLOCK_APPENDED, depending on the size */
95 pa_memblock *pa_memblock_new(pa_mempool *, size_t length);
96
97 /* Allocate a new memory block of type PA_MEMBLOCK_MEMPOOL. If the requested size is too large, return NULL */
98 pa_memblock *pa_memblock_new_pool(pa_mempool *, size_t length);
99
100 /* Allocate a new memory block of type PA_MEMBLOCK_USER */
101 pa_memblock *pa_memblock_new_user(pa_mempool *, void *data, size_t length, void (*free_cb)(void *p), int read_only);
102
103 /* A special case of pa_memblock_new_user: take a memory buffer previously allocated with pa_xmalloc() */
104 #define pa_memblock_new_malloced(p,data,length) pa_memblock_new_user(p, data, length, pa_xfree, 0)
105
106 /* Allocate a new memory block of type PA_MEMBLOCK_FIXED */
107 pa_memblock *pa_memblock_new_fixed(pa_mempool *, void *data, size_t length, int read_only);
108
109 void pa_memblock_unref(pa_memblock*b);
110 pa_memblock* pa_memblock_ref(pa_memblock*b);
111
112 /* This special unref function has to be called by the owner of the
113 memory of a static memory block when he wants to release all
114 references to the memory. This causes the memory to be copied and
115 converted into a PA_MEMBLOCK_DYNAMIC type memory block */
116 void pa_memblock_unref_fixed(pa_memblock*b);
117
118 /* The memory block manager */
119 pa_mempool* pa_mempool_new(int shared);
120 void pa_mempool_free(pa_mempool *p);
121 const pa_mempool_stat* pa_mempool_get_stat(pa_mempool *p);
122 void pa_mempool_vacuum(pa_mempool *p);
123 int pa_mempool_get_shm_id(pa_mempool *p, uint32_t *id);
124 int pa_mempool_is_shared(pa_mempool *p);
125
126 /* For recieving blocks from other nodes */
127 pa_memimport* pa_memimport_new(pa_mempool *p, pa_memimport_release_cb_t cb, void *userdata);
128 void pa_memimport_free(pa_memimport *i);
129 pa_memblock* pa_memimport_get(pa_memimport *i, uint32_t block_id, uint32_t shm_id, size_t offset, size_t size);
130 int pa_memimport_process_revoke(pa_memimport *i, uint32_t block_id);
131
132 /* For sending blocks to other nodes */
133 pa_memexport* pa_memexport_new(pa_mempool *p, pa_memexport_revoke_cb_t cb, void *userdata);
134 void pa_memexport_free(pa_memexport *e);
135 int pa_memexport_put(pa_memexport *e, pa_memblock *b, uint32_t *block_id, uint32_t *shm_id, size_t *offset, size_t *size);
136 int pa_memexport_process_release(pa_memexport *e, uint32_t id);
137
138 #endif