]> code.delx.au - pulseaudio/blob - src/pulsecore/memblock.h
generate per-type memory block statistics
[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
30 /* A pa_memblock is a reference counted memory block. PulseAudio
31 * passed references to pa_memblocks around instead of copying
32 * data. See pa_memchunk for a structure that describes parts of
33 * memory blocks. */
34
35 /* The type of memory this block points to */
36 typedef enum pa_memblock_type {
37 PA_MEMBLOCK_POOL, /* Memory is part of the memory pool */
38 PA_MEMBLOCK_POOL_EXTERNAL, /* Data memory is part of the memory pool but the pa_memblock structure itself not */
39 PA_MEMBLOCK_APPENDED, /* the data is appended to the memory block */
40 PA_MEMBLOCK_USER, /* User supplied memory, to be freed with free_cb */
41 PA_MEMBLOCK_FIXED, /* data is a pointer to fixed memory that needs not to be freed */
42 PA_MEMBLOCK_IMPORTED, /* Memory is imported from another process via shm */
43 PA_MEMBLOCK_TYPE_MAX
44 } pa_memblock_type_t;
45
46 typedef struct pa_memblock pa_memblock;
47 typedef struct pa_mempool pa_mempool;
48 typedef struct pa_mempool_stat pa_mempool_stat;
49 typedef struct pa_memimport_segment pa_memimport_segment;
50 typedef struct pa_memimport pa_memimport;
51 typedef struct pa_memexport pa_memexport;
52
53 typedef void (*pa_memimport_release_cb_t)(pa_memimport *i, uint32_t block_id, void *userdata);
54 typedef void (*pa_memexport_revoke_cb_t)(pa_memexport *e, uint32_t block_id, void *userdata);
55
56 struct pa_memblock {
57 pa_memblock_type_t type;
58 int read_only; /* boolean */
59 unsigned ref; /* the reference counter */
60 size_t length;
61 void *data;
62 pa_mempool *pool;
63
64 union {
65 struct {
66 void (*free_cb)(void *p); /* If type == PA_MEMBLOCK_USER this points to a function for freeing this memory block */
67 } user;
68
69 struct {
70 uint32_t id;
71 pa_memimport_segment *segment;
72 } imported;
73 } per_type;
74 };
75
76 struct pa_mempool_stat {
77 unsigned n_allocated;
78 unsigned n_accumulated;
79 unsigned n_imported;
80 unsigned n_exported;
81 size_t allocated_size;
82 size_t accumulated_size;
83 size_t imported_size;
84 size_t exported_size;
85
86 unsigned n_too_large_for_pool;
87 unsigned n_pool_full;
88
89 unsigned n_allocated_by_type[PA_MEMBLOCK_TYPE_MAX];
90 unsigned n_accumulated_by_type[PA_MEMBLOCK_TYPE_MAX];
91 };
92
93 /* Allocate a new memory block of type PA_MEMBLOCK_MEMPOOL or PA_MEMBLOCK_APPENDED, depending on the size */
94 pa_memblock *pa_memblock_new(pa_mempool *, size_t length);
95
96 /* Allocate a new memory block of type PA_MEMBLOCK_MEMPOOL. If the requested size is too large, return NULL */
97 pa_memblock *pa_memblock_new_pool(pa_mempool *, size_t length);
98
99 /* Allocate a new memory block of type PA_MEMBLOCK_USER */
100 pa_memblock *pa_memblock_new_user(pa_mempool *, void *data, size_t length, void (*free_cb)(void *p), int read_only);
101
102 /* A special case of pa_memblock_new_user: take a memory buffer previously allocated with pa_xmalloc() */
103 #define pa_memblock_new_malloced(p,data,length) pa_memblock_new_user(p, data, length, pa_xfree, 0)
104
105 /* Allocate a new memory block of type PA_MEMBLOCK_FIXED */
106 pa_memblock *pa_memblock_new_fixed(pa_mempool *, void *data, size_t length, int read_only);
107
108 void pa_memblock_unref(pa_memblock*b);
109 pa_memblock* pa_memblock_ref(pa_memblock*b);
110
111 /* This special unref function has to be called by the owner of the
112 memory of a static memory block when he wants to release all
113 references to the memory. This causes the memory to be copied and
114 converted into a PA_MEMBLOCK_DYNAMIC type memory block */
115 void pa_memblock_unref_fixed(pa_memblock*b);
116
117 /* The memory block manager */
118 pa_mempool* pa_mempool_new(int shared);
119 void pa_mempool_free(pa_mempool *p);
120 const pa_mempool_stat* pa_mempool_get_stat(pa_mempool *p);
121 void pa_mempool_vacuum(pa_mempool *p);
122 int pa_mempool_get_shm_id(pa_mempool *p, uint32_t *id);
123 int pa_mempool_is_shared(pa_mempool *p);
124
125 /* For recieving blocks from other nodes */
126 pa_memimport* pa_memimport_new(pa_mempool *p, pa_memimport_release_cb_t cb, void *userdata);
127 void pa_memimport_free(pa_memimport *i);
128 pa_memblock* pa_memimport_get(pa_memimport *i, uint32_t block_id, uint32_t shm_id, size_t offset, size_t size);
129 int pa_memimport_process_revoke(pa_memimport *i, uint32_t block_id);
130
131 /* For sending blocks to other nodes */
132 pa_memexport* pa_memexport_new(pa_mempool *p, pa_memexport_revoke_cb_t cb, void *userdata);
133 void pa_memexport_free(pa_memexport *e);
134 int pa_memexport_put(pa_memexport *e, pa_memblock *b, uint32_t *block_id, uint32_t *shm_id, size_t *offset, size_t *size);
135 int pa_memexport_process_release(pa_memexport *e, uint32_t id);
136
137 #endif