Do not pad the final block of a file with zeroes.
[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 void bluesky_string_ref(BlueSkyRCStr *string)
55 {
56     if (string == NULL)
57         return;
58
59     g_atomic_int_inc(&string->refcount);
60 }
61
62 void bluesky_string_unref(BlueSkyRCStr *string)
63 {
64     if (string == NULL)
65         return;
66
67     if (g_atomic_int_dec_and_test(&string->refcount)) {
68         g_free(string->data);
69         g_free(string);
70     }
71 }
72
73 /* Duplicate and return a new reference-counted string, containing a copy of
74  * the original data, with a reference count of 1.  As an optimization, if the
75  * passed-in string already has a reference count of 1, the original is
76  * returned.   Can be used to make a mutable copy of a shared string.  For this
77  * to truly be safe, it is probably needed that there be some type of lock
78  * protecting access to the string. */
79 BlueSkyRCStr *bluesky_string_dup(BlueSkyRCStr *string)
80 {
81     if (string == NULL)
82         return NULL;
83
84     if (g_atomic_int_dec_and_test(&string->refcount)) {
85         /* There are no other shared copies, so return this one. */
86         g_atomic_int_inc(&string->refcount);
87         return string;
88     } else {
89         return bluesky_string_new(g_memdup(string->data, string->len),
90                                   string->len);
91     }
92 }
93
94 /* Resize the data block used by a BlueSkyRCStr.  The data pointer might change
95  * after making this call, so it should not be cached across calls to this
96  * function.  To avoid confusing any other users, the caller probably ought to
97  * hold the only reference to the string (by calling bluesky_string_dup first
98  * if needed). */
99 void bluesky_string_resize(BlueSkyRCStr *string, gsize len)
100 {
101     if (string->len == len)
102         return;
103
104     string->data = g_realloc(string->data, len);
105 }