Add LRU lists for tracking inodes that are dirty/accessed.
[bluesky.git] / bluesky / util.c
1 /* Blue Sky: File Systems in the Cloud
2  *
3  * Copyright (C) 2009  The Regents of the University of California
4  * Written by Michael Vrable <mvrable@cs.ucsd.edu>
5  *
6  * TODO: Licensing
7  */
8
9 #include <stdio.h>
10 #include <stdint.h>
11 #include <glib.h>
12 #include <string.h>
13
14 #include "bluesky-private.h"
15
16 /* Miscellaneous useful functions that don't really fit anywhere else. */
17
18 bluesky_time_hires bluesky_now_hires()
19 {
20     struct timespec time;
21
22     if (clock_gettime(CLOCK_REALTIME, &time) != 0) {
23         perror("clock_gettime");
24         return 0;
25     }
26
27     return (int64_t)(time.tv_sec) * 1000000000 + time.tv_nsec;
28 }
29
30 /* Convert a UTF-8 string to lowercase.  This can be used to implement
31  * case-insensitive lookups and comparisons, by normalizing all values to
32  * lowercase first.  Returns a newly-allocated string as a result. */
33 gchar *bluesky_lowercase(const gchar *s)
34 {
35     /* TODO: Unicode handling; for now just do ASCII. */
36     return g_ascii_strdown(s, -1);
37 }
38
39 /**** Reference-counted strings. ****/
40
41 /* Create and return a new reference-counted string.  The reference count is
42  * initially one.  The newly-returned string takes ownership of the memory
43  * pointed at by data, and will call g_free on it when the reference count
44  * drops to zero. */
45 BlueSkyRCStr *bluesky_string_new(gpointer data, gsize len)
46 {
47     BlueSkyRCStr *string = g_new(BlueSkyRCStr, 1);
48     string->data = data;
49     string->len = len;
50     g_atomic_int_set(&string->refcount, 1);
51     return string;
52 }
53
54 /* Create a new BlueSkyRCStr from a GString.  The GString is destroyed. */
55 BlueSkyRCStr *bluesky_string_new_from_gstring(GString *s)
56 {
57     gsize len = s->len;
58     return bluesky_string_new(g_string_free(s, FALSE), len);
59 }
60
61 void bluesky_string_ref(BlueSkyRCStr *string)
62 {
63     if (string == NULL)
64         return;
65
66     g_atomic_int_inc(&string->refcount);
67 }
68
69 void bluesky_string_unref(BlueSkyRCStr *string)
70 {
71     if (string == NULL)
72         return;
73
74     if (g_atomic_int_dec_and_test(&string->refcount)) {
75         g_free(string->data);
76         g_free(string);
77     }
78 }
79
80 /* Duplicate and return a new reference-counted string, containing a copy of
81  * the original data, with a reference count of 1.  As an optimization, if the
82  * passed-in string already has a reference count of 1, the original is
83  * returned.   Can be used to make a mutable copy of a shared string.  For this
84  * to truly be safe, it is probably needed that there be some type of lock
85  * protecting access to the string. */
86 BlueSkyRCStr *bluesky_string_dup(BlueSkyRCStr *string)
87 {
88     if (string == NULL)
89         return NULL;
90
91     if (g_atomic_int_dec_and_test(&string->refcount)) {
92         /* There are no other shared copies, so return this one. */
93         g_atomic_int_inc(&string->refcount);
94         return string;
95     } else {
96         return bluesky_string_new(g_memdup(string->data, string->len),
97                                   string->len);
98     }
99 }
100
101 /* Resize the data block used by a BlueSkyRCStr.  The data pointer might change
102  * after making this call, so it should not be cached across calls to this
103  * function.  To avoid confusing any other users, the caller probably ought to
104  * hold the only reference to the string (by calling bluesky_string_dup first
105  * if needed). */
106 void bluesky_string_resize(BlueSkyRCStr *string, gsize len)
107 {
108     if (string->len == len)
109         return;
110
111     g_warn_if_fail(string->refcount == 1);
112
113     string->data = g_realloc(string->data, len);
114     string->len = len;
115 }
116
117 /* Cache LRU list management functions.  These manage the doubly-linked list of
118  * inodes sorted by accessed/modified time.  The FS lock should be held while
119  * calling these.
120  *
121  * _remove will unlink an inode from the linked list.
122  *
123  * _prepend and _append insert an inode at the head or tail of the linked list,
124  * and return a pointer to the linked list element (which should be stored in
125  * the inode); the inode should not already be in the list.
126  *
127  * _head and _tail simply return the first or last item inode in the list. */
128 void bluesky_list_unlink(GList *head, GList *item)
129 {
130     if (item == NULL)
131         return;
132
133     if (head->prev == item)
134         head->prev = item->prev;
135     head->next = g_list_delete_link(head->next, item);
136 }
137
138 GList *bluesky_list_prepend(GList *head, BlueSkyInode *inode)
139 {
140     head->next = g_list_prepend(head->next, inode);
141     if (head->prev == NULL)
142         head->prev = g_list_last(head->next);
143     return head->next;
144 }
145
146 GList *bluesky_list_append(GList *head, BlueSkyInode *inode)
147 {
148     if (head->next == NULL)
149         return bluesky_list_prepend(head, inode);
150
151     g_assert(head->prev != NULL && head->prev->next == NULL);
152
153     GList *link = g_list_alloc();
154     link->data = inode;
155     link->next = NULL;
156     link->prev = head->prev;
157     head->prev->next = link;
158     head->prev = link;
159     return link;
160 }
161
162 BlueSkyInode *bluesky_list_head(GList *head)
163 {
164     if (head->next == NULL)
165         return NULL;
166     else
167         return (BlueSkyInode *)head->next->data;
168 }
169
170 BlueSkyInode *bluesky_list_tail(GList *head)
171 {
172     if (head->prev == NULL)
173         return NULL;
174     else
175         return (BlueSkyInode *)head->prev->data;
176 }