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>
|
10232
|
17 #include <string.h>
|
9922
|
18 #include <inttypes.h>
|
|
19 #include "xbuffer.h"
|
|
20
|
|
21
|
|
22 typedef struct {
|
|
23 uint32_t size;
|
|
24 uint32_t chunk_size;
|
|
25 } xbuffer_header_t;
|
|
26
|
|
27 #define XBUFFER_HEADER_SIZE sizeof (xbuffer_header_t)
|
|
28
|
|
29
|
|
30
|
|
31 void *xbuffer_init(int chunk_size) {
|
|
32 uint8_t *data=calloc(1,chunk_size+XBUFFER_HEADER_SIZE);
|
|
33
|
|
34 xbuffer_header_t *header=(xbuffer_header_t*)data;
|
|
35
|
|
36 header->size=chunk_size;
|
|
37 header->chunk_size=chunk_size;
|
|
38
|
|
39 return data+XBUFFER_HEADER_SIZE;
|
|
40 }
|
|
41
|
|
42
|
|
43
|
|
44 void *xbuffer_free(void *buf) {
|
|
45 if (!buf) {
|
|
46 return NULL;
|
|
47 }
|
|
48
|
|
49 free(((uint8_t*)buf)-XBUFFER_HEADER_SIZE);
|
|
50
|
|
51 return NULL;
|
|
52 }
|
|
53
|
|
54
|
|
55
|
|
56 void *xbuffer_copyin(void *buf, int index, const void *data, int len) {
|
|
57 if (!buf || !data) {
|
|
58 return NULL;
|
|
59 }
|
|
60
|
|
61 buf = xbuffer_ensure_size(buf, index+len);
|
|
62 memcpy(((uint8_t*)buf)+index, data, len);
|
|
63
|
|
64 return buf;
|
|
65 }
|
|
66
|
|
67
|
|
68
|
|
69 void *xbuffer_ensure_size(void *buf, int size) {
|
|
70 xbuffer_header_t *xbuf;
|
|
71 int new_size;
|
|
72
|
|
73 if (!buf) {
|
|
74 return 0;
|
|
75 }
|
|
76
|
|
77 xbuf = ((xbuffer_header_t*)(((uint8_t*)buf)-XBUFFER_HEADER_SIZE));
|
|
78
|
|
79 if (xbuf->size < size) {
|
|
80 new_size = size + xbuf->chunk_size - (size % xbuf->chunk_size);
|
|
81 xbuf->size = new_size;
|
|
82 buf = ((uint8_t*)realloc(((uint8_t*)buf)-XBUFFER_HEADER_SIZE,
|
|
83 new_size+XBUFFER_HEADER_SIZE)) + XBUFFER_HEADER_SIZE;
|
|
84 }
|
|
85
|
|
86 return buf;
|
|
87 }
|