[svn:parrot] r47494 - in trunk: src/pmc t/pmc
NotFound at svn.parrot.org
NotFound at svn.parrot.org
Tue Jun 8 23:45:40 UTC 2010
Author: NotFound
Date: Tue Jun 8 23:45:40 2010
New Revision: 47494
URL: https://trac.parrot.org/parrot/changeset/47494
Log:
some more functionality in ByteBuffer
Modified:
trunk/src/pmc/bytebuffer.pmc
trunk/t/pmc/bytebuffer.t
Modified: trunk/src/pmc/bytebuffer.pmc
==============================================================================
--- trunk/src/pmc/bytebuffer.pmc Tue Jun 8 23:12:00 2010 (r47493)
+++ trunk/src/pmc/bytebuffer.pmc Tue Jun 8 23:45:40 2010 (r47494)
@@ -22,6 +22,7 @@
pmclass ByteBuffer auto_attrs {
ATTR INTVAL allocated_size;
ATTR INTVAL size;
+ ATTR STRING *source;
ATTR unsigned char *content;
/*
@@ -36,6 +37,9 @@
=item C<void init_int()>
Create a buffer of initial_size capacity.
+=item C<void mark()>
+Mark the source string if any.
+
=item C<void destroy()>
Free the buffer when destroying.
@@ -44,7 +48,7 @@
*/
VTABLE void init() {
- PObj_custom_destroy_SET(SELF);
+ PObj_custom_mark_destroy_SETALL(SELF);
}
VTABLE void init_int(INTVAL initial_size) {
@@ -56,10 +60,21 @@
SET_ATTR_content(INTERP, SELF, content);
}
+ VTABLE void mark() {
+ const STRING * source;
+ GET_ATTR_source(INTERP, SELF, source);
+ if (!STRING_IS_NULL(source))
+ Parrot_gc_mark_STRING_alive(INTERP, source);
+ }
+
VTABLE void destroy() {
- unsigned char *content;
- GET_ATTR_content(INTERP, SELF, content);
- Parrot_gc_free_memory_chunk(INTERP, content);
+ INTVAL allocated_size;
+ GET_ATTR_allocated_size(INTERP, SELF, allocated_size);
+ if (allocated_size) {
+ unsigned char *content;
+ GET_ATTR_content(INTERP, SELF, content);
+ Parrot_gc_free_memory_chunk(INTERP, content);
+ }
}
/*
@@ -77,6 +92,46 @@
return size;
}
+/*
+
+=item C<void set_string_native()>
+Reset the buffer with the content of the string.
+
+=cut
+
+*/
+
+ VTABLE void set_string_native(STRING *new_string) {
+ INTVAL allocated_size;
+ GET_ATTR_allocated_size(INTERP, SELF, allocated_size);
+ if (allocated_size) {
+ unsigned char *content;
+ GET_ATTR_content(INTERP, SELF, content);
+ Parrot_gc_free_memory_chunk(INTERP, content);
+ SET_ATTR_allocated_size(INTERP, SELF, 0);
+ }
+ SET_ATTR_source(INTERP, SELF, new_string);
+ SET_ATTR_size(INTERP, SELF, Parrot_str_byte_length(INTERP, new_string));
+ SET_ATTR_content(INTERP, SELF, (unsigned char *)new_string->strstart);
+ }
+
+/*
+
+=item C<INTVAL get_integer_keyed_int()>
+Get the value of the byte at position or 0 if out of bounds.
+
+=cut
+
+*/
+
+ VTABLE INTVAL get_integer_keyed_int(INTVAL position) {
+ INTVAL size;
+ unsigned char *content;
+ GET_ATTR_size(INTERP, SELF, size);
+ GET_ATTR_content(INTERP, SELF, content);
+ return (position >= 0 && position < size) ? content[position] : (INTVAL) 0;
+ }
+
} /* pmclass end */
/*
Modified: trunk/t/pmc/bytebuffer.t
==============================================================================
--- trunk/t/pmc/bytebuffer.t Tue Jun 8 23:12:00 2010 (r47493)
+++ trunk/t/pmc/bytebuffer.t Tue Jun 8 23:45:40 2010 (r47494)
@@ -18,9 +18,10 @@
.sub 'main' :main
.include 'test_more.pir'
- plan(2)
+ plan(7)
test_init()
+ test_set()
.end
.sub test_init
@@ -36,6 +37,27 @@
.end
+.sub test_set
+ .local pmc bb
+ .local string s
+ .local int n, c
+ bb = new ['ByteBuffer']
+ s = 'Hi'
+ bb = s
+ n = elements bb
+ is(n, 2, "size is the same as the source string bytelength")
+ n = bb[0]
+ c = ord 'H'
+ is(n, c, "first byte is the same as the source string")
+ n = bb[1]
+ c = ord 'i'
+ is(n, c, "second byte is the same as the source string")
+ n = bb[2]
+ is(n, 0, "byte out of size is 0")
+ n = bb[-1]
+ is(n, 0, "byte at negative index is 0")
+.end
+
# Local Variables:
# mode: pir
# fill-column: 100
More information about the parrot-commits
mailing list