1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * ALSA sequencer Memory Manager
4 * Copyright (c) 1998 by Frank van de Pol <fvdpol@coil.demon.nl>
5 */
6 #ifndef __SND_SEQ_MEMORYMGR_H
7 #define __SND_SEQ_MEMORYMGR_H
8
9 #include <sound/seq_kernel.h>
10 #include <linux/poll.h>
11
12 struct snd_info_buffer;
13
14 /* container for sequencer event (internal use) */
15 struct snd_seq_event_cell {
16 struct snd_seq_event event;
17 struct snd_seq_pool *pool; /* used pool */
18 struct snd_seq_event_cell *next; /* next cell */
19 };
20
21 /* design note: the pool is a contiguous block of memory, if we dynamicly
22 want to add additional cells to the pool be better store this in another
23 pool as we need to know the base address of the pool when releasing
24 memory. */
25
26 struct snd_seq_pool {
27 struct snd_seq_event_cell *ptr; /* pointer to first event chunk */
28 struct snd_seq_event_cell *free; /* pointer to the head of the free list */
29
30 int total_elements; /* pool size actually allocated */
31 atomic_t counter; /* cells free */
32
33 int size; /* pool size to be allocated */
34 int room; /* watermark for sleep/wakeup */
35
36 int closing;
37
38 /* statistics */
39 int max_used;
40 int event_alloc_nopool;
41 int event_alloc_failures;
42 int event_alloc_success;
43
44 /* Write locking */
45 wait_queue_head_t output_sleep;
46
47 /* Pool lock */
48 spinlock_t lock;
49 };
50
51 void snd_seq_cell_free(struct snd_seq_event_cell *cell);
52
53 int snd_seq_event_dup(struct snd_seq_pool *pool, struct snd_seq_event *event,
54 struct snd_seq_event_cell **cellp, int nonblock,
55 struct file *file, struct mutex *mutexp);
56
57 /* return number of unused (free) cells */
snd_seq_unused_cells(struct snd_seq_pool * pool)58 static inline int snd_seq_unused_cells(struct snd_seq_pool *pool)
59 {
60 return pool ? pool->total_elements - atomic_read(&pool->counter) : 0;
61 }
62
63 /* return total number of allocated cells */
snd_seq_total_cells(struct snd_seq_pool * pool)64 static inline int snd_seq_total_cells(struct snd_seq_pool *pool)
65 {
66 return pool ? pool->total_elements : 0;
67 }
68
69 /* init pool - allocate events */
70 int snd_seq_pool_init(struct snd_seq_pool *pool);
71
72 /* done pool - free events */
73 void snd_seq_pool_mark_closing(struct snd_seq_pool *pool);
74 int snd_seq_pool_done(struct snd_seq_pool *pool);
75
76 /* create pool */
77 struct snd_seq_pool *snd_seq_pool_new(int poolsize);
78
79 /* remove pool */
80 int snd_seq_pool_delete(struct snd_seq_pool **pool);
81
82 /* polling */
83 int snd_seq_pool_poll_wait(struct snd_seq_pool *pool, struct file *file, poll_table *wait);
84
85 void snd_seq_info_pool(struct snd_info_buffer *buffer,
86 struct snd_seq_pool *pool, char *space);
87
88 #endif
89