Changeset 5e718d9 in mainline for uspace/lib/bithenge/blob.c


Ignore:
Timestamp:
2012-08-21T10:04:16Z (13 years ago)
Author:
Vojtech Horky <vojtechhorky@…>
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.
Message:

Merge with upstream (lp:~wtachi/helenos/bithenge)

File:
1 moved

Legend:

Unmodified
Added
Removed
  • uspace/lib/bithenge/blob.c

    r0da6c04 r5e718d9  
    5858        assert(ops->size);
    5959
     60        if (bithenge_should_fail())
     61                return ENOMEM;
     62
    6063        blob->base.type = BITHENGE_NODE_BLOB;
    6164        blob->base.refs = 1;
     
    195198{
    196199        memory_blob_t *blob = blob_as_memory(base);
     200        if (bithenge_should_fail())
     201                return EIO;
    197202        *size = blob->size;
    198203        return EOK;
     
    223228        .destroy = memory_destroy,
    224229};
    225 
    226 /** Create a blob node from data. Unlike with @a
    227  * bithenge_blob_t::bithenge_new_blob_from_buffer, the data is copied into a
    228  * new buffer and the original data can be changed after this call. The blob
    229  * must be freed with @a bithenge_node_t::bithenge_node_destroy after it is
    230  * used.
    231  * @memberof bithenge_blob_t
    232  * @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 }
    263230
    264231/** Create a blob node from a buffer. The buffer must exist as long as the blob
     
    280247
    281248        memory_blob_t *blob = malloc(sizeof(*blob));
    282         if (!blob)
    283                 return ENOMEM;
     249        if (!blob) {
     250                rc = ENOMEM;
     251                goto error;
     252        }
    284253        rc = bithenge_init_random_access_blob(memory_as_blob(blob),
    285254            &memory_ops);
    286         if (rc != EOK) {
    287                 free(blob);
    288                 return rc;
    289         }
     255        if (rc != EOK)
     256                goto error;
    290257        blob->buffer = buffer;
    291258        blob->size = len;
     
    293260        *out = bithenge_blob_as_node(memory_as_blob(blob));
    294261        return EOK;
    295 }
     262       
     263error:
     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. */
     280int 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
    296292
    297293typedef struct {
     
    461457/** Check whether the contents of two blobs are equal.
    462458 * @memberof bithenge_blob_t
     459 * @param[out] out Holds whether the blobs are equal.
    463460 * @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 */
     463int bithenge_blob_equal(bool *out, bithenge_blob_t *a, bithenge_blob_t *b)
    467464{
    468465        assert(a);
     
    476473                rc = bithenge_blob_read(a, offset, buffer_a, &size_a);
    477474                if (rc != EOK)
    478                         return false;
     475                        return rc;
    479476                rc = bithenge_blob_read(b, offset, buffer_b, &size_b);
    480477                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                }
    484483                offset += size_a;
    485484        } while (size_a == sizeof(buffer_a));
    486         return true;
     485        *out = true;
     486        return EOK;
    487487}
    488488
Note: See TracChangeset for help on using the changeset viewer.