jcs
/amend
/amendments
/66
bile: Sync with other projects
jcs made amendment 66 over 2 years ago
--- bile.c Thu Feb 3 17:19:20 2022
+++ bile.c Wed Jun 1 16:39:17 2022
@@ -139,9 +139,7 @@ bile_open(const Str255 filename, short vrefnum)
bile = xmalloczero(sizeof(struct bile));
bile->vrefnum = vrefnum;
bile->frefnum = fh;
-
- if (getpath(vrefnum, filename, &bile->filename, true) != 0)
- memcpy(bile->filename, filename, sizeof(bile->filename));
+ memcpy(bile->filename, filename, sizeof(bile->filename));
bile->file_size = file_size;
/* verify magic */
@@ -287,6 +285,37 @@ bile_count_by_type(struct bile *bile, const OSType typ
return count;
}
+size_t
+bile_sorted_ids_by_type(struct bile *bile, const OSType type, size_t **ret)
+{
+ struct bile_object *o;
+ size_t count = 0, size = 0, n, j, t;
+ size_t *ids;
+
+ for (n = 0; n < bile->nobjects; n++) {
+ o = &bile->map[n - 1];
+ if (o->type != type)
+ continue;
+
+ EXPAND_TO_FIT(ids, size, count * sizeof(size_t), sizeof(size_t),
+ 10 * sizeof(size_t));
+ ids[count++] = o->id;
+ }
+
+ for (n = 0; n < count; n++) {
+ for (j = 0; j < count - n - 1; j++) {
+ if (ids[j] > ids[j + 1]) {
+ t = ids[j];
+ ids[j] = ids[j + 1];
+ ids[j + 1] = t;
+ }
+ }
+ }
+
+ *ret = ids;
+ return count;
+}
+
struct bile_object *
bile_get_nth_of_type(struct bile *bile, const size_t index,
const OSType type)
@@ -335,7 +364,6 @@ bile_next_id(struct bile *bile, const OSType type)
return id;
}
-
short
bile_delete(struct bile *bile, const OSType type, const unsigned long id)
{
@@ -453,7 +481,7 @@ bile_read_object(struct bile *bile, const struct bile_
return 0;
if (rsize != wantlen) {
- warn("bile_read: %s:%lu: needed to read %ld, read %ld",
+ warn("bile_read_object: %s:%lu: needed to read %ld, read %ld",
OSTypeToString(o->type), o->id, wantlen, rsize);
_bile_error = bile->last_error = BILE_ERR_BOGUS_OBJECT;
return 0;
@@ -562,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");
@@ -572,15 +600,19 @@ bile_marshall_object(struct bile *bile,
iterate_fields:
for (n = 0; n < nfields; n++) {
- if (fields[n].size == -1) {
- /* dynamically-sized field, get length from its length field */
+ if (fields[n].size < 0) {
+ /*
+ * Dynamically-sized field, get length from its length field
+ * and multiply by negative size, so -1 is the actual length but
+ * -4 could be used if length field is number of longs
+ */
ptr = (char *)object + fields[n].object_len_off;
- fsize = *(size_t *)ptr;
+ fsize = *(size_t *)ptr * -(fields[n].size);
} else
fsize = fields[n].size;
if (write) {
- if (fields[n].size == -1) {
+ if (fields[n].size < 0) {
/* field is dynamically allocated, first write size */
memcpy(data + size, &fsize, sizeof(fsize));
size += sizeof(fsize);
@@ -589,14 +621,14 @@ iterate_fields:
}
ptr = (char *)object + fields[n].struct_off;
- if (fields[n].size == -1) {
+ if (fields[n].size < 0) {
ptr = (char *)*(unsigned long *)ptr;
if (ptr == 0)
panic("bile_pack_object field[%lu] points to NULL", n);
}
memcpy(data + size, ptr, fsize);
- } else if (fields[n].size == -1) {
+ } else if (fields[n].size < 0) {
/* account for dynamic field length */
size += sizeof(fsize);
}
@@ -606,7 +638,7 @@ iterate_fields:
if (!write) {
data = xmalloc(size);
- write = 1;
+ write = true;
size = 0;
goto iterate_fields;
}
@@ -620,13 +652,13 @@ 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;
for (off = 0, n = 0; n < nfields; n++) {
- if (fields[n].size == -1) {
+ if (fields[n].size < 0) {
/* dynamically-sized field, read length */
memcpy(&fsize, data + off, sizeof(fsize));
off += sizeof(fsize);
@@ -634,17 +666,22 @@ bile_unmarshall_object(struct bile *bile,
fsize = fields[n].size;
if (off + fsize > size)
- panic("bile_unpack_object: overflow at field %lu!", n);
+ panic("bile_unmarshall_object: overflow at field %lu of %lu!",
+ n + 1, nfields);
ptr = (char *)object + fields[n].struct_off;
- if (fields[n].size == -1) {
+ 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;
}
@@ -822,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];
@@ -944,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 Jan 29 13:13:31 2022
+++ bile.h Wed May 25 13:35:51 2022
@@ -105,6 +105,8 @@ struct bile_object * bile_find(struct bile *bile, cons
const unsigned long id);
size_t bile_count_by_type(struct bile *bile,
const OSType type);
+size_t bile_sorted_ids_by_type(struct bile *bile,
+ const OSType type, size_t **ret);
struct bile_object * bile_get_nth_of_type(struct bile *bile,
const size_t index, const OSType type);
size_t bile_next_id(struct bile *bile, const OSType type);
@@ -131,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
--- repo.c Sat Apr 16 13:40:12 2022
+++ repo.c Thu Jun 2 16:23:27 2022
@@ -1061,7 +1061,8 @@ repo_commit(struct repo *repo, struct diffed_file *dif
/* update commit list */
repo->ncommits++;
- repo->commits = xrealloc(repo->commits, repo->ncommits * sizeof(Ptr));
+ repo->commits = xreallocarray(repo->commits, repo->ncommits,
+ sizeof(Ptr));
repo->commits[repo->ncommits - 1] = repo_parse_commit(commit_id,
commit, commit_len);
--- util.h Thu Feb 3 17:20:40 2022
+++ util.h Thu Jun 2 15:58:55 2022
@@ -33,6 +33,15 @@
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define BOUND(a, min, max) ((a) > (max) ? (max) : ((a) < (min) ? (min) : (a)))
+#define EXPAND_TO_FIT(var, var_size, used_size, add, grow_amount) { \
+ if ((used_size) + (add) >= (var_size)) { \
+ while ((used_size) + (add) >= (var_size)) { \
+ (var_size) += (grow_amount); \
+ } \
+ (var) = xrealloc((var), (var_size)); \
+ } \
+}
+
#define SCROLLBAR_WIDTH 16
/* GetMBarHeight() is not very useful */