vec: is_inlined -> not_inlined
If you allocate a vector on the stack and default initialise
it (i.e. {0}), then by default "is_inlined" = 0. This is bad, as
technically speaking we should expect the vector to attempt to use
it's small inline buffer for vector operations before going onto the
heap - this will mess up our functions. So here I adjust the name to
make default stack based allocation a bit easier.
This commit is contained in:
2
alisp.h
2
alisp.h
@@ -66,7 +66,7 @@ typedef struct Vector
|
|||||||
{
|
{
|
||||||
u64 size, capacity;
|
u64 size, capacity;
|
||||||
// Small buffer optimisation
|
// Small buffer optimisation
|
||||||
u8 is_inlined;
|
u8 not_inlined;
|
||||||
union
|
union
|
||||||
{
|
{
|
||||||
void *ptr;
|
void *ptr;
|
||||||
|
|||||||
@@ -21,12 +21,13 @@
|
|||||||
void sys_init(sys_t *sys)
|
void sys_init(sys_t *sys)
|
||||||
{
|
{
|
||||||
memset(sys, 0, sizeof(*sys));
|
memset(sys, 0, sizeof(*sys));
|
||||||
|
vec_init(&sys->conses, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void sys_register(sys_t *sys, lisp_t *ptr)
|
void sys_register(sys_t *sys, lisp_t *ptr)
|
||||||
{
|
{
|
||||||
// Simply append it to the list of currently active conses
|
// Simply append it to the list of currently active conses
|
||||||
vec_append(&sys->conses, ptr, sizeof(ptr));
|
vec_append(&sys->conses, &ptr, sizeof(&ptr));
|
||||||
}
|
}
|
||||||
|
|
||||||
void sys_cleanup(sys_t *sys)
|
void sys_cleanup(sys_t *sys)
|
||||||
|
|||||||
@@ -25,15 +25,15 @@ void vec_init(vec_t *vec, u64 size)
|
|||||||
return;
|
return;
|
||||||
else if (size <= VEC_INLINE_CAPACITY)
|
else if (size <= VEC_INLINE_CAPACITY)
|
||||||
{
|
{
|
||||||
vec->is_inlined = 1;
|
vec->not_inlined = 0;
|
||||||
vec->capacity = VEC_INLINE_CAPACITY;
|
vec->capacity = VEC_INLINE_CAPACITY;
|
||||||
vec->ptr = NULL;
|
vec->ptr = NULL;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
vec->is_inlined = 0;
|
vec->not_inlined = 1;
|
||||||
vec->capacity = size;
|
vec->capacity = size;
|
||||||
vec->ptr = calloc(1, vec->capacity);
|
vec->ptr = calloc(1, vec->capacity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,14 +41,14 @@ void vec_free(vec_t *vec)
|
|||||||
{
|
{
|
||||||
if (!vec)
|
if (!vec)
|
||||||
return;
|
return;
|
||||||
if (!vec->is_inlined && vec->ptr)
|
if (vec->not_inlined && vec->ptr)
|
||||||
free(vec->ptr);
|
free(vec->ptr);
|
||||||
memset(vec, 0, sizeof(*vec));
|
memset(vec, 0, sizeof(*vec));
|
||||||
}
|
}
|
||||||
|
|
||||||
void *vec_data(vec_t *vec)
|
void *vec_data(vec_t *vec)
|
||||||
{
|
{
|
||||||
return vec->is_inlined ? vec->inlined : vec->ptr;
|
return vec->not_inlined ? vec->ptr : vec->inlined;
|
||||||
}
|
}
|
||||||
|
|
||||||
void vec_ensure_free(vec_t *vec, u64 size)
|
void vec_ensure_free(vec_t *vec, u64 size)
|
||||||
@@ -58,7 +58,7 @@ void vec_ensure_free(vec_t *vec, u64 size)
|
|||||||
if (vec->capacity - vec->size < size)
|
if (vec->capacity - vec->size < size)
|
||||||
{
|
{
|
||||||
vec->capacity = MAX(vec->capacity * VEC_MULT, vec->size + size);
|
vec->capacity = MAX(vec->capacity * VEC_MULT, vec->size + size);
|
||||||
if (vec->is_inlined)
|
if (!vec->not_inlined)
|
||||||
{
|
{
|
||||||
// If we're inlined, we need to allocate on the heap now. So let's copy
|
// If we're inlined, we need to allocate on the heap now. So let's copy
|
||||||
// vec->inlined over to vec->ptr, then turn off inlining.
|
// vec->inlined over to vec->ptr, then turn off inlining.
|
||||||
@@ -69,7 +69,7 @@ void vec_ensure_free(vec_t *vec, u64 size)
|
|||||||
memcpy(buffer, vec->inlined, vec->size);
|
memcpy(buffer, vec->inlined, vec->size);
|
||||||
vec->ptr = calloc(1, vec->capacity);
|
vec->ptr = calloc(1, vec->capacity);
|
||||||
memcpy(vec->ptr, buffer, vec->size);
|
memcpy(vec->ptr, buffer, vec->size);
|
||||||
vec->is_inlined = 0;
|
vec->not_inlined = 1;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
vec->ptr = realloc(vec->ptr, vec->capacity);
|
vec->ptr = realloc(vec->ptr, vec->capacity);
|
||||||
|
|||||||
Reference in New Issue
Block a user