1 /*
2  * libInstPatch
3  * Copyright (C) 1999-2014 Element Green <element@elementsofsound.org>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public License
7  * as published by the Free Software Foundation; version 2.1
8  * of the License only.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  * 02110-1301, USA or on the web at http://www.gnu.org.
19  */
20 /**
21  * SECTION: IpatchSampleStoreCache
22  * @short_description: Sample store object for cached samples in RAM
23  * @see_also: #IpatchSampleData
24  * @stability: Stable
25  *
26  * This sample store type is tightly integrated with #IpatchSampleData to provide
27  * managed cached samples in RAM.
28  */
29 #include <string.h>
30 #include <stdlib.h>
31 #include <glib.h>
32 #include <glib-object.h>
33 #include "IpatchSampleStoreCache.h"
34 #include "ipatch_priv.h"
35 #include "i18n.h"
36 
37 /* properties */
38 enum
39 {
40     PROP_0,
41     PROP_LOCATION
42 };
43 
44 /* Defined in IpatchSampleData.c */
45 extern void _ipatch_sample_data_cache_add_unused_size(int size);
46 
47 static void ipatch_sample_store_cache_sample_iface_init(IpatchSampleIface *iface);
48 static void ipatch_sample_store_cache_set_property
49 (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec);
50 static void ipatch_sample_store_cache_get_property
51 (GObject *object, guint property_id, GValue *value, GParamSpec *pspec);
52 static void ipatch_sample_store_cache_finalize(GObject *object);
53 static gboolean ipatch_sample_store_cache_sample_iface_open
54 (IpatchSampleHandle *handle, GError **err);
55 static void ipatch_sample_store_cache_sample_iface_close(IpatchSampleHandle *handle);
56 static gboolean ipatch_sample_store_cache_sample_iface_read
57 (IpatchSampleHandle *handle, guint offset, guint frames, gpointer buf, GError **err);
58 static gboolean ipatch_sample_store_cache_sample_iface_write
59 (IpatchSampleHandle *handle, guint offset, guint frames, gconstpointer buf, GError **err);
60 
61 
G_DEFINE_TYPE_WITH_CODE(IpatchSampleStoreCache,ipatch_sample_store_cache,IPATCH_TYPE_SAMPLE_STORE,G_IMPLEMENT_INTERFACE (IPATCH_TYPE_SAMPLE,ipatch_sample_store_cache_sample_iface_init))62 G_DEFINE_TYPE_WITH_CODE(IpatchSampleStoreCache, ipatch_sample_store_cache,
63                         IPATCH_TYPE_SAMPLE_STORE,
64                         G_IMPLEMENT_INTERFACE(IPATCH_TYPE_SAMPLE,
65                                 ipatch_sample_store_cache_sample_iface_init))
66 
67 static void
68 ipatch_sample_store_cache_sample_iface_init(IpatchSampleIface *iface)
69 {
70     iface->open = ipatch_sample_store_cache_sample_iface_open;
71     iface->close = ipatch_sample_store_cache_sample_iface_close;
72     iface->read = ipatch_sample_store_cache_sample_iface_read;
73     iface->write = ipatch_sample_store_cache_sample_iface_write;
74 }
75 
76 static void
ipatch_sample_store_cache_class_init(IpatchSampleStoreCacheClass * klass)77 ipatch_sample_store_cache_class_init(IpatchSampleStoreCacheClass *klass)
78 {
79     GObjectClass *obj_class = G_OBJECT_CLASS(klass);
80     IpatchItemClass *item_class = IPATCH_ITEM_CLASS(klass);
81 
82     obj_class->finalize = ipatch_sample_store_cache_finalize;
83     obj_class->get_property = ipatch_sample_store_cache_get_property;
84     item_class->item_set_property = ipatch_sample_store_cache_set_property;
85 
86     g_object_class_install_property(obj_class, PROP_LOCATION,
87                                     g_param_spec_pointer("location", "Location", "Sample data pointer",
88                                             G_PARAM_READWRITE));
89 }
90 
91 static void
ipatch_sample_store_cache_set_property(GObject * object,guint property_id,const GValue * value,GParamSpec * pspec)92 ipatch_sample_store_cache_set_property(GObject *object, guint property_id,
93                                        const GValue *value, GParamSpec *pspec)
94 {
95     IpatchSampleStoreCache *store = IPATCH_SAMPLE_STORE_CACHE(object);
96 
97     switch(property_id)
98     {
99     case PROP_LOCATION:
100         g_return_if_fail(store->location == NULL);
101 
102         /* Lock not needed, should be set only once before use */
103         store->location = g_value_get_pointer(value);
104         break;
105 
106     default:
107         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
108     }
109 }
110 
111 static void
ipatch_sample_store_cache_get_property(GObject * object,guint property_id,GValue * value,GParamSpec * pspec)112 ipatch_sample_store_cache_get_property(GObject *object, guint property_id,
113                                        GValue *value, GParamSpec *pspec)
114 {
115     IpatchSampleStoreCache *store = IPATCH_SAMPLE_STORE_CACHE(object);
116 
117     switch(property_id)
118     {
119     case PROP_LOCATION:
120         /* Lock not needed, should be set only once before use */
121         g_value_set_pointer(value, store->location);
122         break;
123 
124     default:
125         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
126     }
127 }
128 
129 static void
ipatch_sample_store_cache_init(IpatchSampleStoreCache * store)130 ipatch_sample_store_cache_init(IpatchSampleStoreCache *store)
131 {
132     GTimeVal timeval;
133 
134     /* Initialize last open to current time */
135     g_get_current_time(&timeval);
136     store->last_open = timeval.tv_sec;
137 }
138 
139 static void
ipatch_sample_store_cache_finalize(GObject * object)140 ipatch_sample_store_cache_finalize(GObject *object)
141 {
142     IpatchSampleStoreCache *store = IPATCH_SAMPLE_STORE_CACHE(object);
143 
144     g_free(store->location);
145     store->location = NULL;
146 
147     if(G_OBJECT_CLASS(ipatch_sample_store_cache_parent_class)->finalize)
148     {
149         G_OBJECT_CLASS(ipatch_sample_store_cache_parent_class)->finalize(object);
150     }
151 }
152 
153 static gboolean
ipatch_sample_store_cache_sample_iface_open(IpatchSampleHandle * handle,GError ** err)154 ipatch_sample_store_cache_sample_iface_open(IpatchSampleHandle *handle,
155         GError **err)
156 {
157     IpatchSampleStoreCache *store = IPATCH_SAMPLE_STORE_CACHE(handle->sample);
158     guint bytes;
159 
160     g_return_val_if_fail(!handle->read_mode || store->location, FALSE);
161 
162     IPATCH_ITEM_WLOCK(store);
163     store->last_open = 0;         /* Reset time to 0 to indicate store is open */
164 
165     if(store->open_count == 0)    /* Recursive lock: store, sample_cache_vars */
166         _ipatch_sample_data_cache_add_unused_size
167         (-(int)ipatch_sample_store_get_size_bytes((IpatchSampleStore *)store));
168 
169     g_atomic_int_inc(&store->open_count);
170     IPATCH_ITEM_WUNLOCK(store);
171 
172     /* Locking not needed, since new samples will be written with audio before
173      * being used by multiple threads */
174     if(!store->location)
175     {
176         bytes = ipatch_sample_store_get_size_bytes((IpatchSampleStore *)store);
177         store->location = g_malloc0(bytes);
178     }
179 
180     /* Store frame size to data1 */
181     handle->data1 = GUINT_TO_POINTER(ipatch_sample_format_size(ipatch_sample_store_get_format(store)));
182 
183     return (TRUE);
184 }
185 
186 static void
ipatch_sample_store_cache_sample_iface_close(IpatchSampleHandle * handle)187 ipatch_sample_store_cache_sample_iface_close(IpatchSampleHandle *handle)
188 {
189     IpatchSampleStoreCache *store = IPATCH_SAMPLE_STORE_CACHE(handle->sample);
190     GTimeVal timeval;
191 
192     IPATCH_ITEM_WLOCK(store);
193 
194     /* Set last open time if there are no more open handles */
195     if(g_atomic_int_dec_and_test(&store->open_count))
196     {
197         g_get_current_time(&timeval);
198         store->last_open = timeval.tv_sec;
199 
200         /* Recursive lock: store, sample_cache_vars */
201         _ipatch_sample_data_cache_add_unused_size
202         (ipatch_sample_store_get_size_bytes((IpatchSampleStore *)store));
203     }
204 
205     IPATCH_ITEM_WUNLOCK(store);
206 }
207 
208 static gboolean
ipatch_sample_store_cache_sample_iface_read(IpatchSampleHandle * handle,guint offset,guint frames,gpointer buf,GError ** err)209 ipatch_sample_store_cache_sample_iface_read(IpatchSampleHandle *handle,
210         guint offset, guint frames,
211         gpointer buf, GError **err)
212 {
213     IpatchSampleStoreCache *store = (IpatchSampleStoreCache *)(handle->sample);
214     guint8 frame_size = GPOINTER_TO_UINT(handle->data1);
215 
216     /* No need to lock, sample data should not change after initial load */
217 
218     memcpy(buf, &((gint8 *)(store->location))[offset * frame_size],
219            frames * frame_size);
220 
221     return (TRUE);
222 }
223 
224 static gboolean
ipatch_sample_store_cache_sample_iface_write(IpatchSampleHandle * handle,guint offset,guint frames,gconstpointer buf,GError ** err)225 ipatch_sample_store_cache_sample_iface_write(IpatchSampleHandle *handle,
226         guint offset, guint frames,
227         gconstpointer buf, GError **err)
228 {
229     IpatchSampleStoreCache *store = (IpatchSampleStoreCache *)(handle->sample);
230     guint8 frame_size = GPOINTER_TO_UINT(handle->data1);
231 
232     /* No need to lock, sample data written only once and before used by
233        multiple threads */
234 
235     memcpy(&((gint8 *)(store->location))[offset * frame_size], buf,
236            frames * frame_size);
237 
238     return (TRUE);
239 }
240 
241 /**
242  * ipatch_sample_store_cache_new:
243  * @location: Location of existing sample data or %NULL if the sample buffer
244  *   should be allocated (in which case the sample must be written to first).
245  *
246  * Creates a new cached RAM sample store.  If @location is provided, its allocation
247  * is taken over by the store.
248  *
249  * NOTE: This store type should not be used outside of the #IpatchSampleData
250  * implementation, as it is tightly coupled with it.
251  *
252  * Returns: (type IpatchSampleStoreCache): New cached RAM sample store, cast
253  *   as a #IpatchSample for convenience.
254  */
255 IpatchSample *
ipatch_sample_store_cache_new(gpointer location)256 ipatch_sample_store_cache_new(gpointer location)
257 {
258     return (IPATCH_SAMPLE(g_object_new(IPATCH_TYPE_SAMPLE_STORE_CACHE,
259                                        "location", location,
260                                        NULL)));
261 }
262 
263 /**
264  * ipatch_sample_store_cache_open:
265  * @store: Sample cache store
266  *
267  * A dummy open function which can be used if the location pointer will be
268  * accessed directly, rather than opening a #IpatchSampleHandle.  Keeping a
269  * cached sample store open will ensure it isn't destroyed.  Call
270  * ipatch_sample_store_cache_close() when done with it.
271  */
272 void
ipatch_sample_store_cache_open(IpatchSampleStoreCache * store)273 ipatch_sample_store_cache_open(IpatchSampleStoreCache *store)
274 {
275     int size;
276 
277     g_return_if_fail(IPATCH_IS_SAMPLE_STORE_CACHE(store));
278 
279     size = ipatch_sample_store_get_size_bytes((IpatchSampleStore *)store);
280 
281     IPATCH_ITEM_WLOCK(store);
282     store->last_open = 0;         /* Reset time to 0 to indicate store is open */
283 
284     if(store->open_count == 0)    /* Recursive lock: store, sample_cache_vars */
285     {
286         _ipatch_sample_data_cache_add_unused_size(-size);
287     }
288 
289     g_atomic_int_inc(&store->open_count);
290     IPATCH_ITEM_WUNLOCK(store);
291 }
292 
293 /**
294  * ipatch_sample_store_cache_close:
295  * @store: Sample cache store
296  *
297  * A dummy close function which is called after a sample store cache is no
298  * longer needed after opening it with ipatch_sample_store_cache_open().
299  */
300 void
ipatch_sample_store_cache_close(IpatchSampleStoreCache * store)301 ipatch_sample_store_cache_close(IpatchSampleStoreCache *store)
302 {
303     GTimeVal timeval;
304     int size;
305 
306     g_return_if_fail(IPATCH_IS_SAMPLE_STORE_CACHE(store));
307 
308     size = ipatch_sample_store_get_size_bytes((IpatchSampleStore *)store);
309 
310     IPATCH_ITEM_WLOCK(store);
311 
312     /* Set last open time if there are no more open handles */
313     if(g_atomic_int_dec_and_test(&store->open_count))
314     {
315         g_get_current_time(&timeval);
316         store->last_open = timeval.tv_sec;
317 
318         /* Recursive lock: store, sample_cache_vars */
319         _ipatch_sample_data_cache_add_unused_size(size);
320     }
321 
322     IPATCH_ITEM_WUNLOCK(store);
323 }
324