1/* Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 */
5
6#ifndef BUFFER_SHARE_H_
7#define BUFFER_SHARE_H_
8
9#define INITIAL_ID_SIZE 3
10
11struct id_offset {
12	unsigned int used;
13	unsigned int id;
14	unsigned int offset;
15	void *data;
16};
17
18struct buffer_share {
19	unsigned int buf_sz;
20	unsigned int id_sz;
21	struct id_offset *wr_idx;
22};
23
24/*
25 * Creates a buffer share object.  This object is used to manage the read or
26 * write offsets of several users in one shared buffer.
27 */
28struct buffer_share *buffer_share_create(unsigned int buf_sz);
29
30/* Destroys a buffer_share returned from buffer_share_create. */
31void buffer_share_destroy(struct buffer_share *mix);
32
33/* Adds an ID that shares the buffer. */
34int buffer_share_add_id(struct buffer_share *mix, unsigned int id, void *data);
35
36/* Removes an ID that shares the buffer. */
37int buffer_share_rm_id(struct buffer_share *mix, unsigned int id);
38
39/* Updates the offset of the given user into the shared buffer. */
40int buffer_share_offset_update(struct buffer_share *mix, unsigned int id,
41			       unsigned int frames);
42
43/*
44 * Updates the write point to the minimum offset from all users.
45 * Returns the number of minimum number of frames written.
46 */
47unsigned int buffer_share_get_new_write_point(struct buffer_share *mix);
48
49/*
50 * The amount by which the user given by id is ahead of the current write
51 * point.
52 */
53unsigned int buffer_share_id_offset(const struct buffer_share *mix,
54				    unsigned int id);
55
56/*
57 * Gets the data pointer for given id.
58 */
59void *buffer_share_get_data(const struct buffer_share *mix,
60			    unsigned int id);
61
62#endif /* BUFFER_SHARE_H_ */
63