[util] Pass context to the plist array/symtab parser allocator

This commit is contained in:
Bill Currie 2021-02-09 15:01:55 +09:00
parent a408fd40da
commit 14e4fd9f6a
3 changed files with 19 additions and 13 deletions

View file

@ -109,7 +109,7 @@ typedef struct plfield_s {
typedef struct plelement_s {
pltype_t type; ///< the required type of the array elements
size_t stride; ///< the size of each element
void *(*alloc) (size_t size); ///< allocator for array memory
void *(*alloc) (void *ctx, size_t size);///< allocator for array memory
plparser_t parser; ///< custom parser function
void *data; ///< additional data for \a parser
} plelement_t;
@ -360,7 +360,7 @@ void PL_TypeMismatch (plitem_t *messages, const plitem_t *item,
int PL_ParseStruct (const plfield_t *fields, const plitem_t *dict,
void *data, plitem_t *messages, void *context);
/** Parse an array object into a dynamic arrah (see darray.h).
/** Parse an array object into a dynamic array (see darray.h).
For each object in the array, the field item is used to determine how to
parse the object. If the array is empty, the destination will be
@ -378,7 +378,7 @@ int PL_ParseStruct (const plfield_t *fields, const plitem_t *dict,
\param array The array object to parse
\param data Pointer to the pointer to which the dynamic array will
be written. The dynamic array is allocated using
DARRAY_ALLOCFIXED().
DARRAY_ALLOCFIXED_OBJ().
\param messages Array object supplied by the caller used for storing
messages. The messages may or may not indicate errors (its
contents are not checked). This function itself will add
@ -390,7 +390,7 @@ int PL_ParseStruct (const plfield_t *fields, const plitem_t *dict,
message format is "[line number]: [message]". If the line
number is 0, then the actual line is unknown (due to the
source item not being parsed from a file or string).
\param context Additional context data passed to the parser.
\param context Additional context data passed to the parser and allocator.
\return 0 if there are any errors, 1 if there are no errors.
*/
int PL_ParseArray (const plfield_t *field, const plitem_t *array,
@ -438,7 +438,7 @@ int PL_ParseArray (const plfield_t *field, const plitem_t *array,
message format is "[line number]: [message]". If the line
number is 0, then the actual line is unknown (due to the
source item not being parsed from a file or string).
\param context Additional context data passed to the parser.
\param context Additional context data passed to the parser and allocator.
\return 0 if there are any errors, 1 if there are no errors.
*/
int PL_ParseSymtab (const plfield_t *field, const plitem_t *dict,

View file

@ -1272,8 +1272,8 @@ PL_ParseArray (const plfield_t *field, const plitem_t *array, void *data,
parser = pl_default_parser;
}
arr = DARRAY_ALLOCFIXED (arr_t, plarray->numvals * element->stride,
element->alloc);
arr = DARRAY_ALLOCFIXED_OBJ (arr_t, plarray->numvals * element->stride,
element->alloc, context);
memset (arr->a, 0, arr->size);
// the array is allocated using bytes, but need the actual number of
// elements in the array
@ -1329,7 +1329,7 @@ PL_ParseSymtab (const plfield_t *field, const plitem_t *dict, void *data,
return 1;
}
void *obj = element->alloc (element->stride);
void *obj = element->alloc (context, element->stride);
memset (obj, 0, element->stride);
while ((current = (dictkey_t *) *l++)) {
const char *key = current->key;
@ -1349,7 +1349,7 @@ PL_ParseSymtab (const plfield_t *field, const plitem_t *dict, void *data,
result = 0;
} else {
Hash_Add (tab, obj);
obj = element->alloc (element->stride);
obj = element->alloc (context, element->stride);
memset (obj, 0, element->stride);
}
}

View file

@ -259,6 +259,12 @@ parse_single (const plfield_t *field, const plitem_t *item,
return 1;
}
static void *
array_alloc (void *context, size_t size)
{
return malloc (size);
}
static int
parse_array (const plfield_t *field, const plitem_t *item,
void *data, plitem_t *messages, void *context)
@ -270,7 +276,7 @@ parse_array (const plfield_t *field, const plitem_t *item,
plelement_t element = {
array->type,
array->stride,
malloc,
array_alloc,
array->parser,
0,
};
@ -630,7 +636,7 @@ typedef struct qfv_renderpass_s {
static plelement_t parse_qfv_renderpass_attachments_data = {
QFDictionary,
sizeof (VkAttachmentDescription),
malloc,
array_alloc,
parse_VkAttachmentDescription,
0,
};
@ -638,7 +644,7 @@ static plelement_t parse_qfv_renderpass_attachments_data = {
static plelement_t parse_qfv_renderpass_subpasses_data = {
QFDictionary,
sizeof (VkSubpassDescription),
malloc,
array_alloc,
parse_VkSubpassDescription,
0,
};
@ -646,7 +652,7 @@ static plelement_t parse_qfv_renderpass_subpasses_data = {
static plelement_t parse_qfv_renderpass_dependencies_data = {
QFDictionary,
sizeof (VkSubpassDependency),
malloc,
array_alloc,
parse_VkSubpassDependency,
0,
};