From 0c80c384756a48d5f731eeafa62b6cd5f3861749 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Mon, 25 May 2009 18:31:27 -0700 Subject: A flurry of activity. Doesn't compile yet. - a descriptor.c that describes the data structures in descriptor.proto using the data structures in descriptor.h. - everything renamed pbstream -> upb. - modularization rethought. - Doesn't compile yet, but should once things settle back down. --- upb_fieldmap.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 upb_fieldmap.c (limited to 'upb_fieldmap.c') diff --git a/upb_fieldmap.c b/upb_fieldmap.c new file mode 100644 index 0000000..015b2e1 --- /dev/null +++ b/upb_fieldmap.c @@ -0,0 +1,50 @@ +/* + * upb - a minimalist implementation of protocol buffers. + * + * Copyright (c) 2009 Joshua Haberman. See LICENSE for details. + */ + +#include "upb_fieldmap.h" + +#include + +void pbstream_init_fieldmap(struct pbstream_fieldmap *fieldmap, + struct pbstream_field *fields, + int num_fields) +{ + qsort(fields, num_fields, sizeof(*fields), compare_fields); + + /* Find the largest n for which at least half the fieldnums 8 && maybe_n/(i+1) >= 2) break; + n = maybe_n; + } + + fieldmap->num_fields = num_fields; + fieldmap->fields = malloc(sizeof(*fieldmap->fields)*num_fields); + memcpy(fieldmap->fields, fields, sizeof(*fields)*num_fields); + + fieldmap->array_size = n; + fieldmap->array = malloc(sizeof(*fieldmap->array)*n); + memset(fieldmap->array, 0, sizeof(*fieldmap->array)*n); + + for (int i = 0; i < num_fields && fields[i].field_number <= n; i++) + fieldmap->array[fields[i].field_number-1] = &fieldmap->fields[i]; + + /* Until we support the hashtable part... */ + assert(n == fields[num_fields-1].field_number); +} + +void pbstream_free_fieldmap(struct pbstream_fieldmap *fieldmap) +{ + free(fieldmap->fields); + free(fieldmap->array); +} + +/* Emit definition for inline function. */ +extern void *upb_fieldmap_find(struct upb_fieldmap *fm, + pbstream_field_number_t num, + size_t info_size); -- cgit v1.2.3