AmendHub

Download:

jcs

/

subtext

/

amendments

/

111

bile: Allow shallow unmarshalling

When quickly iterating over a bunch of objects, it's useful to be
able to avoid all the malloc/free of unmarshalling objects that have
dynamic-length strings

jcs made amendment 111 over 2 years ago
--- bile.c Sun May 22 21:14:06 2022 +++ bile.c Wed Jun 1 16:39:17 2022 @@ -590,7 +590,7 @@ bile_marshall_object(struct bile *bile, { char *data, *ptr; size_t size = 0, fsize = 0, n; - bool write = 0; + bool write = false; if (ret == NULL) panic("bile_pack_object invalid ret"); @@ -638,7 +638,7 @@ iterate_fields: if (!write) { data = xmalloc(size); - write = 1; + write = true; size = 0; goto iterate_fields; } @@ -652,7 +652,7 @@ iterate_fields: short bile_unmarshall_object(struct bile *bile, const struct bile_object_field *fields, const size_t nfields, - const char *data, const size_t size, void *object) + const char *data, const size_t size, void *object, bool deep) { size_t off, fsize = 0, n; char *ptr, *dptr; @@ -671,13 +671,17 @@ bile_unmarshall_object(struct bile *bile, ptr = (char *)object + fields[n].struct_off; - if (fields[n].size < 0) { + if (fields[n].size < 0 && deep) { dptr = xmalloc(fsize); memcpy(ptr, &dptr, sizeof(dptr)); ptr = dptr; } - memcpy(ptr, data + off, fsize); + if (fields[n].size < 0 && !deep) + memset(ptr, 0, sizeof(dptr)); + else + memcpy(ptr, data + off, fsize); + off += fsize; } @@ -855,7 +859,7 @@ bile_write_map(struct bile *bile) new_map_size = BILE_OBJECT_SIZE * new_nobjects; new_map_obj = bile_alloc(bile, BILE_TYPE_MAP, new_map_id, new_map_size); - new_map = xmallocarray(BILE_OBJECT_SIZE, new_nobjects); + new_map = xcalloc(BILE_OBJECT_SIZE, new_nobjects); for (n = 0, new_nobjects = 0; n < bile->nobjects; n++) { obj = &bile->map[n]; @@ -977,7 +981,7 @@ bile_xwriteat(struct bile *bile, const size_t pos, con void bile_sort_by_pos(struct bile *bile) { - size_t n, j; + ssize_t n, j; struct bile_object o; if (bile == NULL) --- bile.h Sat May 21 21:49:37 2022 +++ bile.h Wed May 25 13:35:51 2022 @@ -133,6 +133,6 @@ short bile_marshall_object(struct bile *bile, short bile_unmarshall_object(struct bile *bile, const struct bile_object_field *fields, const size_t nfields, const char *data, - const size_t size, void *object); + const size_t size, void *object, bool deep); #endif