Tizen 2.0 Release
[platform/core/messaging/email-service.git] / email-common-use / include / tpl.h
1 /*
2 Copyright (c) 2005-2010, Troy D. Hanson     http://tpl.sourceforge.net
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8     * Redistributions of source code must retain the above copyright
9       notice, this list of conditions and the following disclaimer.
10
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.
22 */
23
24 #ifndef TPL_H
25 #define TPL_H 
26
27 #include <stddef.h>     /* size_t */
28
29 #ifdef __INTEL_COMPILER
30 #include <tbb/tbbmalloc_proxy.h>
31 #endif /* Intel Compiler efficient memcpy etc */
32
33 #ifdef _MSC_VER
34 typedef unsigned int uint32_t;
35 #else
36 #include <inttypes.h>   /* uint32_t */
37 #endif
38
39 #if defined __cplusplus
40 extern "C" {
41 #endif
42
43 #ifdef _WIN32
44 #ifdef TPL_EXPORTS
45 #define TPL_API __declspec(dllexport)
46 #else                                                   /*  */
47 #ifdef TPL_NOLIB
48 #define TPL_API
49 #else
50 #define TPL_API __declspec(dllimport)
51 #endif /* TPL_NOLIB */
52 #endif  /* TPL_EXPORTS*/
53 #else
54 #define TPL_API __attribute__((visibility("default")))
55 #endif
56
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! */
68
69 /* flags for tpl_gather mode */
70 #define TPL_GATHER_BLOCKING    1
71 #define TPL_GATHER_NONBLOCKING 2
72 #define TPL_GATHER_MEM         3
73
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, ...);
80
81 typedef struct tpl_hook_t {
82     tpl_print_fcn *oops;
83     tpl_malloc_fcn *malloc;
84     tpl_realloc_fcn *realloc;
85     tpl_free_fcn *free;
86     tpl_fatal_fcn *fatal;
87     size_t gather_max;
88 } tpl_hook_t;
89
90 typedef struct tpl_node {
91     int type;
92     void *addr;
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 */
99 } tpl_node;
100
101 /* used when un/packing 'B' type (binary buffers) */
102 typedef struct tpl_bin {
103     void *addr;
104     uint32_t sz;
105 } tpl_bin;
106
107 /* for async/piecemeal reading of tpl images */
108 typedef struct tpl_gather_t {
109     char *img;
110     int len;
111 } tpl_gather_t;
112
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);
115
116 /* Prototypes */
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 */
127
128 #if defined __cplusplus
129     }
130 #endif
131
132 #endif /* TPL_H */
133