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