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/12/12 12:30:47 UTC

[1/6] lucy-clownfish git commit: Remove Blob_compare and BB_compare

Repository: lucy-clownfish
Updated Branches:
  refs/heads/master 42b766804 -> fe23648e7


Remove Blob_compare and BB_compare


Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/d4665edf
Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/d4665edf
Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/d4665edf

Branch: refs/heads/master
Commit: d4665edf94bdb5e43b180ca13585b0ff8b520608
Parents: 42b7668
Author: Nick Wellnhofer <we...@aevum.de>
Authored: Wed Dec 9 14:16:51 2015 +0100
Committer: Nick Wellnhofer <we...@aevum.de>
Committed: Wed Dec 9 14:16:51 2015 +0100

----------------------------------------------------------------------
 runtime/core/Clownfish/Blob.c             | 21 +++++++--------------
 runtime/core/Clownfish/Blob.cfh           |  6 ------
 runtime/core/Clownfish/ByteBuf.c          | 21 +++++++--------------
 runtime/core/Clownfish/ByteBuf.cfh        |  6 ------
 runtime/core/Clownfish/Test/TestBlob.c    | 15 ++++++++-------
 runtime/core/Clownfish/Test/TestByteBuf.c | 15 ++++++++-------
 6 files changed, 30 insertions(+), 54 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/d4665edf/runtime/core/Clownfish/Blob.c
----------------------------------------------------------------------
diff --git a/runtime/core/Clownfish/Blob.c b/runtime/core/Clownfish/Blob.c
index 4d3b66b..3e37e0c 100644
--- a/runtime/core/Clownfish/Blob.c
+++ b/runtime/core/Clownfish/Blob.c
@@ -114,25 +114,18 @@ Blob_Equals_Bytes_IMP(Blob *self, const void *bytes, size_t size) {
     return SI_equals_bytes(self, bytes, size);
 }
 
-int
-Blob_compare(const void *va, const void *vb) {
-    Blob *a = *(Blob**)va;
-    Blob *b = *(Blob**)vb;
-    const size_t size = a->size < b->size ? a->size : b->size;
+int32_t
+Blob_Compare_To_IMP(Blob *self, Obj *other) {
+    Blob *twin = (Blob*)CERTIFY(other, BLOB);
+    const size_t size = self->size < twin->size ? self->size : twin->size;
 
-    int32_t comparison = memcmp(a->buf, b->buf, size);
+    int32_t comparison = memcmp(self->buf, twin->buf, size);
 
-    if (comparison == 0 && a->size != b->size) {
-        comparison = a->size < b->size ? -1 : 1;
+    if (comparison == 0 && self->size != twin->size) {
+        comparison = self->size < twin->size ? -1 : 1;
     }
 
     return comparison;
 }
 
-int32_t
-Blob_Compare_To_IMP(Blob *self, Obj *other) {
-    CERTIFY(other, BLOB);
-    return Blob_compare(&self, &other);
-}
-
 

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/d4665edf/runtime/core/Clownfish/Blob.cfh
----------------------------------------------------------------------
diff --git a/runtime/core/Clownfish/Blob.cfh b/runtime/core/Clownfish/Blob.cfh
index 0be3226..9832c5f 100644
--- a/runtime/core/Clownfish/Blob.cfh
+++ b/runtime/core/Clownfish/Blob.cfh
@@ -44,12 +44,6 @@ public final class Clownfish::Blob inherits Clownfish::Obj {
     public inert Blob*
     init_wrap(Blob *self, const char *buf, size_t size);
 
-    /** Lexical comparison of two Blobs, with level of indirection set to
-     * please qsort and friends.
-     */
-    inert int
-    compare(const void *va, const void *vb);
-
     void*
     To_Host(Blob *self);
 

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/d4665edf/runtime/core/Clownfish/ByteBuf.c
----------------------------------------------------------------------
diff --git a/runtime/core/Clownfish/ByteBuf.c b/runtime/core/Clownfish/ByteBuf.c
index 11a0acc..976c64f 100644
--- a/runtime/core/Clownfish/ByteBuf.c
+++ b/runtime/core/Clownfish/ByteBuf.c
@@ -217,25 +217,18 @@ BB_Trusted_Utf8_To_String_IMP(ByteBuf *self) {
     return Str_new_from_trusted_utf8(self->buf, self->size);
 }
 
-int
-BB_compare(const void *va, const void *vb) {
-    ByteBuf *a = *(ByteBuf**)va;
-    ByteBuf *b = *(ByteBuf**)vb;
-    const size_t size = a->size < b->size ? a->size : b->size;
+int32_t
+BB_Compare_To_IMP(ByteBuf *self, Obj *other) {
+    ByteBuf *twin = (ByteBuf*)CERTIFY(other, BYTEBUF);
+    const size_t size = self->size < twin->size ? self->size : twin->size;
 
-    int32_t comparison = memcmp(a->buf, b->buf, size);
+    int32_t comparison = memcmp(self->buf, twin->buf, size);
 
-    if (comparison == 0 && a->size != b->size) {
-        comparison = a->size < b->size ? -1 : 1;
+    if (comparison == 0 && self->size != twin->size) {
+        comparison = self->size < twin->size ? -1 : 1;
     }
 
     return comparison;
 }
 
-int32_t
-BB_Compare_To_IMP(ByteBuf *self, Obj *other) {
-    CERTIFY(other, BYTEBUF);
-    return BB_compare(&self, &other);
-}
-
 

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/d4665edf/runtime/core/Clownfish/ByteBuf.cfh
----------------------------------------------------------------------
diff --git a/runtime/core/Clownfish/ByteBuf.cfh b/runtime/core/Clownfish/ByteBuf.cfh
index f6f6233..e154ffd 100644
--- a/runtime/core/Clownfish/ByteBuf.cfh
+++ b/runtime/core/Clownfish/ByteBuf.cfh
@@ -56,12 +56,6 @@ final class Clownfish::ByteBuf nickname BB inherits Clownfish::Obj {
     init_steal_bytes(ByteBuf *self, void *bytes, size_t size,
                      size_t capacity);
 
-    /** Lexical comparison of two ByteBufs, with level of indirection set to
-     * please qsort and friends.
-     */
-    inert int
-    compare(const void *va, const void *vb);
-
     void*
     To_Host(ByteBuf *self);
 

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/d4665edf/runtime/core/Clownfish/Test/TestBlob.c
----------------------------------------------------------------------
diff --git a/runtime/core/Clownfish/Test/TestBlob.c b/runtime/core/Clownfish/Test/TestBlob.c
index e64e49a..9307f5f 100644
--- a/runtime/core/Clownfish/Test/TestBlob.c
+++ b/runtime/core/Clownfish/Test/TestBlob.c
@@ -75,12 +75,12 @@ test_Clone(TestBatchRunner *runner) {
 }
 
 static void
-test_compare(TestBatchRunner *runner) {
+test_Compare_To(TestBatchRunner *runner) {
     {
         Blob *a = Blob_new("foo", 4);
         Blob *b = Blob_new("foo", 4);
-        TEST_INT_EQ(runner, Blob_compare(&a, &b), 0,
-                    "Blob_compare returns 0 for equal Blobs");
+        TEST_INT_EQ(runner, Blob_Compare_To(a, (Obj*)b), 0,
+                    "Compare_To returns 0 for equal Blobs");
         DECREF(a);
         DECREF(b);
     }
@@ -88,7 +88,8 @@ test_compare(TestBatchRunner *runner) {
     {
         Blob *a = Blob_new("foo", 3);
         Blob *b = Blob_new("foo\0b", 5);
-        TEST_TRUE(runner, Blob_compare(&a, &b) < 0, "shorter Blob sorts first");
+        TEST_TRUE(runner, Blob_Compare_To(a, (Obj*)b) < 0,
+                  "shorter Blob sorts first");
         DECREF(a);
         DECREF(b);
     }
@@ -96,8 +97,8 @@ test_compare(TestBatchRunner *runner) {
     {
         Blob *a = Blob_new("foo\0a", 5);
         Blob *b = Blob_new("foo\0b", 5);
-        TEST_TRUE(runner, Blob_compare(&a, &b) < 0,
-                  "NULL doesn't interfere with Blob_compare");
+        TEST_TRUE(runner, Blob_Compare_To(a, (Obj*)b) < 0,
+                  "NULL doesn't interfere with Compare_To");
         DECREF(a);
         DECREF(b);
     }
@@ -108,7 +109,7 @@ TestBlob_Run_IMP(TestBlob *self, TestBatchRunner *runner) {
     TestBatchRunner_Plan(runner, (TestBatch*)self, 11);
     test_Equals(runner);
     test_Clone(runner);
-    test_compare(runner);
+    test_Compare_To(runner);
 }
 
 

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/d4665edf/runtime/core/Clownfish/Test/TestByteBuf.c
----------------------------------------------------------------------
diff --git a/runtime/core/Clownfish/Test/TestByteBuf.c b/runtime/core/Clownfish/Test/TestByteBuf.c
index 57238bc..c2f799d 100644
--- a/runtime/core/Clownfish/Test/TestByteBuf.c
+++ b/runtime/core/Clownfish/Test/TestByteBuf.c
@@ -82,22 +82,23 @@ test_Clone(TestBatchRunner *runner) {
 }
 
 static void
-test_compare(TestBatchRunner *runner) {
+test_Compare_To(TestBatchRunner *runner) {
     ByteBuf *a = BB_new_bytes("foo\0a", 5);
     ByteBuf *b = BB_new_bytes("foo\0b", 5);
 
     BB_Set_Size(a, 4);
     BB_Set_Size(b, 4);
-    TEST_INT_EQ(runner, BB_compare(&a, &b), 0,
-                "BB_compare returns 0 for equal ByteBufs");
+    TEST_INT_EQ(runner, BB_Compare_To(a, (Obj*)b), 0,
+                "Compare_To returns 0 for equal ByteBufs");
 
     BB_Set_Size(a, 3);
-    TEST_TRUE(runner, BB_compare(&a, &b) < 0, "shorter ByteBuf sorts first");
+    TEST_TRUE(runner, BB_Compare_To(a, (Obj*)b) < 0,
+              "shorter ByteBuf sorts first");
 
     BB_Set_Size(a, 5);
     BB_Set_Size(b, 5);
-    TEST_TRUE(runner, BB_compare(&a, &b) < 0,
-              "NULL doesn't interfere with BB_compare");
+    TEST_TRUE(runner, BB_Compare_To(a, (Obj*)b) < 0,
+              "NULL doesn't interfere with Compare_To");
 
     DECREF(a);
     DECREF(b);
@@ -172,7 +173,7 @@ TestBB_Run_IMP(TestByteBuf *self, TestBatchRunner *runner) {
     test_Equals(runner);
     test_Grow(runner);
     test_Clone(runner);
-    test_compare(runner);
+    test_Compare_To(runner);
     test_Mimic(runner);
     test_Utf8_To_String(runner);
     test_Cat(runner);


[3/6] lucy-clownfish git commit: Make ByteBuf and its methods public

Posted by nw...@apache.org.
Make ByteBuf and its methods public


Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/8c4b2905
Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/8c4b2905
Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/8c4b2905

Branch: refs/heads/master
Commit: 8c4b2905a269c1a2dc7a781c6eb61d0fe4d35680
Parents: c8c942d
Author: Nick Wellnhofer <we...@aevum.de>
Authored: Wed Dec 9 14:31:56 2015 +0100
Committer: Nick Wellnhofer <we...@aevum.de>
Committed: Wed Dec 9 14:31:56 2015 +0100

----------------------------------------------------------------------
 runtime/core/Clownfish/ByteBuf.cfh | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/8c4b2905/runtime/core/Clownfish/ByteBuf.cfh
----------------------------------------------------------------------
diff --git a/runtime/core/Clownfish/ByteBuf.cfh b/runtime/core/Clownfish/ByteBuf.cfh
index 8cf5e49..217d075 100644
--- a/runtime/core/Clownfish/ByteBuf.cfh
+++ b/runtime/core/Clownfish/ByteBuf.cfh
@@ -20,7 +20,7 @@ parcel Clownfish;
  * Growable buffer holding arbitrary bytes.
  */
 
-final class Clownfish::ByteBuf nickname BB inherits Clownfish::Obj {
+public final class Clownfish::ByteBuf nickname BB inherits Clownfish::Obj {
 
     char    *buf;
     size_t   size;  /* number of valid bytes */
@@ -62,34 +62,34 @@ final class Clownfish::ByteBuf nickname BB inherits Clownfish::Obj {
     /** Set the object's size member.  If greater than the object's capacity,
      * throws an error.
      */
-    void
+    public void
     Set_Size(ByteBuf *self, size_t size);
 
     /** Accessor for "size" member.
      */
-    size_t
+    public size_t
     Get_Size(ByteBuf *self);
 
     /** Accessor for raw internal buffer.
      */
-    nullable char*
+    public nullable char*
     Get_Buf(ByteBuf *self);
 
     /** Return the number of bytes in the Object's allocation.
      */
-    size_t
+    public size_t
     Get_Capacity(ByteBuf *self);
 
     /** Concatenate the passed-in bytes onto the end of the ByteBuf. Allocate
      * more memory as needed.
      */
-    void
+    public void
     Cat_Bytes(ByteBuf *self, const void *bytes, size_t size);
 
     /** Concatenate the contents of `other` onto the end of the
      * original ByteBuf. Allocate more memory as needed.
      */
-    void
+    public void
     Cat(ByteBuf *self, Blob *blob);
 
     /** Assign more memory to the ByteBuf, if it doesn't already have enough
@@ -97,12 +97,12 @@ final class Clownfish::ByteBuf nickname BB inherits Clownfish::Obj {
      *
      * @return a pointer to the raw buffer.
      */
-    nullable char*
+    public nullable char*
     Grow(ByteBuf *self, size_t size);
 
     /** Return the content of the ByteBuf as Blob and clear the ByteBuf.
      */
-    incremented Blob*
+    public incremented Blob*
     Yield_Blob(ByteBuf *self);
 
     /** Return a String which holds a copy of the UTF-8 character data in
@@ -119,7 +119,7 @@ final class Clownfish::ByteBuf nickname BB inherits Clownfish::Obj {
 
     /** Test whether the ByteBuf matches the passed-in bytes.
      */
-    bool
+    public bool
     Equals_Bytes(ByteBuf *self, const void *bytes, size_t size);
 
     public int32_t


[6/6] lucy-clownfish git commit: Merge branch 'bytebuf-api'

Posted by nw...@apache.org.
Merge branch 'bytebuf-api'


Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/fe23648e
Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/fe23648e
Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/fe23648e

Branch: refs/heads/master
Commit: fe23648e7e30bedbbbb45a7f40b929d8c05c3157
Parents: 42b7668 b42f55e
Author: Nick Wellnhofer <we...@aevum.de>
Authored: Sat Dec 12 12:30:19 2015 +0100
Committer: Nick Wellnhofer <we...@aevum.de>
Committed: Sat Dec 12 12:30:19 2015 +0100

----------------------------------------------------------------------
 runtime/core/Clownfish/Blob.c             |  21 ++--
 runtime/core/Clownfish/Blob.cfh           |   6 -
 runtime/core/Clownfish/ByteBuf.c          | 151 ++++++++++++++-----------
 runtime/core/Clownfish/ByteBuf.cfh        |  32 ++----
 runtime/core/Clownfish/Test/TestBlob.c    |  15 +--
 runtime/core/Clownfish/Test/TestByteBuf.c | 104 +++++++----------
 runtime/go/clownfish/bytebuf_test.go      |  41 +++----
 runtime/perl/t/binding/022-bytebuf.t      |   5 +-
 8 files changed, 166 insertions(+), 209 deletions(-)
----------------------------------------------------------------------



[5/6] lucy-clownfish git commit: Fix public ByteBuf methods in Go bindings

Posted by nw...@apache.org.
Fix public ByteBuf methods in Go bindings


Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/b42f55e4
Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/b42f55e4
Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/b42f55e4

Branch: refs/heads/master
Commit: b42f55e4af5ac615c91a0b8e0b72180d0a6462b0
Parents: 7165ca5
Author: Nick Wellnhofer <we...@aevum.de>
Authored: Wed Dec 9 18:26:02 2015 +0100
Committer: Nick Wellnhofer <we...@aevum.de>
Committed: Wed Dec 9 18:26:02 2015 +0100

----------------------------------------------------------------------
 runtime/go/clownfish/bytebuf_test.go | 30 +++++++++++++++---------------
 1 file changed, 15 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/b42f55e4/runtime/go/clownfish/bytebuf_test.go
----------------------------------------------------------------------
diff --git a/runtime/go/clownfish/bytebuf_test.go b/runtime/go/clownfish/bytebuf_test.go
index 2cc2a27..fba7708 100644
--- a/runtime/go/clownfish/bytebuf_test.go
+++ b/runtime/go/clownfish/bytebuf_test.go
@@ -22,8 +22,8 @@ import "reflect"
 func TestByteBufCat(t *testing.T) {
 	bb := NewByteBuf(0)
 	content := []byte("foo")
-	bb.cat(content)
-	if got := bb.yieldBlob(); !reflect.DeepEqual(got, content) {
+	bb.Cat(content)
+	if got := bb.YieldBlob(); !reflect.DeepEqual(got, content) {
 		t.Errorf("Expected %v, got %v", content, got)
 	}
 }
@@ -31,20 +31,20 @@ func TestByteBufCat(t *testing.T) {
 func TestByteBufSetSizeGetSize(t *testing.T) {
 	bb := NewByteBuf(0)
 	content := []byte("abc")
-	bb.cat(content)
-	bb.setSize(2)
-	if got := bb.getSize(); got != 2 {
+	bb.Cat(content)
+	bb.SetSize(2)
+	if got := bb.GetSize(); got != 2 {
 		t.Errorf("Expected size 2, got %d", got)
 	}
 	expected := []byte("ab")
-	if got := bb.yieldBlob(); !reflect.DeepEqual(got, expected) {
+	if got := bb.YieldBlob(); !reflect.DeepEqual(got, expected) {
 		t.Errorf("Expected %v, got %v", expected, got)
 	}
 }
 
 func TestByteBufGetCapacity(t *testing.T) {
 	bb := NewByteBuf(5)
-	if cap := bb.getCapacity(); cap < 5 {
+	if cap := bb.GetCapacity(); cap < 5 {
 		t.Errorf("Expected at least 5, got %d", cap)
 	}
 }
@@ -53,12 +53,12 @@ func TestByteBufEquals(t *testing.T) {
 	bb := NewByteBuf(0)
 	other := NewByteBuf(0)
 	content := []byte("foo")
-	bb.cat(content)
-	other.cat(content)
+	bb.Cat(content)
+	other.Cat(content)
 	if !bb.Equals(other) {
 		t.Errorf("Equals against equal ByteBuf")
 	}
-	other.setSize(2)
+	other.SetSize(2)
 	if bb.Equals(other) {
 		t.Errorf("Equals against non-equal ByteBuf")
 	}
@@ -70,9 +70,9 @@ func TestByteBufEquals(t *testing.T) {
 func TestByteBufClone(t *testing.T) {
 	content := []byte("foo")
 	bb := NewByteBuf(0)
-	bb.cat(content)
+	bb.Cat(content)
 	clone := bb.Clone().(ByteBuf)
-	if got := clone.yieldBlob(); !reflect.DeepEqual(got, content) {
+	if got := clone.YieldBlob(); !reflect.DeepEqual(got, content) {
 		t.Errorf("Expected %v, got %v", content, got)
 	}
 }
@@ -81,12 +81,12 @@ func TestByteBufCompareTo(t *testing.T) {
 	bb := NewByteBuf(0)
 	other := NewByteBuf(0)
 	content := []byte("foo")
-	bb.cat(content)
-	other.cat(content)
+	bb.Cat(content)
+	other.Cat(content)
 	if got := bb.CompareTo(other); got != 0 {
 		t.Errorf("CompareTo equal, got %d", got)
 	}
-	other.setSize(2)
+	other.SetSize(2)
 	if got := bb.CompareTo(other); got <= 0 {
 		t.Errorf("CompareTo lesser, got %d", got)
 	}


[4/6] lucy-clownfish git commit: Rework allocation and oversizing of ByteBufs

Posted by nw...@apache.org.
Rework allocation and oversizing of ByteBufs

Always round up capacity to next multiple of eight.

Don't call Memory_oversize, but oversize by custom amount of 25%.

Check for integer overflow. This should only matter on exotic platforms,
but it doesn't cost much.


Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/7165ca56
Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/7165ca56
Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/7165ca56

Branch: refs/heads/master
Commit: 7165ca565d24596191eb8c417d3af1523dcf1a1d
Parents: 8c4b290
Author: Nick Wellnhofer <we...@aevum.de>
Authored: Wed Dec 9 14:55:53 2015 +0100
Committer: Nick Wellnhofer <we...@aevum.de>
Committed: Wed Dec 9 15:04:01 2015 +0100

----------------------------------------------------------------------
 runtime/core/Clownfish/ByteBuf.c | 107 ++++++++++++++++++++++++----------
 1 file changed, 77 insertions(+), 30 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/7165ca56/runtime/core/Clownfish/ByteBuf.c
----------------------------------------------------------------------
diff --git a/runtime/core/Clownfish/ByteBuf.c b/runtime/core/Clownfish/ByteBuf.c
index 3ab5ddd..14ede6f 100644
--- a/runtime/core/Clownfish/ByteBuf.c
+++ b/runtime/core/Clownfish/ByteBuf.c
@@ -29,8 +29,21 @@
 #include "Clownfish/String.h"
 #include "Clownfish/Util/Memory.h"
 
+// Ensure that the ByteBuf's capacity is at least (size + extra).
+// If the buffer must be grown, oversize the allocation.
+static CFISH_INLINE void
+SI_add_grow_and_oversize(ByteBuf *self, size_t size, size_t extra);
+
+// Compilers tend to inline this function although this is the unlikely
+// slow path. If we ever add cross-platform support for the noinline
+// attribute, it should be marked as such to reduce code size.
+static void
+S_grow_and_oversize(ByteBuf *self, size_t min_size);
+
+// Not inlining the THROW macro reduces code size and complexity of
+// SI_add_grow_and_oversize.
 static void
-S_grow(ByteBuf *self, size_t size);
+S_overflow_error();
 
 ByteBuf*
 BB_new(size_t capacity) {
@@ -39,12 +52,15 @@ BB_new(size_t capacity) {
 }
 
 ByteBuf*
-BB_init(ByteBuf *self, size_t capacity) {
-    size_t amount = capacity ? capacity : sizeof(int64_t);
-    self->buf   = NULL;
-    self->size  = 0;
-    self->cap   = 0;
-    S_grow(self, amount);
+BB_init(ByteBuf *self, size_t min_cap) {
+    // Round up to next multiple of eight.
+    size_t capacity = (min_cap + 7) & ~7;
+    // Check for overflow.
+    if (capacity < min_cap) { capacity = SIZE_MAX; }
+
+    self->buf  = (char*)MALLOCATE(capacity);
+    self->size = 0;
+    self->cap  = capacity;
     return self;
 }
 
@@ -56,9 +72,15 @@ BB_new_bytes(const void *bytes, size_t size) {
 
 ByteBuf*
 BB_init_bytes(ByteBuf *self, const void *bytes, size_t size) {
-    BB_init(self, size);
-    memcpy(self->buf, bytes, size);
+    // Round up to next multiple of eight.
+    size_t capacity = (size + 7) & ~7;
+    // Check for overflow.
+    if (capacity < size) { capacity = SIZE_MAX; }
+
+    self->buf  = (char*)MALLOCATE(capacity);
     self->size = size;
+    self->cap  = capacity;
+    memcpy(self->buf, bytes, size);
     return self;
 }
 
@@ -133,12 +155,9 @@ BB_Equals_Bytes_IMP(ByteBuf *self, const void *bytes, size_t size) {
 
 static CFISH_INLINE void
 SI_cat_bytes(ByteBuf *self, const void *bytes, size_t size) {
-    const size_t new_size = self->size + size;
-    if (new_size > self->cap) {
-        S_grow(self, Memory_oversize(new_size, sizeof(char)));
-    }
-    memcpy((self->buf + self->size), bytes, size);
-    self->size = new_size;
+    SI_add_grow_and_oversize(self, self->size, size);
+    memcpy(self->buf + self->size, bytes, size);
+    self->size += size;
 }
 
 void
@@ -151,23 +170,18 @@ BB_Cat_IMP(ByteBuf *self, Blob *blob) {
     SI_cat_bytes(self, Blob_Get_Buf(blob), Blob_Get_Size(blob));
 }
 
-static void
-S_grow(ByteBuf *self, size_t size) {
-    if (size > self->cap) {
-        size_t amount    = size;
-        size_t remainder = amount % sizeof(int64_t);
-        if (remainder) {
-            amount += sizeof(int64_t);
-            amount -= remainder;
-        }
-        self->buf = (char*)REALLOCATE(self->buf, amount);
-        self->cap = amount;
+char*
+BB_Grow_IMP(ByteBuf *self, size_t min_cap) {
+    if (min_cap > self->cap) {
+        // Round up to next multiple of eight.
+        size_t capacity = (min_cap + 7) & ~7;
+        // Check for overflow.
+        if (capacity < min_cap) { capacity = SIZE_MAX; }
+
+        self->buf = (char*)REALLOCATE(self->buf, capacity);
+        self->cap = capacity;
     }
-}
 
-char*
-BB_Grow_IMP(ByteBuf *self, size_t size) {
-    if (size > self->cap) { S_grow(self, size); }
     return self->buf;
 }
 
@@ -204,4 +218,37 @@ BB_Compare_To_IMP(ByteBuf *self, Obj *other) {
     return comparison;
 }
 
+static CFISH_INLINE void
+SI_add_grow_and_oversize(ByteBuf *self, size_t size, size_t extra) {
+    size_t min_size = size + extra;
+    if (min_size < size) {
+        S_overflow_error();
+        return;
+    }
+
+    if (min_size > self->cap) {
+        S_grow_and_oversize(self, min_size);
+    }
+}
+
+static void
+S_grow_and_oversize(ByteBuf *self, size_t min_size) {
+    // Oversize by 25%, but at least eight bytes.
+    size_t extra = min_size / 4;
+    // Round up to next multiple of eight.
+    extra = (extra + 7) & ~7;
+
+    size_t capacity = min_size + extra;
+    // Check for overflow.
+    if (capacity < min_size) { capacity = SIZE_MAX; }
+
+    self->buf = (char*)REALLOCATE(self->buf, capacity);
+    self->cap = capacity;
+}
+
+static void
+S_overflow_error() {
+    THROW(ERR, "ByteBuf buffer overflow");
+}
+
 


[2/6] lucy-clownfish git commit: Remove BB_Mimic and BB_Mimic_Bytes

Posted by nw...@apache.org.
Remove BB_Mimic and BB_Mimic_Bytes

It can be replaced with BB_Set_Size(bb, 0) followed by BB_Cat_*.


Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/c8c942d3
Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/c8c942d3
Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/c8c942d3

Branch: refs/heads/master
Commit: c8c942d3231345da5e9ba5ea38c9af18b82152d2
Parents: d4665ed
Author: Nick Wellnhofer <we...@aevum.de>
Authored: Wed Dec 9 14:29:46 2015 +0100
Committer: Nick Wellnhofer <we...@aevum.de>
Committed: Wed Dec 9 14:29:46 2015 +0100

----------------------------------------------------------------------
 runtime/core/Clownfish/ByteBuf.c          | 27 --------
 runtime/core/Clownfish/ByteBuf.cfh        |  6 --
 runtime/core/Clownfish/Test/TestByteBuf.c | 89 ++++++++++----------------
 runtime/go/clownfish/bytebuf_test.go      | 11 ----
 runtime/perl/t/binding/022-bytebuf.t      |  5 +-
 5 files changed, 36 insertions(+), 102 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/c8c942d3/runtime/core/Clownfish/ByteBuf.c
----------------------------------------------------------------------
diff --git a/runtime/core/Clownfish/ByteBuf.c b/runtime/core/Clownfish/ByteBuf.c
index 976c64f..3ab5ddd 100644
--- a/runtime/core/Clownfish/ByteBuf.c
+++ b/runtime/core/Clownfish/ByteBuf.c
@@ -132,33 +132,6 @@ BB_Equals_Bytes_IMP(ByteBuf *self, const void *bytes, size_t size) {
 }
 
 static CFISH_INLINE void
-SI_mimic_bytes(ByteBuf *self, const void *bytes, size_t size) {
-    if (size > self->cap) { S_grow(self, size); }
-    memmove(self->buf, bytes, size);
-    self->size = size;
-}
-
-void
-BB_Mimic_Bytes_IMP(ByteBuf *self, const void *bytes, size_t size) {
-    SI_mimic_bytes(self, bytes, size);
-}
-
-void
-BB_Mimic_IMP(ByteBuf *self, Obj *other) {
-    if (Obj_is_a(other, BYTEBUF)) {
-        ByteBuf *twin = (ByteBuf*)other;
-        SI_mimic_bytes(self, twin->buf, twin->size);
-    }
-    else if (Obj_is_a(other, STRING)) {
-        String *string = (String*)other;
-        SI_mimic_bytes(self, Str_Get_Ptr8(string), Str_Get_Size(string));
-    }
-    else {
-        THROW(ERR, "ByteBuf can't mimic %o", Obj_get_class_name(other));
-    }
-}
-
-static CFISH_INLINE void
 SI_cat_bytes(ByteBuf *self, const void *bytes, size_t size) {
     const size_t new_size = self->size + size;
     if (new_size > self->cap) {

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/c8c942d3/runtime/core/Clownfish/ByteBuf.cfh
----------------------------------------------------------------------
diff --git a/runtime/core/Clownfish/ByteBuf.cfh b/runtime/core/Clownfish/ByteBuf.cfh
index e154ffd..8cf5e49 100644
--- a/runtime/core/Clownfish/ByteBuf.cfh
+++ b/runtime/core/Clownfish/ByteBuf.cfh
@@ -80,12 +80,6 @@ final class Clownfish::ByteBuf nickname BB inherits Clownfish::Obj {
     size_t
     Get_Capacity(ByteBuf *self);
 
-    public void
-    Mimic(ByteBuf *self, Obj *other);
-
-    void
-    Mimic_Bytes(ByteBuf *self, const void *bytes, size_t size);
-
     /** Concatenate the passed-in bytes onto the end of the ByteBuf. Allocate
      * more memory as needed.
      */

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/c8c942d3/runtime/core/Clownfish/Test/TestByteBuf.c
----------------------------------------------------------------------
diff --git a/runtime/core/Clownfish/Test/TestByteBuf.c b/runtime/core/Clownfish/Test/TestByteBuf.c
index c2f799d..6e8063c 100644
--- a/runtime/core/Clownfish/Test/TestByteBuf.c
+++ b/runtime/core/Clownfish/Test/TestByteBuf.c
@@ -36,29 +36,37 @@ TestBB_new() {
 
 static void
 test_Equals(TestBatchRunner *runner) {
-    ByteBuf *wanted = BB_new_bytes("foo", 4); // Include terminating NULL.
-    ByteBuf *got    = BB_new_bytes("foo", 4);
+    ByteBuf *bb = BB_new_bytes("foo", 4); // Include terminating NULL.
 
-    TEST_TRUE(runner, BB_Equals(wanted, (Obj*)got), "Equals");
+    {
+        ByteBuf *other = BB_new_bytes("foo", 4);
+        TEST_TRUE(runner, BB_Equals(bb, (Obj*)other), "Equals");
+        DECREF(other);
+    }
 
-    TEST_TRUE(runner, BB_Equals_Bytes(got, "foo", 4), "Equals_Bytes");
-    TEST_FALSE(runner, BB_Equals_Bytes(got, "foo", 3),
+    TEST_TRUE(runner, BB_Equals_Bytes(bb, "foo", 4), "Equals_Bytes");
+    TEST_FALSE(runner, BB_Equals_Bytes(bb, "foo", 3),
                "Equals_Bytes spoiled by different size");
-    TEST_FALSE(runner, BB_Equals_Bytes(got, "bar", 4),
+    TEST_FALSE(runner, BB_Equals_Bytes(bb, "bar", 4),
                "Equals_Bytes spoiled by different content");
 
-    BB_Set_Size(got, 3);
-    TEST_FALSE(runner, BB_Equals(wanted, (Obj*)got),
-               "Different size spoils Equals");
+    {
+        ByteBuf *other = BB_new_bytes("foo", 3);
+        TEST_FALSE(runner, BB_Equals(bb, (Obj*)other),
+                   "Different size spoils Equals");
+        DECREF(other);
+    }
 
-    BB_Mimic_Bytes(got, "bar", 4);
-    TEST_INT_EQ(runner, BB_Get_Size(wanted), BB_Get_Size(got),
-                "same length");
-    TEST_FALSE(runner, BB_Equals(wanted, (Obj*)got),
-               "Different content spoils Equals");
+    {
+        ByteBuf *other = BB_new_bytes("bar", 4);
+        TEST_INT_EQ(runner, BB_Get_Size(bb), BB_Get_Size(other),
+                    "same length");
+        TEST_FALSE(runner, BB_Equals(bb, (Obj*)other),
+                   "Different content spoils Equals");
+        DECREF(other);
+    }
 
-    DECREF(got);
-    DECREF(wanted);
+    DECREF(bb);
 }
 
 static void
@@ -105,46 +113,20 @@ test_Compare_To(TestBatchRunner *runner) {
 }
 
 static void
-test_Mimic(TestBatchRunner *runner) {
-    ByteBuf *a = BB_new_bytes("foo", 3);
-    ByteBuf *b = BB_new(0);
-
-    BB_Mimic(b, (Obj*)a);
-    TEST_TRUE(runner, BB_Equals(a, (Obj*)b), "Mimic");
-
-    BB_Mimic_Bytes(a, "bar", 4);
-    TEST_TRUE(runner, strcmp(BB_Get_Buf(a), "bar") == 0,
-              "Mimic_Bytes content");
-    TEST_INT_EQ(runner, BB_Get_Size(a), 4, "Mimic_Bytes size");
-
-    BB_Mimic(b, (Obj*)a);
-    TEST_TRUE(runner, BB_Equals(a, (Obj*)b), "Mimic");
-
-    String *string = Str_newf("baz");
-    BB_Mimic(b, (Obj*)string);
-    DECREF(string);
-    TEST_TRUE(runner, BB_Equals_Bytes(b, "baz", 3), "Mimic String");
-
-    DECREF(a);
-    DECREF(b);
-}
-
-static void
 test_Cat(TestBatchRunner *runner) {
-    ByteBuf *wanted  = BB_new_bytes("foobar", 6);
-    ByteBuf *got     = BB_new_bytes("foo", 3);
-    Blob    *blob    = Blob_new("bar", 3);
+    ByteBuf *bb = BB_new_bytes("foo", 3);
 
-    BB_Cat(got, blob);
-    TEST_TRUE(runner, BB_Equals(wanted, (Obj*)got), "Cat");
+    {
+        Blob *blob = Blob_new("bar", 3);
+        BB_Cat(bb, blob);
+        TEST_TRUE(runner, BB_Equals_Bytes(bb, "foobar", 6), "Cat");
+        DECREF(blob);
+    }
 
-    BB_Mimic_Bytes(wanted, "foobarbaz", 9);
-    BB_Cat_Bytes(got, "baz", 3);
-    TEST_TRUE(runner, BB_Equals(wanted, (Obj*)got), "Cat_Bytes");
+    BB_Cat_Bytes(bb, "baz", 3);
+    TEST_TRUE(runner, BB_Equals_Bytes(bb, "foobarbaz", 9), "Cat_Bytes");
 
-    DECREF(blob);
-    DECREF(got);
-    DECREF(wanted);
+    DECREF(bb);
 }
 
 static void
@@ -169,12 +151,11 @@ test_Utf8_To_String(TestBatchRunner *runner) {
 
 void
 TestBB_Run_IMP(TestByteBuf *self, TestBatchRunner *runner) {
-    TestBatchRunner_Plan(runner, (TestBatch*)self, 22);
+    TestBatchRunner_Plan(runner, (TestBatch*)self, 17);
     test_Equals(runner);
     test_Grow(runner);
     test_Clone(runner);
     test_Compare_To(runner);
-    test_Mimic(runner);
     test_Utf8_To_String(runner);
     test_Cat(runner);
 }

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/c8c942d3/runtime/go/clownfish/bytebuf_test.go
----------------------------------------------------------------------
diff --git a/runtime/go/clownfish/bytebuf_test.go b/runtime/go/clownfish/bytebuf_test.go
index 66c9772..2cc2a27 100644
--- a/runtime/go/clownfish/bytebuf_test.go
+++ b/runtime/go/clownfish/bytebuf_test.go
@@ -49,17 +49,6 @@ func TestByteBufGetCapacity(t *testing.T) {
 	}
 }
 
-func TestByteBufMimic(t *testing.T) {
-	bb := NewByteBuf(0)
-	content := []byte("foo")
-	bb.cat(content)
-	other := NewByteBuf(0)
-	other.Mimic(bb)
-	if got := other.yieldBlob(); !reflect.DeepEqual(got, content) {
-		t.Errorf("Expected %v, got %v", content, got)
-	}
-}
-
 func TestByteBufEquals(t *testing.T) {
 	bb := NewByteBuf(0)
 	other := NewByteBuf(0)

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/c8c942d3/runtime/perl/t/binding/022-bytebuf.t
----------------------------------------------------------------------
diff --git a/runtime/perl/t/binding/022-bytebuf.t b/runtime/perl/t/binding/022-bytebuf.t
index 09edae8..b6bd61e 100644
--- a/runtime/perl/t/binding/022-bytebuf.t
+++ b/runtime/perl/t/binding/022-bytebuf.t
@@ -17,7 +17,7 @@ use strict;
 use warnings;
 use lib 'buildlib';
 
-use Test::More tests => 11;
+use Test::More tests => 10;
 use Clownfish;
 
 my $buf = Clownfish::ByteBuf->new('abc');
@@ -36,9 +36,6 @@ ok( $buf->equals($buf), 'equals true');
 ok( !$buf->equals($other), 'equals false');
 ok( $buf->compare_to($other) < 0, 'compare_to');
 
-$buf->mimic($other);
-ok( $buf->equals($other), 'mimic' );
-
 $buf = $other->clone_raw;
 isa_ok( $buf, 'Clownfish::ByteBuf', 'clone' );
 ok( $buf->equals($other), 'equals after clone' );