summaryrefslogtreecommitdiff
path: root/Build/source/texk/web2c/luatexdir/luapplib/src/util/utilmemheapiof.c
blob: f459780df3e5b4d98498255e70473749202782e0 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192

#include "utilmemheapiof.h"

// this is identical to stock iof suite, keep in sync

static size_t heap8_writer (iof *O, iof_mode mode)
{
  heap8 *heap;
  size_t written;
  heap = (heap8 *)O->link;
  switch (mode)
  {
    case IOFFLUSH:
      written = (size_t)iof_size(O);
      heap8_done(heap, O->buf, written);
      O->buf = _heap8_some(heap, 0, &O->space);
      O->pos = O->buf;
      O->end = O->buf + O->space;
      break;
    case IOFWRITE:
      written = (size_t)iof_size(O);
      O->space = written << 1;
      O->buf = heap8_more(heap, O->buf, written, O->space);
      O->pos = O->buf + written;
      O->end = O->buf + O->space;
      return written; // eq (space - written)
    case IOFCLOSE:
    default:
      break;
  }
  return 0;
}

static size_t heap16_writer (iof *O, iof_mode mode)
{
  heap16 *heap;
  size_t written;
  heap = (heap16 *)O->link;
  switch (mode)
  {
    case IOFFLUSH:
      written = (size_t)iof_size(O);
      heap16_done(heap, O->buf, written);
      O->buf = _heap16_some(heap, 0, &O->space);
      O->pos = O->buf;
      O->end = O->buf + O->space;
      break;
    case IOFWRITE:
      written = (size_t)iof_size(O);
      O->space = written << 1;
      O->buf = heap16_more(heap, O->buf, written, O->space);
      O->pos = O->buf + written;
      O->end = O->buf + O->space;
      return written; // eq (space - written)
    case IOFCLOSE:
    default:
      break;
  }
  return 0;
}

static size_t heap32_writer (iof *O, iof_mode mode)
{
  heap32 *heap;
  size_t written;
  heap = (heap32 *)O->link;
  switch (mode)
  {
    case IOFFLUSH:
      written = (size_t)iof_size(O);
      heap32_done(heap, O->buf, written);
      O->buf = _heap32_some(heap, 0, &O->space);
      O->pos = O->buf;
      O->end = O->buf + O->space;
      break;
    case IOFWRITE:
      written = (size_t)iof_size(O);
      O->space = written << 1;
      O->buf = heap32_more(heap, O->buf, written, O->space);
      O->pos = O->buf + written;
      O->end = O->buf + O->space;
      return written; // eq (space - written)
    case IOFCLOSE:
    default:
      break;
  }
  return 0;
}

static size_t heap64_writer (iof *O, iof_mode mode)
{
  heap64 *heap;
  size_t written;
  heap = (heap64 *)O->link;
  switch (mode)
  {
    case IOFFLUSH:
      written = (size_t)iof_size(O);
      heap64_done(heap, O->buf, written);
      O->buf = _heap64_some(heap, 0, &O->space);
      O->pos = O->buf;
      O->end = O->buf + O->space;
      break;
    case IOFWRITE:
      written = (size_t)iof_size(O);
      O->space = written << 1;
      O->buf = heap64_more(heap, O->buf, written, O->space);
      O->pos = O->buf + written;
      O->end = O->buf + O->space;
      return written; // eq (space - written)
    case IOFCLOSE:
    default:
      break;
  }
  return 0;
}

/* buffer init (made once) */

iof * heap8_buffer_init (heap8 *heap, iof *O)
{
  void *data;
  size_t space;
  data = heap8_some(heap, 0, &space);
  if (iof_writer(O, (void *)heap, heap8_writer, data, space) == NULL) // sanity
    return NULL;
  return O;
}

iof * heap16_buffer_init (heap16 *heap, iof *O)
{
  void *data;
  size_t space;
  data = heap16_some(heap, 0, &space);
  if (iof_writer(O, (void *)heap, heap16_writer, data, space) == NULL)
    return NULL;
  return O;
}

iof * heap32_buffer_init (heap32 *heap, iof *O)
{
  void *data;
  size_t space;
  data = heap32_some(heap, 0, &space);
  if (iof_writer(O, (void *)heap, heap32_writer, data, space) == NULL)
    return NULL;
  return O;
}

iof * heap64_buffer_init (heap64 *heap, iof *O)
{
  void *data;
  size_t space;
  data = heap64_some(heap, 0, &space);
  if (iof_writer(O, (void *)heap, heap64_writer, data, space) == NULL)
    return NULL;
  return O;
}

/* buffer for some */

iof * heap8_buffer_some (heap8 *heap, iof *O, size_t atleast)
{
  O->buf = _heap8_some(heap, atleast, &O->space);
  O->pos = O->buf;
  O->end = O->buf + O->space;
  return O;
}

iof * heap16_buffer_some (heap16 *heap, iof *O, size_t atleast)
{
  O->buf = _heap16_some(heap, atleast, &O->space);
  O->pos = O->buf;
  O->end = O->buf + O->space;
  return O;
}

iof * heap32_buffer_some (heap32 *heap, iof *O, size_t atleast)
{
  O->buf = _heap32_some(heap, atleast, &O->space);
  O->pos = O->buf;
  O->end = O->buf + O->space;
  return O;
}

iof * heap64_buffer_some (heap64 *heap, iof *O, size_t atleast)
{
  O->buf = _heap64_some(heap, atleast, &O->space);
  O->pos = O->buf;
  O->end = O->buf + O->space;
  return O;
}