kvector.c (1579B)
1 /* 2 ** kvector.c 3 ** Kernel Vectors (heterogenous arrays) 4 ** See Copyright Notice in klisp.h 5 */ 6 7 #include <string.h> 8 9 #include "kvector.h" 10 #include "kobject.h" 11 #include "kstate.h" 12 #include "kmem.h" 13 #include "kgc.h" 14 15 /* helper function allocating vectors */ 16 17 /* XXX I'm not too convinced this is the best way to handle the empty 18 vector... Try to find a better way */ 19 static Vector *kvector_alloc(klisp_State *K, bool m, uint32_t length) 20 { 21 Vector *new_vector; 22 23 if (length > (SIZE_MAX - sizeof(Vector)) / sizeof(TValue)) 24 klispM_toobig(K); 25 26 klisp_assert(!m || length > 0); 27 28 size_t size = sizeof(Vector) + length * sizeof(TValue); 29 new_vector = (Vector *) klispM_malloc(K, size); 30 klispC_link(K, (GCObject *) new_vector, K_TVECTOR, 31 (m? 0 : K_FLAG_IMMUTABLE)); 32 new_vector->mark = KFALSE; 33 new_vector->sizearray = length; 34 35 return new_vector; 36 } 37 38 TValue kvector_new_sf(klisp_State *K, uint32_t length, TValue fill) 39 { 40 Vector *v = kvector_alloc(K, true, length); 41 for (int i = 0; i < length; i++) 42 v->array[i] = fill; 43 return gc2vector(v); 44 } 45 46 TValue kvector_new_bs_g(klisp_State *K, bool m, 47 const TValue *buf, uint32_t length) 48 { 49 Vector *v = kvector_alloc(K, m, length); 50 memcpy(v->array, buf, sizeof(TValue) * length); 51 return gc2vector(v); 52 } 53 54 bool kvectorp(TValue obj) 55 { 56 return ttisvector(obj); 57 } 58 59 bool kimmutable_vectorp(TValue obj) 60 { 61 return ttisvector(obj) && kis_immutable(obj); 62 } 63 64 bool kmutable_vectorp(TValue obj) 65 { 66 return ttisvector(obj) && kis_mutable(obj); 67 }