Updated Branches: refs/heads/master b7ba116ef -> 8ab44edd4
TS-1345: fix signed/unsigned compilation issues in Vec Project: http://git-wip-us.apache.org/repos/asf/trafficserver/repo Commit: http://git-wip-us.apache.org/repos/asf/trafficserver/commit/8ab44edd Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/8ab44edd Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/8ab44edd Branch: refs/heads/master Commit: 8ab44edd4e9c27c48f9c1482b837a6adcd9ed541 Parents: b7ba116 Author: James Peach <[email protected]> Authored: Wed Jul 11 20:49:23 2012 -0700 Committer: James Peach <[email protected]> Committed: Wed Jul 11 20:49:23 2012 -0700 ---------------------------------------------------------------------- CHANGES | 2 + lib/ts/Map.h | 38 +++++++++------- lib/ts/Vec.cc | 20 ++++---- lib/ts/Vec.h | 114 ++++++++++++++++++++++++++--------------------- lib/ts/test_Map.cc | 10 ++++ lib/ts/test_Vec.cc | 18 ++++---- 6 files changed, 115 insertions(+), 87 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/trafficserver/blob/8ab44edd/CHANGES ---------------------------------------------------------------------- diff --git a/CHANGES b/CHANGES index b404605..5479800 100644 --- a/CHANGES +++ b/CHANGES @@ -1,5 +1,7 @@ -*- coding: utf-8 -*- Changes with Apache Traffic Server 3.3.0 + *) [TS-1345] fix signed/unsigned compilation issues in Vec + *) [TS-1343] Stat system doesn't check buffer sizes *) [TS-1342] Lua plugin initial hook support http://git-wip-us.apache.org/repos/asf/trafficserver/blob/8ab44edd/lib/ts/Map.h ---------------------------------------------------------------------- diff --git a/lib/ts/Map.h b/lib/ts/Map.h index 81732df..e18bddf 100644 --- a/lib/ts/Map.h +++ b/lib/ts/Map.h @@ -75,7 +75,7 @@ template <class K, class C, class A = DefaultAlloc> class Map : public Vec<MapEl void get_keys_set(Vec<K> &keys); void get_values(Vec<C> &values); void map_union(Map<K,C> &m); - int some_disjunction(Map<K,C> &m); + bool some_disjunction(Map<K,C> &m) const; }; template <class C> class HashFns { @@ -106,8 +106,8 @@ template <class K, class AHashFns, class C, class A = DefaultAlloc> class HashMa }; #define form_Map(_c, _p, _v) if ((_v).n) for (_c *qq__##_p = (_c*)0, *_p = &(_v).v[0]; \ - ((intptr_t)(qq__##_p) < (_v).n) && ((_p = &(_v).v[(intptr_t)qq__##_p]) || 1); \ - qq__##_p = (_c*)(((intptr_t)qq__##_p) + 1)) \ + ((uintptr_t)(qq__##_p) < (_v).n) && ((_p = &(_v).v[(uintptr_t)qq__##_p]) || 1); \ + qq__##_p = (_c*)(((uintptr_t)qq__##_p) + 1)) \ if ((_p)->key) @@ -302,15 +302,19 @@ Map<K,C,A>::map_union(Map<K,C> &m) { put(m.v[i].key, m.v[i].value); } -template <class K, class C, class A> inline int -Map<K,C,A>::some_disjunction(Map<K,C> &m) { - for (int i = 0; i < m.n; i++) - if (m.v[i].key && get(m.v[i].key) != m.v[i].value) - return 1; - for (int i = 0; i < n; i++) - if (v[i].key && m.get(v[i].key) != v[i].value) - return 1; - return 0; +template <class K, class C, class A> inline bool +Map<K,C,A>::some_disjunction(Map<K,C> &m) const { + for (size_t i = 0; i < m.n; i++) { + if (m.v[i].key && get(m.v[i].key) != m.v[i].value) { + return true; + } + } + for (size_t i = 0; i < n; i++) { + if (v[i].key && m.get(v[i].key) != v[i].value) { + return true; + } + } + return false; } template <class K, class C, class A> inline void @@ -397,7 +401,7 @@ HashMap<K,AHashFns,C,A>::get_internal(K akey) { } uintptr_t h = AHashFns::hash(akey); h = h % n; - for (int k = h, j = 0; j < i + 3;j++) { + for (size_t k = h, j = 0; j < i + 3;j++) { if (!v[k].key) return 0; else if (AHashFns::equal(akey, v[k].key)) @@ -433,7 +437,7 @@ HashMap<K,AHashFns,C,A>::put(K akey, C avalue) { if (n > MAP_INTEGRAL_SIZE) { uintptr_t h = AHashFns::hash(akey); h = h % n; - for (int k = h, j = 0; j < i + 3; j++) { + for (size_t k = h, j = 0; j < i + 3; j++) { if (!v[k].key) { v[k].key = akey; v[k].value = avalue; @@ -446,7 +450,7 @@ HashMap<K,AHashFns,C,A>::put(K akey, C avalue) { } HashMap<K,AHashFns,C,A> vv(*this); Map<K,C,A>::set_expand(); - for (int i = 0; i < vv.n; i++) + for (size_t i = 0; i < vv.n; i++) if (vv.v[i].key) put(vv.v[i].key, vv.v[i].value); return put(akey, avalue); @@ -659,7 +663,7 @@ ChainHashMap<K, AHashFns, C, A>::del(K akey) { template <class K, class AHashFns, class C, class A> void ChainHashMap<K, AHashFns, C, A>::get_keys(Vec<K> &keys) { - for (int i = 0; i < n; i++) { + for (size_t i = 0; i < n; i++) { List<MapElem<K,C> > *l = &v[i].value; if (l->head) for (ConsCell<MapElem<K,C>,A> *p = l->head; p; p = p->cdr) @@ -669,7 +673,7 @@ ChainHashMap<K, AHashFns, C, A>::get_keys(Vec<K> &keys) { template <class K, class AHashFns, class C, class A> void ChainHashMap<K, AHashFns, C, A>::get_values(Vec<C> &values) { - for (int i = 0; i < n; i++) { + for (size_t i = 0; i < n; i++) { List<MapElem<K,C>,A> *l = &v[i].value; if (l->head) for (ConsCell<MapElem<K,C>,A> *p = l->head; p; p = p->cdr) http://git-wip-us.apache.org/repos/asf/trafficserver/blob/8ab44edd/lib/ts/Vec.cc ---------------------------------------------------------------------- diff --git a/lib/ts/Vec.cc b/lib/ts/Vec.cc index a9d24ed..0a16b4e 100644 --- a/lib/ts/Vec.cc +++ b/lib/ts/Vec.cc @@ -70,7 +70,7 @@ uintptr_t open_hash_primes[256] = { // binary search over intervals static int -i_find(Intervals *i, int x) { +i_find(const Intervals *i, int x) { ink_assert(i->n); int l = 0, h = i->n; Lrecurse: @@ -93,13 +93,13 @@ i_find(Intervals *i, int x) { return (l + 1); } -int -Intervals::in(int x) { +bool +Intervals::in(int x) const { if (!n) - return 0; + return false; if (i_find(this, x) > 0) - return 1; - return 0; + return true; + return false; } // insert into interval with merge @@ -121,7 +121,7 @@ Intervals::insert(int x) { goto Lmerge; } l += 2; - if (l < n) { + if (l < (int)n) { if (x == v[l] - 1) { v[l]--; goto Lmerge; @@ -156,7 +156,7 @@ Intervals::insert(int x) { goto Ldomerge; } } - if (l < n - 2) { + if (l < (int)(n - 2)) { if (v[l + 2] - v[l + 1] < 2) goto Ldomerge; } @@ -169,9 +169,9 @@ Intervals::insert(int x) { void UnionFind::size(int s) { - int nn = n; + size_t nn = n; fill(s); - for (int i = nn; i < n; i++) + for (size_t i = nn; i < n; i++) v[i] = -1; } http://git-wip-us.apache.org/repos/asf/trafficserver/blob/8ab44edd/lib/ts/Vec.h ---------------------------------------------------------------------- diff --git a/lib/ts/Vec.h b/lib/ts/Vec.h index e87c533..d36fe49 100644 --- a/lib/ts/Vec.h +++ b/lib/ts/Vec.h @@ -44,8 +44,8 @@ template <class C, class A = DefaultAlloc, int S = VEC_INTEGRAL_SHIFT_DEFAULT> // S must be a power of 2 class Vec { public: - int n; - int i; // size index for sets, reserve for vectors + size_t n; + size_t i; // size index for sets, reserve for vectors C *v; C e[VEC_INTEGRAL_SIZE]; @@ -56,10 +56,10 @@ class Vec { C &operator[](int i) const { return v[i]; } - C get(int i); + C get(size_t i); void add(C a); void push_back(C a) { add(a); } // std::vector name - int add_exclusive(C a); + bool add_exclusive(C a); C& add(); C pop(); void reset(); @@ -71,7 +71,7 @@ class Vec { C *set_add(C a); void set_remove(C a); // expensive, use BlockHash for cheaper remove C *set_add_internal(C a); - int set_union(Vec<C,A,S> &v); + 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); @@ -79,30 +79,30 @@ class Vec { 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); - int set_count(); - int count(); + size_t set_count() const; + size_t count() const; C *in(C a); C *set_in(C a); C first_in_set(); C *set_in_internal(C a); void set_expand(); - int index(C a); + 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 fill(int n); + void fill(size_t n); void append(const Vec<C> &v); 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(int index); - void insert(int index, Vec<C> &vv); - void insert(int index, C a); + 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 reverse(); - void reserve(int n); + void reserve(size_t n); C* end() const { return v + n; } C &first() const { return v[0]; } C &last() const { return v[n-1]; } @@ -143,7 +143,7 @@ template <class C, class A = DefaultAlloc, int S = VEC_INTEGRAL_SHIFT_DEFAULT> c class Intervals : public Vec<int> { public: void insert(int n); - int in(int n); + bool in(int n) const; }; // UnionFind supports fast unify and finding of @@ -183,11 +183,11 @@ Vec<C,A,S>::Vec(C c) { } template <class C, class A, int S> inline C -Vec<C,A,S>::get(int i) { +Vec<C,A,S>::get(size_t i) { if (i < n && i >= 0) return v[i]; else - return 0; + return C(); } template <class C, class A, int S> inline void @@ -240,8 +240,9 @@ Vec<C,A,S>::set_add(C a) { if (n == SET_LINEAR_SIZE) { Vec<C,A,S> vv(*this); clear(); - for (C *c = vv.v; c < vv.v + vv.n; c++) + for (C *c = vv.v; c < vv.v + vv.n; c++) { set_add_internal(*c); + } } return set_add_internal(a); } @@ -255,8 +256,8 @@ Vec<C,A,S>::set_remove(C a) { set_add(a); } -template <class C, class A, int S> inline int -Vec<C,A,S>::count() { +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) @@ -272,13 +273,13 @@ Vec<C,A,S>::in(C a) { return NULL; } -template <class C, class A, int S> inline int +template <class C, class A, int S> inline bool Vec<C,A,S>::add_exclusive(C a) { if (!in(a)) { add(a); - return 1; + return true; } else - return 0; + return false; } template <class C, class A, int S> inline C * @@ -296,11 +297,13 @@ Vec<C,A,S>::first_in_set() { return 0; } -template <class C, class A, int S> inline int -Vec<C,A,S>::index(C a) { - for (C *c = v; c < v + n; c++) - if (*c == a) +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; + } + } return -1; } @@ -338,8 +341,8 @@ 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(int nn) { - for (int i = n; i < nn; i++) +Vec<C,A,S>::fill(size_t nn) { + for (size_t i = n; i < nn; i++) add() = 0; } @@ -385,7 +388,7 @@ Vec<C,A,S>::add_internal() { template <class C, class A, int S> C * Vec<C,A,S>::set_add_internal(C c) { - int j, k; + size_t j, k; if (n) { uintptr_t h = (uintptr_t)c; h = h % n; @@ -393,22 +396,24 @@ Vec<C,A,S>::set_add_internal(C c) { if (!v[k]) { v[k] = c; return &v[k]; - } else if (v[k] == c) + } else if (v[k] == c) { return 0; + } k = (k + open_hash_primes[j]) % n; } } Vec<C,A,S> vv; vv.move_internal(*this); set_expand(); - if (vv.v) + if (vv.v) { set_union(vv); + } return set_add(c); } template <class C, class A, int S> C * Vec<C,A,S>::set_in_internal(C c) { - int j, k; + size_t j, k; if (n) { uintptr_t h = (uintptr_t)c; h = h % n; @@ -423,12 +428,14 @@ Vec<C,A,S>::set_in_internal(C c) { return 0; } -template <class C, class A, int S> int +template <class C, class A, int S> bool Vec<C,A,S>::set_union(Vec<C,A,S> &vv) { - int changed = 0; - for (int i = 0; i < vv.n; i++) - if (vv.v[i]) + bool changed = false; + for (size_t i = 0; i < vv.n; i++) { + if (vv.v[i]) { changed = set_add(vv.v[i]) || changed; + } + } return changed; } @@ -506,12 +513,14 @@ Vec<C,A,S>::some_difference(Vec<C,A,S> &vv) { return 0; } -template <class C, class A, int S> int -Vec<C,A,S>::set_count() { - int x = 0; - for (int i = 0; i < n; i++) - if (v[i]) +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]) { x++; + } + } return x; } @@ -554,14 +563,14 @@ Vec<C,A,S>::remove_index(int index) { } template <class C, class A, int S> void -Vec<C,A,S>::insert(int index, C a) { +Vec<C,A,S>::insert(size_t index, C a) { add(); 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(int index, Vec<C> &vv) { +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)); for (int x = 0; x < vv.n; x++) @@ -569,7 +578,7 @@ Vec<C,A,S>::insert(int index, Vec<C> &vv) { } template <class C, class A, int S> C & -Vec<C,A,S>::insert(int index) { +Vec<C,A,S>::insert(size_t index) { add(); memmove(&v[index+1], &v[index], (n - index - 1) * sizeof(C)); memset(&v[index], 0, sizeof(C)); @@ -612,10 +621,10 @@ Vec<C,A,S>::set_expand() { } template <class C, class A, int S> inline void -Vec<C,A,S>::reserve(int x) { +Vec<C,A,S>::reserve(size_t x) { if (x <= n) return; - int xx = 1 << VEC_INITIAL_SHIFT; + unsigned xx = 1 << VEC_INITIAL_SHIFT; while (xx < x) xx *= 2; i = xx; void *vv = (void*)v; @@ -640,11 +649,12 @@ Vec<C,A,S>::addx() { memset(&v[n], 0, (VEC_INITIAL_SIZE - n) * sizeof(C)); } else { if ((n & (n-1)) == 0) { - int nl = n * 2; - if (nl <= i) + size_t nl = n * 2; + if (nl <= i) { return; - else + } else { i = 0; + } void *vv = (void*)v; v = (C*)A::alloc(nl * sizeof(C)); memcpy(v, vv, n * sizeof(C)); @@ -682,9 +692,11 @@ Vec<C,A,S>::free_and_clear() { template <class C, class A, int S> inline void Vec<C,A,S>::delete_and_clear() { - for (int x = 0; x < n; x++) - if (v[x]) + for (size_t x = 0; x < n; x++) { + if (v[x]) { delete v[x]; + } + } clear(); } http://git-wip-us.apache.org/repos/asf/trafficserver/blob/8ab44edd/lib/ts/test_Map.cc ---------------------------------------------------------------------- diff --git a/lib/ts/test_Map.cc b/lib/ts/test_Map.cc index 4248a73..d5a504c 100644 --- a/lib/ts/test_Map.cc +++ b/lib/ts/test_Map.cc @@ -36,6 +36,16 @@ int main(int argc, char **argv) { ssm.put("d", "D"); form_SSMap(x, ssm) { /* nop */ } +/* + if ((ssm).n) + for (SSMapElem *qq__x = (SSMapElem*)0, *x = &(ssm).v[0]; + ((intptr_t)(qq__x) < (ssm).n) && ((x = &(ssm).v[(intptr_t)qq__x]) || 1); + qq__x = (SSMapElem*)(((intptr_t)qq__x) + 1)) + if ((x)->key) { + // nop + } + */ + StringChainHash<> h; cchar *hi = "hi", *ho = "ho", *hum = "hum", *hhi = "hhi"; hhi++; http://git-wip-us.apache.org/repos/asf/trafficserver/blob/8ab44edd/lib/ts/test_Vec.cc ---------------------------------------------------------------------- diff --git a/lib/ts/test_Vec.cc b/lib/ts/test_Vec.cc index f62dd5e..611565f 100644 --- a/lib/ts/test_Vec.cc +++ b/lib/ts/test_Vec.cc @@ -57,21 +57,21 @@ static void test_basic() Vec<void *> v, vv, vvv; int tt = 99 * 50, t = 0; - for (int i = 0; i < 100; i++) + for (size_t i = 0; i < 100; i++) v.add((void*)(intptr_t)i); - for (int i = 0; i < 100; i++) + for (size_t i = 0; i < 100; i++) t += (int)(intptr_t)v.v[i]; ink_assert(t == tt); t = 0; - for (int i = 1; i < 100; i++) + for (size_t i = 1; i < 100; i++) vv.set_add((void*)(intptr_t)i); - for (int i = 1; i < 100; i++) + for (size_t i = 1; i < 100; i++) vvv.set_add((void*)(intptr_t)i); - for (int i = 1; i < 100; i++) + for (size_t i = 1; i < 100; i++) vvv.set_add((void*)(intptr_t)(i * 1000)); vv.set_union(vvv); - for (int i = 0; i < vv.n; i++) + for (size_t i = 0; i < vv.n; i++) if (vv.v[i]) t += (int)(intptr_t)vv.v[i]; ink_assert(t == tt + 1000 * tt); @@ -79,12 +79,12 @@ static void test_basic() v.clear(); v.reserve(1000); t = 0; - for (int i = 0; i < 1000; i++) + for (size_t i = 0; i < 1000; i++) v.add((void*)(intptr_t)i); - for (int i = 0; i < 1000; i++) + for (size_t i = 0; i < 1000; i++) t += (int)(intptr_t)v.v[i]; ink_assert(t == 999 * 500); - printf("%d %d\n", v.n, v.i); + printf("%zu %zu\n", v.n, v.i); Intervals in; in.insert(1);
