1 | /* ----------------------------------------------------------------------- * |
2 | * |
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. |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following |
9 | * conditions are met: |
10 | * |
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. |
17 | * |
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. |
31 | * |
32 | * ----------------------------------------------------------------------- */ |
33 | |
34 | #ifndef NASM_SAA_H |
35 | #define NASM_SAA_H |
36 | |
37 | #include "compiler.h" |
38 | |
39 | /* |
40 | * Routines to manage a dynamic sequential-access array, under the |
41 | * same restriction on maximum mallocable block. This array may be |
42 | * written to in two ways: a contiguous chunk can be reserved of a |
43 | * given size with a pointer returned OR single-byte data may be |
44 | * written. The array can also be read back in the same two ways: |
45 | * as a series of big byte-data blocks or as a list of structures |
46 | * of a given size. |
47 | */ |
48 | |
49 | struct SAA { |
50 | /* |
51 | * members `end' and `elem_len' are only valid in first link in |
52 | * list; `rptr' and `rpos' are used for reading |
53 | */ |
54 | size_t elem_len; /* Size of each element */ |
55 | size_t blk_len; /* Size of each allocation block */ |
56 | size_t nblks; /* Total number of allocated blocks */ |
57 | size_t nblkptrs; /* Total number of allocation block pointers */ |
58 | size_t length; /* Total allocated length of the array */ |
59 | size_t datalen; /* Total data length of the array */ |
60 | char **wblk; /* Write block pointer */ |
61 | size_t wpos; /* Write position inside block */ |
62 | size_t wptr; /* Absolute write position */ |
63 | char **rblk; /* Read block pointer */ |
64 | size_t rpos; /* Read position inside block */ |
65 | size_t rptr; /* Absolute read position */ |
66 | char **blk_ptrs; /* Pointer to pointer blocks */ |
67 | }; |
68 | |
69 | struct SAA * never_null saa_init(size_t elem_len); /* 1 == byte */ |
70 | void saa_free(struct SAA *); |
71 | void *saa_wstruct(struct SAA *); /* return a structure of elem_len */ |
72 | void saa_wbytes(struct SAA *, const void *, size_t); /* write arbitrary bytes */ |
73 | size_t saa_wcstring(struct SAA *s, const char *str); /* write a C string */ |
74 | void saa_rewind(struct SAA *); /* for reading from beginning */ |
75 | void *saa_rstruct(struct SAA *); /* return NULL on EOA */ |
76 | const void *saa_rbytes(struct SAA *, size_t *); /* return 0 on EOA */ |
77 | void saa_rnbytes(struct SAA *, void *, size_t); /* read a given no. of bytes */ |
78 | /* random access */ |
79 | void saa_fread(struct SAA *, size_t, void *, size_t); |
80 | void saa_fwrite(struct SAA *, size_t, const void *, size_t); |
81 | |
82 | /* dump to file */ |
83 | void saa_fpwrite(struct SAA *, FILE *); |
84 | |
85 | /* Write specific-sized values */ |
86 | void saa_write8(struct SAA *s, uint8_t v); |
87 | void saa_write16(struct SAA *s, uint16_t v); |
88 | void saa_write32(struct SAA *s, uint32_t v); |
89 | void saa_write64(struct SAA *s, uint64_t v); |
90 | void saa_wleb128u(struct SAA *, int); /* write unsigned LEB128 value */ |
91 | void saa_wleb128s(struct SAA *, int); /* write signed LEB128 value */ |
92 | void saa_writeaddr(struct SAA *, uint64_t, size_t); |
93 | |
94 | #endif /* NASM_SAA_H */ |
95 | |