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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
|
/* copyright (c) 2007 magnus auvinen, see licence.txt for more info */
#ifndef BASE_SYSTEM_H
#define BASE_SYSTEM_H
#include "e_detect.h"
#ifdef __cplusplus
extern "C" {
#endif
/* Group: Debug */
/**********
Function: dbg_assert
Breaks into the debugger based on a test.
Parameters:
test - Result of the test.
msg - Message that should be printed if the test fails.
Remarks:
Does nothing in release version of the library.
See Also:
<dbg_break>
**********/
void dbg_assert(int test, const char *msg);
#define dbg_assert(test,msg) dbg_assert_imp(__FILE__, __LINE__, test, msg)
void dbg_assert_imp(const char *filename, int line, int test, const char *msg);
/**********
Function: dbg_break
Breaks into the debugger.
Remarks:
Does nothing in release version of the library.
See Also:
<dbg_assert>
**********/
void dbg_break();
/**********
Function: dbg_msg
Prints a debug message.
Parameters:
sys - A string that describes what system the message belongs to
fmt - A printf styled format string.
Remarks:
Does nothing in relase version of the library.
See Also:
<dbg_assert>
**********/
void dbg_msg(const char *sys, const char *fmt, ...);
/* Group: Memory */
/**********
Function: mem_alloc
Allocates memory.
Parameters:
size - Size of the needed block.
alignment - Alignment for the block.
Returns:
Returns a pointer to the newly allocated block. Returns a
null pointer if the memory couldn't be allocated.
Remarks:
- Passing 0 to size will allocated the smallest amount possible
and return a unique pointer.
See Also:
<mem_free>
**********/
void *mem_alloc_debug(const char *filename, int line, unsigned size, unsigned alignment);
#define mem_alloc(s,a) mem_alloc_debug(__FILE__, __LINE__, (s), (a))
/**********
Function: mem_free
Frees a block allocated through <mem_alloc>.
Remarks:
- In the debug version of the library the function will assert if
a non-valid block is passed, like a null pointer or a block that
isn't allocated.
See Also:
<mem_alloc>
**********/
void mem_free(void *block);
/**********
Function: mem_copy
Copies a a memory block.
Parameters:
dest - Destination.
source - Source to copy.
size - Size of the block to copy.
Remarks:
- This functions DOES NOT handles cases where source and
destination is overlapping.
See Also:
<mem_move>
**********/
void mem_copy(void *dest, const void *source, unsigned size);
/**********
Function: mem_move
Copies a a memory block.
Parameters:
dest - Destination.
source - Source to copy.
size - Size of the block to copy.
Remarks:
- This functions handles cases where source and destination is overlapping.
See Also:
<mem_copy>
**********/
void mem_move(void *dest, const void *source, unsigned size);
/**********
Function: mem_zero
Sets a complete memory block to 0.
Parameters:
block - Pointer to the block to zero out.
size - Size of the block.
**********/
void mem_zero(void *block, unsigned size);
/* ------- io ------- */
enum {
IOFLAG_READ = 1,
IOFLAG_WRITE = 2,
IOFLAG_RANDOM = 4,
IOSEEK_START = 0,
IOSEEK_CUR = 1,
IOSEEK_END = 2
};
typedef struct IOINTERNAL *IOHANDLE;
/**** Group: File IO ****/
/****
Function: io_open
Opens a file.
Parameters:
filename - File to open.
flags - A set of flags. IOFLAG_READ, IOFLAG_WRITE, IOFLAG_RANDOM.
Returns:
Returns a handle to the file on success and 0 on failure.
****/
IOHANDLE io_open(const char *filename, int flags);
/****
Function: io_read
Reads data into a buffer from a file.
Parameters:
io - Handle to the file to read data from.
buffer - Pointer to the buffer that will recive the data.
size - Number of bytes to read from the file.
Returns:
Number of bytes read.
****/
unsigned io_read(IOHANDLE io, void *buffer, unsigned size);
/*****
Function: io_skip
Skips data in a file.
Parameters:
io - Handle to the file.
size - Number of bytes to skip.
Returns:
Number of bytes skipped.
****/
unsigned io_skip(IOHANDLE io, unsigned size);
/*****
Function: io_write
Writes data from a buffer to file.
Parameters:
io - Handle to the file.
buffer - Pointer to the data that should be written.
size - Number of bytes to write.
Returns:
Number of bytes written.
*****/
unsigned io_write(IOHANDLE io, const void *buffer, unsigned size);
/*****
Function: io_seek
Seeks to a specified offset in the file.
Parameters:
io - Handle to the file.
offset - Offset from pos to stop.
origin - Position to start searching from.
Returns:
Returns 0 on success.
*****/
int io_seek(IOHANDLE io, int offset, int origin);
/*****
Function: io_tell
Gets the current position in the file.
Parameters:
io - Handle to the file.
Returns:
Returns the current position. -1L if an error occured.
*****/
long int io_tell(IOHANDLE io);
/*****
Function: io_length
Gets the total length of the file. Resetting cursor to the beginning
Parameters:
io - Handle to the file.
Returns:
Returns the total size. -1L if an error occured.
*****/
long int io_length(IOHANDLE io);
/*****
Function: io_close
Closes a file.
Parameters:
io - Handle to the file.
Returns:
Returns 0 on success.
*****/
int io_close(IOHANDLE io);
/**** Group: Threads ****/
int thread_create(); /* NOT IMPLEMENTED */
int thread_destory(); /* NOT IMPLEMENTED */
int thread_run(); /* NOT IMPLEMENTED */
int thread_pause(); /* NOT IMPLEMENTED */
int thread_wait(); /* NOT IMPLEMENTED */
/*****
Function: thread_sleep
Suspends the current thread for a given period.
Parameters:
milliseconds - Number of milliseconds to sleep.
*****/
void thread_sleep(int milliseconds);
/**** Group: Locks ****/
typedef void* LOCK;
LOCK lock_create();
void lock_destroy(LOCK lock);
int lock_try(LOCK lock);
void lock_wait(LOCK lock);
void lock_release(LOCK lock);
/**** Group: Timer ****/
#ifdef __GNUC__
/* if compiled with -pedantic-errors it will complain about long
not being a C90 thing.
*/
__extension__ typedef long long int64;
#else
typedef long long int64;
#endif
/*****
Function: time_get
Fetches a sample from a high resolution timer.
Returns:
Current value of the timer.
Remarks:
To know how fast the timer is ticking, see <time_freq>.
*****/
int64 time_get();
/*****
Function: time_freq
Returns the frequency of the high resolution timer.
Returns:
Returns the frequency of the high resolution timer.
*****/
int64 time_freq();
/**** Group: Network General ipv4 ****/
typedef int NETSOCKET;
enum
{
NETSOCKET_INVALID = -1
};
typedef struct
{
unsigned char ip[4];
unsigned short port;
} NETADDR4;
/*****
Function: net_host_lookup
Does a hostname lookup by name and fills out the passed NETADDE4 struct with the recieved details.
Returns:
0 on success.
*****/
int net_host_lookup(const char *hostname, unsigned short port, NETADDR4 *addr);
/**** Group: Network UDP4 ****/
/*****
Function: net_udp4_create
Creates a UDP4 socket and binds it to a port.
Parameters:
port - Port to bind the socket to.
Returns:
On success it returns an handle to the socket. On failure it returns NETSOCKET_INVALID.
*****/
NETSOCKET net_udp4_create(NETADDR4 bindaddr);
/*****
Function: net_udp4_send
Sends a packet over an UDP4 socket.
Parameters:
sock - Socket to use.
addr - Where to send the packet.
data - Pointer to the packet data to send.
size - Size of the packet.
Returns:
On success it returns the number of bytes sent. Returns -1 on error.
*****/
int net_udp4_send(NETSOCKET sock, const NETADDR4 *addr, const void *data, int size);
/*****
Function: net_udp4_recv
Recives a packet over an UDP4 socket.
Parameters:
sock - Socket to use.
addr - Pointer to an NETADDR4 that will recive the address.
data - Pointer to a buffer that will recive the data.
maxsize - Maximum size to recive.
Returns:
On success it returns the number of bytes recived. Returns -1 on error.
*****/
int net_udp4_recv(NETSOCKET sock, NETADDR4 *addr, void *data, int maxsize);
/*****
Function: net_udp4_close
Closes an UDP4 socket.
Parameters:
sock - Socket to close.
Returns:
Returns 0 on success. -1 on error.
*****/
int net_udp4_close(NETSOCKET sock);
/**** Group: Network TCP4 ****/
/*****
Function: net_tcp4_create
DOCTODO: serp
*****/
NETSOCKET net_tcp4_create(const NETADDR4 *a);
/*****
Function: net_tcp4_set_non_blocking
DOCTODO: serp
*****/
int net_tcp4_set_non_blocking(NETSOCKET sock);
/*****
Function: net_tcp4_set_non_blocking
DOCTODO: serp
*****/
int net_tcp4_set_blocking(NETSOCKET sock);
/*****
Function: net_tcp4_listen
DOCTODO: serp
*****/
int net_tcp4_listen(NETSOCKET sock, int backlog);
/*****
Function: net_tcp4_accept
DOCTODO: serp
*****/
int net_tcp4_accept(NETSOCKET sock, NETSOCKET *new_sock, NETADDR4 *a);
/*****
Function: net_tcp4_connect
DOCTODO: serp
*****/
int net_tcp4_connect(NETSOCKET sock, const NETADDR4 *a);
/*****
Function: net_tcp4_connect_non_blocking
DOCTODO: serp
*****/
int net_tcp4_connect_non_blocking(NETSOCKET sock, const NETADDR4 *a);
/*****
Function: net_tcp4_send
DOCTODO: serp
*****/
int net_tcp4_send(NETSOCKET sock, const void *data, int size);
/*****
Function: net_tcp4_recv
DOCTODO: serp
*****/
int net_tcp4_recv(NETSOCKET sock, void *data, int maxsize);
/*****
Function: net_tcp4_close
DOCTODO: serp
*****/
int net_tcp4_close(NETSOCKET sock);
/*****
Function: net_errno
DOCTODO: serp
*****/
int net_errno();
/*****
Function: net_would_block
DOCTODO: serp
*****/
int net_would_block();
/*****
Function: net_init
DOCTODO: serp
*****/
int net_init();
/* NOT DOCUMENTED */
typedef void (*fs_listdir_callback)(const char *name, int is_dir, void *user);
int fs_listdir(const char *dir, fs_listdir_callback cb, void *user);
int fs_storage_path(const char *appname, char *path, int max);
int fs_makedir(const char *path);
int net_addr4_cmp(const NETADDR4 *a, const NETADDR4 *b);
void mem_debug_dump();
int mem_allocated();
void swap_endian(void *data, unsigned elem_size, unsigned num);
/* #define cache_prefetch(addr) __builtin_prefetch(addr) */
#ifdef __cplusplus
}
#endif
#endif
|