Index: trunk/src_3rd/load_cache/load_cache.c =================================================================== --- trunk/src_3rd/load_cache/load_cache.c (revision 756) +++ trunk/src_3rd/load_cache/load_cache.c (revision 757) @@ -1,3 +1,4 @@ +#include #include #include #include @@ -73,7 +74,7 @@ } else { - if (low->uid != file->low_parser) + if (low->uid != file->low_parser->uid) return NULL; /* can't parse the same file twice, with different low level */ } @@ -87,11 +88,11 @@ if (data == NULL) return NULL; *d = data; + data->high_parser = high; } else data = *d; - data->refco++; return data; } @@ -111,3 +112,32 @@ return ldch_load_(ctx, filename, low, high, low_call_ctx, high_call_ctx); } + +static void ldch_free_data(ldch_ctx_t *ctx, ldch_data_t *data) +{ + if (data->high_parser->free_payload != NULL) + data->high_parser->free_payload(data); + free(data); +} + +static void ldch_free_file(ldch_ctx_t *ctx, ldch_file_t *file) +{ + long n; + + for(n = 0; n < file->data.used; n++) + ldch_free_data(ctx, file->data.array[n]); + + vtp0_uninit(&file->data); + + if (file->low_parser->free_payload != NULL) + file->low_parser->free_payload(file); + + free(file->name); + free(file); +} + +void ldch_unload(ldch_ctx_t *ctx, ldch_file_t *file) +{ + htsp_pop(&ctx->files, file->name); + ldch_free_file(ctx, file); +} Index: trunk/src_3rd/load_cache/load_cache.h =================================================================== --- trunk/src_3rd/load_cache/load_cache.h (revision 756) +++ trunk/src_3rd/load_cache/load_cache.h (revision 757) @@ -12,6 +12,7 @@ char *name; ldch_uid_t uid; ldch_file_t *(*parse)(ldch_low_parser_t *parser, void *call_ctx, const char *fn); + void (*free_payload)(ldch_file_t *file); void *used_data; }; @@ -18,12 +19,13 @@ struct ldch_high_parser_s { char *name; ldch_data_t *(*parse)(ldch_high_parser_t *parser, void *call_ctx, ldch_file_t *file, const char *fn); + void (*free_payload)(ldch_data_t *data); void *used_data; }; struct ldch_data_s { ldch_file_t *parent; - int refco; + ldch_high_parser_t *high_parser; int payload_size; char payload[1]; /* real size: payload_size (caches parsed data) */ }; @@ -33,7 +35,7 @@ char *name; /* file name */ vtp0_t data; /* -> (ldch_data_t *), indexed by high_parser uid */ void *user_data; - ldch_uid_t low_parser; + ldch_low_parser_t *low_parser; int low_payload_size; char low_payload[1]; /* real size: low_payload_size (caches low level parsed data) */ }; @@ -58,3 +60,5 @@ ldch_data_t *ldch_load_(ldch_ctx_t *ctx, const char *filename, ldch_low_parser_t *low, ldch_high_parser_t *high, void *low_call_ctx, void *high_call_ctx); ldch_data_t *ldch_load(ldch_ctx_t *ctx, const char *filename, const char *low, const char *high, void *low_call_ctx, void *high_call_ctx); +void ldch_unload(ldch_ctx_t *ctx, ldch_file_t *file); +