OLD | NEW |
1 /* | 1 /* |
2 * Copyright © 2009 Red Hat, Inc. | 2 * Copyright © 2009 Red Hat, Inc. |
3 * | 3 * |
4 * This is part of HarfBuzz, a text shaping library. | 4 * This is part of HarfBuzz, a text shaping library. |
5 * | 5 * |
6 * Permission is hereby granted, without written agreement and without | 6 * Permission is hereby granted, without written agreement and without |
7 * license or royalty fees, to use, copy, modify, and distribute this | 7 * license or royalty fees, to use, copy, modify, and distribute this |
8 * software and its documentation for any purpose, provided that the | 8 * software and its documentation for any purpose, provided that the |
9 * above copyright notice and the following two paragraphs appear in | 9 * above copyright notice and the following two paragraphs appear in |
10 * all copies of this software. | 10 * all copies of this software. |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 static void | 72 static void |
73 _hb_blob_destroy_user_data (hb_blob_t *blob) | 73 _hb_blob_destroy_user_data (hb_blob_t *blob) |
74 { | 74 { |
75 if (blob->destroy) { | 75 if (blob->destroy) { |
76 blob->destroy (blob->user_data); | 76 blob->destroy (blob->user_data); |
77 blob->user_data = NULL; | 77 blob->user_data = NULL; |
78 blob->destroy = NULL; | 78 blob->destroy = NULL; |
79 } | 79 } |
80 } | 80 } |
81 | 81 |
| 82 /** |
| 83 * hb_blob_create: (Xconstructor) |
| 84 * @data: (array length=length) (closure user_data) (destroy destroy) (scope not
ified) (transfer none): Pointer to blob data. |
| 85 * @length: Length of @data in bytes. |
| 86 * @mode: Memory mode for @data. |
| 87 * @user_data: Data parameter to pass to @destroy. |
| 88 * @destroy: Callback to call when @data is not needed anymore. |
| 89 * |
| 90 * Creates a new "blob" object wrapping @data. The @mode parameter is used |
| 91 * to negotiate ownership and lifecycle of @data. |
| 92 * |
| 93 * Return value: New blob, or the empty blob if something failed or if @length i
s |
| 94 * zero. Destroy with hb_blob_destroy(). |
| 95 * |
| 96 * Since: 1.0 |
| 97 **/ |
82 hb_blob_t * | 98 hb_blob_t * |
83 hb_blob_create (const char *data, | 99 hb_blob_create (const char *data, |
84 unsigned int length, | 100 unsigned int length, |
85 hb_memory_mode_t mode, | 101 hb_memory_mode_t mode, |
86 void *user_data, | 102 void *user_data, |
87 hb_destroy_func_t destroy) | 103 hb_destroy_func_t destroy) |
88 { | 104 { |
89 hb_blob_t *blob; | 105 hb_blob_t *blob; |
90 | 106 |
91 if (!length || !(blob = hb_object_create<hb_blob_t> ())) { | 107 if (!length || !(blob = hb_object_create<hb_blob_t> ())) { |
(...skipping 13 matching lines...) Expand all Loading... |
105 blob->mode = HB_MEMORY_MODE_READONLY; | 121 blob->mode = HB_MEMORY_MODE_READONLY; |
106 if (!_try_writable (blob)) { | 122 if (!_try_writable (blob)) { |
107 hb_blob_destroy (blob); | 123 hb_blob_destroy (blob); |
108 return hb_blob_get_empty (); | 124 return hb_blob_get_empty (); |
109 } | 125 } |
110 } | 126 } |
111 | 127 |
112 return blob; | 128 return blob; |
113 } | 129 } |
114 | 130 |
| 131 /** |
| 132 * hb_blob_create_sub_blob: |
| 133 * @parent: Parent blob. |
| 134 * @offset: Start offset of sub-blob within @parent, in bytes. |
| 135 * @length: Length of sub-blob. |
| 136 * |
| 137 * Returns a blob that represents a range of bytes in @parent. The new |
| 138 * blob is always created with %HB_MEMORY_MODE_READONLY, meaning that it |
| 139 * will never modify data in the parent blob. The parent data is not |
| 140 * expected to be modified, and will result in undefined behavior if it |
| 141 * is. |
| 142 * |
| 143 * Makes @parent immutable. |
| 144 * |
| 145 * Return value: New blob, or the empty blob if something failed or if |
| 146 * @length is zero or @offset is beyond the end of @parent's data. Destroy |
| 147 * with hb_blob_destroy(). |
| 148 * |
| 149 * Since: 1.0 |
| 150 **/ |
115 hb_blob_t * | 151 hb_blob_t * |
116 hb_blob_create_sub_blob (hb_blob_t *parent, | 152 hb_blob_create_sub_blob (hb_blob_t *parent, |
117 unsigned int offset, | 153 unsigned int offset, |
118 unsigned int length) | 154 unsigned int length) |
119 { | 155 { |
120 hb_blob_t *blob; | 156 hb_blob_t *blob; |
121 | 157 |
122 if (!length || offset >= parent->length) | 158 if (!length || offset >= parent->length) |
123 return hb_blob_get_empty (); | 159 return hb_blob_get_empty (); |
124 | 160 |
125 hb_blob_make_immutable (parent); | 161 hb_blob_make_immutable (parent); |
126 | 162 |
127 blob = hb_blob_create (parent->data + offset, | 163 blob = hb_blob_create (parent->data + offset, |
128 MIN (length, parent->length - offset), | 164 MIN (length, parent->length - offset), |
129 HB_MEMORY_MODE_READONLY, | 165 HB_MEMORY_MODE_READONLY, |
130 hb_blob_reference (parent), | 166 hb_blob_reference (parent), |
131 (hb_destroy_func_t) hb_blob_destroy); | 167 (hb_destroy_func_t) hb_blob_destroy); |
132 | 168 |
133 return blob; | 169 return blob; |
134 } | 170 } |
135 | 171 |
| 172 /** |
| 173 * hb_blob_get_empty: |
| 174 * |
| 175 * Returns the singleton empty blob. |
| 176 * |
| 177 * See TODO:link object types for more information. |
| 178 * |
| 179 * Return value: (transfer full): the empty blob. |
| 180 * |
| 181 * Since: 1.0 |
| 182 **/ |
136 hb_blob_t * | 183 hb_blob_t * |
137 hb_blob_get_empty (void) | 184 hb_blob_get_empty (void) |
138 { | 185 { |
139 static const hb_blob_t _hb_blob_nil = { | 186 static const hb_blob_t _hb_blob_nil = { |
140 HB_OBJECT_HEADER_STATIC, | 187 HB_OBJECT_HEADER_STATIC, |
141 | 188 |
142 true, /* immutable */ | 189 true, /* immutable */ |
143 | 190 |
144 NULL, /* data */ | 191 NULL, /* data */ |
145 0, /* length */ | 192 0, /* length */ |
146 HB_MEMORY_MODE_READONLY, /* mode */ | 193 HB_MEMORY_MODE_READONLY, /* mode */ |
147 | 194 |
148 NULL, /* user_data */ | 195 NULL, /* user_data */ |
149 NULL /* destroy */ | 196 NULL /* destroy */ |
150 }; | 197 }; |
151 | 198 |
152 return const_cast<hb_blob_t *> (&_hb_blob_nil); | 199 return const_cast<hb_blob_t *> (&_hb_blob_nil); |
153 } | 200 } |
154 | 201 |
| 202 /** |
| 203 * hb_blob_reference: (skip) |
| 204 * @blob: a blob. |
| 205 * |
| 206 * Increases the reference count on @blob. |
| 207 * |
| 208 * See TODO:link object types for more information. |
| 209 * |
| 210 * Return value: @blob. |
| 211 * |
| 212 * Since: 1.0 |
| 213 **/ |
155 hb_blob_t * | 214 hb_blob_t * |
156 hb_blob_reference (hb_blob_t *blob) | 215 hb_blob_reference (hb_blob_t *blob) |
157 { | 216 { |
158 return hb_object_reference (blob); | 217 return hb_object_reference (blob); |
159 } | 218 } |
160 | 219 |
| 220 /** |
| 221 * hb_blob_destroy: (skip) |
| 222 * @blob: a blob. |
| 223 * |
| 224 * Descreases the reference count on @blob, and if it reaches zero, destroys |
| 225 * @blob, freeing all memory, possibly calling the destroy-callback the blob |
| 226 * was created for if it has not been called already. |
| 227 * |
| 228 * See TODO:link object types for more information. |
| 229 * |
| 230 * Since: 1.0 |
| 231 **/ |
161 void | 232 void |
162 hb_blob_destroy (hb_blob_t *blob) | 233 hb_blob_destroy (hb_blob_t *blob) |
163 { | 234 { |
164 if (!hb_object_destroy (blob)) return; | 235 if (!hb_object_destroy (blob)) return; |
165 | 236 |
166 _hb_blob_destroy_user_data (blob); | 237 _hb_blob_destroy_user_data (blob); |
167 | 238 |
168 free (blob); | 239 free (blob); |
169 } | 240 } |
170 | 241 |
| 242 /** |
| 243 * hb_blob_set_user_data: (skip) |
| 244 * @blob: a blob. |
| 245 * @key: key for data to set. |
| 246 * @data: data to set. |
| 247 * @destroy: callback to call when @data is not needed anymore. |
| 248 * @replace: whether to replace an existing data with the same key. |
| 249 * |
| 250 * Return value: |
| 251 * |
| 252 * Since: 1.0 |
| 253 **/ |
171 hb_bool_t | 254 hb_bool_t |
172 hb_blob_set_user_data (hb_blob_t *blob, | 255 hb_blob_set_user_data (hb_blob_t *blob, |
173 hb_user_data_key_t *key, | 256 hb_user_data_key_t *key, |
174 void * data, | 257 void * data, |
175 hb_destroy_func_t destroy, | 258 hb_destroy_func_t destroy, |
176 hb_bool_t replace) | 259 hb_bool_t replace) |
177 { | 260 { |
178 return hb_object_set_user_data (blob, key, data, destroy, replace); | 261 return hb_object_set_user_data (blob, key, data, destroy, replace); |
179 } | 262 } |
180 | 263 |
| 264 /** |
| 265 * hb_blob_get_user_data: (skip) |
| 266 * @blob: a blob. |
| 267 * @key: key for data to get. |
| 268 * |
| 269 * |
| 270 * |
| 271 * Return value: (transfer none): |
| 272 * |
| 273 * Since: 1.0 |
| 274 **/ |
181 void * | 275 void * |
182 hb_blob_get_user_data (hb_blob_t *blob, | 276 hb_blob_get_user_data (hb_blob_t *blob, |
183 hb_user_data_key_t *key) | 277 hb_user_data_key_t *key) |
184 { | 278 { |
185 return hb_object_get_user_data (blob, key); | 279 return hb_object_get_user_data (blob, key); |
186 } | 280 } |
187 | 281 |
188 | 282 |
| 283 /** |
| 284 * hb_blob_make_immutable: |
| 285 * @blob: a blob. |
| 286 * |
| 287 * |
| 288 * |
| 289 * Since: 1.0 |
| 290 **/ |
189 void | 291 void |
190 hb_blob_make_immutable (hb_blob_t *blob) | 292 hb_blob_make_immutable (hb_blob_t *blob) |
191 { | 293 { |
192 if (hb_object_is_inert (blob)) | 294 if (hb_object_is_inert (blob)) |
193 return; | 295 return; |
194 | 296 |
195 blob->immutable = true; | 297 blob->immutable = true; |
196 } | 298 } |
197 | 299 |
| 300 /** |
| 301 * hb_blob_is_immutable: |
| 302 * @blob: a blob. |
| 303 * |
| 304 * |
| 305 * |
| 306 * Return value: TODO |
| 307 * |
| 308 * Since: 1.0 |
| 309 **/ |
198 hb_bool_t | 310 hb_bool_t |
199 hb_blob_is_immutable (hb_blob_t *blob) | 311 hb_blob_is_immutable (hb_blob_t *blob) |
200 { | 312 { |
201 return blob->immutable; | 313 return blob->immutable; |
202 } | 314 } |
203 | 315 |
204 | 316 |
| 317 /** |
| 318 * hb_blob_get_length: |
| 319 * @blob: a blob. |
| 320 * |
| 321 * |
| 322 * |
| 323 * Return value: the length of blob data in bytes. |
| 324 * |
| 325 * Since: 1.0 |
| 326 **/ |
205 unsigned int | 327 unsigned int |
206 hb_blob_get_length (hb_blob_t *blob) | 328 hb_blob_get_length (hb_blob_t *blob) |
207 { | 329 { |
208 return blob->length; | 330 return blob->length; |
209 } | 331 } |
210 | 332 |
| 333 /** |
| 334 * hb_blob_get_data: |
| 335 * @blob: a blob. |
| 336 * @length: (out): |
| 337 * |
| 338 * |
| 339 * |
| 340 * Returns: (transfer none) (array length=length): |
| 341 * |
| 342 * Since: 1.0 |
| 343 **/ |
211 const char * | 344 const char * |
212 hb_blob_get_data (hb_blob_t *blob, unsigned int *length) | 345 hb_blob_get_data (hb_blob_t *blob, unsigned int *length) |
213 { | 346 { |
214 if (length) | 347 if (length) |
215 *length = blob->length; | 348 *length = blob->length; |
216 | 349 |
217 return blob->data; | 350 return blob->data; |
218 } | 351 } |
219 | 352 |
| 353 /** |
| 354 * hb_blob_get_data_writable: |
| 355 * @blob: a blob. |
| 356 * @length: (out): output length of the writable data. |
| 357 * |
| 358 * Tries to make blob data writable (possibly copying it) and |
| 359 * return pointer to data. |
| 360 * |
| 361 * Fails if blob has been made immutable, or if memory allocation |
| 362 * fails. |
| 363 * |
| 364 * Returns: (transfer none) (array length=length): Writable blob data, |
| 365 * or %NULL if failed. |
| 366 * |
| 367 * Since: 1.0 |
| 368 **/ |
220 char * | 369 char * |
221 hb_blob_get_data_writable (hb_blob_t *blob, unsigned int *length) | 370 hb_blob_get_data_writable (hb_blob_t *blob, unsigned int *length) |
222 { | 371 { |
223 if (!_try_writable (blob)) { | 372 if (!_try_writable (blob)) { |
224 if (length) | 373 if (length) |
225 *length = 0; | 374 *length = 0; |
226 | 375 |
227 return NULL; | 376 return NULL; |
228 } | 377 } |
229 | 378 |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 | 469 |
321 memcpy (new_data, blob->data, blob->length); | 470 memcpy (new_data, blob->data, blob->length); |
322 _hb_blob_destroy_user_data (blob); | 471 _hb_blob_destroy_user_data (blob); |
323 blob->mode = HB_MEMORY_MODE_WRITABLE; | 472 blob->mode = HB_MEMORY_MODE_WRITABLE; |
324 blob->data = new_data; | 473 blob->data = new_data; |
325 blob->user_data = new_data; | 474 blob->user_data = new_data; |
326 blob->destroy = free; | 475 blob->destroy = free; |
327 | 476 |
328 return true; | 477 return true; |
329 } | 478 } |
330 | |
331 | |
OLD | NEW |