jcs
/subtext
/amendments
/320
bile: Add bile_resize, don't zero in bile_read_alloc
We're going to fill up the buffer we just malloced with existing
data, no need to zero it before hand.
jcs made amendment 320 about 1 year ago
--- bile.c Thu Feb 23 14:47:37 2023
+++ bile.c Thu Feb 23 22:48:26 2023
@@ -595,8 +595,39 @@ bile_read_alloc(struct bile *bile, const OSType type,
snprintf(note, sizeof(note), "bile_read_alloc %s %ld",
OSTypeToString(type), id);
- *data = xmalloczero(o->size, note);
+ *data = xmalloc(o->size, note);
ret = bile_read_object(bile, o, *data, o->size);
+
+ return ret;
+}
+
+size_t
+bile_resize(struct bile *bile, const OSType type,
+ const unsigned long id, size_t new_size)
+{
+ struct bile_object *o, ocopy;
+ size_t ret;
+ char *data;
+
+ bile_check_sanity(bile);
+
+ o = bile_object_in_map(bile, type, id);
+ if (o == NULL) {
+ _bile_error = bile->last_error = -1;
+ return 0;
+ }
+
+ /* if we're growing, fill new space with zero */
+ data = xmalloczero(MAX(o->size, new_size), "bile_resize_object");
+ ret = bile_read_object(bile, o, data, o->size);
+ if (ret != o->size) {
+ _bile_error = bile->last_error = -1;
+ return 0;
+ }
+
+ memcpy(&ocopy, o, sizeof(ocopy));
+ ret = bile_write(bile, ocopy.type, ocopy.id, data, new_size);
+ xfree(&data);
return ret;
}
--- bile.h Thu Feb 23 14:47:52 2023
+++ bile.h Thu Feb 23 21:42:35 2023
@@ -134,6 +134,8 @@ size_t bile_read(struct bile *bile, const OSType t
size_t bile_read_alloc(struct bile *bile,
const OSType type, const unsigned long id,
void *data_ptr);
+size_t bile_resize(struct bile *bile, const OSType type,
+ const unsigned long id, size_t new_size);
size_t bile_write(struct bile *bile, OSType type,
const unsigned long id, const void *data,
const size_t len);