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