mirror of
				https://github.com/janet-lang/janet
				synced 2025-10-29 22:53:03 +00:00 
			
		
		
		
	refactor(c-api): Use size_t on some JanetStructHead fields
				
					
				
			This commit is contained in:
		| @@ -29,12 +29,11 @@ | |||||||
| #endif | #endif | ||||||
|  |  | ||||||
| /* Begin creation of a struct */ | /* Begin creation of a struct */ | ||||||
| JanetKV *janet_struct_begin(int32_t count) { | JanetKV *janet_struct_begin(size_t count) { | ||||||
|     /* Calculate capacity as power of 2 after 2 * count. */ |     /* Calculate capacity as power of 2 after 2 * count. */ | ||||||
|     int32_t capacity = janet_tablen(2 * count); |     size_t capacity = janet_tablen(2 * count); | ||||||
|     if (capacity < 0) capacity = janet_tablen(count + 1); |  | ||||||
|  |  | ||||||
|     size_t size = sizeof(JanetStructHead) + (size_t) capacity * sizeof(JanetKV); |     size_t size = sizeof(JanetStructHead) + capacity * sizeof(JanetKV); | ||||||
|     JanetStructHead *head = janet_gcalloc(JANET_MEMORY_STRUCT, size); |     JanetStructHead *head = janet_gcalloc(JANET_MEMORY_STRUCT, size); | ||||||
|     head->length = count; |     head->length = count; | ||||||
|     head->capacity = capacity; |     head->capacity = capacity; | ||||||
| @@ -49,9 +48,9 @@ JanetKV *janet_struct_begin(int32_t count) { | |||||||
| /* Find an item in a struct without looking for prototypes. Should be similar to janet_dict_find, but | /* Find an item in a struct without looking for prototypes. Should be similar to janet_dict_find, but | ||||||
|  * specialized to structs (slightly more compact). */ |  * specialized to structs (slightly more compact). */ | ||||||
| const JanetKV *janet_struct_find(const JanetKV *st, Janet key) { | const JanetKV *janet_struct_find(const JanetKV *st, Janet key) { | ||||||
|     int32_t cap = janet_struct_capacity(st); |     size_t cap = janet_struct_capacity(st); | ||||||
|     int32_t index = janet_maphash(cap, janet_hash(key)); |     int32_t index = janet_maphash(cap, janet_hash(key)); | ||||||
|     int32_t i; |     size_t i; | ||||||
|     for (i = index; i < cap; i++) |     for (i = index; i < cap; i++) | ||||||
|         if (janet_checktype(st[i].key, JANET_NIL) || janet_equals(st[i].key, key)) |         if (janet_checktype(st[i].key, JANET_NIL) || janet_equals(st[i].key, key)) | ||||||
|             return st + i; |             return st + i; | ||||||
| @@ -70,7 +69,7 @@ const JanetKV *janet_struct_find(const JanetKV *st, Janet key) { | |||||||
|  * hash map is independent of insertion order. |  * hash map is independent of insertion order. | ||||||
|  */ |  */ | ||||||
| void janet_struct_put_ext(JanetKV *st, Janet key, Janet value, int replace) { | void janet_struct_put_ext(JanetKV *st, Janet key, Janet value, int replace) { | ||||||
|     int32_t cap = janet_struct_capacity(st); |     size_t cap = janet_struct_capacity(st); | ||||||
|     int32_t hash = janet_hash(key); |     int32_t hash = janet_hash(key); | ||||||
|     int32_t index = janet_maphash(cap, hash); |     int32_t index = janet_maphash(cap, hash); | ||||||
|     int32_t i, j, dist; |     int32_t i, j, dist; | ||||||
| @@ -144,7 +143,7 @@ const JanetKV *janet_struct_end(JanetKV *st) { | |||||||
|          * the struct using only the values that went in. The second creation should always |          * the struct using only the values that went in. The second creation should always | ||||||
|          * succeed. */ |          * succeed. */ | ||||||
|         JanetKV *newst = janet_struct_begin(janet_struct_hash(st)); |         JanetKV *newst = janet_struct_begin(janet_struct_hash(st)); | ||||||
|         for (int32_t i = 0; i < janet_struct_capacity(st); i++) { |         for (size_t i = 0; i < janet_struct_capacity(st); i++) { | ||||||
|             JanetKV *kv = st + i; |             JanetKV *kv = st + i; | ||||||
|             if (!janet_checktype(kv->key, JANET_NIL)) { |             if (!janet_checktype(kv->key, JANET_NIL)) { | ||||||
|                 janet_struct_put(newst, kv->key, kv->value); |                 janet_struct_put(newst, kv->key, kv->value); | ||||||
| @@ -192,7 +191,7 @@ Janet janet_struct_get_ex(const JanetKV *st, Janet key, JanetStruct *which) { | |||||||
| /* Convert struct to table */ | /* Convert struct to table */ | ||||||
| JanetTable *janet_struct_to_table(const JanetKV *st) { | JanetTable *janet_struct_to_table(const JanetKV *st) { | ||||||
|     JanetTable *table = janet_table(janet_struct_capacity(st)); |     JanetTable *table = janet_table(janet_struct_capacity(st)); | ||||||
|     int32_t i; |     size_t i; | ||||||
|     for (i = 0; i < janet_struct_capacity(st); i++) { |     for (i = 0; i < janet_struct_capacity(st); i++) { | ||||||
|         const JanetKV *kv = st + i; |         const JanetKV *kv = st + i; | ||||||
|         if (!janet_checktype(kv->key, JANET_NIL)) { |         if (!janet_checktype(kv->key, JANET_NIL)) { | ||||||
| @@ -245,14 +244,14 @@ JANET_CORE_FN(cfun_struct_flatten, | |||||||
|         cursor = janet_struct_proto(cursor); |         cursor = janet_struct_proto(cursor); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     if (pair_count > INT32_MAX) { |     if (pair_count > JANET_INTMAX_INT64) { | ||||||
|         janet_panic("struct too large"); |         janet_panic("struct too large"); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     JanetKV *accum = janet_struct_begin((int32_t) pair_count); |     JanetKV *accum = janet_struct_begin((size_t) pair_count); | ||||||
|     cursor = st; |     cursor = st; | ||||||
|     while (cursor) { |     while (cursor) { | ||||||
|         for (int32_t i = 0; i < janet_struct_capacity(cursor); i++) { |         for (size_t i = 0; i < janet_struct_capacity(cursor); i++) { | ||||||
|             const JanetKV *kv = cursor + i; |             const JanetKV *kv = cursor + i; | ||||||
|             if (!janet_checktype(kv->key, JANET_NIL)) { |             if (!janet_checktype(kv->key, JANET_NIL)) { | ||||||
|                 janet_struct_put_ext(accum, kv->key, kv->value, 0); |                 janet_struct_put_ext(accum, kv->key, kv->value, 0); | ||||||
| @@ -283,7 +282,7 @@ JANET_CORE_FN(cfun_struct_to_table, | |||||||
|         } |         } | ||||||
|         /* TODO - implement as memcpy since struct memory should be compatible |         /* TODO - implement as memcpy since struct memory should be compatible | ||||||
|          * with table memory */ |          * with table memory */ | ||||||
|         for (int32_t i = 0; i < janet_struct_capacity(cursor); i++) { |         for (size_t i = 0; i < janet_struct_capacity(cursor); i++) { | ||||||
|             const JanetKV *kv = cursor + i; |             const JanetKV *kv = cursor + i; | ||||||
|             if (!janet_checktype(kv->key, JANET_NIL)) { |             if (!janet_checktype(kv->key, JANET_NIL)) { | ||||||
|                 janet_table_put(tab_cursor, kv->key, kv->value); |                 janet_table_put(tab_cursor, kv->key, kv->value); | ||||||
|   | |||||||
| @@ -1007,9 +1007,9 @@ struct JanetTupleHead { | |||||||
| /* Prefix for a struct */ | /* Prefix for a struct */ | ||||||
| struct JanetStructHead { | struct JanetStructHead { | ||||||
|     JanetGCObject gc; |     JanetGCObject gc; | ||||||
|     int32_t length; |     size_t length; | ||||||
|     int32_t hash; |     int32_t hash; | ||||||
|     int32_t capacity; |     size_t capacity; | ||||||
|     const JanetKV *proto; |     const JanetKV *proto; | ||||||
|     const JanetKV data[]; |     const JanetKV data[]; | ||||||
| }; | }; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 GrayJack
					GrayJack