Skip to content

Instantly share code, notes, and snippets.

@smspillaz
Created January 16, 2015 13:55
Show Gist options
  • Save smspillaz/e839f7da550ee21b191c to your computer and use it in GitHub Desktop.
Save smspillaz/e839f7da550ee21b191c to your computer and use it in GitHub Desktop.
Coverage Serialization
static unsigned int COVERAGE_STATISTICS_CACHE_MAGIC = 0xC0432463;
static GjsCoverageCacheHeader *
create_coverage_statistics_cache(GjsCoverage *coverage)
{
GjsCoveragePrivate *priv = (GjsCoveragePrivate *) gjs_coverage_get_instance_private(coverage);
gsize allocation_len = calculate_coverage_statistics_cache_len(file_statistics_array);
void *allocation = g_malloc(allocation_len);
void *allocation_index = allocation;
/* Header */
GjsCoverageCacheHeader *header = (GjsCoverageCacheHeader *) allocation_index;
header->magic = COVERAGE_STATISTICS_CACHE_MAGIC;
header->total_len = sizeof(GjsCoverageCacheHeader);
header->n_files = g_strv_length(priv->covered_paths);
allocation_index = (void *) ++header;
header = NULL;
for (size_t file_index = 0; file_index < file_statistics_array->len; ++file_index) {
GjsCoverageFileStatistics *statistics = (GjsCoverageFileStatistics *) &(g_array_index(file_statistics_array, GjsCoverageFileStatistics, file_index));
/* File header */
GjsCoverageCacheFileEntry *file_entry = (GjsCoverageCacheFileEntry *) allocation_index;
file_entry->filename_len = strlen(statistics->filename);
file_entry->file_mtime = get_path_mtime(statistics->filename);
file_entry->total_len = sizeof(GjsCoverageCacheFileEntry);
allocation_index = (void *) (file_entry + 1);
/* Filename */
char *filename_in_entry = (char *) allocation_index;
strncpy(filename_in_entry, statistics->filename, file_entry->filename_len);
filename_in_entry += file_entry->filename_len;
allocation_index = (void *) filename_in_entry;
file_entry->total_len += filename_len * sizeof(char);
/* Executable lines */
GjsCoverageCacheExecutableLinesEntry *executable_lines_entry = (GjsCoverageCacheExecutableLinesEntry *) allocation_index;
executable_lines_entry->n_executable_lines = statistics->lines->len;
executable_lines_entry->total_len = sizeof(GjsCoverageCacheExecutableLinesEntry);
allocation_index = (void *) (executable_lines_entry + 1);
unsigned int *executable_lines_array = (unsigned int *) allocation_index;
memcpy(executable_lines_array, statistics->lines->data, sizeof(unsigned int) * statistics->lines->len);
allocation_index = (void *) (executable_lines_array + statistics->lines->len);
executable_lines_entry->total_len += sizeof(unsigned int) * statistics->lines->len;
file_entry->total_len += executable_lines_entry->total_len;
/* Branches */
GjsCoverageCacheBranchesEntry *branches_entry = (GjsCoverageCacheBranchesEntry *) allocation_index;
branches_entry->n_branches = statistics->branches->len;
branches_entry->total_len = sizeof(GjsCoverageCacheBranchesEntry);
allocation_index = (void *) (branches_entry + 1);
for (size_t branch_index = 0; branch_index < statistics->branches->len; ++branch_index) {
GjsCoverageBranch *branch = (GjsCoverageBranch *) &(g_array_index(file_statistics_array, GjsCoverageBranch, file_index));
GjsCoverageCacheBranchEntry *branch_entry = (GjsCoverageCacheBranchEntry *) allocation_index;
branch_entry->branch_point = branch->point;
branch_entry->n_exits = branch->exits->len;
branch_entry->total_len = sizeof(GjsCoverageCacheBranchEntry);
allocation_index = (void *) (branch_entry + 1);
unsigned int *branch_cache_entry_exits = (unsigned int *) allocation_index;
for (size_t branch_exit_index = 0; branch_exit_index < branch_entry->n_exits; ++branch_exit_index, ++branch_cache_entry_exits) {
GjsCoverageBranchExit *branch_exit = (GjsCoverageBranchExit *) &(g_array_index(file_statistics_array, GjsCoverageBranchExit, file_index));
*branch_cache_entry_exits = branch_exit->line;
}
branch_entry->total_len += sizeof(unsigned int) * branch_entry->n_exits;
branches_entry->total_len += branch_entry->total_len;
}
file_entry->total_len += branches_entry->total_len;
/* Functions */
GjsCoverageCacheFunctionsEntry *functions_entry = (GjsCoverageCacheFunctionEntry *) allocation_index;
functions_entry->n_functions = statistics->functions->len;
functions_entry->total_len = sizeof(GjsCoverageCacheFunctionsEntry);
allocation_index = (void *) (functions_entry + 1);
for (size_t function_index; function_index < statistics->functions->len; ++function_index) {
GjsCoverageFunction *function = (GjsCoverageFunction *) &(g_array_index(file_statistics_array, GjsCoverageFunction, function_index));
GjsCoverageCacheFunctionEntry *function_entry = (GjsCoverageCacheFunctionEntry *) allocation_index;
function_entry->function_name_len = strlen(function->key);
allocation_index = (void *) function_entry + 1;
function_entry->total_len = sizeof(GjsCoverageCacheFunctionEntry) + sizeof(char) * function_entry->function_name_len;
functions_entry->total_len += function_entry->total_len;
}
file_entry->total_len += branches_entry->total_len;
header->total_len += file_entry->total_len;
}
ptrdiff_t total_written = ((ptrdiff_t) allocation_index) - ((ptrdiff_t) allocation);
g_assert(header->total_len == allocation_len);
g_assert(total_written == header->total_len);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment