Annotation of /trunk/mkinitrd-magellan/busybox/libbb/inode_hash.c
Parent Directory | Revision Log
Revision 816 -
(hide annotations)
(download)
Fri Apr 24 18:33:46 2009 UTC (15 years, 1 month ago) by niro
File MIME type: text/plain
File size: 2081 byte(s)
Fri Apr 24 18:33:46 2009 UTC (15 years, 1 month ago) by niro
File MIME type: text/plain
File size: 2081 byte(s)
-updated to busybox-1.13.4
1 | niro | 532 | /* vi: set sw=4 ts=4: */ |
2 | /* | ||
3 | * Utility routines. | ||
4 | * | ||
5 | * Copyright (C) many different people. | ||
6 | * If you wrote this, please acknowledge your work. | ||
7 | * | ||
8 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. | ||
9 | */ | ||
10 | |||
11 | #include "libbb.h" | ||
12 | |||
13 | niro | 816 | typedef struct ino_dev_hash_bucket_struct { |
14 | struct ino_dev_hash_bucket_struct *next; | ||
15 | ino_t ino; | ||
16 | dev_t dev; | ||
17 | char name[1]; | ||
18 | } ino_dev_hashtable_bucket_t; | ||
19 | |||
20 | niro | 532 | #define HASH_SIZE 311 /* Should be prime */ |
21 | #define hash_inode(i) ((i) % HASH_SIZE) | ||
22 | |||
23 | niro | 816 | /* array of [HASH_SIZE] elements */ |
24 | static ino_dev_hashtable_bucket_t **ino_dev_hashtable; | ||
25 | niro | 532 | |
26 | /* | ||
27 | niro | 816 | * Return name if statbuf->st_ino && statbuf->st_dev are recorded in |
28 | * ino_dev_hashtable, else return NULL | ||
29 | niro | 532 | */ |
30 | niro | 816 | char* FAST_FUNC is_in_ino_dev_hashtable(const struct stat *statbuf) |
31 | niro | 532 | { |
32 | ino_dev_hashtable_bucket_t *bucket; | ||
33 | |||
34 | niro | 816 | if (!ino_dev_hashtable) |
35 | return NULL; | ||
36 | |||
37 | niro | 532 | bucket = ino_dev_hashtable[hash_inode(statbuf->st_ino)]; |
38 | while (bucket != NULL) { | ||
39 | niro | 816 | if ((bucket->ino == statbuf->st_ino) |
40 | && (bucket->dev == statbuf->st_dev) | ||
41 | ) { | ||
42 | return bucket->name; | ||
43 | } | ||
44 | bucket = bucket->next; | ||
45 | niro | 532 | } |
46 | niro | 816 | return NULL; |
47 | niro | 532 | } |
48 | |||
49 | /* Add statbuf to statbuf hash table */ | ||
50 | niro | 816 | void FAST_FUNC add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name) |
51 | niro | 532 | { |
52 | int i; | ||
53 | ino_dev_hashtable_bucket_t *bucket; | ||
54 | |||
55 | i = hash_inode(statbuf->st_ino); | ||
56 | niro | 816 | if (!name) |
57 | name = ""; | ||
58 | bucket = xmalloc(sizeof(ino_dev_hashtable_bucket_t) + strlen(name)); | ||
59 | niro | 532 | bucket->ino = statbuf->st_ino; |
60 | bucket->dev = statbuf->st_dev; | ||
61 | niro | 816 | strcpy(bucket->name, name); |
62 | |||
63 | if (!ino_dev_hashtable) | ||
64 | ino_dev_hashtable = xzalloc(HASH_SIZE * sizeof(*ino_dev_hashtable)); | ||
65 | |||
66 | niro | 532 | bucket->next = ino_dev_hashtable[i]; |
67 | ino_dev_hashtable[i] = bucket; | ||
68 | } | ||
69 | |||
70 | niro | 816 | #if ENABLE_FEATURE_CLEAN_UP |
71 | niro | 532 | /* Clear statbuf hash table */ |
72 | niro | 816 | void FAST_FUNC reset_ino_dev_hashtable(void) |
73 | niro | 532 | { |
74 | int i; | ||
75 | ino_dev_hashtable_bucket_t *bucket; | ||
76 | |||
77 | niro | 816 | for (i = 0; ino_dev_hashtable && i < HASH_SIZE; i++) { |
78 | niro | 532 | while (ino_dev_hashtable[i] != NULL) { |
79 | bucket = ino_dev_hashtable[i]->next; | ||
80 | free(ino_dev_hashtable[i]); | ||
81 | ino_dev_hashtable[i] = bucket; | ||
82 | } | ||
83 | } | ||
84 | niro | 816 | free(ino_dev_hashtable); |
85 | ino_dev_hashtable = NULL; | ||
86 | niro | 532 | } |
87 | #endif |