Changeset 5e718d9 in mainline for uspace/lib/bithenge/blob.c
- Timestamp:
- 2012-08-21T10:04:16Z (12 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 67edca6
- Parents:
- 0da6c04 (diff), 6a97f2e (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/bithenge/blob.c
r0da6c04 r5e718d9 58 58 assert(ops->size); 59 59 60 if (bithenge_should_fail()) 61 return ENOMEM; 62 60 63 blob->base.type = BITHENGE_NODE_BLOB; 61 64 blob->base.refs = 1; … … 195 198 { 196 199 memory_blob_t *blob = blob_as_memory(base); 200 if (bithenge_should_fail()) 201 return EIO; 197 202 *size = blob->size; 198 203 return EOK; … … 223 228 .destroy = memory_destroy, 224 229 }; 225 226 /** Create a blob node from data. Unlike with @a227 * bithenge_blob_t::bithenge_new_blob_from_buffer, the data is copied into a228 * new buffer and the original data can be changed after this call. The blob229 * must be freed with @a bithenge_node_t::bithenge_node_destroy after it is230 * used.231 * @memberof bithenge_blob_t232 * @param[out] out Stores the created blob node.233 * @param[in] data The data.234 * @param len The length of the data.235 * @return EOK on success or an error code from errno.h. */236 int bithenge_new_blob_from_data(bithenge_node_t **out, const void *data,237 size_t len)238 {239 int rc;240 assert(data || !len);241 242 memory_blob_t *blob = malloc(sizeof(*blob));243 if (!blob)244 return ENOMEM;245 rc = bithenge_init_random_access_blob(memory_as_blob(blob),246 &memory_ops);247 if (rc != EOK) {248 free(blob);249 return rc;250 }251 char *buffer = malloc(len);252 if (!buffer) {253 free(blob);254 return rc;255 }256 memcpy(buffer, data, len);257 blob->buffer = buffer;258 blob->size = len;259 blob->needs_free = true;260 *out = bithenge_blob_as_node(memory_as_blob(blob));261 return EOK;262 }263 230 264 231 /** Create a blob node from a buffer. The buffer must exist as long as the blob … … 280 247 281 248 memory_blob_t *blob = malloc(sizeof(*blob)); 282 if (!blob) 283 return ENOMEM; 249 if (!blob) { 250 rc = ENOMEM; 251 goto error; 252 } 284 253 rc = bithenge_init_random_access_blob(memory_as_blob(blob), 285 254 &memory_ops); 286 if (rc != EOK) { 287 free(blob); 288 return rc; 289 } 255 if (rc != EOK) 256 goto error; 290 257 blob->buffer = buffer; 291 258 blob->size = len; … … 293 260 *out = bithenge_blob_as_node(memory_as_blob(blob)); 294 261 return EOK; 295 } 262 263 error: 264 if (needs_free) 265 free((void *)buffer); 266 free(blob); 267 return rc; 268 } 269 270 /** Create a blob node from data. Unlike with @a 271 * bithenge_blob_t::bithenge_new_blob_from_buffer, the data is copied into a 272 * new buffer and the original data can be changed after this call. The blob 273 * must be freed with @a bithenge_node_t::bithenge_node_destroy after it is 274 * used. 275 * @memberof bithenge_blob_t 276 * @param[out] out Stores the created blob node. 277 * @param[in] data The data. 278 * @param len The length of the data. 279 * @return EOK on success or an error code from errno.h. */ 280 int bithenge_new_blob_from_data(bithenge_node_t **out, const void *data, 281 size_t len) 282 { 283 char *buffer = malloc(len); 284 if (!buffer) 285 return ENOMEM; 286 memcpy(buffer, data, len); 287 288 return bithenge_new_blob_from_buffer(out, buffer, len, true); 289 } 290 291 296 292 297 293 typedef struct { … … 461 457 /** Check whether the contents of two blobs are equal. 462 458 * @memberof bithenge_blob_t 459 * @param[out] out Holds whether the blobs are equal. 463 460 * @param a, b Blobs to compare. 464 * @return Whether the blobs are equal. If an error occurs, returns false.465 */ 466 bool bithenge_blob_equal(bithenge_blob_t *a, bithenge_blob_t *b)461 * @return EOK on success, or an error code from errno.h. 462 */ 463 int bithenge_blob_equal(bool *out, bithenge_blob_t *a, bithenge_blob_t *b) 467 464 { 468 465 assert(a); … … 476 473 rc = bithenge_blob_read(a, offset, buffer_a, &size_a); 477 474 if (rc != EOK) 478 return false;475 return rc; 479 476 rc = bithenge_blob_read(b, offset, buffer_b, &size_b); 480 477 if (rc != EOK) 481 return false; 482 if (size_a != size_b || bcmp(buffer_a, buffer_b, size_a)) 483 return false; 478 return rc; 479 if (size_a != size_b || bcmp(buffer_a, buffer_b, size_a)) { 480 *out = false; 481 return EOK; 482 } 484 483 offset += size_a; 485 484 } while (size_a == sizeof(buffer_a)); 486 return true; 485 *out = true; 486 return EOK; 487 487 } 488 488
Note:
See TracChangeset
for help on using the changeset viewer.