http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/ts/Vec.h ---------------------------------------------------------------------- diff --git a/lib/ts/Vec.h b/lib/ts/Vec.h index b5b3a8d..ea97e8b 100644 --- a/lib/ts/Vec.h +++ b/lib/ts/Vec.h @@ -33,34 +33,39 @@ // Simple Vector class, also supports open hashed sets -#define VEC_INTEGRAL_SHIFT_DEFAULT 2 /* power of 2 (1 << VEC_INTEGRAL_SHIFT)*/ -#define VEC_INTEGRAL_SIZE (1 << (S)) -#define VEC_INITIAL_SHIFT ((S)+1) -#define VEC_INITIAL_SIZE (1 << VEC_INITIAL_SHIFT) - -#define SET_LINEAR_SIZE 4 /* must be <= than VEC_INTEGRAL_SIZE */ -#define SET_INITIAL_INDEX 2 - -template <class C, class A = DefaultAlloc, int S = VEC_INTEGRAL_SHIFT_DEFAULT> // S must be a power of 2 -class Vec { - public: - size_t n; - size_t i; // size index for sets, reserve for vectors - C *v; - C e[VEC_INTEGRAL_SIZE]; +#define VEC_INTEGRAL_SHIFT_DEFAULT 2 /* power of 2 (1 << VEC_INTEGRAL_SHIFT)*/ +#define VEC_INTEGRAL_SIZE (1 << (S)) +#define VEC_INITIAL_SHIFT ((S) + 1) +#define VEC_INITIAL_SIZE (1 << VEC_INITIAL_SHIFT) + +#define SET_LINEAR_SIZE 4 /* must be <= than VEC_INTEGRAL_SIZE */ +#define SET_INITIAL_INDEX 2 + +template <class C, class A = DefaultAlloc, int S = VEC_INTEGRAL_SHIFT_DEFAULT> // S must be a power of 2 +class Vec +{ +public: + size_t n; + size_t i; // size index for sets, reserve for vectors + C *v; + C e[VEC_INTEGRAL_SIZE]; Vec(); - Vec<C,A,S>(const Vec<C,A,S> &vv); - Vec<C,A,S>(const C c); + Vec<C, A, S>(const Vec<C, A, S> &vv); + Vec<C, A, S>(const C c); ~Vec(); C &operator[](int i) const { return v[i]; } C get(size_t i) const; void add(C a); - void push_back(C a) { add(a); } // std::vector name + void + push_back(C a) + { + add(a); + } // std::vector name bool add_exclusive(C a); - C& add(); + C &add(); void drop(); C pop(); void reset(); @@ -71,14 +76,14 @@ class Vec { C *set_add(C a); void set_remove(C a); // expensive, use BlockHash for cheaper remove C *set_add_internal(C a); - bool set_union(Vec<C,A,S> &v); - int set_intersection(Vec<C,A,S> &v); - int some_intersection(Vec<C,A,S> &v); - int some_disjunction(Vec<C,A,S> &v); - int some_difference(Vec<C,A,S> &v); - void set_intersection(Vec<C,A,S> &v, Vec<C,A,S> &result); - void set_disjunction(Vec<C,A,S> &v, Vec<C,A,S> &result); - void set_difference(Vec<C,A,S> &v, Vec<C,A,S> &result); + bool set_union(Vec<C, A, S> &v); + int set_intersection(Vec<C, A, S> &v); + int some_intersection(Vec<C, A, S> &v); + int some_disjunction(Vec<C, A, S> &v); + int some_difference(Vec<C, A, S> &v); + void set_intersection(Vec<C, A, S> &v, Vec<C, A, S> &result); + void set_disjunction(Vec<C, A, S> &v, Vec<C, A, S> &result); + void set_difference(Vec<C, A, S> &v, Vec<C, A, S> &result); size_t set_count() const; size_t count() const; C *in(C a); @@ -89,60 +94,118 @@ class Vec { ssize_t index(C a) const; void set_to_vec(); void vec_to_set(); - void move(Vec<C,A,S> &v); - void copy(const Vec<C,A,S> &v); + void move(Vec<C, A, S> &v); + void copy(const Vec<C, A, S> &v); void fill(size_t n); void append(const Vec<C> &v); - template <typename CountType> void append(const C * src, CountType count); + template <typename CountType> void append(const C *src, CountType count); void prepend(const Vec<C> &v); void remove_index(int index); - void remove(C a) { int i = index(a); if (i>=0) remove_index(i); } - C& insert(size_t index); + void + remove(C a) + { + int i = index(a); + if (i >= 0) + remove_index(i); + } + C &insert(size_t index); void insert(size_t index, Vec<C> &vv); void insert(size_t index, C a); - void push(C a) { insert(0, a); } + void + push(C a) + { + insert(0, a); + } void reverse(); void reserve(size_t n); - C* end() const { return v + n; } - C &first() const { return v[0]; } - C &last() const { return v[n-1]; } - Vec<C,A,S>& operator=(Vec<C,A,S> &v) { this->copy(v); return *this; } - unsigned length () const { return n; } + C * + end() const + { + return v + n; + } + C & + first() const + { + return v[0]; + } + C & + last() const + { + return v[n - 1]; + } + Vec<C, A, S> &operator=(Vec<C, A, S> &v) + { + this->copy(v); + return *this; + } + unsigned + length() const + { + return n; + } // vector::size() intentionally not implemented because it should mean "bytes" not count of elements int write(int fd); int read(int fd); - void qsort(bool (*lt)(C,C)); + void qsort(bool (*lt)(C, C)); void qsort(bool (*lt)(const C &, const C &)); private: - void move_internal(Vec<C,A,S> &v); - void copy_internal(const Vec<C,A,S> &v); + void move_internal(Vec<C, A, S> &v); + void copy_internal(const Vec<C, A, S> &v); void add_internal(C a); - C& add_internal(); + C &add_internal(); void addx(); }; // c -- class, p -- pointer to elements of v, v -- vector -#define forv_Vec(_c, _p, _v) if ((_v).n) for (_c *qq__##_p = (_c*)0, *_p = (_v).v[0]; \ - ((uintptr_t)(qq__##_p) < (_v).length()) && ((_p = (_v).v[(intptr_t)qq__##_p]) || 1); qq__##_p = (_c*)(((intptr_t)qq__##_p) + 1)) -#define for_Vec(_c, _p, _v) if ((_v).n) for (_c *qq__##_p = (_c*)0, _p = (_v).v[0]; \ - ((uintptr_t)(qq__##_p) < (_v).length()) && ((_p = (_v).v[(intptr_t)qq__##_p]) || 1); qq__##_p = (_c*)(((intptr_t)qq__##_p) + 1)) -#define forvp_Vec(_c, _p, _v) if ((_v).n) for (_c *qq__##_p = (_c*)0, *_p = &(_v).v[0]; \ - ((uintptr_t)(qq__##_p) < (_v).length()) && ((_p = &(_v).v[(intptr_t)qq__##_p]) || 1); qq__##_p = (_c*)(((intptr_t)qq__##_p) + 1)) - -template <class C, class A = DefaultAlloc, int S = VEC_INTEGRAL_SHIFT_DEFAULT> class Accum { public: - Vec<C,A,S> asset; - Vec<C,A,S> asvec; - void add(C c) { if (asset.set_add(c)) asvec.add(c); } - void add(Vec<C,A,S> v) { for (int i = 0; i < v.n; i++) if (v.v[i]) add(v.v[i]); } - void clear() { asset.clear(); asvec.clear(); } +#define forv_Vec(_c, _p, _v) \ + if ((_v).n) \ + for (_c *qq__##_p = (_c *)0, *_p = (_v).v[0]; \ + ((uintptr_t)(qq__##_p) < (_v).length()) && ((_p = (_v).v[(intptr_t)qq__##_p]) || 1); \ + qq__##_p = (_c *)(((intptr_t)qq__##_p) + 1)) +#define for_Vec(_c, _p, _v) \ + if ((_v).n) \ + for (_c *qq__##_p = (_c *)0, _p = (_v).v[0]; \ + ((uintptr_t)(qq__##_p) < (_v).length()) && ((_p = (_v).v[(intptr_t)qq__##_p]) || 1); \ + qq__##_p = (_c *)(((intptr_t)qq__##_p) + 1)) +#define forvp_Vec(_c, _p, _v) \ + if ((_v).n) \ + for (_c *qq__##_p = (_c *)0, *_p = &(_v).v[0]; \ + ((uintptr_t)(qq__##_p) < (_v).length()) && ((_p = &(_v).v[(intptr_t)qq__##_p]) || 1); \ + qq__##_p = (_c *)(((intptr_t)qq__##_p) + 1)) + +template <class C, class A = DefaultAlloc, int S = VEC_INTEGRAL_SHIFT_DEFAULT> class Accum +{ +public: + Vec<C, A, S> asset; + Vec<C, A, S> asvec; + void + add(C c) + { + if (asset.set_add(c)) + asvec.add(c); + } + void + add(Vec<C, A, S> v) + { + for (int i = 0; i < v.n; i++) + if (v.v[i]) + add(v.v[i]); + } + void + clear() + { + asset.clear(); + asvec.clear(); + } }; // Intervals store sets in interval format (e.g. [1..10][12..12]). // Inclusion test is by binary search on intervals. // Deletion is not supported -class Intervals : public Vec<int> { - public: +class Intervals : public Vec<int> +{ +public: void insert(int n); bool in(int n) const; }; @@ -150,8 +213,9 @@ class Intervals : public Vec<int> { // UnionFind supports fast unify and finding of // 'representitive elements'. // Elements are numbered from 0 to N-1. -class UnionFind : public Vec<int> { - public: +class UnionFind : public Vec<int> +{ +public: // set number of elements, initialized to singletons, may be called repeatedly to increase size void size(int n); // return representitive element @@ -165,26 +229,28 @@ extern const uintptr_t open_hash_primes[256]; /* IMPLEMENTATION */ -template <class C, class A, int S> inline -Vec<C,A,S>::Vec() : n(0), i(0), v(0) { +template <class C, class A, int S> inline Vec<C, A, S>::Vec() : n(0), i(0), v(0) +{ memset(&e[0], 0, sizeof(e)); } -template <class C, class A, int S> inline -Vec<C,A,S>::Vec(const Vec<C,A,S> &vv) { +template <class C, class A, int S> inline Vec<C, A, S>::Vec(const Vec<C, A, S> &vv) +{ copy(vv); } -template <class C, class A, int S> inline -Vec<C,A,S>::Vec(C c) { +template <class C, class A, int S> inline Vec<C, A, S>::Vec(C c) +{ n = 1; i = 0; v = &e[0]; e[0] = c; } -template <class C, class A, int S> inline C -Vec<C,A,S>::get(size_t i) const { +template <class C, class A, int S> +inline C +Vec<C, A, S>::get(size_t i) const +{ if (i < n) { return v[i]; } else { @@ -192,9 +258,11 @@ Vec<C,A,S>::get(size_t i) const { } } -template <class C, class A, int S> inline void -Vec<C,A,S>::add(C a) { - if (n & (VEC_INTEGRAL_SIZE-1)) +template <class C, class A, int S> +inline void +Vec<C, A, S>::add(C a) +{ + if (n & (VEC_INTEGRAL_SIZE - 1)) v[n++] = a; else if (!v) (v = e)[n++] = a; @@ -202,10 +270,12 @@ Vec<C,A,S>::add(C a) { add_internal(a); } -template <class C, class A, int S> inline C& -Vec<C,A,S>::add() { +template <class C, class A, int S> +inline C & +Vec<C, A, S>::add() +{ C *ret; - if (n & (VEC_INTEGRAL_SIZE-1)) + if (n & (VEC_INTEGRAL_SIZE - 1)) ret = &v[n++]; else if (!v) ret = &(v = e)[n++]; @@ -214,14 +284,18 @@ Vec<C,A,S>::add() { return *ret; } -template <class C, class A, int S> inline void -Vec<C,A,S>::drop() { +template <class C, class A, int S> +inline void +Vec<C, A, S>::drop() +{ if (n && 0 == --n) clear(); } -template <class C, class A, int S> inline C -Vec<C,A,S>::pop() { +template <class C, class A, int S> +inline C +Vec<C, A, S>::pop() +{ if (!n) return 0; n--; @@ -231,22 +305,26 @@ Vec<C,A,S>::pop() { return ret; } -template <class C, class A, int S> inline void -Vec<C,A,S>::set_clear() { +template <class C, class A, int S> +inline void +Vec<C, A, S>::set_clear() +{ memset(v, 0, n * sizeof(C)); } -template <class C, class A, int S> inline C * -Vec<C,A,S>::set_add(C a) { +template <class C, class A, int S> +inline C * +Vec<C, A, S>::set_add(C a) +{ if (n < SET_LINEAR_SIZE) { for (C *c = v; c < v + n; c++) if (*c == a) return 0; add(a); - return &v[n-1]; + return &v[n - 1]; } if (n == SET_LINEAR_SIZE) { - Vec<C,A,S> vv(*this); + Vec<C, A, S> vv(*this); clear(); for (C *c = vv.v; c < vv.v + vv.n; c++) { set_add_internal(*c); @@ -255,17 +333,21 @@ Vec<C,A,S>::set_add(C a) { return set_add_internal(a); } -template <class C, class A, int S> void -Vec<C,A,S>::set_remove(C a) { - Vec<C,A,S> tmp; +template <class C, class A, int S> +void +Vec<C, A, S>::set_remove(C a) +{ + Vec<C, A, S> tmp; tmp.move(*this); for (C *c = tmp.v; c < tmp.v + tmp.n; c++) if (*c != a) set_add(a); } -template <class C, class A, int S> inline size_t -Vec<C,A,S>::count() const { +template <class C, class A, int S> +inline size_t +Vec<C, A, S>::count() const +{ int x = 0; for (C *c = v; c < v + n; c++) if (*c) @@ -273,16 +355,20 @@ Vec<C,A,S>::count() const { return x; } -template <class C, class A, int S> inline C* -Vec<C,A,S>::in(C a) { +template <class C, class A, int S> +inline C * +Vec<C, A, S>::in(C a) +{ for (C *c = v; c < v + n; c++) if (*c == a) return c; return NULL; } -template <class C, class A, int S> inline bool -Vec<C,A,S>::add_exclusive(C a) { +template <class C, class A, int S> +inline bool +Vec<C, A, S>::add_exclusive(C a) +{ if (!in(a)) { add(a); return true; @@ -290,23 +376,29 @@ Vec<C,A,S>::add_exclusive(C a) { return false; } -template <class C, class A, int S> inline C * -Vec<C,A,S>::set_in(C a) { +template <class C, class A, int S> +inline C * +Vec<C, A, S>::set_in(C a) +{ if (n <= SET_LINEAR_SIZE) return in(a); return set_in_internal(a); } -template <class C, class A, int S> inline C -Vec<C,A,S>::first_in_set() { +template <class C, class A, int S> +inline C +Vec<C, A, S>::first_in_set() +{ for (C *c = v; c < v + n; c++) if (*c) return *c; return 0; } -template <class C, class A, int S> inline ssize_t -Vec<C,A,S>::index(C a) const { +template <class C, class A, int S> +inline ssize_t +Vec<C, A, S>::index(C a) const +{ for (C *c = v; c < v + n; c++) { if (*c == a) { return c - v; @@ -315,8 +407,10 @@ Vec<C,A,S>::index(C a) const { return -1; } -template <class C, class A, int S> inline void -Vec<C,A,S>::move_internal(Vec<C,A,S> &vv) { +template <class C, class A, int S> +inline void +Vec<C, A, S>::move_internal(Vec<C, A, S> &vv) +{ n = vv.n; i = vv.i; if (vv.v == &vv.e[0]) { @@ -326,15 +420,19 @@ Vec<C,A,S>::move_internal(Vec<C,A,S> &vv) { v = vv.v; } -template <class C, class A, int S> inline void -Vec<C,A,S>::move(Vec<C,A,S> &vv) { +template <class C, class A, int S> +inline void +Vec<C, A, S>::move(Vec<C, A, S> &vv) +{ move_internal(vv); vv.v = 0; vv.clear(); } -template <class C, class A, int S> inline void -Vec<C,A,S>::copy(const Vec<C,A,S> &vv) { +template <class C, class A, int S> +inline void +Vec<C, A, S>::copy(const Vec<C, A, S> &vv) +{ n = vv.n; i = vv.i; if (vv.v == &vv.e[0]) { @@ -348,14 +446,18 @@ Vec<C,A,S>::copy(const Vec<C,A,S> &vv) { } } -template <class C, class A, int S> inline void -Vec<C,A,S>::fill(size_t nn) { +template <class C, class A, int S> +inline void +Vec<C, A, S>::fill(size_t nn) +{ for (size_t i = n; i < nn; i++) add() = 0; } -template <class C, class A, int S> inline void -Vec<C,A,S>::append(const Vec<C> &vv) { +template <class C, class A, int S> +inline void +Vec<C, A, S>::append(const Vec<C> &vv) +{ for (C *c = vv.v; c < vv.v + vv.n; c++) if (*c != 0) add(*c); @@ -364,15 +466,18 @@ Vec<C,A,S>::append(const Vec<C> &vv) { template <class C, class A, int S> template <typename CountType> inline void -Vec<C,A,S>::append(const C * src, CountType count) { +Vec<C, A, S>::append(const C *src, CountType count) +{ reserve(length() + count); for (CountType c = 0; c < count; ++c) { add(src[c]); } } -template <class C, class A, int S> inline void -Vec<C,A,S>::prepend(const Vec<C> &vv) { +template <class C, class A, int S> +inline void +Vec<C, A, S>::prepend(const Vec<C> &vv) +{ if (vv.n) { int oldn = n; fill(n + vv.n); @@ -382,20 +487,26 @@ Vec<C,A,S>::prepend(const Vec<C> &vv) { } } -template <class C, class A, int S> void -Vec<C,A,S>::add_internal(C a) { +template <class C, class A, int S> +void +Vec<C, A, S>::add_internal(C a) +{ addx(); v[n++] = a; } -template <class C, class A, int S> C& -Vec<C,A,S>::add_internal() { +template <class C, class A, int S> +C & +Vec<C, A, S>::add_internal() +{ addx(); return v[n++]; } -template <class C, class A, int S> C * -Vec<C,A,S>::set_add_internal(C c) { +template <class C, class A, int S> +C * +Vec<C, A, S>::set_add_internal(C c) +{ size_t j, k; if (n) { uintptr_t h = (uintptr_t)c; @@ -410,7 +521,7 @@ Vec<C,A,S>::set_add_internal(C c) { k = (k + open_hash_primes[j]) % n; } } - Vec<C,A,S> vv; + Vec<C, A, S> vv; vv.move_internal(*this); set_expand(); if (vv.v) { @@ -419,8 +530,10 @@ Vec<C,A,S>::set_add_internal(C c) { return set_add(c); } -template <class C, class A, int S> C * -Vec<C,A,S>::set_in_internal(C c) { +template <class C, class A, int S> +C * +Vec<C, A, S>::set_in_internal(C c) +{ size_t j, k; if (n) { uintptr_t h = (uintptr_t)c; @@ -436,8 +549,10 @@ Vec<C,A,S>::set_in_internal(C c) { return 0; } -template <class C, class A, int S> bool -Vec<C,A,S>::set_union(Vec<C,A,S> &vv) { +template <class C, class A, int S> +bool +Vec<C, A, S>::set_union(Vec<C, A, S> &vv) +{ bool changed = false; for (size_t i = 0; i < vv.n; i++) { if (vv.v[i]) { @@ -447,9 +562,11 @@ Vec<C,A,S>::set_union(Vec<C,A,S> &vv) { return changed; } -template <class C, class A, int S> int -Vec<C,A,S>::set_intersection(Vec<C,A,S> &vv) { - Vec<C,A,S> tv; +template <class C, class A, int S> +int +Vec<C, A, S>::set_intersection(Vec<C, A, S> &vv) +{ + Vec<C, A, S> tv; tv.move(*this); int changed = 0; for (int i = 0; i < tv.n; i++) @@ -462,8 +579,10 @@ Vec<C,A,S>::set_intersection(Vec<C,A,S> &vv) { return changed; } -template <class C, class A, int S> int -Vec<C,A,S>::some_intersection(Vec<C,A,S> &vv) { +template <class C, class A, int S> +int +Vec<C, A, S>::some_intersection(Vec<C, A, S> &vv) +{ for (int i = 0; i < n; i++) if (v[i]) if (vv.set_in(v[i])) @@ -471,8 +590,10 @@ Vec<C,A,S>::some_intersection(Vec<C,A,S> &vv) { return 0; } -template <class C, class A, int S> int -Vec<C,A,S>::some_disjunction(Vec<C,A,S> &vv) { +template <class C, class A, int S> +int +Vec<C, A, S>::some_disjunction(Vec<C, A, S> &vv) +{ for (int i = 0; i < n; i++) if (v[i]) if (!vv.set_in(v[i])) @@ -484,16 +605,20 @@ Vec<C,A,S>::some_disjunction(Vec<C,A,S> &vv) { return 0; } -template <class C, class A, int S> void -Vec<C,A,S>::set_intersection(Vec<C,A,S> &vv, Vec<C,A,S> &result) { +template <class C, class A, int S> +void +Vec<C, A, S>::set_intersection(Vec<C, A, S> &vv, Vec<C, A, S> &result) +{ for (int i = 0; i < n; i++) if (v[i]) if (vv.set_in(v[i])) result.set_add(v[i]); } -template <class C, class A, int S> void -Vec<C,A,S>::set_disjunction(Vec<C,A,S> &vv, Vec<C,A,S> &result) { +template <class C, class A, int S> +void +Vec<C, A, S>::set_disjunction(Vec<C, A, S> &vv, Vec<C, A, S> &result) +{ for (int i = 0; i < n; i++) if (v[i]) if (!vv.set_in(v[i])) @@ -504,16 +629,20 @@ Vec<C,A,S>::set_disjunction(Vec<C,A,S> &vv, Vec<C,A,S> &result) { result.set_add(vv.v[i]); } -template <class C, class A, int S> void -Vec<C,A,S>::set_difference(Vec<C,A,S> &vv, Vec<C,A,S> &result) { +template <class C, class A, int S> +void +Vec<C, A, S>::set_difference(Vec<C, A, S> &vv, Vec<C, A, S> &result) +{ for (int i = 0; i < n; i++) if (v[i]) if (!vv.set_in(v[i])) result.set_add(v[i]); } -template <class C, class A, int S> int -Vec<C,A,S>::some_difference(Vec<C,A,S> &vv) { +template <class C, class A, int S> +int +Vec<C, A, S>::some_difference(Vec<C, A, S> &vv) +{ for (int i = 0; i < n; i++) if (v[i]) if (!vv.set_in(v[i])) @@ -521,8 +650,10 @@ Vec<C,A,S>::some_difference(Vec<C,A,S> &vv) { return 0; } -template <class C, class A, int S> size_t -Vec<C,A,S>::set_count() const { +template <class C, class A, int S> +size_t +Vec<C, A, S>::set_count() const +{ size_t x = 0; for (size_t i = 0; i < n; i++) { if (v[i]) { @@ -532,18 +663,20 @@ Vec<C,A,S>::set_count() const { return x; } -template <class C, class A, int S> void -Vec<C,A,S>::set_to_vec() { +template <class C, class A, int S> +void +Vec<C, A, S>::set_to_vec() +{ C *x = &v[0], *y = x; for (; y < v + n; y++) { if (*y) { if (x != y) - *x = *y; - x++; + *x = *y; + x++; } } if (i) { - i = prime2[i]; // convert set allocation to reserve + i = prime2[i]; // convert set allocation to reserve if (i - n > 0) memset(&v[n], 0, (i - n) * (sizeof(C))); } else { @@ -553,49 +686,61 @@ Vec<C,A,S>::set_to_vec() { } } -template <class C, class A, int S> void -Vec<C,A,S>::vec_to_set() { - Vec<C,A,S> vv; +template <class C, class A, int S> +void +Vec<C, A, S>::vec_to_set() +{ + Vec<C, A, S> vv; vv.move(*this); for (C *c = vv.v; c < vv.v + vv.n; c++) set_add(*c); } -template <class C, class A, int S> void -Vec<C,A,S>::remove_index(int index) { +template <class C, class A, int S> +void +Vec<C, A, S>::remove_index(int index) +{ if (n > 1) - memmove(&v[index], &v[index+1], (n - 1 - index) * sizeof(v[0])); + memmove(&v[index], &v[index + 1], (n - 1 - index) * sizeof(v[0])); n--; if (n <= 0) v = e; } -template <class C, class A, int S> void -Vec<C,A,S>::insert(size_t index, C a) { +template <class C, class A, int S> +void +Vec<C, A, S>::insert(size_t index, C a) +{ add(); - memmove(&v[index+1], &v[index], (n - index - 1) * sizeof(C)); + memmove(&v[index + 1], &v[index], (n - index - 1) * sizeof(C)); v[index] = a; } -template <class C, class A, int S> void -Vec<C,A,S>::insert(size_t index, Vec<C> &vv) { +template <class C, class A, int S> +void +Vec<C, A, S>::insert(size_t index, Vec<C> &vv) +{ fill(n + vv.n); - memmove(&v[index+vv.n], &v[index], (n - index - 1) * sizeof(C)); + memmove(&v[index + vv.n], &v[index], (n - index - 1) * sizeof(C)); for (int x = 0; x < vv.n; x++) v[index + x] = vv[x]; } -template <class C, class A, int S> C & -Vec<C,A,S>::insert(size_t index) { +template <class C, class A, int S> +C & +Vec<C, A, S>::insert(size_t index) +{ add(); - memmove(&v[index+1], &v[index], (n - index - 1) * sizeof(C)); + memmove(&v[index + 1], &v[index], (n - index - 1) * sizeof(C)); memset(&v[index], 0, sizeof(C)); return v[index]; } -template <class C, class A, int S> void -Vec<C,A,S>::reverse() { - for (int i = 0; i < n/2; i++) { +template <class C, class A, int S> +void +Vec<C, A, S>::reverse() +{ + for (int i = 0; i < n / 2; i++) { C *s = &v[i], *e = &v[n - 1 - i]; C t; memcpy(&t, s, sizeof(t)); @@ -604,67 +749,79 @@ Vec<C,A,S>::reverse() { } } -template <class C, class A, int S> void -Vec<C,A,S>::copy_internal(const Vec<C,A,S> &vv) { +template <class C, class A, int S> +void +Vec<C, A, S>::copy_internal(const Vec<C, A, S> &vv) +{ int l = n, nl = (1 + VEC_INITIAL_SHIFT); l = l >> VEC_INITIAL_SHIFT; - while (l) { l = l >> 1; nl++; } + while (l) { + l = l >> 1; + nl++; + } nl = 1 << nl; - v = (C*)A::alloc(nl * sizeof(C)); + v = (C *)A::alloc(nl * sizeof(C)); memcpy(v, vv.v, n * sizeof(C)); memset(v + n, 0, (nl - n) * sizeof(C)); - if (i > n) // reset reserve + if (i > n) // reset reserve i = 0; } -template <class C, class A, int S> void -Vec<C,A,S>::set_expand() { +template <class C, class A, int S> +void +Vec<C, A, S>::set_expand() +{ if (!n) i = SET_INITIAL_INDEX; else i = i + 1; n = prime2[i]; - v = (C*)A::alloc(n * sizeof(C)); + v = (C *)A::alloc(n * sizeof(C)); memset(v, 0, n * sizeof(C)); } -template <class C, class A, int S> inline void -Vec<C,A,S>::reserve(size_t x) { +template <class C, class A, int S> +inline void +Vec<C, A, S>::reserve(size_t x) +{ if (x <= n) return; unsigned xx = 1 << VEC_INITIAL_SHIFT; - while (xx < x) xx *= 2; + while (xx < x) + xx *= 2; i = xx; - void *vv = (void*)v; - v = (C*)A::alloc(i * sizeof(C)); + void *vv = (void *)v; + v = (C *)A::alloc(i * sizeof(C)); if (vv && n) memcpy(v, vv, n * sizeof(C)); - memset(&v[n], 0, (i-n) * sizeof(C)); + memset(&v[n], 0, (i - n) * sizeof(C)); if (vv && vv != e) A::free(vv); } -template <class C, class A, int S> inline void -Vec<C,A,S>::addx() { +template <class C, class A, int S> +inline void +Vec<C, A, S>::addx() +{ if (!v) { v = e; return; } if (v == e) { - v = (C*)A::alloc(VEC_INITIAL_SIZE * sizeof(C)); + v = (C *)A::alloc(VEC_INITIAL_SIZE * sizeof(C)); memcpy(v, &e[0], n * sizeof(C)); ink_assert(n < VEC_INITIAL_SIZE); memset(&v[n], 0, (VEC_INITIAL_SIZE - n) * sizeof(C)); } else { - if ((n & (n-1)) == 0) { + if ((n & (n - 1)) == 0) { size_t nl = n * 2; if (nl <= i) { return; } else { i = 0; } - void *vv = (void*)v; - v = (C*)A::alloc(nl * sizeof(C)); + void *vv = (void *)v; + v = (C *)A::alloc(nl * sizeof(C)); memcpy(v, vv, n * sizeof(C)); memset(&v[n], 0, n * sizeof(C)); A::free(vv); @@ -672,29 +829,38 @@ Vec<C,A,S>::addx() { } } -template <class C, class A, int S> inline void -Vec<C,A,S>::reset() { +template <class C, class A, int S> +inline void +Vec<C, A, S>::reset() +{ v = NULL; n = 0; i = 0; } -template <class C, class A, int S> inline void -Vec<C,A,S>::clear() { - if (v && v != e) A::free(v); +template <class C, class A, int S> +inline void +Vec<C, A, S>::clear() +{ + if (v && v != e) + A::free(v); reset(); } -template <class C, class A, int S> inline void -Vec<C,A,S>::free_and_clear() { +template <class C, class A, int S> +inline void +Vec<C, A, S>::free_and_clear() +{ for (int x = 0; x < n; x++) if (v[x]) A::free(v[x]); clear(); } -template <class C, class A, int S> inline void -Vec<C,A,S>::delete_and_clear() { +template <class C, class A, int S> +inline void +Vec<C, A, S>::delete_and_clear() +{ for (size_t x = 0; x < n; x++) { if (v[x]) { delete v[x]; @@ -703,13 +869,16 @@ Vec<C,A,S>::delete_and_clear() { clear(); } -template <class C, class A, int S> -inline Vec<C,A,S>::~Vec() { - if (v && v != e) A::free(v); +template <class C, class A, int S> inline Vec<C, A, S>::~Vec() +{ + if (v && v != e) + A::free(v); } template <class C, class A, int S> -inline int marshal_size(Vec<C,A,S> &v) { +inline int +marshal_size(Vec<C, A, S> &v) +{ int l = sizeof(int) * 2; for (int x = 0; x < v.n; x++) l += ::marshal_size(v.v[x]); @@ -717,22 +886,30 @@ inline int marshal_size(Vec<C,A,S> &v) { } template <class C, class A, int S> -inline int marshal(Vec<C,A,S> &v, char *buf) { +inline int +marshal(Vec<C, A, S> &v, char *buf) +{ char *x = buf; - *(int*)x = v.n; x += sizeof(int); - *(int*)x = v.i; x += sizeof(int); + *(int *)x = v.n; + x += sizeof(int); + *(int *)x = v.i; + x += sizeof(int); for (int i = 0; i < v.n; i++) x += ::marshal(v.v[i], x); return x - buf; } template <class C, class A, int S> -inline int unmarshal(Vec<C,A,S> &v, char *buf) { +inline int +unmarshal(Vec<C, A, S> &v, char *buf) +{ char *x = buf; - v.n = *(int*)x; x += sizeof(int); - v.i = *(int*)x; x += sizeof(int); + v.n = *(int *)x; + x += sizeof(int); + v.i = *(int *)x; + x += sizeof(int); if (v.n) { - v.v = (C*)A::alloc(sizeof(C) * v.n); + v.v = (C *)A::alloc(sizeof(C) * v.n); memset(v.v, 0, sizeof(C) * v.n); } else v.v = v.e; @@ -742,30 +919,44 @@ inline int unmarshal(Vec<C,A,S> &v, char *buf) { } template <class C, class A, int S> -inline int Vec<C,A,S>::write(int fd) { +inline int +Vec<C, A, S>::write(int fd) +{ int r = 0, t = 0; - if ((r = ::write(fd, this, sizeof(*this))) < 0) return r; t += r; - if ((r = ::write(fd, v, n * sizeof(C))) < 0) return r; t += r; + if ((r = ::write(fd, this, sizeof(*this))) < 0) + return r; + t += r; + if ((r = ::write(fd, v, n * sizeof(C))) < 0) + return r; + t += r; return t; } template <class C, class A, int S> -inline int Vec<C,A,S>::read(int fd) { +inline int +Vec<C, A, S>::read(int fd) +{ int r = 0, t = 0; - if ((r = ::read(fd, this, sizeof(*this))) < 0) return r; t += r; - v = (C*)A::alloc(sizeof(C) * n); + if ((r = ::read(fd, this, sizeof(*this))) < 0) + return r; + t += r; + v = (C *)A::alloc(sizeof(C) * n); memset(v, 0, sizeof(C) * n); - if ((r = ::read(fd, v, n * sizeof(C))) < 0) return r; t += r; + if ((r = ::read(fd, v, n * sizeof(C))) < 0) + return r; + t += r; return t; } template <class C> -inline void qsort_Vec(C *left, C *right, bool (*lt)(C,C)) { - Lagain: +inline void +qsort_Vec(C *left, C *right, bool (*lt)(C, C)) +{ +Lagain: if (right - left < 5) { for (C *y = right - 1; y > left; y--) { for (C *x = left; x < y; x++) { - if (lt(x[1],x[0])) { + if (lt(x[1], x[0])) { C t = x[0]; x[0] = x[1]; x[1] = t; @@ -773,16 +964,20 @@ inline void qsort_Vec(C *left, C *right, bool (*lt)(C,C)) { } } } else { - C *i = left + 1, *j = right - 1; + C *i = left + 1, *j = right - 1; C x = *left; for (;;) { - while (lt(x,*j)) j--; - while (i < j && lt(*i,x)) i++; - if (i >= j) break; + while (lt(x, *j)) + j--; + while (i < j && lt(*i, x)) + i++; + if (i >= j) + break; C t = *i; *i = *j; *j = t; - i++; j--; + i++; + j--; } if (j == right - 1) { *left = *(right - 1); @@ -790,18 +985,22 @@ inline void qsort_Vec(C *left, C *right, bool (*lt)(C,C)) { right--; goto Lagain; } - if (left < j) qsort_Vec<C>(left, j + 1, lt); - if (j + 2 < right) qsort_Vec<C>(j + 1, right, lt); + if (left < j) + qsort_Vec<C>(left, j + 1, lt); + if (j + 2 < right) + qsort_Vec<C>(j + 1, right, lt); } } template <class C> -inline void qsort_VecRef(C *left, C *right, bool (*lt)(const C &, const C &)) { - Lagain: +inline void +qsort_VecRef(C *left, C *right, bool (*lt)(const C &, const C &)) +{ +Lagain: if (right - left < 5) { for (C *y = right - 1; y > left; y--) { for (C *x = left; x < y; x++) { - if (lt(x[1],x[0])) { + if (lt(x[1], x[0])) { C t = x[0]; x[0] = x[1]; x[1] = t; @@ -809,16 +1008,20 @@ inline void qsort_VecRef(C *left, C *right, bool (*lt)(const C &, const C &)) { } } } else { - C *i = left + 1, *j = right - 1; + C *i = left + 1, *j = right - 1; C x = *left; for (;;) { - while (lt(x,*j)) j--; - while (i < j && lt(*i,x)) i++; - if (i >= j) break; + while (lt(x, *j)) + j--; + while (i < j && lt(*i, x)) + i++; + if (i >= j) + break; C t = *i; *i = *j; *j = t; - i++; j--; + i++; + j--; } if (j == right - 1) { *left = *(right - 1); @@ -826,19 +1029,25 @@ inline void qsort_VecRef(C *left, C *right, bool (*lt)(const C &, const C &)) { right--; goto Lagain; } - if (left < j) qsort_VecRef<C>(left, j + 1, lt); - if (j + 2 < right) qsort_VecRef<C>(j + 1, right, lt); + if (left < j) + qsort_VecRef<C>(left, j + 1, lt); + if (j + 2 < right) + qsort_VecRef<C>(j + 1, right, lt); } } template <class C, class A, int S> -inline void Vec<C,A,S>::qsort(bool (*lt)(C,C)) { +inline void +Vec<C, A, S>::qsort(bool (*lt)(C, C)) +{ if (n) qsort_Vec<C>(&v[0], end(), lt); } template <class C, class A, int S> -inline void Vec<C,A,S>::qsort(bool (*lt)(const C &, const C &)) { +inline void +Vec<C, A, S>::qsort(bool (*lt)(const C &, const C &)) +{ if (n) qsort_VecRef<C>(&v[0], end(), lt); }
http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/ts/Version.cc ---------------------------------------------------------------------- diff --git a/lib/ts/Version.cc b/lib/ts/Version.cc index 19fc201..30669c0 100644 --- a/lib/ts/Version.cc +++ b/lib/ts/Version.cc @@ -41,30 +41,24 @@ AppVersionInfo::AppVersionInfo() void -AppVersionInfo::setup(const char *pkg_name, const char *app_name, const char *app_version, - const char *build_date, const char *build_time, const char *build_machine, - const char *build_person, const char *build_cflags) +AppVersionInfo::setup(const char *pkg_name, const char *app_name, const char *app_version, const char *build_date, + const char *build_time, const char *build_machine, const char *build_person, const char *build_cflags) { char month_name[8]; int year, month, day, hour, minute, second; bool invalid_datetime; - static const char *months[] = { - "Jan", "Feb", "Mar", "Apr", "May", "Jun", - "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "???" - }; + static const char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "???"}; - invalid_datetime = - sscanf(build_time, "%d:%d:%d", &hour, &minute, &second) < 3; - invalid_datetime |= - sscanf(build_date, "%3s %d %d", month_name, &day, &year) < 3; + invalid_datetime = sscanf(build_time, "%d:%d:%d", &hour, &minute, &second) < 3; + invalid_datetime |= sscanf(build_date, "%3s %d %d", month_name, &day, &year) < 3; // Jan=1, Feb=2 ... Dec=12, ???=13 for (month = 0; month < 11; month++) { if (strcasecmp(months[month], month_name) == 0) break; } - month ++; + month++; /////////////////////////////////////////// // now construct the version information // @@ -77,7 +71,7 @@ AppVersionInfo::setup(const char *pkg_name, const char *app_name, const char *ap // Otherwise take the build timestamp ("??????" if invalid). if (0 != strlen(BUILD_NUMBER)) { snprintf(BldNumStr, sizeof(BldNumStr), "%s", BUILD_NUMBER); - } else if (! invalid_datetime) { + } else if (!invalid_datetime) { snprintf(BldNumStr, sizeof(BldNumStr), "%02d%02d%02d", month, day, hour); } else { snprintf(BldNumStr, sizeof(BldNumStr), "??????"); @@ -113,9 +107,8 @@ AppVersionInfo::setup(const char *pkg_name, const char *app_name, const char *ap if (FullVersionInfoStr[0] == '\0') ink_strlcpy(FullVersionInfoStr, "?", sizeof(FullVersionInfoStr)); - snprintf(FullVersionInfoStr, sizeof(FullVersionInfoStr), - "%s - %s - %s - (build # %s on %s at %s)", - PkgStr, AppStr, VersionStr, BldNumStr, BldDateStr, BldTimeStr); + snprintf(FullVersionInfoStr, sizeof(FullVersionInfoStr), "%s - %s - %s - (build # %s on %s at %s)", PkgStr, AppStr, VersionStr, + BldNumStr, BldDateStr, BldTimeStr); defined = 1; } @@ -127,8 +120,7 @@ AppVersionInfo::setup(const char *pkg_name, const char *app_name, const char *ap /** * AppVersionInfo class test. */ -REGRESSION_TEST(AppVersionInfo)(RegressionTest* t, int /* atype ATS_UNUSED */, - int* pstatus) +REGRESSION_TEST(AppVersionInfo)(RegressionTest *t, int /* atype ATS_UNUSED */, int *pstatus) { *pstatus = REGRESSION_TEST_PASSED; @@ -136,16 +128,13 @@ REGRESSION_TEST(AppVersionInfo)(RegressionTest* t, int /* atype ATS_UNUSED */, TestBox tb(t, pstatus); - const char * errMsgFormat = "wrong build number, expected '%s', got '%s'"; - const char * bench[][3] = - { - // date, time, resulting build number - {"Oct 4 1957", "19:28:34", BUILD_NUMBER}, - {"Oct 4 1957", "19:28:34", "100419"}, - {"Apr 4 1957", "09:08:04", "040409"}, - {" 4 Apr 1957", "09:08:04", "??????"}, - {"Apr 4 1957", "09-08-04", "??????"} - }; + const char *errMsgFormat = "wrong build number, expected '%s', got '%s'"; + const char *bench[][3] = {// date, time, resulting build number + {"Oct 4 1957", "19:28:34", BUILD_NUMBER}, + {"Oct 4 1957", "19:28:34", "100419"}, + {"Apr 4 1957", "09:08:04", "040409"}, + {" 4 Apr 1957", "09:08:04", "??????"}, + {"Apr 4 1957", "09-08-04", "??????"}}; int benchSize = sizeof(bench) / sizeof(bench[0]); @@ -154,16 +143,12 @@ REGRESSION_TEST(AppVersionInfo)(RegressionTest* t, int /* atype ATS_UNUSED */, // possible to change the version value from inside the regression test. // If not empty BUILD_NUMBER overrides any result, in this case run only // this test (the rest will always fail). - info.setup("Apache Traffic Server", "traffic_server", "5.2.1", - bench[0][0], bench[0][1], "build_slave", "builder", ""); - tb.check(0 == strcmp(info.BldNumStr, bench[0][2]), errMsgFormat, - bench[0][2], info.BldNumStr); + info.setup("Apache Traffic Server", "traffic_server", "5.2.1", bench[0][0], bench[0][1], "build_slave", "builder", ""); + tb.check(0 == strcmp(info.BldNumStr, bench[0][2]), errMsgFormat, bench[0][2], info.BldNumStr); } else { for (int i = 1; i < benchSize; i++) { - info.setup("Apache Traffic Server", "traffic_server", "5.2.1", - bench[i][0], bench[i][1], "build_slave", "builder", ""); - tb.check(0 == strcmp(info.BldNumStr, bench[i][2]), errMsgFormat, - bench[i][2], info.BldNumStr); + info.setup("Apache Traffic Server", "traffic_server", "5.2.1", bench[i][0], bench[i][1], "build_slave", "builder", ""); + tb.check(0 == strcmp(info.BldNumStr, bench[i][2]), errMsgFormat, bench[i][2], info.BldNumStr); } } } http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/ts/defalloc.h ---------------------------------------------------------------------- diff --git a/lib/ts/defalloc.h b/lib/ts/defalloc.h index 90ff451..ce20678 100644 --- a/lib/ts/defalloc.h +++ b/lib/ts/defalloc.h @@ -21,14 +21,23 @@ limitations under the License. */ #ifndef _defalloc_H_ -#define _defalloc_H_ +#define _defalloc_H_ #include "ink_memory.h" -class DefaultAlloc { public: - static void *alloc(int s) { return ats_malloc(s); } - static void free(void *p) { ats_free(p); } +class DefaultAlloc +{ +public: + static void * + alloc(int s) + { + return ats_malloc(s); + } + static void + free(void *p) + { + ats_free(p); + } }; #endif - http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/ts/fastlz.c ---------------------------------------------------------------------- diff --git a/lib/ts/fastlz.c b/lib/ts/fastlz.c index 9ce8803..f077edd 100644 --- a/lib/ts/fastlz.c +++ b/lib/ts/fastlz.c @@ -36,11 +36,11 @@ * Give hints to the compiler for branch prediction optimization. */ #if defined(__GNUC__) && (__GNUC__ > 2) -#define FASTLZ_EXPECT_CONDITIONAL(c) (__builtin_expect((c), 1)) -#define FASTLZ_UNEXPECT_CONDITIONAL(c) (__builtin_expect((c), 0)) +#define FASTLZ_EXPECT_CONDITIONAL(c) (__builtin_expect((c), 1)) +#define FASTLZ_UNEXPECT_CONDITIONAL(c) (__builtin_expect((c), 0)) #else -#define FASTLZ_EXPECT_CONDITIONAL(c) (c) -#define FASTLZ_UNEXPECT_CONDITIONAL(c) (c) +#define FASTLZ_EXPECT_CONDITIONAL(c) (c) +#define FASTLZ_UNEXPECT_CONDITIONAL(c) (c) #endif /* @@ -59,7 +59,7 @@ */ #if !defined(FASTLZ_STRICT_ALIGN) #define FASTLZ_STRICT_ALIGN -#if defined(__i386__) || defined(__386) /* GNU C, Sun Studio */ +#if defined(__i386__) || defined(__386) /* GNU C, Sun Studio */ #undef FASTLZ_STRICT_ALIGN #elif defined(__i486__) || defined(__i586__) || defined(__i686__) /* GNU C */ #undef FASTLZ_STRICT_ALIGN @@ -77,29 +77,34 @@ /* * FIXME: use preprocessor magic to set this on different platforms! */ -typedef unsigned char flzuint8; +typedef unsigned char flzuint8; typedef unsigned short flzuint16; -typedef unsigned int flzuint32; +typedef unsigned int flzuint32; /* prototypes */ -int fastlz_compress(const void* input, int length, void* output); -int fastlz_compress_level(int level, const void* input, int length, void* output); -int fastlz_decompress(const void* input, int length, void* output, int maxout); +int fastlz_compress(const void *input, int length, void *output); +int fastlz_compress_level(int level, const void *input, int length, void *output); +int fastlz_decompress(const void *input, int length, void *output, int maxout); -#define MAX_COPY 32 -#define MAX_LEN 264 /* 256 + 8 */ +#define MAX_COPY 32 +#define MAX_LEN 264 /* 256 + 8 */ #define MAX_DISTANCE 8192 #if !defined(FASTLZ_STRICT_ALIGN) -#define FASTLZ_READU16(p) *((const flzuint16*)(p)) +#define FASTLZ_READU16(p) *((const flzuint16 *)(p)) #else -#define FASTLZ_READU16(p) ((p)[0] | (p)[1]<<8) +#define FASTLZ_READU16(p) ((p)[0] | (p)[1] << 8) #endif -#define HASH_LOG 13 -#define HASH_SIZE (1<< HASH_LOG) -#define HASH_MASK (HASH_SIZE-1) -#define HASH_FUNCTION(v,p) { v = FASTLZ_READU16(p); v ^= FASTLZ_READU16(p+1)^(v>>(16-HASH_LOG));v &= HASH_MASK; } +#define HASH_LOG 13 +#define HASH_SIZE (1 << HASH_LOG) +#define HASH_MASK (HASH_SIZE - 1) +#define HASH_FUNCTION(v, p) \ + { \ + v = FASTLZ_READU16(p); \ + v ^= FASTLZ_READU16(p + 1) ^ (v >> (16 - HASH_LOG)); \ + v &= HASH_MASK; \ + } #undef FASTLZ_LEVEL #define FASTLZ_LEVEL 1 @@ -108,8 +113,8 @@ int fastlz_decompress(const void* input, int length, void* output, int maxout); #undef FASTLZ_DECOMPRESSOR #define FASTLZ_COMPRESSOR fastlz1_compress #define FASTLZ_DECOMPRESSOR fastlz1_decompress -static FASTLZ_INLINE int FASTLZ_COMPRESSOR(const void* input, int length, void* output); -static FASTLZ_INLINE int FASTLZ_DECOMPRESSOR(const void* input, int length, void* output, int maxout); +static FASTLZ_INLINE int FASTLZ_COMPRESSOR(const void *input, int length, void *output); +static FASTLZ_INLINE int FASTLZ_DECOMPRESSOR(const void *input, int length, void *output, int maxout); #include "fastlz.c" #undef FASTLZ_LEVEL @@ -117,45 +122,48 @@ static FASTLZ_INLINE int FASTLZ_DECOMPRESSOR(const void* input, int length, void #undef MAX_DISTANCE #define MAX_DISTANCE 8191 -#define MAX_FARDISTANCE (65535+MAX_DISTANCE-1) +#define MAX_FARDISTANCE (65535 + MAX_DISTANCE - 1) #undef FASTLZ_COMPRESSOR #undef FASTLZ_DECOMPRESSOR #define FASTLZ_COMPRESSOR fastlz2_compress #define FASTLZ_DECOMPRESSOR fastlz2_decompress -static FASTLZ_INLINE int FASTLZ_COMPRESSOR(const void* input, int length, void* output); -static FASTLZ_INLINE int FASTLZ_DECOMPRESSOR(const void* input, int length, void* output, int maxout); +static FASTLZ_INLINE int FASTLZ_COMPRESSOR(const void *input, int length, void *output); +static FASTLZ_INLINE int FASTLZ_DECOMPRESSOR(const void *input, int length, void *output, int maxout); #include "fastlz.c" -int fastlz_compress(const void* input, int length, void* output) +int +fastlz_compress(const void *input, int length, void *output) { /* for short block, choose fastlz1 */ - if(length < 65536) + if (length < 65536) return fastlz1_compress(input, length, output); /* else... */ return fastlz2_compress(input, length, output); } -int fastlz_decompress(const void* input, int length, void* output, int maxout) +int +fastlz_decompress(const void *input, int length, void *output, int maxout) { /* magic identifier for compression level */ - int level = ((*(const flzuint8*)input) >> 5) + 1; + int level = ((*(const flzuint8 *)input) >> 5) + 1; - if(level == 1) + if (level == 1) return fastlz1_decompress(input, length, output, maxout); - if(level == 2) + if (level == 2) return fastlz2_decompress(input, length, output, maxout); /* unknown level, trigger error */ return 0; } -int fastlz_compress_level(int level, const void* input, int length, void* output) +int +fastlz_compress_level(int level, const void *input, int length, void *output) { - if(level == 1) + if (level == 1) return fastlz1_compress(input, length, output); - if(level == 2) + if (level == 2) return fastlz2_compress(input, length, output); return 0; @@ -163,32 +171,30 @@ int fastlz_compress_level(int level, const void* input, int length, void* output #else /* !defined(FASTLZ_COMPRESSOR) && !defined(FASTLZ_DECOMPRESSOR) */ -static FASTLZ_INLINE int FASTLZ_COMPRESSOR(const void* input, int length, void* output) +static FASTLZ_INLINE int +FASTLZ_COMPRESSOR(const void *input, int length, void *output) { - const flzuint8* ip = (const flzuint8*) input; - const flzuint8* ip_bound = ip + length - 2; - const flzuint8* ip_limit = ip + length - 12; - flzuint8* op = (flzuint8*) output; + const flzuint8 *ip = (const flzuint8 *)input; + const flzuint8 *ip_bound = ip + length - 2; + const flzuint8 *ip_limit = ip + length - 12; + flzuint8 *op = (flzuint8 *)output; - const flzuint8* htab[HASH_SIZE]; - const flzuint8** hslot; + const flzuint8 *htab[HASH_SIZE]; + const flzuint8 **hslot; flzuint32 hval; flzuint32 copy; /* sanity check */ - if(FASTLZ_UNEXPECT_CONDITIONAL(length < 4)) - { - if(length) - { + if (FASTLZ_UNEXPECT_CONDITIONAL(length < 4)) { + if (length) { /* create literal copy only */ - *op++ = length-1; + *op++ = length - 1; ip_bound++; - while(ip <= ip_bound) + while (ip <= ip_bound) *op++ = *ip++; - return length+1; - } - else + return length + 1; + } else return 0; } @@ -198,26 +204,24 @@ static FASTLZ_INLINE int FASTLZ_COMPRESSOR(const void* input, int length, void* /* we start with literal copy */ copy = 2; - *op++ = MAX_COPY-1; + *op++ = MAX_COPY - 1; *op++ = *ip++; *op++ = *ip++; /* main loop */ - while(FASTLZ_EXPECT_CONDITIONAL(ip < ip_limit)) - { - const flzuint8* ref; + while (FASTLZ_EXPECT_CONDITIONAL(ip < ip_limit)) { + const flzuint8 *ref; flzuint32 distance; /* minimum match length */ flzuint32 len = 3; /* comparison starting-point */ - const flzuint8* anchor = ip; + const flzuint8 *anchor = ip; - /* check for a run */ -#if FASTLZ_LEVEL==2 - if(ip[0] == ip[-1] && FASTLZ_READU16(ip-1)==FASTLZ_READU16(ip+1)) - { +/* check for a run */ +#if FASTLZ_LEVEL == 2 + if (ip[0] == ip[-1] && FASTLZ_READU16(ip - 1) == FASTLZ_READU16(ip + 1)) { distance = 1; ref = anchor - 1 + 3; goto match; @@ -225,7 +229,7 @@ static FASTLZ_INLINE int FASTLZ_COMPRESSOR(const void* input, int length, void* #endif /* find potential match */ - HASH_FUNCTION(hval,ip); + HASH_FUNCTION(hval, ip); hslot = htab + hval; ref = htab[hval]; @@ -236,25 +240,24 @@ static FASTLZ_INLINE int FASTLZ_COMPRESSOR(const void* input, int length, void* *hslot = anchor; /* is this a match? check the first 3 bytes */ - if(distance==0 || -#if FASTLZ_LEVEL==1 - (distance >= MAX_DISTANCE) || + if (distance == 0 || +#if FASTLZ_LEVEL == 1 + (distance >= MAX_DISTANCE) || #else - (distance >= MAX_FARDISTANCE) || + (distance >= MAX_FARDISTANCE) || #endif - *ref++ != *ip++ || *ref++!=*ip++ || *ref++!=*ip++) + *ref++ != *ip++ || *ref++ != *ip++ || *ref++ != *ip++) goto literal; -#if FASTLZ_LEVEL==2 +#if FASTLZ_LEVEL == 2 /* far, needs at least 5-byte match */ - if(distance >= MAX_DISTANCE) - { - if(*ip++ != *ref++ || *ip++!= *ref++) + if (distance >= MAX_DISTANCE) { + if (*ip++ != *ref++ || *ip++ != *ref++) goto literal; len += 2; } - match: + match: #endif /* last matched byte */ @@ -263,34 +266,43 @@ static FASTLZ_INLINE int FASTLZ_COMPRESSOR(const void* input, int length, void* /* distance is biased */ distance--; - if(!distance) - { + if (!distance) { /* zero distance means a run */ flzuint8 x = ip[-1]; - while(ip < ip_bound) - if(*ref++ != x) break; else ip++; - } - else - for(;;) - { - /* safe because the outer check against ip limit */ - if(*ref++ != *ip++) break; - if(*ref++ != *ip++) break; - if(*ref++ != *ip++) break; - if(*ref++ != *ip++) break; - if(*ref++ != *ip++) break; - if(*ref++ != *ip++) break; - if(*ref++ != *ip++) break; - if(*ref++ != *ip++) break; - while(ip < ip_bound) - if(*ref++ != *ip++) break; - break; - } + while (ip < ip_bound) + if (*ref++ != x) + break; + else + ip++; + } else + for (;;) { + /* safe because the outer check against ip limit */ + if (*ref++ != *ip++) + break; + if (*ref++ != *ip++) + break; + if (*ref++ != *ip++) + break; + if (*ref++ != *ip++) + break; + if (*ref++ != *ip++) + break; + if (*ref++ != *ip++) + break; + if (*ref++ != *ip++) + break; + if (*ref++ != *ip++) + break; + while (ip < ip_bound) + if (*ref++ != *ip++) + break; + break; + } /* if we have copied something, adjust the copy count */ - if(copy) + if (copy) /* copy is biased, '0' means 1 byte copy */ - *(op-copy-1) = copy-1; + *(op - copy - 1) = copy - 1; else /* back, to overwrite the copy count */ op--; @@ -302,40 +314,31 @@ static FASTLZ_INLINE int FASTLZ_COMPRESSOR(const void* input, int length, void* ip -= 3; len = ip - anchor; - /* encode the match */ -#if FASTLZ_LEVEL==2 - if(distance < MAX_DISTANCE) - { - if(len < 7) - { +/* encode the match */ +#if FASTLZ_LEVEL == 2 + if (distance < MAX_DISTANCE) { + if (len < 7) { *op++ = (len << 5) + (distance >> 8); *op++ = (distance & 255); - } - else - { + } else { *op++ = (7 << 5) + (distance >> 8); - for(len-=7; len >= 255; len-= 255) + for (len -= 7; len >= 255; len -= 255) *op++ = 255; *op++ = len; *op++ = (distance & 255); } - } - else - { + } else { /* far away, but not yet in the another galaxy... */ - if(len < 7) - { + if (len < 7) { distance -= MAX_DISTANCE; *op++ = (len << 5) + 31; *op++ = 255; *op++ = distance >> 8; *op++ = distance & 255; - } - else - { + } else { distance -= MAX_DISTANCE; *op++ = (7 << 5) + 31; - for(len-=7; len >= 255; len-= 255) + for (len -= 7; len >= 255; len -= 255) *op++ = 255; *op++ = len; *op++ = 255; @@ -345,22 +348,18 @@ static FASTLZ_INLINE int FASTLZ_COMPRESSOR(const void* input, int length, void* } #else - if(FASTLZ_UNEXPECT_CONDITIONAL(len > MAX_LEN-2)) - while(len > MAX_LEN-2) - { + if (FASTLZ_UNEXPECT_CONDITIONAL(len > MAX_LEN - 2)) + while (len > MAX_LEN - 2) { *op++ = (7 << 5) + (distance >> 8); - *op++ = MAX_LEN - 2 - 7 -2; + *op++ = MAX_LEN - 2 - 7 - 2; *op++ = (distance & 255); - len -= MAX_LEN-2; + len -= MAX_LEN - 2; } - if(len < 7) - { + if (len < 7) { *op++ = (len << 5) + (distance >> 8); *op++ = (distance & 255); - } - else - { + } else { *op++ = (7 << 5) + (distance >> 8); *op++ = len - 7; *op++ = (distance & 255); @@ -368,127 +367,118 @@ static FASTLZ_INLINE int FASTLZ_COMPRESSOR(const void* input, int length, void* #endif /* update the hash at match boundary */ - HASH_FUNCTION(hval,ip); + HASH_FUNCTION(hval, ip); htab[hval] = ip++; - HASH_FUNCTION(hval,ip); + HASH_FUNCTION(hval, ip); htab[hval] = ip++; /* assuming literal copy */ - *op++ = MAX_COPY-1; + *op++ = MAX_COPY - 1; continue; - literal: - *op++ = *anchor++; - ip = anchor; - copy++; - if(FASTLZ_UNEXPECT_CONDITIONAL(copy == MAX_COPY)) - { - copy = 0; - *op++ = MAX_COPY-1; - } + literal: + *op++ = *anchor++; + ip = anchor; + copy++; + if (FASTLZ_UNEXPECT_CONDITIONAL(copy == MAX_COPY)) { + copy = 0; + *op++ = MAX_COPY - 1; + } } /* left-over as literal copy */ ip_bound++; - while(ip <= ip_bound) - { + while (ip <= ip_bound) { *op++ = *ip++; copy++; - if(copy == MAX_COPY) - { + if (copy == MAX_COPY) { copy = 0; - *op++ = MAX_COPY-1; + *op++ = MAX_COPY - 1; } } /* if we have copied something, adjust the copy length */ - if(copy) - *(op-copy-1) = copy-1; + if (copy) + *(op - copy - 1) = copy - 1; else op--; -#if FASTLZ_LEVEL==2 +#if FASTLZ_LEVEL == 2 /* marker for fastlz2 */ - *(flzuint8*)output |= (1 << 5); + *(flzuint8 *)output |= (1 << 5); #endif - return op - (flzuint8*)output; + return op - (flzuint8 *)output; } -static FASTLZ_INLINE int FASTLZ_DECOMPRESSOR(const void* input, int length, void* output, int maxout) +static FASTLZ_INLINE int +FASTLZ_DECOMPRESSOR(const void *input, int length, void *output, int maxout) { - const flzuint8* ip = (const flzuint8*) input; - const flzuint8* ip_limit = ip + length; - flzuint8* op = (flzuint8*) output; - flzuint8* op_limit = op + maxout; + const flzuint8 *ip = (const flzuint8 *)input; + const flzuint8 *ip_limit = ip + length; + flzuint8 *op = (flzuint8 *)output; + flzuint8 *op_limit = op + maxout; flzuint32 ctrl = (*ip++) & 31; int loop = 1; - do - { - const flzuint8* ref = op; + do { + const flzuint8 *ref = op; flzuint32 len = ctrl >> 5; flzuint32 ofs = (ctrl & 31) << 8; - if(ctrl >= 32) - { -#if FASTLZ_LEVEL==2 + if (ctrl >= 32) { +#if FASTLZ_LEVEL == 2 flzuint8 code; #endif len--; ref -= ofs; - if (len == 7-1) -#if FASTLZ_LEVEL==1 + if (len == 7 - 1) +#if FASTLZ_LEVEL == 1 len += *ip++; ref -= *ip++; #else - do - { + do { code = *ip++; len += code; - } while (code==255); + } while (code == 255); code = *ip++; ref -= code; /* match from 16-bit distance */ - if(FASTLZ_UNEXPECT_CONDITIONAL(code==255)) - if(FASTLZ_EXPECT_CONDITIONAL(ofs==(31 << 8))) - { - ofs = (*ip++) << 8; - ofs += *ip++; - ref = op - ofs - MAX_DISTANCE; - } + if (FASTLZ_UNEXPECT_CONDITIONAL(code == 255)) + if (FASTLZ_EXPECT_CONDITIONAL(ofs == (31 << 8))) { + ofs = (*ip++) << 8; + ofs += *ip++; + ref = op - ofs - MAX_DISTANCE; + } #endif #ifdef FASTLZ_SAFE if (FASTLZ_UNEXPECT_CONDITIONAL(op + len + 3 > op_limit)) return 0; - if (FASTLZ_UNEXPECT_CONDITIONAL(ref-1 < (flzuint8 *)output)) + if (FASTLZ_UNEXPECT_CONDITIONAL(ref - 1 < (flzuint8 *)output)) return 0; #endif - if(FASTLZ_EXPECT_CONDITIONAL(ip < ip_limit)) + if (FASTLZ_EXPECT_CONDITIONAL(ip < ip_limit)) ctrl = *ip++; else loop = 0; - if(ref == op) - { + if (ref == op) { /* optimize copy for a run */ flzuint8 b = ref[-1]; *op++ = b; *op++ = b; *op++ = b; - for(; len; --len) + for (; len; --len) *op++ = b; - } - else - { + } else { #if !defined(FASTLZ_STRICT_ALIGN) - const flzuint16* p; - flzuint16* q; + const flzuint16 *p; + flzuint16 *q; #endif /* copy from reference */ ref--; @@ -498,33 +488,29 @@ static FASTLZ_INLINE int FASTLZ_DECOMPRESSOR(const void* input, int length, void #if !defined(FASTLZ_STRICT_ALIGN) /* copy a byte, so that now it's word aligned */ - if(len & 1) - { + if (len & 1) { *op++ = *ref++; len--; } /* copy 16-bit at once */ - q = (flzuint16*) op; + q = (flzuint16 *)op; op += len; - p = (const flzuint16*) ref; - for(len>>=1; len > 4; len-=4) - { + p = (const flzuint16 *)ref; + for (len >>= 1; len > 4; len -= 4) { *q++ = *p++; *q++ = *p++; *q++ = *p++; *q++ = *p++; } - for(; len; --len) + for (; len; --len) *q++ = *p++; #else - for(; len; --len) + for (; len; --len) *op++ = *ref++; #endif } - } - else - { + } else { ctrl++; #ifdef FASTLZ_SAFE if (FASTLZ_UNEXPECT_CONDITIONAL(op + ctrl > op_limit)) @@ -534,17 +520,16 @@ static FASTLZ_INLINE int FASTLZ_DECOMPRESSOR(const void* input, int length, void #endif *op++ = *ip++; - for(--ctrl; ctrl; ctrl--) + for (--ctrl; ctrl; ctrl--) *op++ = *ip++; loop = FASTLZ_EXPECT_CONDITIONAL(ip < ip_limit); - if(loop) + if (loop) ctrl = *ip++; } - } - while(FASTLZ_EXPECT_CONDITIONAL(loop)); + } while (FASTLZ_EXPECT_CONDITIONAL(loop)); - return op - (flzuint8*)output; + return op - (flzuint8 *)output; } #endif /* !defined(FASTLZ_COMPRESSOR) && !defined(FASTLZ_DECOMPRESSOR) */ http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/ts/fastlz.h ---------------------------------------------------------------------- diff --git a/lib/ts/fastlz.h b/lib/ts/fastlz.h index e5ca8df..a71f992 100644 --- a/lib/ts/fastlz.h +++ b/lib/ts/fastlz.h @@ -29,13 +29,13 @@ #define FASTLZ_VERSION 0x000100 -#define FASTLZ_VERSION_MAJOR 0 -#define FASTLZ_VERSION_MINOR 0 -#define FASTLZ_VERSION_REVISION 0 +#define FASTLZ_VERSION_MAJOR 0 +#define FASTLZ_VERSION_MINOR 0 +#define FASTLZ_VERSION_REVISION 0 #define FASTLZ_VERSION_STRING "0.1.0" -#if defined (__cplusplus) +#if defined(__cplusplus) extern "C" { #endif @@ -53,7 +53,7 @@ extern "C" { The input buffer and the output buffer can not overlap. */ -int fastlz_compress(const void* input, int length, void* output); +int fastlz_compress(const void *input, int length, void *output); /** Decompress a block of compressed data and returns the size of the @@ -67,7 +67,7 @@ int fastlz_compress(const void* input, int length, void* output); more than what is specified in maxout. */ -int fastlz_decompress(const void* input, int length, void* output, int maxout); +int fastlz_decompress(const void *input, int length, void *output, int maxout); /** Compress a block of data in the input buffer and returns the size of @@ -91,9 +91,9 @@ int fastlz_decompress(const void* input, int length, void* output, int maxout); decompressed using the function fastlz_decompress above. */ -int fastlz_compress_level(int level, const void* input, int length, void* output); +int fastlz_compress_level(int level, const void *input, int length, void *output); -#if defined (__cplusplus) +#if defined(__cplusplus) } #endif http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/ts/ink_aiocb.h ---------------------------------------------------------------------- diff --git a/lib/ts/ink_aiocb.h b/lib/ts/ink_aiocb.h index 0271d2e..0689437 100644 --- a/lib/ts/ink_aiocb.h +++ b/lib/ts/ink_aiocb.h @@ -36,28 +36,27 @@ /* TODO use native aiocb where possible */ -#define LIO_READ 0x1 -#define LIO_WRITE 0x2 +#define LIO_READ 0x1 +#define LIO_WRITE 0x2 -struct ink_aiocb -{ +struct ink_aiocb { int aio_fildes; -#if defined(__STDC__) - volatile void *aio_buf; /* buffer location */ +#if defined(__STDC__) + volatile void *aio_buf; /* buffer location */ #else - void *aio_buf; /* buffer location */ + void *aio_buf; /* buffer location */ #endif - size_t aio_nbytes; /* length of transfer */ + size_t aio_nbytes; /* length of transfer */ // TODO change to off_t - off_t aio_offset; /* file offset */ + off_t aio_offset; /* file offset */ - int aio_reqprio; /* request priority offset */ + int aio_reqprio; /* request priority offset */ // struct sigevent aio_sigevent; /* signal number and offset */ - int aio_lio_opcode; /* listio operation */ + int aio_lio_opcode; /* listio operation */ // aio_result_t aio_resultp; /* results */ - int aio_state; /* state flag for List I/O */ - int aio__pad[1]; /* extension padding */ + int aio_state; /* state flag for List I/O */ + int aio__pad[1]; /* extension padding */ }; #endif http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/ts/ink_align.h ---------------------------------------------------------------------- diff --git a/lib/ts/ink_align.h b/lib/ts/ink_align.h index f865d8d..63f966a 100644 --- a/lib/ts/ink_align.h +++ b/lib/ts/ink_align.h @@ -25,7 +25,7 @@ #ifndef _ink_align_h_ #define _ink_align_h_ -# include "ink_time.h" +#include "ink_time.h" union Alias32 { uint8_t byte[4]; @@ -48,8 +48,7 @@ union Alias64 { #define INK_MIN_ALIGN 8 /* INK_ALIGN() is only to be used to align on a power of 2 boundary */ -#define INK_ALIGN(size, boundary) \ - (((size) + ((boundary) - 1)) & ~((boundary) - 1)) +#define INK_ALIGN(size, boundary) (((size) + ((boundary)-1)) & ~((boundary)-1)) /** Default alignment */ #define INK_ALIGN_DEFAULT(size) INK_ALIGN(size, INK_MIN_ALIGN) @@ -60,13 +59,13 @@ union Alias64 { static inline void * align_pointer_forward(const void *pointer_, size_t alignment) { - char *pointer = (char *) pointer_; + char *pointer = (char *)pointer_; // // Round up alignment.. // - pointer = (char *) INK_ALIGN((ptrdiff_t) pointer, alignment); + pointer = (char *)INK_ALIGN((ptrdiff_t)pointer, alignment); - return (void *) pointer; + return (void *)pointer; } // @@ -76,8 +75,8 @@ align_pointer_forward(const void *pointer_, size_t alignment) static inline void * align_pointer_forward_and_zero(const void *pointer_, size_t alignment) { - char *pointer = (char *) pointer_; - char *aligned = (char *) INK_ALIGN((ptrdiff_t) pointer, alignment); + char *pointer = (char *)pointer_; + char *aligned = (char *)INK_ALIGN((ptrdiff_t)pointer, alignment); // // Fill the skippings.. // @@ -86,7 +85,7 @@ align_pointer_forward_and_zero(const void *pointer_, size_t alignment) pointer++; } - return (void *) aligned; + return (void *)aligned; } // http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/ts/ink_apidefs.h ---------------------------------------------------------------------- diff --git a/lib/ts/ink_apidefs.h b/lib/ts/ink_apidefs.h index 16f1bb9..0eb1d04 100644 --- a/lib/ts/ink_apidefs.h +++ b/lib/ts/ink_apidefs.h @@ -31,17 +31,17 @@ #if defined(__GNUC__) || defined(__clang__) #ifndef likely -#define likely(x) __builtin_expect (!!(x), 1) +#define likely(x) __builtin_expect(!!(x), 1) #endif #ifndef unlikely -#define unlikely(x) __builtin_expect (!!(x), 0) +#define unlikely(x) __builtin_expect(!!(x), 0) #endif #else #ifndef likely -#define likely(x) (x) +#define likely(x) (x) #endif #ifndef unlikely -#define unlikely(x) (x) +#define unlikely(x) (x) #endif #endif @@ -65,7 +65,7 @@ #if !defined(TS_NONNULL) #if defined(__GNUC__) || defined(__clang__) -#define TS_NONNULL(...) __attribute__((nonnull (__VA_ARGS__))) +#define TS_NONNULL(...) __attribute__((nonnull(__VA_ARGS__))) #else #define TS_NONNULL(...) #endif http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/ts/ink_args.cc ---------------------------------------------------------------------- diff --git a/lib/ts/ink_args.cc b/lib/ts/ink_args.cc index c121813..12ab5c0 100644 --- a/lib/ts/ink_args.cc +++ b/lib/ts/ink_args.cc @@ -32,49 +32,41 @@ Process arguments // Global variables // -const char *file_arguments[MAX_FILE_ARGUMENTS] = { 0 }; -const char *program_name = (char *) "Traffic Server"; +const char *file_arguments[MAX_FILE_ARGUMENTS] = {0}; +const char *program_name = (char *)"Traffic Server"; unsigned n_file_arguments = 0; // // Local variables // -static const char *argument_types_keys = (char *) "ISDfFTL"; -static const char *argument_types_descriptions[] = { - (char *) "int ", - (char *) "str ", - (char *) "dbl ", - (char *) "off ", - (char *) "on ", - (char *) "tog ", - (char *) "i64 ", - (char *) " " -}; +static const char *argument_types_keys = (char *)"ISDfFTL"; +static const char *argument_types_descriptions[] = {(char *)"int ", (char *) "str ", (char *) "dbl ", (char *) "off ", + (char *) "on ", (char *) "tog ", (char *) "i64 ", (char *) " "}; // // Functions // static bool -arg_is_version_flag(const ArgumentDescription * arg) +arg_is_version_flag(const ArgumentDescription *arg) { return strcmp(arg->name, "version") == 0 && arg->key == 'V'; } static void -append_file_argument(const char * arg) +append_file_argument(const char *arg) { - if (n_file_arguments >= countof(file_arguments)) { - ink_fatal("too many files"); - } + if (n_file_arguments >= countof(file_arguments)) { + ink_fatal("too many files"); + } - file_arguments[n_file_arguments++] = arg; + file_arguments[n_file_arguments++] = arg; } static bool -process_arg(const AppVersionInfo * appinfo, const ArgumentDescription * argument_descriptions, - unsigned n_argument_descriptions, int i, const char ***argv) +process_arg(const AppVersionInfo *appinfo, const ArgumentDescription *argument_descriptions, unsigned n_argument_descriptions, + int i, const char ***argv) { const char *arg = NULL; @@ -86,9 +78,9 @@ process_arg(const AppVersionInfo * appinfo, const ArgumentDescription * argument if (argument_descriptions[i].type) { char type = argument_descriptions[i].type[0]; if (type == 'F' || type == 'f') { - *(int *) argument_descriptions[i].location = type == 'F' ? 1 : 0; + *(int *)argument_descriptions[i].location = type == 'F' ? 1 : 0; } else if (type == 'T') { - *(int *) argument_descriptions[i].location = !*(int *) argument_descriptions[i].location; + *(int *)argument_descriptions[i].location = !*(int *)argument_descriptions[i].location; } else { arg = *++(**argv) ? **argv : *++(*argv); if (!arg) { @@ -96,20 +88,20 @@ process_arg(const AppVersionInfo * appinfo, const ArgumentDescription * argument } switch (type) { case 'I': - *(int *) argument_descriptions[i].location = atoi(arg); + *(int *)argument_descriptions[i].location = atoi(arg); break; case 'D': - *(double *) argument_descriptions[i].location = atof(arg); + *(double *)argument_descriptions[i].location = atof(arg); break; case 'L': - *(int64_t *) argument_descriptions[i].location = ink_atoi64(arg); + *(int64_t *)argument_descriptions[i].location = ink_atoi64(arg); break; case 'S': if (argument_descriptions[i].type[1] == '*') { - char ** out = (char **)argument_descriptions[i].location; + char **out = (char **)argument_descriptions[i].location; *out = ats_strdup(arg); } else { - ink_strlcpy((char *) argument_descriptions[i].location, arg, atoi(argument_descriptions[i].type + 1)); + ink_strlcpy((char *)argument_descriptions[i].location, arg, atoi(argument_descriptions[i].type + 1)); } break; default: @@ -129,7 +121,7 @@ process_arg(const AppVersionInfo * appinfo, const ArgumentDescription * argument void -show_argument_configuration(const ArgumentDescription * argument_descriptions, unsigned n_argument_descriptions) +show_argument_configuration(const ArgumentDescription *argument_descriptions, unsigned n_argument_descriptions) { printf("Argument Configuration\n"); for (unsigned i = 0; i < n_argument_descriptions; i++) { @@ -139,19 +131,19 @@ show_argument_configuration(const ArgumentDescription * argument_descriptions, u case 'F': case 'f': case 'T': - printf(*(int *) argument_descriptions[i].location ? "TRUE" : "FALSE"); + printf(*(int *)argument_descriptions[i].location ? "TRUE" : "FALSE"); break; case 'I': - printf("%d", *(int *) argument_descriptions[i].location); + printf("%d", *(int *)argument_descriptions[i].location); break; case 'D': - printf("%f", *(double *) argument_descriptions[i].location); + printf("%f", *(double *)argument_descriptions[i].location); break; case 'L': - printf("%" PRId64 "", *(int64_t *) argument_descriptions[i].location); + printf("%" PRId64 "", *(int64_t *)argument_descriptions[i].location); break; case 'S': - printf("%s", (char *) argument_descriptions[i].location); + printf("%s", (char *)argument_descriptions[i].location); break; default: ink_fatal("bad argument description"); @@ -163,16 +155,17 @@ show_argument_configuration(const ArgumentDescription * argument_descriptions, u } void -process_args(const AppVersionInfo * appinfo, const ArgumentDescription * argument_descriptions, unsigned n_argument_descriptions, const char **argv, const char *usage_string) +process_args(const AppVersionInfo *appinfo, const ArgumentDescription *argument_descriptions, unsigned n_argument_descriptions, + const char **argv, const char *usage_string) { if (!process_args_ex(appinfo, argument_descriptions, n_argument_descriptions, argv)) { - usage(argument_descriptions, n_argument_descriptions, usage_string); + usage(argument_descriptions, n_argument_descriptions, usage_string); } } bool -process_args_ex(const AppVersionInfo * appinfo, const ArgumentDescription * argument_descriptions, - unsigned n_argument_descriptions, const char **argv) +process_args_ex(const AppVersionInfo *appinfo, const ArgumentDescription *argument_descriptions, unsigned n_argument_descriptions, + const char **argv) { unsigned i = 0; // @@ -188,16 +181,16 @@ process_args_ex(const AppVersionInfo * appinfo, const ArgumentDescription * argu case 'f': case 'F': case 'I': - *(int *) argument_descriptions[i].location = atoi(env); + *(int *)argument_descriptions[i].location = atoi(env); break; case 'D': - *(double *) argument_descriptions[i].location = atof(env); + *(double *)argument_descriptions[i].location = atof(env); break; case 'L': - *(int64_t *) argument_descriptions[i].location = atoll(env); + *(int64_t *)argument_descriptions[i].location = atoll(env); break; case 'S': - ink_strlcpy((char *) argument_descriptions[i].location, env, atoi(argument_descriptions[i].type + 1)); + ink_strlcpy((char *)argument_descriptions[i].location, env, atoi(argument_descriptions[i].type + 1)); break; } } @@ -206,7 +199,6 @@ process_args_ex(const AppVersionInfo * appinfo, const ArgumentDescription * argu // program_name = appinfo->AppStr; while (*++argv) { - // Hack for supporting '-' as a file argument. if (strcmp(*argv, "-") == 0) { append_file_argument(*argv); @@ -249,7 +241,6 @@ process_args_ex(const AppVersionInfo * appinfo, const ArgumentDescription * argu } } } - } // If we have any arguments left, slurp them up into file_arguments. @@ -263,11 +254,11 @@ process_args_ex(const AppVersionInfo * appinfo, const ArgumentDescription * argu } void -usage(const ArgumentDescription * argument_descriptions, unsigned n_argument_descriptions, const char *usage_string) +usage(const ArgumentDescription *argument_descriptions, unsigned n_argument_descriptions, const char *usage_string) { - (void) argument_descriptions; - (void) n_argument_descriptions; - (void) usage_string; + (void)argument_descriptions; + (void)n_argument_descriptions; + (void)usage_string; if (usage_string) fprintf(stderr, "%s\n", usage_string); else @@ -279,25 +270,24 @@ usage(const ArgumentDescription * argument_descriptions, unsigned n_argument_des fprintf(stderr, " "); - if ('-' == argument_descriptions[i].key) fprintf(stderr, " "); - else fprintf(stderr, "-%c,", argument_descriptions[i].key); + if ('-' == argument_descriptions[i].key) + fprintf(stderr, " "); + else + fprintf(stderr, "-%c,", argument_descriptions[i].key); - fprintf(stderr, " --%-17s %s", - argument_descriptions[i].name, + fprintf(stderr, " --%-17s %s", argument_descriptions[i].name, argument_types_descriptions[argument_descriptions[i].type ? - strchr(argument_types_keys, - argument_descriptions[i].type[0]) - - argument_types_keys : strlen(argument_types_keys) - ]); + strchr(argument_types_keys, argument_descriptions[i].type[0]) - argument_types_keys : + strlen(argument_types_keys)]); switch (argument_descriptions[i].type ? argument_descriptions[i].type[0] : 0) { case 0: fprintf(stderr, " "); break; case 'L': - fprintf(stderr, " %-9" PRId64 "", *(int64_t *) argument_descriptions[i].location); + fprintf(stderr, " %-9" PRId64 "", *(int64_t *)argument_descriptions[i].location); break; case 'S': { - char * location; + char *location; if (argument_descriptions[i].type[1] == '*') { location = *(char **)argument_descriptions[i].location; } else { @@ -316,16 +306,16 @@ usage(const ArgumentDescription * argument_descriptions, unsigned n_argument_des break; } case 'D': - fprintf(stderr, " %-9.3f", *(double *) argument_descriptions[i].location); + fprintf(stderr, " %-9.3f", *(double *)argument_descriptions[i].location); break; case 'I': - fprintf(stderr, " %-9d", *(int *) argument_descriptions[i].location); + fprintf(stderr, " %-9d", *(int *)argument_descriptions[i].location); break; case 'T': case 'f': case 'F': if (argument_descriptions[i].location) { - fprintf(stderr, " %-9s", *(int *) argument_descriptions[i].location ? "true " : "false"); + fprintf(stderr, " %-9s", *(int *)argument_descriptions[i].location ? "true " : "false"); } else { fprintf(stderr, " %-9s", "false"); } http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/ts/ink_args.h ---------------------------------------------------------------------- diff --git a/lib/ts/ink_args.h b/lib/ts/ink_args.h index 824f919..f535fda 100644 --- a/lib/ts/ink_args.h +++ b/lib/ts/ink_args.h @@ -44,23 +44,22 @@ Process arguments struct ArgumentDescription; class AppVersionInfo; -typedef void ArgumentFunction(const ArgumentDescription * argument_descriptions, unsigned n_argument_descriptions, const char *arg); +typedef void ArgumentFunction(const ArgumentDescription *argument_descriptions, unsigned n_argument_descriptions, const char *arg); -struct ArgumentDescription -{ +struct ArgumentDescription { const char *name; char key; // set to '-' if no single character key. - /* - "I" = integer - "L" = int64_t - "D" = double (floating point) - "T" = toggle - "F" = set flag to TRUE (default is FALSE) - "f" = set flag to FALSE (default is TRUE) - "T" = toggle - "S80" = read string, 80 chars max - "S*" = read unbounded string, allocating - */ + /* + "I" = integer + "L" = int64_t + "D" = double (floating point) + "T" = toggle + "F" = set flag to TRUE (default is FALSE) + "f" = set flag to FALSE (default is TRUE) + "T" = toggle + "S80" = read string, 80 chars max + "S*" = read unbounded string, allocating + */ const char *description; const char *type; void *location; @@ -68,27 +67,33 @@ struct ArgumentDescription ArgumentFunction *pfn; }; -#define VERSION_ARGUMENT_DESCRIPTION() {"version", 'V', "Print version string", NULL, NULL, NULL, NULL} -#define HELP_ARGUMENT_DESCRIPTION() {"help", 'h', "Print usage information", NULL, NULL, NULL, usage} +#define VERSION_ARGUMENT_DESCRIPTION() \ + { \ + "version", 'V', "Print version string", NULL, NULL, NULL, NULL \ + } +#define HELP_ARGUMENT_DESCRIPTION() \ + { \ + "help", 'h', "Print usage information", NULL, NULL, NULL, usage \ + } /* Global Data */ -extern const char *file_arguments[]; // exported by process_args() +extern const char *file_arguments[]; // exported by process_args() extern unsigned n_file_arguments; // exported by process_args() -extern const char *program_name; // exported by process_args() +extern const char *program_name; // exported by process_args() /* Print out arguments and values */ -void show_argument_configuration(const ArgumentDescription * argument_descriptions, unsigned n_argument_descriptions); +void show_argument_configuration(const ArgumentDescription *argument_descriptions, unsigned n_argument_descriptions); -void usage(const ArgumentDescription * argument_descriptions, unsigned n_argument_descriptions, const char *arg_unused) TS_NORETURN; +void usage(const ArgumentDescription *argument_descriptions, unsigned n_argument_descriptions, const char *arg_unused) TS_NORETURN; /* Process all arguments */ -void process_args(const AppVersionInfo * appinfo, const ArgumentDescription * argument_descriptions, - unsigned n_argument_descriptions, const char **argv, const char *usage_string = 0); +void process_args(const AppVersionInfo *appinfo, const ArgumentDescription *argument_descriptions, unsigned n_argument_descriptions, + const char **argv, const char *usage_string = 0); -bool process_args_ex(const AppVersionInfo * appinfo, const ArgumentDescription * argument_descriptions, - unsigned n_argument_descriptions, const char **argv); +bool process_args_ex(const AppVersionInfo *appinfo, const ArgumentDescription *argument_descriptions, + unsigned n_argument_descriptions, const char **argv); #endif /*_INK_ARGS_H*/ http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/ts/ink_assert.h ---------------------------------------------------------------------- diff --git a/lib/ts/ink_assert.h b/lib/ts/ink_assert.h index 7e89da3..cadefcb 100644 --- a/lib/ts/ink_assert.h +++ b/lib/ts/ink_assert.h @@ -32,9 +32,8 @@ Assertions #include "ink_error.h" #ifdef __cplusplus -extern "C" -{ -#endif /* __cplusplus */ +extern "C" { +#endif /* __cplusplus */ /* don't use assert, no really DON'T use assert */ #undef assert @@ -44,23 +43,19 @@ extern "C" #undef __ASSERT_H__ #define __ASSERT_H__ - inkcoreapi void _ink_assert(const char *a, const char *f, int l) TS_NORETURN; +inkcoreapi void _ink_assert(const char *a, const char *f, int l) TS_NORETURN; #if defined(DEBUG) || defined(__clang_analyzer__) || defined(__COVERITY__) -#define ink_assert(EX) ( \ - (void)(likely(EX) ? (void)0 : _ink_assert(#EX, __FILE__, __LINE__))\ -) +#define ink_assert(EX) ((void)(likely(EX) ? (void)0 : _ink_assert(#EX, __FILE__, __LINE__))) #else #define ink_assert(EX) (void)(EX) #endif -#define ink_release_assert(EX) ( \ - (void)(likely(EX) ? (void)0 : _ink_assert(#EX, __FILE__, __LINE__)) \ -) +#define ink_release_assert(EX) ((void)(likely(EX) ? (void)0 : _ink_assert(#EX, __FILE__, __LINE__))) #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif /*_INK_ASSERT_H*/ http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/ts/ink_atomic.h ---------------------------------------------------------------------- diff --git a/lib/ts/ink_atomic.h b/lib/ts/ink_atomic.h index 1fcd60a..84fc470 100644 --- a/lib/ts/ink_atomic.h +++ b/lib/ts/ink_atomic.h @@ -35,7 +35,7 @@ ****************************************************************************/ #ifndef _ink_atomic_h_ -#define _ink_atomic_h_ +#define _ink_atomic_h_ #include <stdio.h> #include <string.h> @@ -69,30 +69,38 @@ typedef vvoidp *pvvoidp; // ink_atomic_swap(ptr, value) // Writes @value into @ptr, returning the previous value. -template <typename T> static inline T -ink_atomic_swap(volatile T * mem, T value) { +template <typename T> +static inline T +ink_atomic_swap(volatile T *mem, T value) +{ return __sync_lock_test_and_set(mem, value); } // ink_atomic_cas(mem, prev, next) // Atomically store the value @next into the pointer @mem, but only if the current value at @mem is @prev. // Returns true if @next was successfully stored. -template <typename T> static inline bool -ink_atomic_cas(volatile T * mem, T prev, T next) { +template <typename T> +static inline bool +ink_atomic_cas(volatile T *mem, T prev, T next) +{ return __sync_bool_compare_and_swap(mem, prev, next); } // ink_atomic_increment(ptr, count) // Increment @ptr by @count, returning the previous value. -template <typename Type, typename Amount> static inline Type -ink_atomic_increment(volatile Type * mem, Amount count) { +template <typename Type, typename Amount> +static inline Type +ink_atomic_increment(volatile Type *mem, Amount count) +{ return __sync_fetch_and_add(mem, (Type)count); } // ink_atomic_decrement(ptr, count) // Decrement @ptr by @count, returning the previous value. -template <typename Type, typename Amount> static inline Type -ink_atomic_decrement(volatile Type * mem, Amount count) { +template <typename Type, typename Amount> +static inline Type +ink_atomic_decrement(volatile Type *mem, Amount count) +{ return __sync_fetch_and_sub(mem, (Type)count); } @@ -100,9 +108,10 @@ ink_atomic_decrement(volatile Type * mem, Amount count) { #if (defined(__arm__) || defined(__mips__)) && (SIZEOF_VOIDP == 4) extern ink_mutex __global_death; -template<> +template <> inline int64_t -ink_atomic_swap<int64_t>(pvint64 mem, int64_t value) { +ink_atomic_swap<int64_t>(pvint64 mem, int64_t value) +{ int64_t old; ink_mutex_acquire(&__global_death); old = *mem; @@ -111,20 +120,25 @@ ink_atomic_swap<int64_t>(pvint64 mem, int64_t value) { return old; } -template<> +template <> inline bool -ink_atomic_cas<int64_t>(pvint64 mem, int64_t old, int64_t new_value) { +ink_atomic_cas<int64_t>(pvint64 mem, int64_t old, int64_t new_value) +{ int64_t curr; ink_mutex_acquire(&__global_death); curr = *mem; - if(old == curr) *mem = new_value; + if (old == curr) + *mem = new_value; ink_mutex_release(&__global_death); - if(old == curr) return 1; + if (old == curr) + return 1; return 0; } -template<typename Amount> static inline int64_t -ink_atomic_increment(pvint64 mem, Amount value) { +template <typename Amount> +static inline int64_t +ink_atomic_increment(pvint64 mem, Amount value) +{ int64_t curr; ink_mutex_acquire(&__global_death); curr = *mem; @@ -133,8 +147,10 @@ ink_atomic_increment(pvint64 mem, Amount value) { return curr; } -template<typename Amount> static inline int64_t -ink_atomic_decrement(pvint64 mem, Amount value) { +template <typename Amount> +static inline int64_t +ink_atomic_decrement(pvint64 mem, Amount value) +{ int64_t curr; ink_mutex_acquire(&__global_death); curr = *mem; @@ -143,8 +159,10 @@ ink_atomic_decrement(pvint64 mem, Amount value) { return curr; } -template<typename Amount> static inline uint64_t -ink_atomic_increment(pvuint64 mem, Amount value) { +template <typename Amount> +static inline uint64_t +ink_atomic_increment(pvuint64 mem, Amount value) +{ uint64_t curr; ink_mutex_acquire(&__global_death); curr = *mem; @@ -153,8 +171,10 @@ ink_atomic_increment(pvuint64 mem, Amount value) { return curr; } -template<typename Amount> static inline uint64_t -ink_atomic_decrement(pvuint64 mem, Amount value) { +template <typename Amount> +static inline uint64_t +ink_atomic_decrement(pvuint64 mem, Amount value) +{ uint64_t curr; ink_mutex_acquire(&__global_death); curr = *mem; @@ -174,4 +194,4 @@ ink_atomic_decrement(pvuint64 mem, Amount value) { #error Need a compiler / libc that supports atomic operations, e.g. gcc v4.1.2 or later #endif -#endif /* _ink_atomic_h_ */ +#endif /* _ink_atomic_h_ */ http://git-wip-us.apache.org/repos/asf/trafficserver/blob/65477944/lib/ts/ink_auth_api.cc ---------------------------------------------------------------------- diff --git a/lib/ts/ink_auth_api.cc b/lib/ts/ink_auth_api.cc index be82c46..401a66c 100644 --- a/lib/ts/ink_auth_api.cc +++ b/lib/ts/ink_auth_api.cc @@ -28,8 +28,7 @@ #include "ink_auth_api.h" static int s_rand_seed = time(NULL); // + s_rand_seed; -static InkRand -s_rand_gen(ink_rand_r((unsigned int *) &s_rand_seed) ^ (uintptr_t) &s_rand_seed); +static InkRand s_rand_gen(ink_rand_r((unsigned int *) & s_rand_seed) ^ (uintptr_t)&s_rand_seed); inline uint32_t ink_get_rand_intrn() @@ -38,14 +37,14 @@ ink_get_rand_intrn() } inline void -ink_make_token_intrn(INK_AUTH_TOKEN * tok, const INK_AUTH_SEED * const *seeds, int slen) +ink_make_token_intrn(INK_AUTH_TOKEN *tok, const INK_AUTH_SEED *const *seeds, int slen) { INK_DIGEST_CTX ctx; ink_code_incr_md5_init(&ctx); while (slen-- > 0) { - ink_code_incr_md5_update(&ctx, (const char *) seeds[slen]->data(), seeds[slen]->length()); + ink_code_incr_md5_update(&ctx, (const char *)seeds[slen]->data(), seeds[slen]->length()); } - ink_code_incr_md5_final((char *) &(tok->u8[0]), &ctx); + ink_code_incr_md5_final((char *)&(tok->u8[0]), &ctx); } uint32_t @@ -55,15 +54,15 @@ ink_get_rand() } void -ink_make_token(INK_AUTH_TOKEN * tok, const INK_AUTH_TOKEN & mask, const INK_AUTH_SEED * const *seeds, int slen) +ink_make_token(INK_AUTH_TOKEN *tok, const INK_AUTH_TOKEN &mask, const INK_AUTH_SEED *const *seeds, int slen) { ink_make_token_intrn(tok, seeds, slen); - for (int i = 3; i >= 0; i--) // randomize masked bits + for (int i = 3; i >= 0; i--) // randomize masked bits tok->u32[i] ^= mask.u32[i] & ink_get_rand_intrn(); } uint32_t -ink_make_token32(uint32_t mask, const INK_AUTH_SEED * const *seeds, int slen) +ink_make_token32(uint32_t mask, const INK_AUTH_SEED *const *seeds, int slen) { INK_AUTH_TOKEN tok; ink_make_token_intrn(&tok, seeds, slen); @@ -73,10 +72,10 @@ ink_make_token32(uint32_t mask, const INK_AUTH_SEED * const *seeds, int slen) } uint64_t -ink_make_token64(uint64_t mask, const INK_AUTH_SEED * const *seeds, int slen) +ink_make_token64(uint64_t mask, const INK_AUTH_SEED *const *seeds, int slen) { INK_AUTH_TOKEN tok; ink_make_token_intrn(&tok, seeds, slen); tok.u64[1] ^= tok.u64[0]; - return tok.u64[1] ^ (mask & ((uint64_t) ink_get_rand_intrn() + (((uint64_t) ink_get_rand_intrn()) << 32))); + return tok.u64[1] ^ (mask & ((uint64_t)ink_get_rand_intrn() + (((uint64_t)ink_get_rand_intrn()) << 32))); }
