From 2e058e99c1ff891da3708f4b77fa627f6accd618 Mon Sep 17 00:00:00 2001 From: Erik Charlebois Date: Sat, 11 May 2013 22:29:07 -0400 Subject: [PATCH] VM: Refactor strings.cpp/hpp to Factor style --- vm/strings.cpp | 184 +++++++++++++++++++++++-------------------------- vm/strings.hpp | 13 ++-- 2 files changed, 90 insertions(+), 107 deletions(-) diff --git a/vm/strings.cpp b/vm/strings.cpp index ef93d8c503..f73ae0802a 100644 --- a/vm/strings.cpp +++ b/vm/strings.cpp @@ -1,126 +1,114 @@ #include "master.hpp" -namespace factor -{ +namespace factor { /* Allocates memory */ -string *factor_vm::allot_string_internal(cell capacity) -{ - string *str = allot(string_size(capacity)); +string* factor_vm::allot_string_internal(cell capacity) { + string* str = allot(string_size(capacity)); - str->length = tag_fixnum(capacity); - str->hashcode = false_object; - str->aux = false_object; + str->length = tag_fixnum(capacity); + str->hashcode = false_object; + str->aux = false_object; - return str; + return str; } /* Allocates memory */ -void factor_vm::fill_string(string *str_, cell start, cell capacity, cell fill) -{ - data_root str(str_,this); - - if(fill <= 0x7f) - memset(&str->data()[start],(u8)fill,capacity - start); - else - { - byte_array *aux; - if(to_boolean(str->aux)) - aux = untag(str->aux); - else - { - aux = allot_uninitialized_array(untag_fixnum(str->length) * 2); - str->aux = tag(aux); - write_barrier(&str->aux); - } - - u8 lo_fill = (u8)((fill & 0x7f) | 0x80); - u16 hi_fill = (u16)((fill >> 7) ^ 0x1); - memset(&str->data()[start],lo_fill,capacity - start); - memset_2(&aux->data()[start],hi_fill,(capacity - start) * sizeof(u16)); - } +void factor_vm::fill_string(string* str_, cell start, cell capacity, + cell fill) { + data_root str(str_, this); + + if (fill <= 0x7f) + memset(&str->data()[start], (u8) fill, capacity - start); + else { + byte_array* aux; + if (to_boolean(str->aux)) + aux = untag(str->aux); + else { + aux = + allot_uninitialized_array(untag_fixnum(str->length) * 2); + str->aux = tag(aux); + write_barrier(&str->aux); + } + + u8 lo_fill = (u8)((fill & 0x7f) | 0x80); + u16 hi_fill = (u16)((fill >> 7) ^ 0x1); + memset(&str->data()[start], lo_fill, capacity - start); + memset_2(&aux->data()[start], hi_fill, + (capacity - start) * sizeof(u16)); + } } /* Allocates memory */ -string *factor_vm::allot_string(cell capacity, cell fill) -{ - data_root str(allot_string_internal(capacity),this); - fill_string(str.untagged(),0,capacity,fill); - return str.untagged(); +string* factor_vm::allot_string(cell capacity, cell fill) { + data_root str(allot_string_internal(capacity), this); + fill_string(str.untagged(), 0, capacity, fill); + return str.untagged(); } /* Allocates memory */ -void factor_vm::primitive_string() -{ - cell initial = to_cell(ctx->pop()); - cell length = unbox_array_size(); - ctx->push(tag(allot_string(length,initial))); +void factor_vm::primitive_string() { + cell initial = to_cell(ctx->pop()); + cell length = unbox_array_size(); + ctx->push(tag(allot_string(length, initial))); } -bool factor_vm::reallot_string_in_place_p(string *str, cell capacity) -{ - return nursery.contains_p(str) - && (!to_boolean(str->aux) || nursery.contains_p(untag(str->aux))) - && capacity <= string_capacity(str); +bool factor_vm::reallot_string_in_place_p(string* str, cell capacity) { + return nursery.contains_p(str) && + (!to_boolean(str->aux) || + nursery.contains_p(untag(str->aux))) && + capacity <= string_capacity(str); } /* Allocates memory */ -string* factor_vm::reallot_string(string *str_, cell capacity) -{ - data_root str(str_,this); - - if(reallot_string_in_place_p(str.untagged(),capacity)) - { - str->length = tag_fixnum(capacity); - - if(to_boolean(str->aux)) - { - byte_array *aux = untag(str->aux); - aux->capacity = tag_fixnum(capacity * 2); - } - - return str.untagged(); - } - else - { - cell to_copy = string_capacity(str.untagged()); - if(capacity < to_copy) - to_copy = capacity; - - data_root new_str(allot_string_internal(capacity),this); - - memcpy(new_str->data(),str->data(),to_copy); - - if(to_boolean(str->aux)) - { - byte_array *new_aux = allot_uninitialized_array(capacity * 2); - new_str->aux = tag(new_aux); - write_barrier(&new_str->aux); - - byte_array *aux = untag(str->aux); - memcpy(new_aux->data(),aux->data(),to_copy * sizeof(u16)); - } - - fill_string(new_str.untagged(),to_copy,capacity,'\0'); - return new_str.untagged(); - } +string* factor_vm::reallot_string(string* str_, cell capacity) { + data_root str(str_, this); + + if (reallot_string_in_place_p(str.untagged(), capacity)) { + str->length = tag_fixnum(capacity); + + if (to_boolean(str->aux)) { + byte_array* aux = untag(str->aux); + aux->capacity = tag_fixnum(capacity * 2); + } + + return str.untagged(); + } else { + cell to_copy = string_capacity(str.untagged()); + if (capacity < to_copy) + to_copy = capacity; + + data_root new_str(allot_string_internal(capacity), this); + + memcpy(new_str->data(), str->data(), to_copy); + + if (to_boolean(str->aux)) { + byte_array* new_aux = allot_uninitialized_array(capacity * 2); + new_str->aux = tag(new_aux); + write_barrier(&new_str->aux); + + byte_array* aux = untag(str->aux); + memcpy(new_aux->data(), aux->data(), to_copy * sizeof(u16)); + } + + fill_string(new_str.untagged(), to_copy, capacity, '\0'); + return new_str.untagged(); + } } /* Allocates memory */ -void factor_vm::primitive_resize_string() -{ - data_root str(ctx->pop(),this); - str.untag_check(this); - cell capacity = unbox_array_size(); - ctx->push(tag(reallot_string(str.untagged(),capacity))); +void factor_vm::primitive_resize_string() { + data_root str(ctx->pop(), this); + str.untag_check(this); + cell capacity = unbox_array_size(); + ctx->push(tag(reallot_string(str.untagged(), capacity))); } -void factor_vm::primitive_set_string_nth_fast() -{ - string *str = untag(ctx->pop()); - cell index = untag_fixnum(ctx->pop()); - cell value = untag_fixnum(ctx->pop()); - str->data()[index] = (u8)value; +void factor_vm::primitive_set_string_nth_fast() { + string* str = untag(ctx->pop()); + cell index = untag_fixnum(ctx->pop()); + cell value = untag_fixnum(ctx->pop()); + str->data()[index] = (u8) value; } } diff --git a/vm/strings.hpp b/vm/strings.hpp index 54ff981d99..af9813329d 100644 --- a/vm/strings.hpp +++ b/vm/strings.hpp @@ -1,14 +1,9 @@ -namespace factor -{ +namespace factor { -inline static cell string_capacity(const string *str) -{ - return untag_fixnum(str->length); +inline static cell string_capacity(const string* str) { + return untag_fixnum(str->length); } -inline static cell string_size(cell size) -{ - return sizeof(string) + size; -} +inline static cell string_size(cell size) { return sizeof(string) + size; } } -- 2.34.1