This commit is contained in:
@@ -40,7 +40,7 @@
|
||||
/*!
|
||||
* \brief Rate at which an evstring grows whenever a resize is needed
|
||||
*/
|
||||
#define EV_STR_GROWTH_FACTOR 3 / 2
|
||||
#define EV_STR_GROWTH_FACTOR 2
|
||||
#endif
|
||||
|
||||
typedef char *evstring;
|
||||
@@ -54,7 +54,7 @@ TYPEDATA_GEN(evstring_error_t, DEFAULT(EV_STR_ERR_NONE));
|
||||
struct evstr_meta_t {
|
||||
EV_DEBUG(u64 magic;)
|
||||
u64 length;
|
||||
u64 size;
|
||||
u64 capacity;
|
||||
enum {
|
||||
EV_STR_ALLOCATION_TYPE_STACK,
|
||||
EV_STR_ALLOCATION_TYPE_HEAP
|
||||
@@ -67,7 +67,7 @@ struct evstr_meta_t {
|
||||
(( struct { struct evstr_meta_t meta; char data[len]; } ) { \
|
||||
EV_DEBUG(.meta.magic = EV_STR_evstring_MAGIC,) \
|
||||
.meta.length = len-1, \
|
||||
.meta.size = len, \
|
||||
.meta.capacity = len, \
|
||||
.meta.allocationType = EV_STR_ALLOCATION_TYPE_STACK, \
|
||||
.data = str \
|
||||
}).data
|
||||
@@ -249,9 +249,10 @@ evstring_new_impl(
|
||||
const char *data,
|
||||
u64 len)
|
||||
{
|
||||
u64 size = sizeof(struct evstr_meta_t) + len + 1;
|
||||
u64 str_cap = len + 1;
|
||||
u64 alloc_size = sizeof(struct evstr_meta_t) + str_cap;
|
||||
|
||||
void *p = ev_str_malloc(size);
|
||||
void *p = ev_str_malloc(alloc_size);
|
||||
assert(p); // Raised if malloc fails
|
||||
|
||||
struct evstr_meta_t *meta = (struct evstr_meta_t *)p;
|
||||
@@ -260,7 +261,7 @@ EV_DEBUG
|
||||
meta->magic = EV_STR_evstring_MAGIC;
|
||||
)
|
||||
meta->length = len;
|
||||
meta->size = size;
|
||||
meta->capacity = str_cap;
|
||||
meta->allocationType = EV_STR_ALLOCATION_TYPE_HEAP;
|
||||
|
||||
evstring s = (evstring)(meta + 1);
|
||||
@@ -343,9 +344,9 @@ evstring_getLength(
|
||||
}
|
||||
|
||||
evstring_error_t
|
||||
evstring_setSize(
|
||||
evstring_setCapacity(
|
||||
evstring *s,
|
||||
size_t newsize)
|
||||
size_t new_capacity)
|
||||
{
|
||||
evstr_asserttype(*s);
|
||||
struct evstr_meta_t *meta = META(*s);
|
||||
@@ -353,12 +354,12 @@ evstring_setSize(
|
||||
return EV_STR_ERR_OOM;
|
||||
}
|
||||
|
||||
if(meta->size == newsize) {
|
||||
if(meta->capacity == new_capacity) {
|
||||
return EV_STR_ERR_NONE;
|
||||
}
|
||||
|
||||
void *buf = (void*)meta;
|
||||
void *tmp = ev_str_realloc(buf, sizeof(struct evstr_meta_t) + newsize);
|
||||
void *tmp = ev_str_realloc(buf, sizeof(struct evstr_meta_t) + new_capacity);
|
||||
|
||||
if (!tmp) {
|
||||
return EV_STR_ERR_OOM;
|
||||
@@ -370,7 +371,7 @@ evstring_setSize(
|
||||
*s = (evstring)(meta+1);
|
||||
}
|
||||
|
||||
meta->size = newsize;
|
||||
meta->capacity = new_capacity;
|
||||
return EV_STR_ERR_NONE;
|
||||
}
|
||||
|
||||
@@ -379,7 +380,7 @@ evstring_grow(
|
||||
evstring *s)
|
||||
{
|
||||
evstr_asserttype(*s);
|
||||
return evstring_setSize(s, META(*s)->size * EV_STR_GROWTH_FACTOR);
|
||||
return evstring_setCapacity(s, META(*s)->capacity * EV_STR_GROWTH_FACTOR);
|
||||
}
|
||||
|
||||
evstring_error_t
|
||||
@@ -393,8 +394,8 @@ evstring_setLength(
|
||||
return EV_STR_ERR_NONE;
|
||||
}
|
||||
|
||||
u64 required_size = sizeof(struct evstr_meta_t) + newlen + 1;
|
||||
while(required_size > meta->size) {
|
||||
u64 required_capacity = newlen + 1;
|
||||
while(required_capacity > meta->capacity) {
|
||||
evstring_error_t grow_err = evstring_grow(s);
|
||||
if(grow_err) {
|
||||
return grow_err;
|
||||
@@ -441,8 +442,8 @@ evstring_push_impl(
|
||||
struct evstr_meta_t *meta = META(*s);
|
||||
|
||||
// TODO Find a more efficient approach?
|
||||
u64 required_size = sizeof(struct evstr_meta_t) + meta->length + sz + 1;
|
||||
while(required_size > meta->size) { // `<=` because of the null terminator
|
||||
u64 required_capacity = meta->length + sz + 1;
|
||||
while(required_capacity > meta->capacity) { // `<=` because of the null terminator
|
||||
evstring_error_t grow_err = evstring_grow(s);
|
||||
if(grow_err != EV_STR_ERR_NONE) {
|
||||
return grow_err;
|
||||
@@ -520,7 +521,7 @@ evstring_getSpace(
|
||||
{
|
||||
evstr_asserttype(s);
|
||||
struct evstr_meta_t *meta = META(s);
|
||||
return meta->size - meta->length - 1 - sizeof(struct evstr_meta_t);
|
||||
return meta->capacity - meta->length - 1;
|
||||
}
|
||||
|
||||
evstring_error_t
|
||||
@@ -529,7 +530,7 @@ evstring_addSpace(
|
||||
u64 space)
|
||||
{
|
||||
evstr_asserttype(*s);
|
||||
return evstring_setSize(s, META(*s)->size + space);
|
||||
return evstring_setCapacity(s, META(*s)->capacity + space);
|
||||
}
|
||||
|
||||
evstring_view
|
||||
|
||||
Reference in New Issue
Block a user