-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy paththe_debuginator_queue.h
349 lines (288 loc) · 13.6 KB
/
the_debuginator_queue.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
the_debuginator_queue.h - v0.01 - public domain - Anders Elfgren @srekel, 2018
# THE DEBUGINATOR QUEUE
A utility library to enable simple multi-threading support for The Debuginator.
See github for latest version: https://github.com/Srekel/the-debuginator
## Usage
In *ONE* source file, put:
```C
#define DEBUGINATOR_QUEUE_IMPLEMENTATION
// Define any of these if you wish to override them.
// (There are more. Find them in the beginning of the code.)
#define DEBUGINATOR_assert
#define DEBUGINATOR_memcpy
#define DEBUGINATOR_fabs
#include "the_debuginator.h"
#include "the_debuginator_queue.h"
```
Depending on if its in a binary that has actual access to the debuginator,
also #define DEBUGINATOR_QUEUE_CAN_PROCEES
Other source files should just include the_debuginator_queue.h
## Notes
See the documentation on the github page.
## License
Basically Public Domain / MIT.
See end of file for license information.
*/
#ifndef INCLUDE_THE_DEBUGINATOR_QUEUE_H
#define INCLUDE_THE_DEBUGINATOR_QUEUE_H
#include "the_debuginator.h"
#ifdef __cplusplus
extern "C" {
#endif
#ifndef DEBUGINATOR_ENABLE_WARNINGS
#ifdef _MSC_VER
#pragma warning( push, 0 )
// #pragma warning( disable: 4820 4201)
#endif
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wold-style-cast"
#pragma clang diagnostic ignored "-Wsign-conversion"
#pragma clang diagnostic ignored "-Wunused-value"
#pragma clang diagnostic ignored "-Wcomma"
#pragma clang diagnostic ignored "-Wcast-align"
#pragma clang diagnostic ignored "-Wswitch-enum"
#endif
#endif
#define DEBUGINATOR_QUEUE_MAX_PATH_LENGTH 128
#define DEBUGINATOR_QUEUE_MAX_DESCRIPTION_LENGTH 512
typedef enum DebuginatorQueueItemTypes {
DEBUGINATOR_QUEUE_CreateItem,
DEBUGINATOR_QUEUE_NumItemTypes
} DebuginatorQueueItemTypes;
typedef struct {
char path[DEBUGINATOR_QUEUE_MAX_PATH_LENGTH];
char description[DEBUGINATOR_QUEUE_MAX_DESCRIPTION_LENGTH];
void* userdata;
DebuginatorOnItemChangedCallback callback;
} DebuginatorQueue_CreateItemData;
typedef struct DebuginatorQueueItem {
DebuginatorQueueItemTypes type;
union {
DebuginatorQueue_CreateItemData create_item;
} data;
} DebuginatorQueueItem;
typedef struct TheDebuginatorQueue TheDebuginatorQueue;
typedef void* ( *DebuginatorQueueAllocateFunc )( void* userdata, int bytes );
typedef void ( *DebuginatorQueueDellocateFunc )( void* userdata, void* ptr );
typedef void ( *DebuginatorQueueItemCleanupFunc )( TheDebuginatorQueue* queue,
DebuginatorQueueItem* item );
typedef struct TheDebuginatorQueue {
void* userdata;
DebuginatorQueueItem* items;
int num_items;
int capacity;
DebuginatorQueueAllocateFunc allocate;
DebuginatorQueueDellocateFunc deallocate;
DebuginatorQueueItemCleanupFunc cleanup_funcs[DEBUGINATOR_QUEUE_NumItemTypes];
} TheDebuginatorQueue;
TheDebuginatorQueue* debuginator_queue_create( int initial_size,
DebuginatorQueueAllocateFunc allocate_func,
DebuginatorQueueDellocateFunc deallocate_func,
void* userdata );
unsigned char* debuginator_queue_data( TheDebuginatorQueue* queue, int* out_size );
void debuginator_queue_clear( TheDebuginatorQueue* queue );
void debuginator_queue_process( const unsigned char* data, int size, TheDebuginator* debuginator );
void debuginator_queue_create_bool_item( TheDebuginatorQueue* queue,
const char* path,
const char* description,
void* userdata );
void debuginator_queue_create_bool_item_with_callback( TheDebuginatorQueue* queue,
const char* path,
const char* description,
void* userdata,
DebuginatorOnItemChangedCallback callback );
#ifdef __cplusplus
}
#endif
#ifdef DEBUGINATOR_QUEUE_IMPLEMENTATION
#ifndef DEBUGINATOR_QUEUE_assert
#include <assert.h>
#define DEBUGINATOR_QUEUE_assert assert;
#endif
#ifndef DEBUGINATOR_QUEUE_memcpy
#include <string.h>
#define DEBUGINATOR_QUEUE_memcpy memcpy
#endif
#ifndef DEBUGINATOR_QUEUE_strcpy_s
#include <string.h>
#define DEBUGINATOR_QUEUE_strcpy_s strcpy_s
#endif
static void
debuginator_queue__ensure_capacity( TheDebuginatorQueue* queue ) {
if ( queue->capacity + 1 == queue->num_items ) {
int bytes = sizeof( DebuginatorQueueItem ) * queue->num_items * 2;
void* items = queue->allocate( queue->userdata, bytes );
DEBUGINATOR_QUEUE_memcpy( items, queue->items, bytes );
queue->deallocate( queue->userdata, queue->items );
queue->items = (DebuginatorQueueItem*)items;
queue->capacity = queue->num_items * 2;
}
}
// static const char*
// debuginator_queue__copy_string( TheDebuginatorQueue* queue, const char* str ) {
// int bytes = strlen( str );
// void* buffer = queue->allocate( queue->userdata, bytes );
// DEBUGINATOR_QUEUE_memcpy( buffer, str, bytes );
// return (const char*)buffer;
// }
void
debuginator_queue_clear( TheDebuginatorQueue* queue ) {
// for ( int i_item = 0; i_item < queue->num_items; ++i_item ) {
// DebuginatorQueueItem* item = &queue->items[i_item];
// queue->cleanup_funcs[item->type]( queue, item );
// }
queue->num_items = 0;
}
unsigned char*
debuginator_queue_data( TheDebuginatorQueue* queue, int* out_size ) {
*out_size = queue->num_items * sizeof( DebuginatorQueueItem );
return (unsigned char*)queue->items;
}
void
debuginator_queue_create_bool_item( TheDebuginatorQueue* queue,
const char* path,
const char* description,
void* userdata ) {
debuginator_queue__ensure_capacity( queue );
DebuginatorQueue_CreateItemData data;
DEBUGINATOR_QUEUE_strcpy_s( data.path, DEBUGINATOR_QUEUE_MAX_PATH_LENGTH, path );
DEBUGINATOR_QUEUE_strcpy_s(
data.description, DEBUGINATOR_QUEUE_MAX_DESCRIPTION_LENGTH, description );
data.userdata = userdata;
DebuginatorQueueItem* item = &queue->items[queue->num_items++];
item->type = DEBUGINATOR_QUEUE_CreateItem;
item->data.create_item = data;
}
void
debuginator_queue_create_bool_item_with_callback( TheDebuginatorQueue* queue,
const char* path,
const char* description,
void* userdata,
DebuginatorOnItemChangedCallback callback ) {
debuginator_queue__ensure_capacity( queue );
DebuginatorQueue_CreateItemData data;
DEBUGINATOR_QUEUE_strcpy_s( data.path, DEBUGINATOR_QUEUE_MAX_PATH_LENGTH, path );
DEBUGINATOR_QUEUE_strcpy_s(
data.description, DEBUGINATOR_QUEUE_MAX_DESCRIPTION_LENGTH, description );
data.userdata = userdata;
data.callback = callback;
DebuginatorQueueItem* item = &queue->items[queue->num_items++];
item->type = DEBUGINATOR_QUEUE_CreateItem;
item->data.create_item = data;
}
// void
// debuginator_queue__create_bool_item_cleanup( TheDebuginatorQueue* queue,
// DebuginatorQueueItem* item ) {
// DebuginatorQueue_CreateItemData* data = (DebuginatorQueue_CreateItemData*)item->data;
// queue->deallocate( queue->userdata, (void*)data->path );
// queue->deallocate( queue->userdata, (void*)data->description );
// queue->deallocate( queue->userdata, (void*)item->data );
// }
void
debuginator_queue_process( const unsigned char* data, int size, TheDebuginator* debuginator ) {
(void)( data, size, debuginator );
#ifdef DEBUGINATOR_QUEUE_CAN_PROCEES
const void* data_end = data + size;
const DebuginatorQueueItem* item = (DebuginatorQueueItem*)(unsigned long long)data;
while ( item < data_end ) {
switch ( item->type ) {
case DEBUGINATOR_QUEUE_CreateItem: {
// debuginator_create_array_item(debuginator, NULL, item->data.create_item.path,
// description, item->data.create_item.callback, item->data.create_item.userdata,
// debuginator->bool_titles, debuginator->bool_values, 2,
// sizeof(debuginator->bool_values[0]));
if ( item->data.create_item.callback == NULL ) {
debuginator_create_bool_item( debuginator,
item->data.create_item.path,
item->data.create_item.description,
item->data.create_item.userdata );
}
else {
debuginator_create_bool_item_with_callback( debuginator,
item->data.create_item.path,
item->data.create_item.description,
item->data.create_item.userdata,
item->data.create_item.callback );
}
} break;
default:
break;
}
item++;
}
DEBUGINATOR_QUEUE_assert( item == data_end );
#endif
}
TheDebuginatorQueue*
debuginator_queue_create( int initial_size,
DebuginatorQueueAllocateFunc allocate_func,
DebuginatorQueueDellocateFunc deallocate_func,
void* userdata ) {
int buffer_bytes = sizeof( TheDebuginatorQueue );
int item_bytes = sizeof( DebuginatorQueueItem ) * initial_size;
void* queue_buffer = allocate_func( userdata, buffer_bytes );
void* item_buffer = allocate_func( userdata, item_bytes );
TheDebuginatorQueue* queue = (TheDebuginatorQueue*)queue_buffer;
queue->num_items = 0;
queue->capacity = initial_size;
queue->userdata = userdata;
queue->allocate = allocate_func;
queue->deallocate = deallocate_func;
queue->items = (DebuginatorQueueItem*)item_buffer;
// queue->cleanup_funcs[DEBUGINATOR_QUEUE_CreateItem] =
// debuginator_queue__create_bool_item_cleanup;
return queue;
}
#endif // DEBUGINATOR_QUEUE_IMPLEMENTATION
#ifndef DEBUGINATOR_ENABLE_WARNINGS
#ifdef _MSC_VER
#pragma warning( pop )
#endif
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#endif
#endif // INCLUDE_THE_DEBUGINATOR_H
/*
------------------------------------------------------------------------------
This software is available under 2 licenses -- choose whichever you prefer.
------------------------------------------------------------------------------
ALTERNATIVE A - MIT License
Copyright (c) 2017 Anders Elfgren
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
------------------------------------------------------------------------------
ALTERNATIVE B - Public Domain (www.unlicense.org)
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
software, either in source code form or as a compiled binary, for any purpose,
commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of this
software dedicate any and all copyright interest in the software to the public
domain. We make this dedication for the benefit of the public at large and to
the detriment of our heirs and successors. We intend this dedication to be an
overt act of relinquishment in perpetuity of all present and future rights to
this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
------------------------------------------------------------------------------
*/