Improve the reading back of objects committed to the journal.
[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 #include <sys/mman.h>
14
15 #include "bluesky-private.h"
16
17 /* Miscellaneous useful functions that don't really fit anywhere else. */
18
19 bluesky_time_hires bluesky_now_hires()
20 {
21     struct timespec time;
22
23     if (clock_gettime(CLOCK_REALTIME, &time) != 0) {
24         perror("clock_gettime");
25         return 0;
26     }
27
28     return (int64_t)(time.tv_sec) * 1000000000 + time.tv_nsec;
29 }
30
31 /* Convert a UTF-8 string to lowercase.  This can be used to implement
32  * case-insensitive lookups and comparisons, by normalizing all values to
33  * lowercase first.  Returns a newly-allocated string as a result. */
34 gchar *bluesky_lowercase(const gchar *s)
35 {
36     /* TODO: Unicode handling; for now just do ASCII. */
37     return g_ascii_strdown(s, -1);
38 }
39
40 gboolean bluesky_inode_is_ready(BlueSkyInode *inode)
41 {
42     if (inode == NULL)
43         return FALSE;
44
45     g_mutex_lock(inode->lock);
46     gboolean valid = (inode->type != BLUESKY_PENDING
47                       && inode->type != BLUESKY_INVALID);
48
49     g_mutex_unlock(inode->lock);
50
51     return valid;
52 }
53
54 /**** Reference-counted strings. ****/
55
56 void bluesky_mmap_unref(BlueSkyMmap *mmap)
57 {
58     if (mmap == NULL)
59         return;
60
61     if (g_atomic_int_dec_and_test(&mmap->refcount)) {
62         munmap((void *)mmap->addr, mmap->len);
63         g_free(mmap);
64     }
65 }
66
67 /* Create and return a new reference-counted string.  The reference count is
68  * initially one.  The newly-returned string takes ownership of the memory
69  * pointed at by data, and will call g_free on it when the reference count
70  * drops to zero. */
71 BlueSkyRCStr *bluesky_string_new(gpointer data, gsize len)
72 {
73     BlueSkyRCStr *string = g_new(BlueSkyRCStr, 1);
74     string->mmap = NULL;
75     string->data = data;
76     string->len = len;
77     g_atomic_int_set(&string->refcount, 1);
78     return string;
79 }
80
81 /* Create a new BlueSkyRCStr from a GString.  The GString is destroyed. */
82 BlueSkyRCStr *bluesky_string_new_from_gstring(GString *s)
83 {
84     gsize len = s->len;
85     return bluesky_string_new(g_string_free(s, FALSE), len);
86 }
87
88 /* Create a new BlueSkyRCStr from a memory-mapped buffer. */
89 BlueSkyRCStr *bluesky_string_new_from_mmap(BlueSkyMmap *mmap,
90                                            int offset, gsize len)
91 {
92     g_assert(offset + len < mmap->len);
93
94     BlueSkyRCStr *string = g_new(BlueSkyRCStr, 1);
95     string->mmap = mmap;
96     g_atomic_int_inc(&mmap->refcount);
97     string->data = (char *)mmap->addr + offset;
98     string->len = len;
99     g_atomic_int_set(&string->refcount, 1);
100     return string;
101 }
102
103 void bluesky_string_ref(BlueSkyRCStr *string)
104 {
105     if (string == NULL)
106         return;
107
108     g_atomic_int_inc(&string->refcount);
109 }
110
111 void bluesky_string_unref(BlueSkyRCStr *string)
112 {
113     if (string == NULL)
114         return;
115
116     if (g_atomic_int_dec_and_test(&string->refcount)) {
117         if (string->mmap == NULL) {
118             g_free(string->data);
119         } else {
120             bluesky_mmap_unref(string->mmap);
121         }
122         g_free(string);
123     }
124 }
125
126 /* Duplicate and return a new reference-counted string, containing a copy of
127  * the original data, with a reference count of 1.  As an optimization, if the
128  * passed-in string already has a reference count of 1, the original is
129  * returned.   Can be used to make a mutable copy of a shared string.  For this
130  * to truly be safe, it is probably needed that there be some type of lock
131  * protecting access to the string. */
132 BlueSkyRCStr *bluesky_string_dup(BlueSkyRCStr *string)
133 {
134     if (string == NULL)
135         return NULL;
136
137     if (string->mmap != NULL) {
138         BlueSkyRCStr *s;
139         s = bluesky_string_new(g_memdup(string->data, string->len),
140                                string->len);
141         bluesky_string_unref(string);
142         return s;
143     }
144
145     if (g_atomic_int_dec_and_test(&string->refcount)) {
146         /* There are no other shared copies, so return this one. */
147         g_atomic_int_inc(&string->refcount);
148         return string;
149     } else {
150         return bluesky_string_new(g_memdup(string->data, string->len),
151                                   string->len);
152     }
153 }
154
155 /* Resize the data block used by a BlueSkyRCStr.  The data pointer might change
156  * after making this call, so it should not be cached across calls to this
157  * function.  To avoid confusing any other users, the caller probably ought to
158  * hold the only reference to the string (by calling bluesky_string_dup first
159  * if needed). */
160 void bluesky_string_resize(BlueSkyRCStr *string, gsize len)
161 {
162     g_assert(string->mmap == NULL);
163
164     if (string->len == len)
165         return;
166
167     g_warn_if_fail(string->refcount == 1);
168
169     string->data = g_realloc(string->data, len);
170     string->len = len;
171 }
172
173 /* Cache LRU list management functions.  These manage the doubly-linked list of
174  * inodes sorted by accessed/modified time.  The FS lock should be held while
175  * calling these.
176  *
177  * _remove will unlink an inode from the linked list.
178  *
179  * _prepend and _append insert an inode at the head or tail of the linked list,
180  * and return a pointer to the linked list element (which should be stored in
181  * the inode); the inode should not already be in the list.
182  *
183  * _head and _tail simply return the first or last item inode in the list. */
184 void bluesky_list_unlink(GList *head, GList *item)
185 {
186     if (item == NULL)
187         return;
188
189     if (head->prev == item)
190         head->prev = item->prev;
191     head->next = g_list_delete_link(head->next, item);
192 }
193
194 GList *bluesky_list_prepend(GList *head, BlueSkyInode *inode)
195 {
196     head->next = g_list_prepend(head->next, inode);
197     if (head->prev == NULL)
198         head->prev = g_list_last(head->next);
199     return head->next;
200 }
201
202 GList *bluesky_list_append(GList *head, BlueSkyInode *inode)
203 {
204     if (head->next == NULL)
205         return bluesky_list_prepend(head, inode);
206
207     g_assert(head->prev != NULL && head->prev->next == NULL);
208
209     GList *link = g_list_alloc();
210     link->data = inode;
211     link->next = NULL;
212     link->prev = head->prev;
213     head->prev->next = link;
214     head->prev = link;
215     return link;
216 }
217
218 BlueSkyInode *bluesky_list_head(GList *head)
219 {
220     if (head->next == NULL)
221         return NULL;
222     else
223         return (BlueSkyInode *)head->next->data;
224 }
225
226 BlueSkyInode *bluesky_list_tail(GList *head)
227 {
228     if (head->prev == NULL)
229         return NULL;
230     else
231         return (BlueSkyInode *)head->prev->data;
232 }