[test/buffer] Test pre_allocate() and allocation_successful()
[apps/home/video-player.git] / src / hb-blob.cc
1 /*
2  * Copyright © 2009  Red Hat, Inc.
3  *
4  *  This is part of HarfBuzz, a text shaping library.
5  *
6  * Permission is hereby granted, without written agreement and without
7  * license or royalty fees, to use, copy, modify, and distribute this
8  * software and its documentation for any purpose, provided that the
9  * above copyright notice and the following two paragraphs appear in
10  * all copies of this software.
11  *
12  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16  * DAMAGE.
17  *
18  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23  *
24  * Red Hat Author(s): Behdad Esfahbod
25  */
26
27 #include "hb-private.hh"
28
29 #include "hb-blob-private.hh"
30
31 #ifdef HAVE_SYS_MMAN_H
32 #ifdef HAVE_UNISTD_H
33 #include <unistd.h>
34 #endif /* HAVE_UNISTD_H */
35 #include <sys/mman.h>
36 #endif /* HAVE_SYS_MMAN_H */
37
38 #include <stdio.h>
39 #include <errno.h>
40
41 HB_BEGIN_DECLS
42
43
44 #ifndef HB_DEBUG_BLOB
45 #define HB_DEBUG_BLOB (HB_DEBUG+0)
46 #endif
47
48 hb_blob_t _hb_blob_nil = {
49   HB_OBJECT_HEADER_STATIC,
50
51   0, /* length */
52
53   HB_MUTEX_INIT, /* lock */
54
55   0, /* lock_count */
56   HB_MEMORY_MODE_READONLY, /* mode */
57
58   NULL, /* data */
59
60   NULL, /* user_data */
61   NULL  /* destroy */
62 };
63
64 static void
65 _hb_blob_destroy_user_data (hb_blob_t *blob)
66 {
67   if (blob->destroy) {
68     blob->destroy (blob->user_data);
69     blob->user_data = NULL;
70     blob->destroy = NULL;
71   }
72 }
73
74 static void
75 _hb_blob_unlock_and_destroy (hb_blob_t *blob)
76 {
77   hb_blob_unlock (blob);
78   hb_blob_destroy (blob);
79 }
80
81 hb_blob_t *
82 hb_blob_create (const char        *data,
83                 unsigned int       length,
84                 hb_memory_mode_t   mode,
85                 void              *user_data,
86                 hb_destroy_func_t  destroy)
87 {
88   hb_blob_t *blob;
89
90   if (!length || !(blob = hb_object_create<hb_blob_t> ())) {
91     if (destroy)
92       destroy (user_data);
93     return &_hb_blob_nil;
94   }
95
96   hb_mutex_init (blob->lock);
97   blob->lock_count = 0;
98
99   blob->data = data;
100   blob->length = length;
101   blob->mode = mode;
102
103   blob->user_data = user_data;
104   blob->destroy = destroy;
105
106   if (blob->mode == HB_MEMORY_MODE_DUPLICATE) {
107     blob->mode = HB_MEMORY_MODE_READONLY;
108     if (!hb_blob_try_writable (blob)) {
109       hb_blob_destroy (blob);
110       return &_hb_blob_nil;
111     }
112   }
113
114   return blob;
115 }
116
117 hb_blob_t *
118 hb_blob_create_sub_blob (hb_blob_t    *parent,
119                          unsigned int  offset,
120                          unsigned int  length)
121 {
122   hb_blob_t *blob;
123   const char *pdata;
124
125   if (!length || offset >= parent->length || !(blob = hb_object_create<hb_blob_t> ()))
126     return &_hb_blob_nil;
127
128   pdata = hb_blob_lock (parent);
129
130   blob->data = pdata + offset;
131   blob->length = MIN (length, parent->length - offset);
132
133   hb_mutex_lock (parent->lock);
134   blob->mode = parent->mode;
135   hb_mutex_unlock (parent->lock);
136
137   blob->user_data = hb_blob_reference (parent);
138   blob->destroy = (hb_destroy_func_t) _hb_blob_unlock_and_destroy;
139
140   return blob;
141 }
142
143 hb_blob_t *
144 hb_blob_create_empty (void)
145 {
146   return &_hb_blob_nil;
147 }
148
149 hb_blob_t *
150 hb_blob_reference (hb_blob_t *blob)
151 {
152   return hb_object_reference (blob);
153 }
154
155 void
156 hb_blob_destroy (hb_blob_t *blob)
157 {
158   if (!hb_object_destroy (blob)) return;
159
160   _hb_blob_destroy_user_data (blob);
161
162   free (blob);
163 }
164
165 hb_bool_t
166 hb_blob_set_user_data (hb_blob_t          *blob,
167                        hb_user_data_key_t *key,
168                        void *              data,
169                        hb_destroy_func_t   destroy)
170 {
171   return hb_object_set_user_data (blob, key, data, destroy);
172 }
173
174 void *
175 hb_blob_get_user_data (hb_blob_t          *blob,
176                        hb_user_data_key_t *key)
177 {
178   return hb_object_get_user_data (blob, key);
179 }
180
181
182 unsigned int
183 hb_blob_get_length (hb_blob_t *blob)
184 {
185   return blob->length;
186 }
187
188 const char *
189 hb_blob_lock (hb_blob_t *blob)
190 {
191   if (hb_object_is_inert (blob))
192     return NULL;
193
194   hb_mutex_lock (blob->lock);
195
196   (void) (HB_DEBUG_BLOB &&
197     fprintf (stderr, "%p %s (%d) -> %p\n", blob, HB_FUNC,
198              blob->lock_count, blob->data));
199
200   blob->lock_count++;
201
202   hb_mutex_unlock (blob->lock);
203
204   return blob->data;
205 }
206
207 void
208 hb_blob_unlock (hb_blob_t *blob)
209 {
210   if (hb_object_is_inert (blob))
211     return;
212
213   hb_mutex_lock (blob->lock);
214
215   (void) (HB_DEBUG_BLOB &&
216     fprintf (stderr, "%p %s (%d) -> %p\n", blob, HB_FUNC,
217              blob->lock_count, blob->data));
218
219   assert (blob->lock_count > 0);
220   blob->lock_count--;
221
222   hb_mutex_unlock (blob->lock);
223 }
224
225 hb_bool_t
226 hb_blob_is_writable (hb_blob_t *blob)
227 {
228   hb_memory_mode_t mode;
229
230   if (hb_object_is_inert (blob))
231     return FALSE;
232
233   hb_mutex_lock (blob->lock);
234
235   mode = blob->mode;
236
237   hb_mutex_unlock (blob->lock);
238
239   return mode == HB_MEMORY_MODE_WRITABLE;
240 }
241
242
243 static hb_bool_t
244 _try_make_writable_inplace_unix_locked (hb_blob_t *blob)
245 {
246 #if defined(HAVE_SYS_MMAN_H) && defined(HAVE_MPROTECT)
247   uintptr_t pagesize = -1, mask, length;
248   const char *addr;
249
250 #if defined(HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
251   pagesize = (uintptr_t) sysconf (_SC_PAGE_SIZE);
252 #elif defined(HAVE_SYSCONF) && defined(_SC_PAGESIZE)
253   pagesize = (uintptr_t) sysconf (_SC_PAGESIZE);
254 #elif defined(HAVE_GETPAGESIZE)
255   pagesize = (uintptr_t) getpagesize ();
256 #endif
257
258   if ((uintptr_t) -1L == pagesize) {
259     (void) (HB_DEBUG_BLOB &&
260       fprintf (stderr, "%p %s: failed to get pagesize: %s\n", blob, HB_FUNC, strerror (errno)));
261     return FALSE;
262   }
263   (void) (HB_DEBUG_BLOB &&
264     fprintf (stderr, "%p %s: pagesize is %lu\n", blob, HB_FUNC, (unsigned long) pagesize));
265
266   mask = ~(pagesize-1);
267   addr = (const char *) (((uintptr_t) blob->data) & mask);
268   length = (const char *) (((uintptr_t) blob->data + blob->length + pagesize-1) & mask)  - addr;
269   (void) (HB_DEBUG_BLOB &&
270     fprintf (stderr, "%p %s: calling mprotect on [%p..%p] (%lu bytes)\n",
271              blob, HB_FUNC,
272              addr, addr+length, (unsigned long) length));
273   if (-1 == mprotect ((void *) addr, length, PROT_READ | PROT_WRITE)) {
274     (void) (HB_DEBUG_BLOB &&
275       fprintf (stderr, "%p %s: %s\n", blob, HB_FUNC, strerror (errno)));
276     return FALSE;
277   }
278
279   (void) (HB_DEBUG_BLOB &&
280     fprintf (stderr, "%p %s: successfully made [%p..%p] (%lu bytes) writable\n",
281              blob, HB_FUNC,
282              addr, addr+length, (unsigned long) length));
283   return TRUE;
284 #else
285   return FALSE;
286 #endif
287 }
288
289 static void
290 try_writable_inplace_locked (hb_blob_t *blob)
291 {
292   (void) (HB_DEBUG_BLOB &&
293     fprintf (stderr, "%p %s: making writable\n", blob, HB_FUNC));
294
295   if (_try_make_writable_inplace_unix_locked (blob)) {
296     (void) (HB_DEBUG_BLOB &&
297       fprintf (stderr, "%p %s: making writable -> succeeded\n", blob, HB_FUNC));
298     blob->mode = HB_MEMORY_MODE_WRITABLE;
299   } else {
300     (void) (HB_DEBUG_BLOB &&
301       fprintf (stderr, "%p %s: making writable -> FAILED\n", blob, HB_FUNC));
302     /* Failed to make writable inplace, mark that */
303     blob->mode = HB_MEMORY_MODE_READONLY;
304   }
305 }
306
307 hb_bool_t
308 hb_blob_try_writable_inplace (hb_blob_t *blob)
309 {
310   hb_memory_mode_t mode;
311
312   if (hb_object_is_inert (blob))
313     return FALSE;
314
315   hb_mutex_lock (blob->lock);
316
317   if (blob->mode == HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE)
318     try_writable_inplace_locked (blob);
319
320   mode = blob->mode;
321
322   hb_mutex_unlock (blob->lock);
323
324   return mode == HB_MEMORY_MODE_WRITABLE;
325 }
326
327 hb_bool_t
328 hb_blob_try_writable (hb_blob_t *blob)
329 {
330   hb_memory_mode_t mode;
331
332   if (hb_object_is_inert (blob))
333     return FALSE;
334
335   hb_mutex_lock (blob->lock);
336
337   if (blob->mode == HB_MEMORY_MODE_READONLY_MAY_MAKE_WRITABLE)
338     try_writable_inplace_locked (blob);
339
340   if (blob->mode == HB_MEMORY_MODE_READONLY)
341   {
342     char *new_data;
343
344     (void) (HB_DEBUG_BLOB &&
345       fprintf (stderr, "%p %s (%d) -> %p\n", blob, HB_FUNC,
346                blob->lock_count, blob->data));
347
348     if (blob->lock_count)
349       goto done;
350
351     new_data = (char *) malloc (blob->length);
352     if (new_data) {
353       (void) (HB_DEBUG_BLOB &&
354         fprintf (stderr, "%p %s: dupped successfully -> %p\n", blob, HB_FUNC, blob->data));
355       memcpy (new_data, blob->data, blob->length);
356       _hb_blob_destroy_user_data (blob);
357       blob->mode = HB_MEMORY_MODE_WRITABLE;
358       blob->data = new_data;
359       blob->user_data = new_data;
360       blob->destroy = free;
361     }
362   }
363
364 done:
365   mode = blob->mode;
366
367   hb_mutex_unlock (blob->lock);
368
369   return mode == HB_MEMORY_MODE_WRITABLE;
370 }
371
372
373 HB_END_DECLS