1 //
2 // Copyright 2020 Serge Martin
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a
5 // copy of this software and associated documentation files (the "Software"),
6 // to deal in the Software without restriction, including without limitation
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 // and/or sell copies of the Software, and to permit persons to whom the
9 // Software is furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 // OTHER DEALINGS IN THE SOFTWARE.
21 //
22 #ifndef U_PRINTF_H
23 #define U_PRINTF_H
24
25 #include <stdarg.h>
26 #include <stddef.h>
27 #include <stdio.h>
28 #include "simple_mtx.h"
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34 typedef struct u_printf_info {
35 unsigned num_args;
36 unsigned *arg_sizes;
37 unsigned string_size;
38 char *strings;
39 } u_printf_info;
40
41 struct blob;
42 struct blob_reader;
43
44 void u_printf_serialize_info(struct blob *blob,
45 const u_printf_info *info,
46 unsigned printf_info_count);
47
48 u_printf_info *u_printf_deserialize_info(void *mem_ctx,
49 struct blob_reader *blob,
50 unsigned *printf_info_count);
51
52 uint32_t u_printf_hash(const u_printf_info *info);
53
54 void u_printf_singleton_init_or_ref(void);
55 void u_printf_singleton_decref(void);
56 void u_printf_singleton_add(const u_printf_info *info, unsigned count);
57 void u_printf_singleton_add_serialized(const void *data, size_t data_size);
58 const u_printf_info *u_printf_singleton_search(uint32_t hash);
59
60 struct u_printf_ctx {
61 simple_mtx_t lock;
62 void *bo;
63 uint32_t *map;
64 };
65
66 const char *util_printf_prev_tok(const char *str);
67
68 /* find next valid printf specifier in a C string wrapper */
69 size_t util_printf_next_spec_pos(const char *str, size_t pos);
70
71 /* Return the length of the string that would be generated by a printf-style
72 * format and argument list, not including the \0 byte.
73 * The untouched_args parameter is left untouched so it can be re-used by the
74 * caller in a vsnprintf() call or similar.
75 */
76 size_t u_printf_length(const char *fmt, va_list untouched_args);
77
78 void u_printf(FILE *out, const char *buffer, size_t buffer_size,
79 const u_printf_info*, unsigned info_size);
80
81 void u_printf_ptr(FILE *out, const char *buffer, size_t buffer_size,
82 const u_printf_info **info, unsigned info_size);
83
84 static inline void
u_printf_init(struct u_printf_ctx * ctx,void * bo,uint32_t * map)85 u_printf_init(struct u_printf_ctx *ctx, void *bo, uint32_t *map)
86 {
87 ctx->bo = bo;
88 ctx->map = map;
89 simple_mtx_init(&ctx->lock, mtx_plain);
90
91 /* Initialize the buffer head to point to just after the size + abort word */
92 map[0] = 8;
93
94 /* Initially there is no abort */
95 map[1] = 0;
96 }
97
98 static inline void
u_printf_destroy(struct u_printf_ctx * ctx)99 u_printf_destroy(struct u_printf_ctx *ctx)
100 {
101 simple_mtx_destroy(&ctx->lock);
102 }
103
104 static inline void
u_printf_with_ctx(FILE * out,struct u_printf_ctx * ctx)105 u_printf_with_ctx(FILE *out, struct u_printf_ctx *ctx)
106 {
107 /* If the printf buffer is empty, early-exit without taking the lock. The
108 * speeds up the happy path and makes this function reasonable to call even
109 * in release builds.
110 */
111 if (ctx->map[0] == 8)
112 return;
113
114 simple_mtx_lock(&ctx->lock);
115 u_printf(out, (char *)(ctx->map + 2), ctx->map[0] - 8, NULL, 0);
116
117 /* Reset */
118 ctx->map[0] = 8;
119 simple_mtx_unlock(&ctx->lock);
120 }
121
122 /*
123 * Flush the printf buffer and return whether there was an abort. This is
124 * intended to be called periodically to handle aborts in a timely manner.
125 */
126 static inline bool
u_printf_check_abort(FILE * out,struct u_printf_ctx * ctx)127 u_printf_check_abort(FILE *out, struct u_printf_ctx *ctx)
128 {
129 u_printf_with_ctx(out, ctx);
130
131 /* Check the aborted flag */
132 return (ctx->map[1] != 0);
133 }
134
135 #ifdef __cplusplus
136 }
137 #endif
138
139 #endif
140