1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2009 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
)
152 void saa_rewind(struct SAA
*s
)
154 s
->rblk
= s
->blk_ptrs
;
155 s
->rpos
= s
->rptr
= 0;
158 void *saa_rstruct(struct SAA
*s
)
162 if (s
->rptr
+ s
->elem_len
> s
->datalen
)
165 nasm_assert((s
->rpos
% s
->elem_len
) == 0);
167 if (s
->rpos
+ s
->elem_len
> s
->blk_len
) {
172 p
= *s
->rblk
+ s
->rpos
;
173 s
->rpos
+= s
->elem_len
;
174 s
->rptr
+= s
->elem_len
;
179 const void *saa_rbytes(struct SAA
*s
, size_t * lenp
)
184 if (s
->rptr
>= s
->datalen
) {
189 if (s
->rpos
>= s
->blk_len
) {
195 if (len
> s
->datalen
- s
->rptr
)
196 len
= s
->datalen
- s
->rptr
;
197 if (len
> s
->blk_len
- s
->rpos
)
198 len
= s
->blk_len
- s
->rpos
;
201 p
= *s
->rblk
+ s
->rpos
;
209 void saa_rnbytes(struct SAA
*s
, void *data
, size_t len
)
213 nasm_assert(s
->rptr
+ len
<= s
->datalen
);
220 p
= saa_rbytes(s
, &l
);
228 /* Same as saa_rnbytes, except position the counter first */
229 void saa_fread(struct SAA
*s
, size_t posn
, void *data
, size_t len
)
233 nasm_assert(posn
+ len
<= s
->datalen
);
235 if (likely(s
->blk_len
== SAA_BLKLEN
)) {
236 ix
= posn
>> SAA_BLKSHIFT
;
237 s
->rpos
= posn
& (SAA_BLKLEN
- 1);
239 ix
= posn
/ s
->blk_len
;
240 s
->rpos
= posn
% s
->blk_len
;
243 s
->rblk
= &s
->blk_ptrs
[ix
];
245 saa_rnbytes(s
, data
, len
);
248 /* Same as saa_wbytes, except position the counter first */
249 void saa_fwrite(struct SAA
*s
, size_t posn
, const void *data
, size_t len
)
253 /* Seek beyond the end of the existing array not supported */
254 nasm_assert(posn
<= s
->datalen
);
256 if (likely(s
->blk_len
== SAA_BLKLEN
)) {
257 ix
= posn
>> SAA_BLKSHIFT
;
258 s
->wpos
= posn
& (SAA_BLKLEN
- 1);
260 ix
= posn
/ s
->blk_len
;
261 s
->wpos
= posn
% s
->blk_len
;
264 s
->wblk
= &s
->blk_ptrs
[ix
];
267 s
->wpos
= s
->blk_len
;
271 saa_wbytes(s
, data
, len
);
274 void saa_fpwrite(struct SAA
*s
, FILE * fp
)
280 while (len
= s
->datalen
, (data
= saa_rbytes(s
, &len
)) != NULL
)
281 fwrite(data
, 1, len
, fp
);
284 void saa_write8(struct SAA
*s
, uint8_t v
)
286 saa_wbytes(s
, &v
, 1);
289 #ifdef WORDS_LITTEENDIAN
291 void saa_write16(struct SAA
*s
, uint16_t v
)
293 saa_wbytes(s
, &v
, 2);
296 void saa_write32(struct SAA
*s
, uint32_t v
)
298 saa_wbytes(s
, &v
, 4);
301 void saa_write64(struct SAA
*s
, uint64_t v
)
303 saa_wbytes(s
, &v
, 8);
306 void saa_writeaddr(struct SAA
*s
, uint64_t v
, size_t len
)
308 saa_wbytes(s
, &v
, len
);
311 #else /* not WORDS_LITTLEENDIAN */
313 void saa_write16(struct SAA
*s
, uint16_t v
)
322 void saa_write32(struct SAA
*s
, uint32_t v
)
333 void saa_write64(struct SAA
*s
, uint64_t v
)
349 void saa_writeaddr(struct SAA
*s
, uint64_t v
, size_t len
)
362 saa_wbytes(s
, b
, len
);
365 #endif /* WORDS_LITTLEENDIAN */
367 /* write unsigned LEB128 value to SAA */
368 void saa_wleb128u(struct SAA
*psaa
, int value
)
370 char temp
[64], *ptemp
;
379 if (value
!= 0) /* more bytes to come */
384 } while (value
!= 0);
385 saa_wbytes(psaa
, temp
, len
);
388 /* write signed LEB128 value to SAA */
389 void saa_wleb128s(struct SAA
*psaa
, int value
)
391 char temp
[64], *ptemp
;
398 negative
= (value
< 0);
399 size
= sizeof(int) * 8;
406 value
|= -(1 << (size
- 7));
407 /* sign bit of byte is second high order bit (0x40) */
408 if ((value
== 0 && !(byte
& 0x40)) ||
409 ((value
== -1) && (byte
& 0x40)))
417 saa_wbytes(psaa
, temp
, len
);