Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * This file is part of FFmpeg. | ||
3 | * | ||
4 | * FFmpeg 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 | * FFmpeg 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 FFmpeg; if not, write to the Free Software | ||
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
17 | */ | ||
18 | |||
19 | #include <stdint.h> | ||
20 | |||
21 | #include "libavcodec/packet.h" | ||
22 | |||
23 | #include "libavutil/common.h" | ||
24 | #include "libavutil/error.h" | ||
25 | #include "libavutil/frame.h" | ||
26 | #include "libavutil/mem.h" | ||
27 | |||
28 | #include "objpool.h" | ||
29 | |||
30 | struct ObjPool { | ||
31 | void *pool[32]; | ||
32 | unsigned int pool_count; | ||
33 | |||
34 | ObjPoolCBAlloc alloc; | ||
35 | ObjPoolCBReset reset; | ||
36 | ObjPoolCBFree free; | ||
37 | }; | ||
38 | |||
39 | 32991 | ObjPool *objpool_alloc(ObjPoolCBAlloc cb_alloc, ObjPoolCBReset cb_reset, | |
40 | ObjPoolCBFree cb_free) | ||
41 | { | ||
42 | 32991 | ObjPool *op = av_mallocz(sizeof(*op)); | |
43 | |||
44 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 32991 times.
|
32991 | if (!op) |
45 | ✗ | return NULL; | |
46 | |||
47 | 32991 | op->alloc = cb_alloc; | |
48 | 32991 | op->reset = cb_reset; | |
49 | 32991 | op->free = cb_free; | |
50 | |||
51 | 32991 | return op; | |
52 | } | ||
53 | |||
54 | 32991 | void objpool_free(ObjPool **pop) | |
55 | { | ||
56 | 32991 | ObjPool *op = *pop; | |
57 | |||
58 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 32991 times.
|
32991 | if (!op) |
59 | ✗ | return; | |
60 | |||
61 |
2/2✓ Branch 0 taken 133570 times.
✓ Branch 1 taken 32991 times.
|
166561 | for (unsigned int i = 0; i < op->pool_count; i++) |
62 | 133570 | op->free(&op->pool[i]); | |
63 | |||
64 | 32991 | av_freep(pop); | |
65 | } | ||
66 | |||
67 | 1760669 | int objpool_get(ObjPool *op, void **obj) | |
68 | { | ||
69 |
2/2✓ Branch 0 taken 1627060 times.
✓ Branch 1 taken 133609 times.
|
1760669 | if (op->pool_count) { |
70 | 1627060 | *obj = op->pool[--op->pool_count]; | |
71 | 1627060 | op->pool[op->pool_count] = NULL; | |
72 | } else | ||
73 | 133609 | *obj = op->alloc(); | |
74 | |||
75 |
1/2✓ Branch 0 taken 1760669 times.
✗ Branch 1 not taken.
|
1760669 | return *obj ? 0 : AVERROR(ENOMEM); |
76 | } | ||
77 | |||
78 | 1760669 | void objpool_release(ObjPool *op, void **obj) | |
79 | { | ||
80 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1760669 times.
|
1760669 | if (!*obj) |
81 | ✗ | return; | |
82 | |||
83 | 1760669 | op->reset(*obj); | |
84 | |||
85 |
2/2✓ Branch 0 taken 1760630 times.
✓ Branch 1 taken 39 times.
|
1760669 | if (op->pool_count < FF_ARRAY_ELEMS(op->pool)) |
86 | 1760630 | op->pool[op->pool_count++] = *obj; | |
87 | else | ||
88 | 39 | op->free(obj); | |
89 | |||
90 | 1760669 | *obj = NULL; | |
91 | } | ||
92 | |||
93 | 68510 | static void *alloc_packet(void) | |
94 | { | ||
95 | 68510 | return av_packet_alloc(); | |
96 | } | ||
97 | 65099 | static void *alloc_frame(void) | |
98 | { | ||
99 | 65099 | return av_frame_alloc(); | |
100 | } | ||
101 | |||
102 | 897985 | static void reset_packet(void *obj) | |
103 | { | ||
104 | 897985 | av_packet_unref(obj); | |
105 | 897985 | } | |
106 | 862684 | static void reset_frame(void *obj) | |
107 | { | ||
108 | 862684 | av_frame_unref(obj); | |
109 | 862684 | } | |
110 | |||
111 | 68510 | static void free_packet(void **obj) | |
112 | { | ||
113 | 68510 | AVPacket *pkt = *obj; | |
114 | 68510 | av_packet_free(&pkt); | |
115 | 68510 | *obj = NULL; | |
116 | 68510 | } | |
117 | 65099 | static void free_frame(void **obj) | |
118 | { | ||
119 | 65099 | AVFrame *frame = *obj; | |
120 | 65099 | av_frame_free(&frame); | |
121 | 65099 | *obj = NULL; | |
122 | 65099 | } | |
123 | |||
124 | 14688 | ObjPool *objpool_alloc_packets(void) | |
125 | { | ||
126 | 14688 | return objpool_alloc(alloc_packet, reset_packet, free_packet); | |
127 | } | ||
128 | 18303 | ObjPool *objpool_alloc_frames(void) | |
129 | { | ||
130 | 18303 | return objpool_alloc(alloc_frame, reset_frame, free_frame); | |
131 | } | ||
132 |