1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2017 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
38 /* Aggregate SAA components smaller than this */
39 #define SAA_BLKSHIFT 16
40 #define SAA_BLKLEN ((size_t)1 << SAA_BLKSHIFT)
42 struct SAA
*saa_init(size_t elem_len
)
47 s
= nasm_zalloc(sizeof(struct SAA
));
49 if (elem_len
>= SAA_BLKLEN
)
50 s
->blk_len
= elem_len
;
52 s
->blk_len
= SAA_BLKLEN
- (SAA_BLKLEN
% elem_len
);
54 s
->elem_len
= elem_len
;
55 s
->length
= s
->blk_len
;
56 data
= nasm_malloc(s
->blk_len
);
57 s
->nblkptrs
= s
->nblks
= 1;
58 s
->blk_ptrs
= nasm_malloc(sizeof(char *));
59 s
->blk_ptrs
[0] = data
;
60 s
->wblk
= s
->rblk
= &s
->blk_ptrs
[0];
65 void saa_free(struct SAA
*s
)
70 for (p
= s
->blk_ptrs
, n
= s
->nblks
; n
; p
++, n
--)
73 nasm_free(s
->blk_ptrs
);
77 /* Add one allocation block to an SAA */
78 static void saa_extend(struct SAA
*s
)
80 size_t blkn
= s
->nblks
++;
82 if (blkn
>= s
->nblkptrs
) {
83 size_t rindex
= s
->rblk
- s
->blk_ptrs
;
84 size_t windex
= s
->wblk
- s
->blk_ptrs
;
88 nasm_realloc(s
->blk_ptrs
, s
->nblkptrs
* sizeof(char *));
90 s
->rblk
= s
->blk_ptrs
+ rindex
;
91 s
->wblk
= s
->blk_ptrs
+ windex
;
94 s
->blk_ptrs
[blkn
] = nasm_malloc(s
->blk_len
);
95 s
->length
+= s
->blk_len
;
98 void *saa_wstruct(struct SAA
*s
)
102 nasm_assert((s
->wpos
% s
->elem_len
) == 0);
104 if (s
->wpos
+ s
->elem_len
> s
->blk_len
) {
105 nasm_assert(s
->wpos
== s
->blk_len
);
106 if (s
->wptr
+ s
->elem_len
> s
->length
)
112 p
= *s
->wblk
+ s
->wpos
;
113 s
->wpos
+= s
->elem_len
;
114 s
->wptr
+= s
->elem_len
;
116 if (s
->wptr
> s
->datalen
)
117 s
->datalen
= s
->wptr
;
122 void saa_wbytes(struct SAA
*s
, const void *data
, size_t len
)
124 const char *d
= data
;
127 size_t l
= s
->blk_len
- s
->wpos
;
132 memcpy(*s
->wblk
+ s
->wpos
, d
, l
);
135 memset(*s
->wblk
+ s
->wpos
, 0, l
);
140 if (s
->datalen
< s
->wptr
)
141 s
->datalen
= s
->wptr
;
144 if (s
->wptr
>= s
->length
)
153 * Writes a string, *including* the final null, to the specified SAA,
154 * and return the number of bytes written.
156 size_t saa_wcstring(struct SAA
*s
, const char *str
)
158 size_t bytes
= strlen(str
) + 1;
160 saa_wbytes(s
, str
, bytes
);
165 void saa_rewind(struct SAA
*s
)
167 s
->rblk
= s
->blk_ptrs
;
168 s
->rpos
= s
->rptr
= 0;
171 void *saa_rstruct(struct SAA
*s
)
175 if (s
->rptr
+ s
->elem_len
> s
->datalen
)
178 nasm_assert((s
->rpos
% s
->elem_len
) == 0);
180 if (s
->rpos
+ s
->elem_len
> s
->blk_len
) {
185 p
= *s
->rblk
+ s
->rpos
;
186 s
->rpos
+= s
->elem_len
;
187 s
->rptr
+= s
->elem_len
;
192 const void *saa_rbytes(struct SAA
*s
, size_t * lenp
)
197 if (s
->rptr
>= s
->datalen
) {
202 if (s
->rpos
>= s
->blk_len
) {
208 if (len
> s
->datalen
- s
->rptr
)
209 len
= s
->datalen
- s
->rptr
;
210 if (len
> s
->blk_len
- s
->rpos
)
211 len
= s
->blk_len
- s
->rpos
;
214 p
= *s
->rblk
+ s
->rpos
;
222 void saa_rnbytes(struct SAA
*s
, void *data
, size_t len
)
226 nasm_assert(s
->rptr
+ len
<= s
->datalen
);
233 p
= saa_rbytes(s
, &l
);
241 /* Same as saa_rnbytes, except position the counter first */
242 void saa_fread(struct SAA
*s
, size_t posn
, void *data
, size_t len
)
246 nasm_assert(posn
+ len
<= s
->datalen
);
248 if (likely(s
->blk_len
== SAA_BLKLEN
)) {
249 ix
= posn
>> SAA_BLKSHIFT
;
250 s
->rpos
= posn
& (SAA_BLKLEN
- 1);
252 ix
= posn
/ s
->blk_len
;
253 s
->rpos
= posn
% s
->blk_len
;
256 s
->rblk
= &s
->blk_ptrs
[ix
];
258 saa_rnbytes(s
, data
, len
);
261 /* Same as saa_wbytes, except position the counter first */
262 void saa_fwrite(struct SAA
*s
, size_t posn
, const void *data
, size_t len
)
266 /* Seek beyond the end of the existing array not supported */
267 nasm_assert(posn
<= s
->datalen
);
269 if (likely(s
->blk_len
== SAA_BLKLEN
)) {
270 ix
= posn
>> SAA_BLKSHIFT
;
271 s
->wpos
= posn
& (SAA_BLKLEN
- 1);
273 ix
= posn
/ s
->blk_len
;
274 s
->wpos
= posn
% s
->blk_len
;
277 s
->wblk
= &s
->blk_ptrs
[ix
];
280 s
->wpos
= s
->blk_len
;
284 saa_wbytes(s
, data
, len
);
287 void saa_fpwrite(struct SAA
*s
, FILE * fp
)
293 while (len
= s
->datalen
, (data
= saa_rbytes(s
, &len
)) != NULL
)
294 nasm_write(data
, len
, fp
);
297 void saa_write8(struct SAA
*s
, uint8_t v
)
299 saa_wbytes(s
, &v
, 1);
302 void saa_write16(struct SAA
*s
, uint16_t v
)
305 saa_wbytes(s
, &v
, 2);
308 void saa_write32(struct SAA
*s
, uint32_t v
)
311 saa_wbytes(s
, &v
, 4);
314 void saa_write64(struct SAA
*s
, uint64_t v
)
317 saa_wbytes(s
, &v
, 8);
320 void saa_writeaddr(struct SAA
*s
, uint64_t v
, size_t len
)
323 saa_wbytes(s
, &v
, len
);
326 /* write unsigned LEB128 value to SAA */
327 void saa_wleb128u(struct SAA
*psaa
, int value
)
329 char temp
[64], *ptemp
;
338 if (value
!= 0) /* more bytes to come */
343 } while (value
!= 0);
344 saa_wbytes(psaa
, temp
, len
);
347 /* write signed LEB128 value to SAA */
348 void saa_wleb128s(struct SAA
*psaa
, int value
)
350 char temp
[64], *ptemp
;
357 negative
= (value
< 0);
358 size
= sizeof(int) * 8;
365 value
|= -(1 << (size
- 7));
366 /* sign bit of byte is second high order bit (0x40) */
367 if ((value
== 0 && !(byte
& 0x40)) ||
368 ((value
== -1) && (byte
& 0x40)))
376 saa_wbytes(psaa
, temp
, len
);