File: htable.c

package info (click to toggle)
apfsprogs 0.2.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,112 kB
  • sloc: ansic: 16,034; makefile: 175; sh: 57
file content (165 lines) | stat: -rw-r--r-- 3,691 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * Copyright (C) 2019 Ernesto A. Fernández <ernesto.mnd.fernandez@gmail.com>
 */

#include <stdlib.h>
#include <stdio.h>
#include <apfs/types.h>
#include "apfsck.h"
#include "htable.h"
#include "super.h"

/**
 * alloc_htable - Allocates and returns an empty hash table
 */
struct htable_entry **alloc_htable(void)
{
	struct htable_entry **table;

	table = calloc(HTABLE_BUCKETS, sizeof(*table));
	if (!table)
		system_error();
	return table;
}

/**
 * free_htable - Free a hash table and all its entries
 * @table:	the catalog table to free
 * @free_entry:	function that checks and frees an entry
 */
void free_htable(struct htable_entry **table,
		 void (*free_entry)(struct htable_entry *))
{
	struct htable_entry *current;
	struct htable_entry *next;
	int i;

	for (i = 0; i < HTABLE_BUCKETS; ++i) {
		current = table[i];
		while (current) {
			next = current->h_next;
			free_entry(current);
			current = next;
		}
	}
	free(table);
}

/**
 * apply_on_htable - Apply a function to all entries in a hash table
 * @table:	the hash table
 * @fn:		function to apply
 */
void apply_on_htable(struct htable_entry **table, void (*fn)(struct htable_entry *))
{
	struct htable_entry *current;
	struct htable_entry *next;
	int i;

	for (i = 0; i < HTABLE_BUCKETS; ++i) {
		current = table[i];
		while (current) {
			next = current->h_next;
			fn(current);
			current = next;
		}
	}
}

/**
 * get_htable_entry - Find or create an entry in a hash table
 * @id:		id of the entry
 * @size:	size of the entry
 * @table:	the hash table
 *
 * Returns the entry, after creating it if necessary.
 */
struct htable_entry *get_htable_entry(u64 id, int size,
				      struct htable_entry **table)
{
	int index = id % HTABLE_BUCKETS; /* Trivial hash function */
	struct htable_entry **entry_p = table + index;
	struct htable_entry *entry = *entry_p;
	struct htable_entry *new;

	/* In each linked list, entries are ordered by id */
	while (entry) {
		if (id == entry->h_id)
			return entry;
		if (id < entry->h_id)
			break;

		entry_p = &entry->h_next;
		entry = *entry_p;
	}

	new = calloc(1, size);
	if (!new)
		system_error();

	new->h_id = id;
	new->h_next = entry;
	*entry_p = new;
	return new;
}

/**
 * htable_entry_exists - Check if an entry exists in a hash table
 * @id:		id of the entry
 * @table:	the hash table
 */
bool htable_entry_exists(u64 id, struct htable_entry **table)
{
	int index = id % HTABLE_BUCKETS; /* Trivial hash function */
	struct htable_entry *entry = *(table + index);

	/* In each linked list, entries are ordered by id */
	while (entry) {
		if (id == entry->h_id)
			return true;
		if (id < entry->h_id)
			return false;
		entry = entry->h_next;
	}
	return false;
}

static void free_cnid(struct htable_entry *entry)
{
	struct listed_cnid *cnid = (struct listed_cnid *)entry;

	if (cnid->c_state & CNID_IN_SIBLING_LINK) {
		if (cnid->c_state & ~CNID_IN_SIBLING_LINK)
			report("Catalog", "sibling link oid reused elsewhere.");
	}

	free(entry);
}

/**
 * free_cnid_table - Free the cnid hash table and all its entries
 * @table: table to free
 *
 * Also performs some consistency checks that can only be done after the whole
 * catalog has been parsed.
 */
void free_cnid_table(struct htable_entry **table)
{
	/* No checks needed here, just call free() on each entry */
	free_htable(table, free_cnid);
}

/**
 * get_listed_cnid - Find or create a cnid structure in the cnid hash table
 * @id: the cnid
 *
 * Returns the cnid structure, after creating it if necessary.
 */
struct listed_cnid *get_listed_cnid(u64 id)
{
	struct htable_entry *entry;

	entry = get_htable_entry(id, sizeof(struct listed_cnid),
				 vsb->v_cnid_table);
	return (struct listed_cnid *)entry;
}