tpl.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. Copyright (c) 2005-2013, Troy D. Hanson http://troydhanson.github.com/tpl/
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  9. IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  10. TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  11. PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  12. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  13. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  14. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  15. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  16. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  17. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  18. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  19. */
  20. #ifndef TPL_H
  21. #define TPL_H
  22. #include <stddef.h> /* size_t */
  23. #include <stdarg.h> /* va_list */
  24. #ifdef __INTEL_COMPILER
  25. #include <tbb/tbbmalloc_proxy.h>
  26. #endif /* Intel Compiler efficient memcpy etc */
  27. #ifdef _MSC_VER
  28. typedef unsigned int uint32_t;
  29. #else
  30. #include <inttypes.h> /* uint32_t */
  31. #endif
  32. #if defined __cplusplus
  33. extern "C" {
  34. #endif
  35. #ifdef _WIN32
  36. #ifdef TPL_EXPORTS
  37. #define TPL_API __declspec(dllexport)
  38. #else /* */
  39. #ifdef TPL_NOLIB
  40. #define TPL_API
  41. #else
  42. #define TPL_API __declspec(dllimport)
  43. #endif /* TPL_NOLIB */
  44. #endif /* TPL_EXPORTS*/
  45. #else
  46. #define TPL_API
  47. #endif
  48. /* bit flags (external) */
  49. #define TPL_FILE (1 << 0)
  50. #define TPL_MEM (1 << 1)
  51. #define TPL_PREALLOCD (1 << 2)
  52. #define TPL_EXCESS_OK (1 << 3)
  53. #define TPL_FD (1 << 4)
  54. #define TPL_UFREE (1 << 5)
  55. #define TPL_DATAPEEK (1 << 6)
  56. #define TPL_FXLENS (1 << 7)
  57. #define TPL_GETSIZE (1 << 8)
  58. /* do not add flags here without renumbering the internal flags! */
  59. /* flags for tpl_gather mode */
  60. #define TPL_GATHER_BLOCKING 1
  61. #define TPL_GATHER_NONBLOCKING 2
  62. #define TPL_GATHER_MEM 3
  63. /* Hooks for error logging, memory allocation functions and fatal */
  64. typedef int (tpl_print_fcn)(const char *fmt, ...);
  65. typedef void *(tpl_malloc_fcn)(size_t sz);
  66. typedef void *(tpl_realloc_fcn)(void *ptr, size_t sz);
  67. typedef void (tpl_free_fcn)(void *ptr);
  68. typedef void (tpl_fatal_fcn)(const char *fmt, ...);
  69. typedef struct tpl_hook_t {
  70. tpl_print_fcn *oops;
  71. tpl_malloc_fcn *malloc;
  72. tpl_realloc_fcn *realloc;
  73. tpl_free_fcn *free;
  74. tpl_fatal_fcn *fatal;
  75. size_t gather_max;
  76. } tpl_hook_t;
  77. typedef struct tpl_node {
  78. int type;
  79. void *addr;
  80. void *data; /* r:tpl_root_data*. A:tpl_atyp*. ow:szof type */
  81. int num; /* length of type if its a C array */
  82. size_t ser_osz; /* serialization output size for subtree */
  83. struct tpl_node *children; /* my children; linked-list */
  84. struct tpl_node *next,*prev; /* my siblings (next child of my parent) */
  85. struct tpl_node *parent; /* my parent */
  86. } tpl_node;
  87. /* used when un/packing 'B' type (binary buffers) */
  88. typedef struct tpl_bin {
  89. void *addr;
  90. uint32_t sz;
  91. } tpl_bin;
  92. /* for async/piecemeal reading of tpl images */
  93. typedef struct tpl_gather_t {
  94. char *img;
  95. int len;
  96. } tpl_gather_t;
  97. /* Callback used when tpl_gather has read a full tpl image */
  98. typedef int (tpl_gather_cb)(void *img, size_t sz, void *data);
  99. /* Prototypes */
  100. TPL_API tpl_node *tpl_map(char *fmt,...); /* define tpl using format */
  101. TPL_API void tpl_free(tpl_node *r); /* free a tpl map */
  102. TPL_API int tpl_pack(tpl_node *r, int i); /* pack the n'th packable */
  103. TPL_API int tpl_unpack(tpl_node *r, int i); /* unpack the n'th packable */
  104. TPL_API int tpl_dump(tpl_node *r, int mode, ...); /* serialize to mem/file */
  105. TPL_API int tpl_load(tpl_node *r, int mode, ...); /* set mem/file to unpack */
  106. TPL_API int tpl_Alen(tpl_node *r, int i); /* array len of packable i */
  107. TPL_API char* tpl_peek(int mode, ...); /* sneak peek at format string */
  108. TPL_API int tpl_gather( int mode, ...); /* non-blocking image gather */
  109. TPL_API int tpl_jot(int mode, ...); /* quick write a simple tpl */
  110. TPL_API tpl_node *tpl_map_va(char *fmt, va_list ap);
  111. #if defined __cplusplus
  112. }
  113. #endif
  114. #endif /* TPL_H */