buffer: add support for pools using caller data in allocation
[libav.git] / libavutil / buffer_internal.h
CommitLineData
8e401dbe
AK
1/*
2 * This file is part of Libav.
3 *
4 * Libav is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * Libav is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with Libav; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#ifndef AVUTIL_BUFFER_INTERNAL_H
20#define AVUTIL_BUFFER_INTERNAL_H
21
22#include <stdint.h>
23
24#include "buffer.h"
fbd6c97f 25#include "thread.h"
8e401dbe
AK
26
27/**
28 * The buffer is always treated as read-only.
29 */
30#define BUFFER_FLAG_READONLY (1 << 0)
31/**
32 * The buffer was av_realloc()ed, so it is reallocatable.
33 */
34#define BUFFER_FLAG_REALLOCATABLE (1 << 1)
35
36struct AVBuffer {
37 uint8_t *data; /**< data described by this buffer */
38 int size; /**< size of data in bytes */
39
40 /**
41 * number of existing AVBufferRef instances referring to this buffer
42 */
43 volatile int refcount;
44
45 /**
46 * a callback for freeing the data
47 */
48 void (*free)(void *opaque, uint8_t *data);
49
50 /**
51 * an opaque pointer, to be used by the freeing callback
52 */
53 void *opaque;
54
55 /**
56 * A combination of BUFFER_FLAG_*
57 */
58 int flags;
59};
60
1cec0624
AK
61typedef struct BufferPoolEntry {
62 uint8_t *data;
63
64 /*
65 * Backups of the original opaque/free of the AVBuffer corresponding to
66 * data. They will be used to free the buffer when the pool is freed.
67 */
68 void *opaque;
69 void (*free)(void *opaque, uint8_t *data);
70
71 AVBufferPool *pool;
fbd6c97f 72 struct BufferPoolEntry *next;
1cec0624
AK
73} BufferPoolEntry;
74
75struct AVBufferPool {
fbd6c97f 76 AVMutex mutex;
77 BufferPoolEntry *pool;
1cec0624
AK
78
79 /*
80 * This is used to track when the pool is to be freed.
81 * The pointer to the pool itself held by the caller is considered to
82 * be one reference. Each buffer requested by the caller increases refcount
83 * by one, returning the buffer to the pool decreases it by one.
84 * refcount reaches zero when the buffer has been uninited AND all the
85 * buffers have been released, then it's safe to free the pool and all
86 * the buffers in it.
87 */
88 volatile int refcount;
89
90 int size;
721a4efc 91 void *opaque;
1cec0624 92 AVBufferRef* (*alloc)(int size);
721a4efc
AK
93 AVBufferRef* (*alloc2)(void *opaque, int size);
94 void (*pool_free)(void *opaque);
1cec0624
AK
95};
96
8e401dbe 97#endif /* AVUTIL_BUFFER_INTERNAL_H */