Network module
[shoggoth.git] / headers / shared / common.h
blobb08d298a794738495ccad38c107afc993bb38cef
1 /* Shoggoth - Distributed File System
2 * Common utils
4 * Copyright (C) 2012 Pawel Dziepak
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, version 3 of the License.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #ifndef COMMON_H_
17 #define COMMON_H_
19 #include <arpa/inet.h>
21 static inline int min(int a, int b) {
22 return a > b ? b : a;
25 static inline int max(int a, int b) {
26 return a < b ? b : a;
29 #define to_be16(x) htons(x)
30 #define to_be32(x) htonl(x)
32 static inline uint64_t to_be64(uint64_t x) {
33 uint64_t y;
34 unsigned char *py = (unsigned char*)&y;
36 py[0] = x >> 56;
37 py[1] = x >> 48;
38 py[2] = x >> 40;
39 py[3] = x >> 32;
40 py[4] = x >> 24;
41 py[5] = x >> 16;
42 py[6] = x >> 8;
43 py[7] = x;
45 return y;
48 #define from_be16(x) ntohs(x)
49 #define from_be32(x) ntohl(x)
51 static inline uint64_t form_be64(uint64_t x) {
52 unsigned char *px = (unsigned char*)&x;
54 return (uint64_t)px[0] << 56 | (uint64_t)px[1] << 48 |
55 (uint64_t)px[2] << 40 | (uint64_t)px[3] << 32 |
56 (uint64_t)px[4] << 24 | (uint64_t)px[5] << 16 |
57 (uint64_t)px[6] << 8 | (uint64_t)px[7];
60 #endif