Move all the SAA code out of nasmlib
[platform/upstream/nasm.git] / saa.h
1 #ifndef NASM_SAA_H
2 #define NASM_SAA_H
3
4 #include "compiler.h"
5 #include "nasmlib.h"
6
7 /*
8  * Routines to manage a dynamic sequential-access array, under the
9  * same restriction on maximum mallocable block. This array may be
10  * written to in two ways: a contiguous chunk can be reserved of a
11  * given size with a pointer returned OR single-byte data may be
12  * written. The array can also be read back in the same two ways:
13  * as a series of big byte-data blocks or as a list of structures
14  * of a given size.
15  */
16
17 struct SAA {
18     /*
19      * members `end' and `elem_len' are only valid in first link in
20      * list; `rptr' and `rpos' are used for reading
21      */
22     size_t elem_len;            /* Size of each element */
23     size_t blk_len;             /* Size of each allocation block */
24     size_t nblks;               /* Total number of allocated blocks */
25     size_t nblkptrs;            /* Total number of allocation block pointers */
26     size_t length;              /* Total allocated length of the array */
27     size_t datalen;             /* Total data length of the array */
28     char **wblk;                /* Write block pointer */
29     size_t wpos;                /* Write position inside block */
30     size_t wptr;                /* Absolute write position */
31     char **rblk;                /* Read block pointer */
32     size_t rpos;                /* Read position inside block */
33     size_t rptr;                /* Absolute read position */
34     char **blk_ptrs;            /* Pointer to pointer blocks */
35 };
36
37 struct SAA *saa_init(size_t elem_len);    /* 1 == byte */
38 void saa_free(struct SAA *);
39 void *saa_wstruct(struct SAA *);        /* return a structure of elem_len */
40 void saa_wbytes(struct SAA *, const void *, size_t);      /* write arbitrary bytes */
41 void saa_rewind(struct SAA *);  /* for reading from beginning */
42 void *saa_rstruct(struct SAA *);        /* return NULL on EOA */
43 const void *saa_rbytes(struct SAA *, size_t *); /* return 0 on EOA */
44 void saa_rnbytes(struct SAA *, void *, size_t);   /* read a given no. of bytes */
45 /* random access */
46 void saa_fread(struct SAA *, size_t, void *, size_t);
47 void saa_fwrite(struct SAA *, size_t, const void *, size_t);
48
49 /* dump to file */
50 void saa_fpwrite(struct SAA *, FILE *);
51
52 /* Write specific-sized values */
53 void saa_write8(struct SAA *s, uint8_t v);
54 void saa_write16(struct SAA *s, uint16_t v);
55 void saa_write32(struct SAA *s, uint32_t v);
56 void saa_write64(struct SAA *s, uint64_t v);
57 void saa_wleb128u(struct SAA *, int); /* write unsigned LEB128 value */
58 void saa_wleb128s(struct SAA *, int); /* write signed LEB128 value */
59
60 #endif /* NASM_SAA_H */