blob: 3879f871bc81dc001876320eebe1956af5615be8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
#include "fitz.h"
fz_buffer *
fz_newbuffer(int size)
{
fz_buffer *b;
b = fz_malloc(sizeof(fz_buffer));
b->refs = 1;
b->ownsdata = 1;
b->bp = fz_malloc(size);
b->rp = b->bp;
b->wp = b->bp;
b->ep = b->bp + size;
b->eof = 0;
return b;
}
fz_buffer *
fz_newbufferwithmemory(unsigned char *data, int size)
{
fz_buffer *b;
b = fz_malloc(sizeof(fz_buffer));
b->refs = 1;
b->ownsdata = 0;
b->bp = data;
b->rp = b->bp;
b->wp = b->bp + size;
b->ep = b->bp + size;
b->eof = 0;
return b;
}
fz_buffer *
fz_keepbuffer(fz_buffer *buf)
{
buf->refs ++;
return buf;
}
void
fz_dropbuffer(fz_buffer *buf)
{
if (--buf->refs == 0)
{
if (buf->ownsdata)
fz_free(buf->bp);
fz_free(buf);
}
}
void
fz_growbuffer(fz_buffer *buf)
{
int rp = buf->rp - buf->bp;
int wp = buf->wp - buf->bp;
int ep = buf->ep - buf->bp;
if (!buf->ownsdata)
{
fz_warn("assert: grow borrowed memory");
return;
}
buf->bp = fz_realloc(buf->bp, (ep * 3) / 2);
buf->rp = buf->bp + rp;
buf->wp = buf->bp + wp;
buf->ep = buf->bp + (ep * 3) / 2;
}
void
fz_rewindbuffer(fz_buffer *buf)
{
if (!buf->ownsdata)
{
fz_warn("assert: rewind borrowed memory");
return;
}
memmove(buf->bp, buf->rp, buf->wp - buf->rp);
buf->wp = buf->bp + (buf->wp - buf->rp);
buf->rp = buf->bp;
}
|