saa.h 4.4 KB

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