|
28 | 28 | #ifndef LLB_INTERNAL |
29 | 29 | #define LLB_INTERNAL |
30 | 30 |
|
| 31 | +#include <assert.h> |
31 | 32 | #include <stdio.h> |
32 | 33 | #include <stdlib.h> |
33 | 34 | #include <unistd.h> |
34 | | -#include <assert.h> |
35 | 35 |
|
36 | | -#define RANDOM(A,B) A + rand()/(RAND_MAX/(B - A)) |
| 36 | +#define RANDOM(A, B) A + rand() / (RAND_MAX / (B - A)) |
37 | 37 |
|
38 | 38 | /* Load-balancing mode */ |
39 | | -#define LLB_TCP_MODE 0 |
40 | | -#define LLB_HTTP_MODE 1 |
| 39 | +#define LLB_TCP_MODE 0 |
| 40 | +#define LLB_HTTP_MODE 1 |
41 | 41 |
|
42 | | -#define LLB_SUCCESS 0 |
43 | | -#define LLB_FAILURE -1 |
| 42 | +#define LLB_SUCCESS 0 |
| 43 | +#define LLB_FAILURE -1 |
44 | 44 |
|
45 | 45 | /* Load-balancing algorithms */ |
46 | 46 | #define ROUND_ROBIN 0 |
|
50 | 50 | #define LEASTTRAFFIC 4 |
51 | 51 | #define WEIGHTED_ROUND_ROBIN 5 |
52 | 52 |
|
53 | | -static inline void *llb_malloc(size_t size) { |
| 53 | +static inline void *llb_malloc(size_t size) |
| 54 | +{ |
54 | 55 | void *ptr = malloc(size); |
55 | 56 | if (!ptr && size != 0) { |
56 | | - fprintf(stderr, "[%s:%ul] Out of memory (%lu bytes)\n", |
57 | | - __FILE__, __LINE__, size); |
| 57 | + fprintf(stderr, "[%s:%ul] Out of memory (%lu bytes)\n", __FILE__, |
| 58 | + __LINE__, size); |
58 | 59 | exit(EXIT_FAILURE); |
59 | 60 | } |
60 | 61 | return ptr; |
61 | 62 | } |
62 | 63 |
|
63 | | -static inline void *llb_calloc(size_t n, size_t size) { |
| 64 | +static inline void *llb_calloc(size_t n, size_t size) |
| 65 | +{ |
64 | 66 | void *ptr = calloc(n, size); |
65 | 67 | if (!ptr && size != 0 && n != 0) { |
66 | | - fprintf(stderr, "[%s:%ul] Out of memory (%lu bytes)\n", |
67 | | - __FILE__, __LINE__, size); |
| 68 | + fprintf(stderr, "[%s:%ul] Out of memory (%lu bytes)\n", __FILE__, |
| 69 | + __LINE__, size); |
68 | 70 | exit(EXIT_FAILURE); |
69 | 71 | } |
70 | 72 | return ptr; |
71 | 73 | } |
72 | 74 |
|
73 | | -static inline void *llb_realloc(void *ptr, size_t size) { |
| 75 | +static inline void *llb_realloc(void *ptr, size_t size) |
| 76 | +{ |
74 | 77 | assert(ptr && size > 0); |
75 | 78 | void *new_ptr = realloc(ptr, size); |
76 | 79 | if (!new_ptr && size != 0) { |
77 | | - fprintf(stderr, "[%s:%ul] Out of memory (%lu bytes)\n", |
78 | | - __FILE__, __LINE__, size); |
| 80 | + fprintf(stderr, "[%s:%ul] Out of memory (%lu bytes)\n", __FILE__, |
| 81 | + __LINE__, size); |
79 | 82 | exit(EXIT_FAILURE); |
80 | 83 | } |
81 | 84 | return new_ptr; |
82 | 85 | } |
83 | 86 |
|
84 | | -static inline void llb_free(void *ptr) { |
85 | | - free(ptr); |
86 | | -} |
| 87 | +static inline void llb_free(void *ptr) { free(ptr); } |
87 | 88 |
|
88 | 89 | /* D. J. Bernstein hash function */ |
89 | | -static inline size_t djb_hash(const char *str) { |
| 90 | +static inline size_t djb_hash(const char *str) |
| 91 | +{ |
90 | 92 | size_t hash = 5381; |
91 | 93 | while (*str) |
92 | | - hash = 33 * hash ^ (unsigned char) *str++; |
| 94 | + hash = 33 * hash ^ (unsigned char)*str++; |
93 | 95 | return hash; |
94 | 96 | } |
95 | 97 |
|
|
0 commit comments