lisp: lisp_reset, vec: vec_reset
Reset method to "clear the memory" of a lisp object. Only operates on heap allocated objects.
This commit is contained in:
@@ -49,6 +49,11 @@ typedef enum Tag
|
|||||||
#define INT_MIN (-(INT_MAX + 1))
|
#define INT_MIN (-(INT_MAX + 1))
|
||||||
|
|
||||||
tag_t tag_get(const lisp_t *);
|
tag_t tag_get(const lisp_t *);
|
||||||
|
u64 tag_sizeof(tag_t);
|
||||||
|
u64 lisp_sizeof(lisp_t *);
|
||||||
|
lisp_t *lisp_reset(lisp_t *);
|
||||||
|
void lisp_print(FILE *, lisp_t *);
|
||||||
|
|
||||||
lisp_t *tag_smi(const i64);
|
lisp_t *tag_smi(const i64);
|
||||||
lisp_t *tag_sym(const char *);
|
lisp_t *tag_sym(const char *);
|
||||||
lisp_t *tag_cons(const cons_t *);
|
lisp_t *tag_cons(const cons_t *);
|
||||||
@@ -65,10 +70,6 @@ str_t *as_str(lisp_t *);
|
|||||||
#define CAR(L) (as_cons(L)->car)
|
#define CAR(L) (as_cons(L)->car)
|
||||||
#define CDR(L) (as_cons(L)->cdr)
|
#define CDR(L) (as_cons(L)->cdr)
|
||||||
|
|
||||||
void lisp_print(FILE *, lisp_t *);
|
|
||||||
u64 tag_sizeof(tag_t);
|
|
||||||
u64 lisp_sizeof(lisp_t *);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Copyright (C) 2026 Aryadev Chavali
|
/* Copyright (C) 2026 Aryadev Chavali
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ static_assert(sizeof(vec_t) == 64, "vec_t has to be 64 bytes as part of SBO");
|
|||||||
|
|
||||||
void vec_init(vec_t *, u64);
|
void vec_init(vec_t *, u64);
|
||||||
void vec_free(vec_t *);
|
void vec_free(vec_t *);
|
||||||
|
void vec_reset(vec_t *);
|
||||||
u8 *vec_data(vec_t *);
|
u8 *vec_data(vec_t *);
|
||||||
|
|
||||||
// Append, possibly reallocating memory
|
// Append, possibly reallocating memory
|
||||||
|
|||||||
34
src/lisp.c
34
src/lisp.c
@@ -238,6 +238,40 @@ u64 lisp_sizeof(lisp_t *lisp)
|
|||||||
return tag_sizeof(tag_get(lisp));
|
return tag_sizeof(tag_get(lisp));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lisp_t *lisp_reset(lisp_t *lisp)
|
||||||
|
{
|
||||||
|
switch (tag_get(lisp))
|
||||||
|
{
|
||||||
|
case TAG_NIL:
|
||||||
|
case TAG_SMI:
|
||||||
|
case TAG_SYM:
|
||||||
|
// Nothing to "reset" here.
|
||||||
|
return lisp;
|
||||||
|
case TAG_CONS:
|
||||||
|
{
|
||||||
|
// Make `car` and `cons` NIL
|
||||||
|
CAR(lisp) = NIL;
|
||||||
|
CDR(lisp) = NIL;
|
||||||
|
return lisp;
|
||||||
|
}
|
||||||
|
case TAG_VEC:
|
||||||
|
{
|
||||||
|
vec_reset(as_vec(lisp));
|
||||||
|
return lisp;
|
||||||
|
}
|
||||||
|
case TAG_STR:
|
||||||
|
{
|
||||||
|
vec_reset(&as_str(lisp)->data);
|
||||||
|
return lisp;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
FAIL("Unreachable");
|
||||||
|
return lisp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* Copyright (C) 2025, 2026 Aryadev Chavali
|
/* Copyright (C) 2025, 2026 Aryadev Chavali
|
||||||
|
|
||||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
|||||||
@@ -38,6 +38,14 @@ void vec_free(vec_t *vec)
|
|||||||
memset(vec, 0, sizeof(*vec));
|
memset(vec, 0, sizeof(*vec));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void vec_reset(vec_t *vec)
|
||||||
|
{
|
||||||
|
if (!vec)
|
||||||
|
return;
|
||||||
|
memset(vec_data(vec), 0, vec->capacity);
|
||||||
|
vec->size = 0;
|
||||||
|
}
|
||||||
|
|
||||||
u8 *vec_data(vec_t *vec)
|
u8 *vec_data(vec_t *vec)
|
||||||
{
|
{
|
||||||
return vec->not_inlined ? vec->ptr : vec->inlined;
|
return vec->not_inlined ? vec->ptr : vec->inlined;
|
||||||
|
|||||||
Reference in New Issue
Block a user