1 /* Blue Sky: File Systems in the Cloud
3 * Copyright (C) 2009 The Regents of the University of California
4 * Written by Michael Vrable <mvrable@cs.ucsd.edu>
15 #include "bluesky-private.h"
17 /* Miscellaneous useful functions that don't really fit anywhere else. */
19 bluesky_time_hires bluesky_now_hires()
23 if (clock_gettime(CLOCK_REALTIME, &time) != 0) {
24 perror("clock_gettime");
28 return (int64_t)(time.tv_sec) * 1000000000 + time.tv_nsec;
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)
36 /* TODO: Unicode handling; for now just do ASCII. */
37 return g_ascii_strdown(s, -1);
40 gboolean bluesky_inode_is_ready(BlueSkyInode *inode)
45 g_mutex_lock(inode->lock);
46 gboolean valid = (inode->type != BLUESKY_PENDING
47 && inode->type != BLUESKY_INVALID);
49 g_mutex_unlock(inode->lock);
54 /**** Reference-counted strings. ****/
56 void bluesky_mmap_unref(BlueSkyMmap *mmap)
61 if (g_atomic_int_dec_and_test(&mmap->refcount)) {
62 munmap((void *)mmap->addr, mmap->len);
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
71 BlueSkyRCStr *bluesky_string_new(gpointer data, gsize len)
73 BlueSkyRCStr *string = g_new(BlueSkyRCStr, 1);
77 g_atomic_int_set(&string->refcount, 1);
81 /* Create a new BlueSkyRCStr from a GString. The GString is destroyed. */
82 BlueSkyRCStr *bluesky_string_new_from_gstring(GString *s)
85 return bluesky_string_new(g_string_free(s, FALSE), len);
88 /* Create a new BlueSkyRCStr from a memory-mapped buffer. */
89 BlueSkyRCStr *bluesky_string_new_from_mmap(BlueSkyMmap *mmap,
90 int offset, gsize len)
92 BlueSkyRCStr *string = g_new(BlueSkyRCStr, 1);
94 g_atomic_int_inc(&mmap->refcount);
95 string->data = (char *)mmap->addr + offset;
97 g_atomic_int_set(&string->refcount, 1);
101 void bluesky_string_ref(BlueSkyRCStr *string)
106 g_atomic_int_inc(&string->refcount);
109 void bluesky_string_unref(BlueSkyRCStr *string)
114 if (g_atomic_int_dec_and_test(&string->refcount)) {
115 if (string->mmap == NULL) {
116 g_free(string->data);
118 bluesky_mmap_unref(string->mmap);
124 /* Duplicate and return a new reference-counted string, containing a copy of
125 * the original data, with a reference count of 1. As an optimization, if the
126 * passed-in string already has a reference count of 1, the original is
127 * returned. Can be used to make a mutable copy of a shared string. For this
128 * to truly be safe, it is probably needed that there be some type of lock
129 * protecting access to the string. */
130 BlueSkyRCStr *bluesky_string_dup(BlueSkyRCStr *string)
135 if (string->mmap != NULL) {
137 s = bluesky_string_new(g_memdup(string->data, string->len),
139 bluesky_string_unref(string);
143 if (g_atomic_int_dec_and_test(&string->refcount)) {
144 /* There are no other shared copies, so return this one. */
145 g_atomic_int_inc(&string->refcount);
148 return bluesky_string_new(g_memdup(string->data, string->len),
153 /* Resize the data block used by a BlueSkyRCStr. The data pointer might change
154 * after making this call, so it should not be cached across calls to this
155 * function. To avoid confusing any other users, the caller probably ought to
156 * hold the only reference to the string (by calling bluesky_string_dup first
158 void bluesky_string_resize(BlueSkyRCStr *string, gsize len)
160 g_assert(string->mmap == NULL);
162 if (string->len == len)
165 g_warn_if_fail(string->refcount == 1);
167 string->data = g_realloc(string->data, len);
171 /* Cache LRU list management functions. These manage the doubly-linked list of
172 * inodes sorted by accessed/modified time. The FS lock should be held while
175 * _remove will unlink an inode from the linked list.
177 * _prepend and _append insert an inode at the head or tail of the linked list,
178 * and return a pointer to the linked list element (which should be stored in
179 * the inode); the inode should not already be in the list.
181 * _head and _tail simply return the first or last item inode in the list. */
182 void bluesky_list_unlink(GList *head, GList *item)
187 if (head->prev == item)
188 head->prev = item->prev;
189 head->next = g_list_delete_link(head->next, item);
192 GList *bluesky_list_prepend(GList *head, BlueSkyInode *inode)
194 head->next = g_list_prepend(head->next, inode);
195 if (head->prev == NULL)
196 head->prev = g_list_last(head->next);
200 GList *bluesky_list_append(GList *head, BlueSkyInode *inode)
202 if (head->next == NULL)
203 return bluesky_list_prepend(head, inode);
205 g_assert(head->prev != NULL && head->prev->next == NULL);
207 GList *link = g_list_alloc();
210 link->prev = head->prev;
211 head->prev->next = link;
216 BlueSkyInode *bluesky_list_head(GList *head)
218 if (head->next == NULL)
221 return (BlueSkyInode *)head->next->data;
224 BlueSkyInode *bluesky_list_tail(GList *head)
226 if (head->prev == NULL)
229 return (BlueSkyInode *)head->prev->data;