2 * This file is part of FFmpeg.
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.
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.
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
21 #include "libavcodec/packet.h"
23 #include "libavutil/common.h"
24 #include "libavutil/error.h"
25 #include "libavutil/frame.h"
26 #include "libavutil/mem.h"
32 unsigned int pool_count
;
39 ObjPool
*objpool_alloc(ObjPoolCBAlloc cb_alloc
, ObjPoolCBReset cb_reset
,
40 ObjPoolCBFree cb_free
)
42 ObjPool
*op
= av_mallocz(sizeof(*op
));
54 void objpool_free(ObjPool
**pop
)
61 for (unsigned int i
= 0; i
< op
->pool_count
; i
++)
62 op
->free(&op
->pool
[i
]);
67 int objpool_get(ObjPool
*op
, void **obj
)
70 *obj
= op
->pool
[--op
->pool_count
];
71 op
->pool
[op
->pool_count
] = NULL
;
75 return *obj
? 0 : AVERROR(ENOMEM
);
78 void objpool_release(ObjPool
*op
, void **obj
)
85 if (op
->pool_count
< FF_ARRAY_ELEMS(op
->pool
))
86 op
->pool
[op
->pool_count
++] = *obj
;
93 static void *alloc_packet(void)
95 return av_packet_alloc();
97 static void *alloc_frame(void)
99 return av_frame_alloc();
102 static void reset_packet(void *obj
)
104 av_packet_unref(obj
);
106 static void reset_frame(void *obj
)
111 static void free_packet(void **obj
)
113 AVPacket
*pkt
= *obj
;
114 av_packet_free(&pkt
);
117 static void free_frame(void **obj
)
119 AVFrame
*frame
= *obj
;
120 av_frame_free(&frame
);
124 ObjPool
*objpool_alloc_packets(void)
126 return objpool_alloc(alloc_packet
, reset_packet
, free_packet
);
128 ObjPool
*objpool_alloc_frames(void)
130 return objpool_alloc(alloc_frame
, reset_frame
, free_frame
);