9922
|
1 /*
|
|
2 * xbuffer code
|
|
3 *
|
|
4 * Includes a minimalistic replacement for xine_buffer functions used in
|
|
5 * Real streaming code. Only function needed by this code are implemented.
|
|
6 *
|
|
7 * Most code comes from xine_buffer.c Copyright (C) 2002 the xine project
|
|
8 *
|
|
9 * WARNING: do not mix original xine_buffer functions with this code!
|
|
10 * xbuffers behave like xine_buffers, but are not byte-compatible with them.
|
|
11 * You must take care of pointers returned by xbuffers functions (no macro to
|
|
12 * do it automatically)
|
|
13 *
|
|
14 */
|
|
15
|
|
16 #include <stdlib.h>
|
|
17 #include <inttypes.h>
|
|
18 #include "xbuffer.h"
|
|
19
|
|
20
|
|
21 typedef struct {
|
|
22 uint32_t size;
|
|
23 uint32_t chunk_size;
|
|
24 } xbuffer_header_t;
|
|
25
|
|
26 #define XBUFFER_HEADER_SIZE sizeof (xbuffer_header_t)
|
|
27
|
|
28
|
|
29
|
|
30 void *xbuffer_init(int chunk_size) {
|
|
31 uint8_t *data=calloc(1,chunk_size+XBUFFER_HEADER_SIZE);
|
|
32
|
|
33 xbuffer_header_t *header=(xbuffer_header_t*)data;
|
|
34
|
|
35 header->size=chunk_size;
|
|
36 header->chunk_size=chunk_size;
|
|
37
|
|
38 return data+XBUFFER_HEADER_SIZE;
|
|
39 }
|
|
40
|
|
41
|
|
42
|
|
43 void *xbuffer_free(void *buf) {
|
|
44 if (!buf) {
|
|
45 return NULL;
|
|
46 }
|
|
47
|
|
48 free(((uint8_t*)buf)-XBUFFER_HEADER_SIZE);
|
|
49
|
|
50 return NULL;
|
|
51 }
|
|
52
|
|
53
|
|
54
|
|
55 void *xbuffer_copyin(void *buf, int index, const void *data, int len) {
|
|
56 if (!buf || !data) {
|
|
57 return NULL;
|
|
58 }
|
|
59
|
|
60 buf = xbuffer_ensure_size(buf, index+len);
|
|
61 memcpy(((uint8_t*)buf)+index, data, len);
|
|
62
|
|
63 return buf;
|
|
64 }
|
|
65
|
|
66
|
|
67
|
|
68 void *xbuffer_ensure_size(void *buf, int size) {
|
|
69 xbuffer_header_t *xbuf;
|
|
70 int new_size;
|
|
71
|
|
72 if (!buf) {
|
|
73 return 0;
|
|
74 }
|
|
75
|
|
76 xbuf = ((xbuffer_header_t*)(((uint8_t*)buf)-XBUFFER_HEADER_SIZE));
|
|
77
|
|
78 if (xbuf->size < size) {
|
|
79 new_size = size + xbuf->chunk_size - (size % xbuf->chunk_size);
|
|
80 xbuf->size = new_size;
|
|
81 buf = ((uint8_t*)realloc(((uint8_t*)buf)-XBUFFER_HEADER_SIZE,
|
|
82 new_size+XBUFFER_HEADER_SIZE)) + XBUFFER_HEADER_SIZE;
|
|
83 }
|
|
84
|
|
85 return buf;
|
|
86 }
|