CLibs
Loading...
Searching...
No Matches
misc.h
Go to the documentation of this file.
1/*
2 * Miscellaneous things that don't fit elsewhere.
3 *
4 * UNUSED, countof, cmp functions, ...
5 */
6
7#ifndef CLIBS_MISC_H
8#define CLIBS_MISC_H
9
10#include "attributes.h"
11#include "extra_types.h"
12
13
15#define UNUSED( UNUSEDARG ) ( ( void ) UNUSEDARG )
16
17
19#define STRLEN( STRING ) ( sizeof( STRING ) - 1 )
20
22#define countof( array ) ( sizeof( array ) / sizeof *( array ) )
23
24
26#define cmpeq( cmp_retval ) ( ( cmp_retval ) == 0 )
27
28
35#define DEFINE_CMP_FUNCTION( TYPE ) \
36 LibraryDefined int cmp_##TYPE( const void *p1, const void *p2 ) \
37 { \
38 TYPE val_1 = *( TYPE * ) p1; \
39 TYPE val_2 = *( TYPE * ) p2; \
40 \
41 if ( val_1 > val_2 ) \
42 return 1; \
43 if ( val_1 < val_2 ) \
44 return -1; \
45 return 0; \
46 }
47
51DEFINE_CMP_FUNCTION( size_t )
52DEFINE_CMP_FUNCTION( int64_t )
53DEFINE_CMP_FUNCTION( uint64_t )
54
55
56
65LibraryDefined uint64_t hash_func( const void *const data, const size_t nbytes )
66{
67 uint64_t hash = 5381 * nbytes;
68
69 const byte *data_byte = ( byte * ) data;
70 for ( size_t i = 0; i < nbytes; ++i )
71 hash = ( hash << 5 ) + hash + data_byte[ i ];
72
73 return hash;
74}
75
76
77#endif //CLIBS_MISC_H
Function and variable attributes.
#define LibraryDefined
Definition attributes.h:107
LibraryDefined uint64_t hash_func(const void *const data, const size_t nbytes)
Definition misc.h:65
#define DEFINE_CMP_FUNCTION(TYPE)
Definition misc.h:35