You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by nw...@apache.org on 2015/04/26 11:51:30 UTC
[1/6] lucy-clownfish git commit: Use size_t for VArray indices
Repository: lucy-clownfish
Updated Branches:
refs/heads/master 36f36eafa -> ba2150d96
Use size_t for VArray indices
Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/2db62be3
Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/2db62be3
Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/2db62be3
Branch: refs/heads/master
Commit: 2db62be359d2fb2ab72bc515bbdcb0d9f65362ac
Parents: 36f36ea
Author: Nick Wellnhofer <we...@aevum.de>
Authored: Thu Apr 23 14:29:16 2015 +0200
Committer: Nick Wellnhofer <we...@aevum.de>
Committed: Thu Apr 23 14:29:16 2015 +0200
----------------------------------------------------------------------
runtime/core/Clownfish/Test/TestVArray.c | 10 +++---
runtime/core/Clownfish/VArray.c | 51 +++++++++++++--------------
runtime/core/Clownfish/VArray.cfh | 28 +++++++--------
3 files changed, 44 insertions(+), 45 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/2db62be3/runtime/core/Clownfish/Test/TestVArray.c
----------------------------------------------------------------------
diff --git a/runtime/core/Clownfish/Test/TestVArray.c b/runtime/core/Clownfish/Test/TestVArray.c
index 2e47a67..551121a 100644
--- a/runtime/core/Clownfish/Test/TestVArray.c
+++ b/runtime/core/Clownfish/Test/TestVArray.c
@@ -263,7 +263,7 @@ test_Slice(TestBatchRunner *runner) {
{
VArray *wanted = VA_new(0);
VA_Push(wanted, (Obj*)Str_newf("9"));
- VArray *slice = VA_Slice(array, 9, UINT32_MAX - 1);
+ VArray *slice = VA_Slice(array, 9, SIZE_MAX - 1);
TEST_TRUE(runner, VA_Get_Size(slice) == 1, "guard against overflow");
DECREF(slice);
DECREF(wanted);
@@ -299,7 +299,7 @@ static void
S_overflow_Push(void *context) {
UNUSED_VAR(context);
VArray *array = VA_new(0);
- array->cap = UINT32_MAX;
+ array->cap = SIZE_MAX;
array->size = array->cap;
VA_Push(array, (Obj*)CFISH_TRUE);
}
@@ -308,7 +308,7 @@ static void
S_overflow_Unshift(void *context) {
UNUSED_VAR(context);
VArray *array = VA_new(0);
- array->cap = UINT32_MAX;
+ array->cap = SIZE_MAX;
array->size = array->cap;
VA_Unshift(array, (Obj*)CFISH_TRUE);
}
@@ -320,7 +320,7 @@ S_overflow_Push_VArray(void *context) {
array->cap = 1000000000;
array->size = array->cap;
VArray *other = VA_new(0);
- other->cap = UINT32_MAX - array->cap + 1;
+ other->cap = SIZE_MAX - array->cap + 1;
other->size = other->cap;
VA_Push_VArray(array, other);
}
@@ -329,7 +329,7 @@ static void
S_overflow_Store(void *context) {
UNUSED_VAR(context);
VArray *array = VA_new(0);
- VA_Store(array, UINT32_MAX, (Obj*)CFISH_TRUE);
+ VA_Store(array, SIZE_MAX, (Obj*)CFISH_TRUE);
}
static void
http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/2db62be3/runtime/core/Clownfish/VArray.c
----------------------------------------------------------------------
diff --git a/runtime/core/Clownfish/VArray.c b/runtime/core/Clownfish/VArray.c
index b6170d3..f395c6d 100644
--- a/runtime/core/Clownfish/VArray.c
+++ b/runtime/core/Clownfish/VArray.c
@@ -27,17 +27,17 @@
#include "Clownfish/Util/SortUtils.h"
static CFISH_INLINE void
-SI_grow_by(VArray *self, uint32_t add_size);
+SI_grow_by(VArray *self, size_t add_size);
VArray*
-VA_new(uint32_t capacity) {
+VA_new(size_t capacity) {
VArray *self = (VArray*)Class_Make_Obj(VARRAY);
VA_init(self, capacity);
return self;
}
VArray*
-VA_init(VArray *self, uint32_t capacity) {
+VA_init(VArray *self, size_t capacity) {
// Init.
self->size = 0;
@@ -68,7 +68,7 @@ VA_Clone_IMP(VArray *self) {
VArray *twin = VA_new(self->size);
// Clone each element.
- for (uint32_t i = 0; i < self->size; i++) {
+ for (size_t i = 0; i < self->size; i++) {
Obj *elem = self->elems[i];
if (elem) {
twin->elems[i] = Obj_Clone(elem);
@@ -88,7 +88,7 @@ VA_Shallow_Copy_IMP(VArray *self) {
Obj **elems = twin->elems;
memcpy(elems, self->elems, self->size * sizeof(Obj*));
twin->size = self->size;
- for (uint32_t i = 0; i < self->size; i++) {
+ for (size_t i = 0; i < self->size; i++) {
if (elems[i] != NULL) {
(void)INCREF(elems[i]);
}
@@ -111,7 +111,7 @@ VA_Push_VArray_IMP(VArray *self, VArray *other) {
if (other->size > self->cap - self->size) {
SI_grow_by(self, other->size);
}
- for (uint32_t i = 0, tick = self->size; i < other->size; i++, tick++) {
+ for (size_t i = 0, tick = self->size; i < other->size; i++, tick++) {
Obj *elem = VA_Fetch(other, i);
if (elem != NULL) {
self->elems[tick] = INCREF(elem);
@@ -156,7 +156,7 @@ VA_Shift_IMP(VArray *self) {
}
Obj*
-VA_Fetch_IMP(VArray *self, uint32_t num) {
+VA_Fetch_IMP(VArray *self, size_t num) {
if (num >= self->size) {
return NULL;
}
@@ -165,9 +165,9 @@ VA_Fetch_IMP(VArray *self, uint32_t num) {
}
void
-VA_Store_IMP(VArray *self, uint32_t tick, Obj *elem) {
+VA_Store_IMP(VArray *self, size_t tick, Obj *elem) {
if (tick >= self->cap) {
- if (tick == UINT32_MAX) {
+ if (tick == SIZE_MAX) {
THROW(ERR, "Invalid tick");
}
SI_grow_by(self, tick + 1 - self->size);
@@ -178,7 +178,7 @@ VA_Store_IMP(VArray *self, uint32_t tick, Obj *elem) {
}
void
-VA_Grow_IMP(VArray *self, uint32_t capacity) {
+VA_Grow_IMP(VArray *self, size_t capacity) {
if (capacity > self->cap) {
self->elems = (Obj**)REALLOCATE(self->elems, capacity * sizeof(Obj*));
self->cap = capacity;
@@ -188,7 +188,7 @@ VA_Grow_IMP(VArray *self, uint32_t capacity) {
}
Obj*
-VA_Delete_IMP(VArray *self, uint32_t num) {
+VA_Delete_IMP(VArray *self, size_t num) {
Obj *elem = NULL;
if (num < self->size) {
elem = self->elems[num];
@@ -198,15 +198,15 @@ VA_Delete_IMP(VArray *self, uint32_t num) {
}
void
-VA_Excise_IMP(VArray *self, uint32_t offset, uint32_t length) {
+VA_Excise_IMP(VArray *self, size_t offset, size_t length) {
if (self->size <= offset) { return; }
else if (self->size < offset + length) { length = self->size - offset; }
- for (uint32_t i = 0; i < length; i++) {
+ for (size_t i = 0; i < length; i++) {
DECREF(self->elems[offset + i]);
}
- uint32_t num_to_move = self->size - (offset + length);
+ size_t num_to_move = self->size - (offset + length);
memmove(self->elems + offset, self->elems + offset + length,
num_to_move * sizeof(Obj*));
self->size -= length;
@@ -218,7 +218,7 @@ VA_Clear_IMP(VArray *self) {
}
void
-VA_Resize_IMP(VArray *self, uint32_t size) {
+VA_Resize_IMP(VArray *self, size_t size) {
if (size < self->size) {
VA_Excise(self, size, self->size - size);
}
@@ -228,12 +228,12 @@ VA_Resize_IMP(VArray *self, uint32_t size) {
self->size = size;
}
-uint32_t
+size_t
VA_Get_Size_IMP(VArray *self) {
return self->size;
}
-uint32_t
+size_t
VA_Get_Capacity_IMP(VArray *self) {
return self->cap;
}
@@ -264,7 +264,7 @@ VA_Equals_IMP(VArray *self, Obj *other) {
return false;
}
else {
- for (uint32_t i = 0, max = self->size; i < max; i++) {
+ for (size_t i = 0, max = self->size; i < max; i++) {
Obj *val = self->elems[i];
Obj *other_val = twin->elems[i];
if ((val && !other_val) || (other_val && !val)) { return false; }
@@ -277,7 +277,7 @@ VA_Equals_IMP(VArray *self, Obj *other) {
VArray*
VA_Gather_IMP(VArray *self, VA_Gather_Test_t test, void *data) {
VArray *gathered = VA_new(self->size);
- for (uint32_t i = 0, max = self->size; i < max; i++) {
+ for (size_t i = 0, max = self->size; i < max; i++) {
if (test(self, i, data)) {
Obj *elem = self->elems[i];
VA_Push(gathered, elem ? INCREF(elem) : NULL);
@@ -287,13 +287,13 @@ VA_Gather_IMP(VArray *self, VA_Gather_Test_t test, void *data) {
}
VArray*
-VA_Slice_IMP(VArray *self, uint32_t offset, uint32_t length) {
+VA_Slice_IMP(VArray *self, size_t offset, size_t length) {
// Adjust ranges if necessary.
if (offset >= self->size) {
offset = 0;
length = 0;
}
- else if (length > UINT32_MAX - offset
+ else if (length > SIZE_MAX - offset
|| offset + length > self->size
) {
length = self->size - offset;
@@ -304,7 +304,7 @@ VA_Slice_IMP(VArray *self, uint32_t offset, uint32_t length) {
slice->size = length;
Obj **slice_elems = slice->elems;
Obj **my_elems = self->elems;
- for (uint32_t i = 0; i < length; i++) {
+ for (size_t i = 0; i < length; i++) {
slice_elems[i] = INCREF(my_elems[offset + i]);
}
@@ -312,14 +312,13 @@ VA_Slice_IMP(VArray *self, uint32_t offset, uint32_t length) {
}
static void
-SI_grow_by(VArray *self, uint32_t add_size) {
+SI_grow_by(VArray *self, size_t add_size) {
size_t min_size = self->size + add_size;
// Check for overflow.
- if ((uint32_t)min_size < add_size) {
+ if (min_size < add_size) {
THROW(ERR, "Array grew too large");
}
size_t new_size = Memory_oversize(min_size, sizeof(Obj*));
- if (new_size > UINT32_MAX) { new_size = UINT32_MAX; }
- VA_Grow(self, (uint32_t)new_size);
+ VA_Grow(self, new_size);
}
http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/2db62be3/runtime/core/Clownfish/VArray.cfh
----------------------------------------------------------------------
diff --git a/runtime/core/Clownfish/VArray.cfh b/runtime/core/Clownfish/VArray.cfh
index 69cc71e..a502b12 100644
--- a/runtime/core/Clownfish/VArray.cfh
+++ b/runtime/core/Clownfish/VArray.cfh
@@ -20,7 +20,7 @@ __C__
#include "Clownfish/Util/SortUtils.h"
typedef bool
-(*CFISH_VA_Gather_Test_t)(cfish_VArray *self, uint32_t tick, void *data);
+(*CFISH_VA_Gather_Test_t)(cfish_VArray *self, size_t tick, void *data);
#ifdef CFISH_USE_SHORT_NAMES
#define VA_Gather_Test_t CFISH_VA_Gather_Test_t
@@ -32,18 +32,18 @@ __END_C__
class Clownfish::VArray nickname VA inherits Clownfish::Obj {
Obj **elems;
- uint32_t size;
- uint32_t cap;
+ size_t size;
+ size_t cap;
inert incremented VArray*
- new(uint32_t capacity = 0);
+ new(size_t capacity = 0);
/**
* @param capacity Initial number of elements that the object will be able
* to hold before reallocation.
*/
inert VArray*
- init(VArray *self, uint32_t capacity = 0);
+ init(VArray *self, size_t capacity = 0);
/** Push an item onto the end of a VArray.
*/
@@ -74,31 +74,31 @@ class Clownfish::VArray nickname VA inherits Clownfish::Obj {
* elements.
*/
void
- Grow(VArray *self, uint32_t capacity);
+ Grow(VArray *self, size_t capacity);
/** Fetch the element at <code>tick</tick>.
*/
nullable Obj*
- Fetch(VArray *self, uint32_t tick);
+ Fetch(VArray *self, size_t tick);
/** Store an element at index `tick`, possibly displacing an
* existing element.
*/
void
- Store(VArray *self, uint32_t tick, decremented Obj *elem = NULL);
+ Store(VArray *self, size_t tick, decremented Obj *elem = NULL);
/** Replace an element in the VArray with NULL and return it.
*
* @return whatever was stored at `tick`.
*/
incremented nullable Obj*
- Delete(VArray *self, uint32_t tick);
+ Delete(VArray *self, size_t tick);
/** Remove `length` elements from the array, starting at
* `offset`. Move elements over to fill in the gap.
*/
void
- Excise(VArray *self, uint32_t offset, uint32_t length);
+ Excise(VArray *self, size_t offset, size_t length);
/** Clone the VArray but merely increment the refcounts of its elements
* rather than clone them.
@@ -127,7 +127,7 @@ class Clownfish::VArray nickname VA inherits Clownfish::Obj {
* than the current size, decrement and discard truncated elements.
*/
void
- Resize(VArray *self, uint32_t size);
+ Resize(VArray *self, size_t size);
/** Empty the VArray.
*/
@@ -136,12 +136,12 @@ class Clownfish::VArray nickname VA inherits Clownfish::Obj {
/** Accessor for `size` member.
*/
- public uint32_t
+ public size_t
Get_Size(VArray *self);
/** Accessor for `capacity` member.
*/
- uint32_t
+ size_t
Get_Capacity(VArray *self);
/** Return all elements for which `test` returns true.
@@ -157,7 +157,7 @@ class Clownfish::VArray nickname VA inherits Clownfish::Obj {
* @param length The maximum number of elements to slice.
*/
public incremented VArray*
- Slice(VArray *self, uint32_t offset, uint32_t length);
+ Slice(VArray *self, size_t offset, size_t length);
public bool
Equals(VArray *self, Obj *other);
[2/6] lucy-clownfish git commit: Avoid potential overflow in VA_Excise
Posted by nw...@apache.org.
Avoid potential overflow in VA_Excise
Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/ed096ff4
Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/ed096ff4
Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/ed096ff4
Branch: refs/heads/master
Commit: ed096ff47a3d92eb06a2204b8352043a32c5fc58
Parents: 2db62be
Author: Nick Wellnhofer <we...@aevum.de>
Authored: Thu Apr 23 14:33:51 2015 +0200
Committer: Nick Wellnhofer <we...@aevum.de>
Committed: Thu Apr 23 14:33:51 2015 +0200
----------------------------------------------------------------------
runtime/core/Clownfish/VArray.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/ed096ff4/runtime/core/Clownfish/VArray.c
----------------------------------------------------------------------
diff --git a/runtime/core/Clownfish/VArray.c b/runtime/core/Clownfish/VArray.c
index f395c6d..294e342 100644
--- a/runtime/core/Clownfish/VArray.c
+++ b/runtime/core/Clownfish/VArray.c
@@ -199,8 +199,8 @@ VA_Delete_IMP(VArray *self, size_t num) {
void
VA_Excise_IMP(VArray *self, size_t offset, size_t length) {
- if (self->size <= offset) { return; }
- else if (self->size < offset + length) { length = self->size - offset; }
+ if (offset >= self->size) { return; }
+ if (length > self->size - offset) { length = self->size - offset; }
for (size_t i = 0; i < length; i++) {
DECREF(self->elems[offset + i]);
[4/6] lucy-clownfish git commit: Check for overflow in VA_Grow
Posted by nw...@apache.org.
Check for overflow in VA_Grow
Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/b93a882a
Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/b93a882a
Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/b93a882a
Branch: refs/heads/master
Commit: b93a882aacd6750235c26059b250bbb8247c3053
Parents: 24ab8ec
Author: Nick Wellnhofer <we...@aevum.de>
Authored: Thu Apr 23 15:05:31 2015 +0200
Committer: Nick Wellnhofer <we...@aevum.de>
Committed: Thu Apr 23 15:05:31 2015 +0200
----------------------------------------------------------------------
runtime/core/Clownfish/VArray.c | 3 +++
1 file changed, 3 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/b93a882a/runtime/core/Clownfish/VArray.c
----------------------------------------------------------------------
diff --git a/runtime/core/Clownfish/VArray.c b/runtime/core/Clownfish/VArray.c
index 690f441..e3c2240 100644
--- a/runtime/core/Clownfish/VArray.c
+++ b/runtime/core/Clownfish/VArray.c
@@ -180,6 +180,9 @@ VA_Store_IMP(VArray *self, size_t tick, Obj *elem) {
void
VA_Grow_IMP(VArray *self, size_t capacity) {
if (capacity > self->cap) {
+ if (capacity > SIZE_MAX / sizeof(Obj*)) {
+ THROW(ERR, "Array grew too large");
+ }
self->elems = (Obj**)REALLOCATE(self->elems, capacity * sizeof(Obj*));
self->cap = capacity;
memset(self->elems + self->size, 0,
[3/6] lucy-clownfish git commit: Simplify range check in VA_Slice
Posted by nw...@apache.org.
Simplify range check in VA_Slice
Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/24ab8ec6
Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/24ab8ec6
Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/24ab8ec6
Branch: refs/heads/master
Commit: 24ab8ec602e6b5085cad56ea10809727e8bb8929
Parents: ed096ff
Author: Nick Wellnhofer <we...@aevum.de>
Authored: Thu Apr 23 14:35:57 2015 +0200
Committer: Nick Wellnhofer <we...@aevum.de>
Committed: Thu Apr 23 14:35:57 2015 +0200
----------------------------------------------------------------------
runtime/core/Clownfish/VArray.c | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/24ab8ec6/runtime/core/Clownfish/VArray.c
----------------------------------------------------------------------
diff --git a/runtime/core/Clownfish/VArray.c b/runtime/core/Clownfish/VArray.c
index 294e342..690f441 100644
--- a/runtime/core/Clownfish/VArray.c
+++ b/runtime/core/Clownfish/VArray.c
@@ -293,9 +293,7 @@ VA_Slice_IMP(VArray *self, size_t offset, size_t length) {
offset = 0;
length = 0;
}
- else if (length > SIZE_MAX - offset
- || offset + length > self->size
- ) {
+ else if (length > self->size - offset) {
length = self->size - offset;
}
[6/6] lucy-clownfish git commit: Merge branch
'CLOWNFISH-35-size_t-indices'
Posted by nw...@apache.org.
Merge branch 'CLOWNFISH-35-size_t-indices'
Fixes CLOWNFISH-35.
Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/ba2150d9
Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/ba2150d9
Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/ba2150d9
Branch: refs/heads/master
Commit: ba2150d96949ee8494f8e96307e24f13c90ff651
Parents: 36f36ea 5e798ae
Author: Nick Wellnhofer <we...@aevum.de>
Authored: Sun Apr 26 11:50:40 2015 +0200
Committer: Nick Wellnhofer <we...@aevum.de>
Committed: Sun Apr 26 11:50:40 2015 +0200
----------------------------------------------------------------------
runtime/core/Clownfish/Hash.c | 50 ++++++++--------
runtime/core/Clownfish/Hash.cfh | 18 +++---
runtime/core/Clownfish/HashIterator.c | 18 +++---
runtime/core/Clownfish/HashIterator.cfh | 6 +-
runtime/core/Clownfish/String.c | 6 +-
runtime/core/Clownfish/String.cfh | 2 +-
.../core/Clownfish/Test/TestLockFreeRegistry.c | 2 +-
.../Clownfish/Test/TestLockFreeRegistry.cfh | 2 +-
runtime/core/Clownfish/Test/TestVArray.c | 10 ++--
runtime/core/Clownfish/VArray.c | 60 ++++++++++----------
runtime/core/Clownfish/VArray.cfh | 28 ++++-----
11 files changed, 100 insertions(+), 102 deletions(-)
----------------------------------------------------------------------
[5/6] lucy-clownfish git commit: Use size_t for Hash indices
Posted by nw...@apache.org.
Use size_t for Hash indices
Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/5e798aea
Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/5e798aea
Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/5e798aea
Branch: refs/heads/master
Commit: 5e798aea6d48f6f2412cb8132b617952e1f5607c
Parents: b93a882
Author: Nick Wellnhofer <we...@aevum.de>
Authored: Fri Apr 24 12:27:06 2015 +0200
Committer: Nick Wellnhofer <we...@aevum.de>
Committed: Fri Apr 24 12:27:06 2015 +0200
----------------------------------------------------------------------
runtime/core/Clownfish/Hash.c | 50 ++++++++++----------
runtime/core/Clownfish/Hash.cfh | 18 +++----
runtime/core/Clownfish/HashIterator.c | 18 +++----
runtime/core/Clownfish/HashIterator.cfh | 6 +--
runtime/core/Clownfish/String.c | 6 +--
runtime/core/Clownfish/String.cfh | 2 +-
.../core/Clownfish/Test/TestLockFreeRegistry.c | 2 +-
.../Clownfish/Test/TestLockFreeRegistry.cfh | 2 +-
8 files changed, 51 insertions(+), 53 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/5e798aea/runtime/core/Clownfish/Hash.c
----------------------------------------------------------------------
diff --git a/runtime/core/Clownfish/Hash.c b/runtime/core/Clownfish/Hash.c
index d0553a7..e23d3b6 100644
--- a/runtime/core/Clownfish/Hash.c
+++ b/runtime/core/Clownfish/Hash.c
@@ -35,14 +35,14 @@ static String *TOMBSTONE;
#define HashEntry cfish_HashEntry
typedef struct HashEntry {
- String *key;
- Obj *value;
- int32_t hash_sum;
+ String *key;
+ Obj *value;
+ size_t hash_sum;
} HashEntry;
// Return the entry associated with the key, if any.
static CFISH_INLINE HashEntry*
-SI_fetch_entry(Hash *self, String *key, int32_t hash_sum);
+SI_fetch_entry(Hash *self, String *key, size_t hash_sum);
// Double the number of buckets and redistribute all entries.
static CFISH_INLINE HashEntry*
@@ -54,31 +54,30 @@ Hash_init_class() {
}
Hash*
-Hash_new(uint32_t capacity) {
+Hash_new(size_t capacity) {
Hash *self = (Hash*)Class_Make_Obj(HASH);
return Hash_init(self, capacity);
}
Hash*
-Hash_init(Hash *self, uint32_t capacity) {
+Hash_init(Hash *self, size_t min_threshold) {
// Allocate enough space to hold the requested number of elements without
// triggering a rebuild.
- uint32_t requested_capacity = capacity < INT32_MAX ? capacity : INT32_MAX;
- uint32_t threshold;
- capacity = 16;
- while (1) {
+ size_t threshold;
+ size_t capacity = 16;
+ do {
threshold = (capacity / 3) * 2;
- if (threshold > requested_capacity) { break; }
+ if (threshold > min_threshold) { break; }
capacity *= 2;
- }
+ } while (capacity <= SIZE_MAX / 2);
// Init.
- self->size = 0;
+ self->size = 0;
// Derive.
- self->capacity = capacity;
- self->entries = (HashEntry*)CALLOCATE(capacity, sizeof(HashEntry));
- self->threshold = threshold;
+ self->capacity = capacity;
+ self->entries = (HashEntry*)CALLOCATE(capacity, sizeof(HashEntry));
+ self->threshold = threshold;
return self;
}
@@ -117,7 +116,7 @@ Hash_Clear_IMP(Hash *self) {
}
static void
-S_do_store(Hash *self, String *key, Obj *value, int32_t hash_sum,
+S_do_store(Hash *self, String *key, Obj *value, size_t hash_sum,
bool incref_key) {
HashEntry *entry = SI_fetch_entry(self, key, hash_sum);
if (entry) {
@@ -129,8 +128,8 @@ S_do_store(Hash *self, String *key, Obj *value, int32_t hash_sum,
HashEntry *entries = self->size >= self->threshold
? SI_rebuild_hash(self)
: (HashEntry*)self->entries;
- uint32_t tick = hash_sum;
- const uint32_t mask = self->capacity - 1;
+ size_t tick = hash_sum;
+ const size_t mask = self->capacity - 1;
while (1) {
tick &= mask;
@@ -170,8 +169,8 @@ Hash_Fetch_Utf8_IMP(Hash *self, const char *key, size_t key_len) {
}
static CFISH_INLINE HashEntry*
-SI_fetch_entry(Hash *self, String *key, int32_t hash_sum) {
- uint32_t tick = hash_sum;
+SI_fetch_entry(Hash *self, String *key, size_t hash_sum) {
+ size_t tick = hash_sum;
HashEntry *const entries = (HashEntry*)self->entries;
HashEntry *entry;
@@ -223,7 +222,7 @@ Hash_Delete_Utf8_IMP(Hash *self, const char *key, size_t key_len) {
}
String*
-Hash_Find_Key_IMP(Hash *self, String *key, int32_t hash_sum) {
+Hash_Find_Key_IMP(Hash *self, String *key, size_t hash_sum) {
HashEntry *entry = SI_fetch_entry(self, key, hash_sum);
return entry ? entry->key : NULL;
}
@@ -281,20 +280,19 @@ Hash_Equals_IMP(Hash *self, Obj *other) {
return true;
}
-uint32_t
+size_t
Hash_Get_Capacity_IMP(Hash *self) {
return self->capacity;
}
-uint32_t
+size_t
Hash_Get_Size_IMP(Hash *self) {
return self->size;
}
static CFISH_INLINE HashEntry*
SI_rebuild_hash(Hash *self) {
- // HashIterator ticks are int32_t.
- if (self->capacity > INT32_MAX / 2) {
+ if (self->capacity > SIZE_MAX / 2) {
THROW(ERR, "Hash grew too large");
}
http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/5e798aea/runtime/core/Clownfish/Hash.cfh
----------------------------------------------------------------------
diff --git a/runtime/core/Clownfish/Hash.cfh b/runtime/core/Clownfish/Hash.cfh
index 54a7456..2960f2a 100644
--- a/runtime/core/Clownfish/Hash.cfh
+++ b/runtime/core/Clownfish/Hash.cfh
@@ -23,23 +23,23 @@ parcel Clownfish;
*/
public class Clownfish::Hash inherits Clownfish::Obj {
- void *entries;
- uint32_t capacity;
- uint32_t size;
- uint32_t threshold; /* rehashing trigger point */
+ void *entries;
+ size_t capacity;
+ size_t size;
+ size_t threshold; /* rehashing trigger point */
inert void
init_class();
public inert incremented Hash*
- new(uint32_t capacity = 0);
+ new(size_t capacity = 0);
/**
* @param capacity The number of elements that the hash will be asked to
* hold initially.
*/
public inert Hash*
- init(Hash *self, uint32_t capacity = 0);
+ init(Hash *self, size_t capacity = 0);
inert String*
get_tombstone();
@@ -83,7 +83,7 @@ public class Clownfish::Hash inherits Clownfish::Obj {
* rather than its value.
*/
nullable String*
- Find_Key(Hash *self, String *key, int32_t hash_sum);
+ Find_Key(Hash *self, String *key, size_t hash_sum);
/** Return an VArray of pointers to the hash's keys.
*/
@@ -95,14 +95,14 @@ public class Clownfish::Hash inherits Clownfish::Obj {
public incremented VArray*
Values(Hash *self);
- uint32_t
+ size_t
Get_Capacity(Hash *self);
/** Accessor for Hash's "size" member.
*
* @return the number of key-value pairs.
*/
- public uint32_t
+ public size_t
Get_Size(Hash *self);
public bool
http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/5e798aea/runtime/core/Clownfish/HashIterator.c
----------------------------------------------------------------------
diff --git a/runtime/core/Clownfish/HashIterator.c b/runtime/core/Clownfish/HashIterator.c
index ee31066..806c325 100644
--- a/runtime/core/Clownfish/HashIterator.c
+++ b/runtime/core/Clownfish/HashIterator.c
@@ -28,9 +28,9 @@
static String *TOMBSTONE;
typedef struct HashEntry {
- String *key;
- Obj *value;
- int32_t hash_sum;
+ String *key;
+ Obj *value;
+ size_t hash_sum;
} HashEntry;
void
@@ -50,7 +50,7 @@ HashIter_new(Hash *hash) {
HashIterator*
HashIter_init(HashIterator *self, Hash *hash) {
self->hash = (Hash*)INCREF(hash);
- self->tick = -1;
+ self->tick = (size_t)-1;
self->capacity = hash->capacity;
return self;
}
@@ -61,7 +61,7 @@ HashIter_Next_IMP(HashIterator *self) {
THROW(ERR, "Hash modified during iteration.");
}
while (1) {
- if (++self->tick >= (int32_t)self->capacity) {
+ if (++self->tick >= self->capacity) {
// Iteration complete. Pin tick at capacity.
self->tick = self->capacity;
return false;
@@ -82,10 +82,10 @@ HashIter_Get_Key_IMP(HashIterator *self) {
if (self->capacity != self->hash->capacity) {
THROW(ERR, "Hash modified during iteration.");
}
- if (self->tick >= (int32_t)self->capacity) {
+ if (self->tick >= self->capacity) {
THROW(ERR, "Invalid call to Get_Key after end of iteration.");
}
- else if (self->tick == -1) {
+ else if (self->tick == (size_t)-1) {
THROW(ERR, "Invalid call to Get_Key before iteration.");
}
@@ -102,10 +102,10 @@ HashIter_Get_Value_IMP(HashIterator *self) {
if (self->capacity != self->hash->capacity) {
THROW(ERR, "Hash modified during iteration.");
}
- if (self->tick >= (int32_t)self->capacity) {
+ if (self->tick >= self->capacity) {
THROW(ERR, "Invalid call to Get_Value after end of iteration.");
}
- else if (self->tick == -1) {
+ else if (self->tick == (size_t)-1) {
THROW(ERR, "Invalid call to Get_Value before iteration.");
}
http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/5e798aea/runtime/core/Clownfish/HashIterator.cfh
----------------------------------------------------------------------
diff --git a/runtime/core/Clownfish/HashIterator.cfh b/runtime/core/Clownfish/HashIterator.cfh
index 0e51273..f4dea3e 100644
--- a/runtime/core/Clownfish/HashIterator.cfh
+++ b/runtime/core/Clownfish/HashIterator.cfh
@@ -21,9 +21,9 @@ parcel Clownfish;
*/
class Clownfish::HashIterator nickname HashIter inherits Clownfish::Obj {
- Hash *hash;
- int32_t tick;
- uint32_t capacity;
+ Hash *hash;
+ size_t tick;
+ size_t capacity;
inert void
init_class();
http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/5e798aea/runtime/core/Clownfish/String.c
----------------------------------------------------------------------
diff --git a/runtime/core/Clownfish/String.c b/runtime/core/Clownfish/String.c
index 2a4bbfe..067a0b2 100644
--- a/runtime/core/Clownfish/String.c
+++ b/runtime/core/Clownfish/String.c
@@ -185,9 +185,9 @@ Str_Destroy_IMP(String *self) {
SUPER_DESTROY(self, STRING);
}
-int32_t
+size_t
Str_Hash_Sum_IMP(String *self) {
- uint32_t hashvalue = 5381;
+ size_t hashvalue = 5381;
StackStringIterator *iter = STR_STACKTOP(self);
const SStrIter_Next_t next
@@ -197,7 +197,7 @@ Str_Hash_Sum_IMP(String *self) {
hashvalue = ((hashvalue << 5) + hashvalue) ^ code_point;
}
- return (int32_t) hashvalue;
+ return hashvalue;
}
static void
http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/5e798aea/runtime/core/Clownfish/String.cfh
----------------------------------------------------------------------
diff --git a/runtime/core/Clownfish/String.cfh b/runtime/core/Clownfish/String.cfh
index 6ca7285..5ad84c9 100644
--- a/runtime/core/Clownfish/String.cfh
+++ b/runtime/core/Clownfish/String.cfh
@@ -217,7 +217,7 @@ class Clownfish::String nickname Str
/** Return a hash code for the string.
*/
- int32_t
+ size_t
Hash_Sum(String *self);
public incremented String*
http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/5e798aea/runtime/core/Clownfish/Test/TestLockFreeRegistry.c
----------------------------------------------------------------------
diff --git a/runtime/core/Clownfish/Test/TestLockFreeRegistry.c b/runtime/core/Clownfish/Test/TestLockFreeRegistry.c
index 1acc1ed..5bc4d30 100644
--- a/runtime/core/Clownfish/Test/TestLockFreeRegistry.c
+++ b/runtime/core/Clownfish/Test/TestLockFreeRegistry.c
@@ -39,7 +39,7 @@ StupidHashString_new(const char *text) {
strlen(text));
}
-int32_t
+size_t
StupidHashString_Hash_Sum_IMP(StupidHashString *self) {
UNUSED_VAR(self);
return 1;
http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/5e798aea/runtime/core/Clownfish/Test/TestLockFreeRegistry.cfh
----------------------------------------------------------------------
diff --git a/runtime/core/Clownfish/Test/TestLockFreeRegistry.cfh b/runtime/core/Clownfish/Test/TestLockFreeRegistry.cfh
index a54f00b..c2cca6e 100644
--- a/runtime/core/Clownfish/Test/TestLockFreeRegistry.cfh
+++ b/runtime/core/Clownfish/Test/TestLockFreeRegistry.cfh
@@ -33,7 +33,7 @@ class Clownfish::Test::StupidHashString inherits Clownfish::String {
new(const char *text);
/** Always returns 1, guaranteeing collisions. */
- int32_t
+ size_t
Hash_Sum(StupidHashString *self);
}