3ac6596808bafd865a7a8c7adb97b588c7ea8997
[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 gboolean bluesky_inode_is_ready(BlueSkyInode *inode)
40 {
41     if (inode == NULL)
42         return FALSE;
43
44     g_mutex_lock(inode->lock);
45     gboolean valid = (inode->type != BLUESKY_PENDING
46                       && inode->type != BLUESKY_INVALID);
47
48     g_mutex_unlock(inode->lock);
49
50     return valid;
51 }
52
53 /**** Reference-counted strings. ****/
54
55 /* Create and return a new reference-counted string.  The reference count is
56  * initially one.  The newly-returned string takes ownership of the memory
57  * pointed at by data, and will call g_free on it when the reference count
58  * drops to zero. */
59 BlueSkyRCStr *bluesky_string_new(gpointer data, gsize len)
60 {
61     BlueSkyRCStr *string = g_new(BlueSkyRCStr, 1);
62     string->data = data;
63     string->len = len;
64     g_atomic_int_set(&string->refcount, 1);
65     return string;
66 }
67
68 /* Create a new BlueSkyRCStr from a GString.  The GString is destroyed. */
69 BlueSkyRCStr *bluesky_string_new_from_gstring(GString *s)
70 {
71     gsize len = s->len;
72     return bluesky_string_new(g_string_free(s, FALSE), len);
73 }
74
75 void bluesky_string_ref(BlueSkyRCStr *string)
76 {
77     if (string == NULL)
78         return;
79
80     g_atomic_int_inc(&string->refcount);
81 }
82
83 void bluesky_string_unref(BlueSkyRCStr *string)
84 {
85     if (string == NULL)
86         return;
87
88     if (g_atomic_int_dec_and_test(&string->refcount)) {
89         g_free(string->data);
90         g_free(string);
91     }
92 }
93
94 /* Duplicate and return a new reference-counted string, containing a copy of
95  * the original data, with a reference count of 1.  As an optimization, if the
96  * passed-in string already has a reference count of 1, the original is
97  * returned.   Can be used to make a mutable copy of a shared string.  For this
98  * to truly be safe, it is probably needed that there be some type of lock
99  * protecting access to the string. */
100 BlueSkyRCStr *bluesky_string_dup(BlueSkyRCStr *string)
101 {
102     if (string == NULL)
103         return NULL;
104
105     if (g_atomic_int_dec_and_test(&string->refcount)) {
106         /* There are no other shared copies, so return this one. */
107         g_atomic_int_inc(&string->refcount);
108         return string;
109     } else {
110         return bluesky_string_new(g_memdup(string->data, string->len),
111                                   string->len);
112     }
113 }
114
115 /* Resize the data block used by a BlueSkyRCStr.  The data pointer might change
116  * after making this call, so it should not be cached across calls to this
117  * function.  To avoid confusing any other users, the caller probably ought to
118  * hold the only reference to the string (by calling bluesky_string_dup first
119  * if needed). */
120 void bluesky_string_resize(BlueSkyRCStr *string, gsize len)
121 {
122     if (string->len == len)
123         return;
124
125     g_warn_if_fail(string->refcount == 1);
126
127     string->data = g_realloc(string->data, len);
128     string->len = len;
129 }
130
131 /* Cache LRU list management functions.  These manage the doubly-linked list of
132  * inodes sorted by accessed/modified time.  The FS lock should be held while
133  * calling these.
134  *
135  * _remove will unlink an inode from the linked list.
136  *
137  * _prepend and _append insert an inode at the head or tail of the linked list,
138  * and return a pointer to the linked list element (which should be stored in
139  * the inode); the inode should not already be in the list.
140  *
141  * _head and _tail simply return the first or last item inode in the list. */
142 void bluesky_list_unlink(GList *head, GList *item)
143 {
144     if (item == NULL)
145         return;
146
147     if (head->prev == item)
148         head->prev = item->prev;
149     head->next = g_list_delete_link(head->next, item);
150 }
151
152 GList *bluesky_list_prepend(GList *head, BlueSkyInode *inode)
153 {
154     head->next = g_list_prepend(head->next, inode);
155     if (head->prev == NULL)
156         head->prev = g_list_last(head->next);
157     return head->next;
158 }
159
160 GList *bluesky_list_append(GList *head, BlueSkyInode *inode)
161 {
162     if (head->next == NULL)
163         return bluesky_list_prepend(head, inode);
164
165     g_assert(head->prev != NULL && head->prev->next == NULL);
166
167     GList *link = g_list_alloc();
168     link->data = inode;
169     link->next = NULL;
170     link->prev = head->prev;
171     head->prev->next = link;
172     head->prev = link;
173     return link;
174 }
175
176 BlueSkyInode *bluesky_list_head(GList *head)
177 {
178     if (head->next == NULL)
179         return NULL;
180     else
181         return (BlueSkyInode *)head->next->data;
182 }
183
184 BlueSkyInode *bluesky_list_tail(GList *head)
185 {
186     if (head->prev == NULL)
187         return NULL;
188     else
189         return (BlueSkyInode *)head->prev->data;
190 }