Changeset 743ce51 in mainline for uspace/app/bithenge/blob.c


Ignore:
Timestamp:
2012-05-25T03:49:21Z (13 years ago)
Author:
Sean Bartell <wingedtachikoma@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
1923501
Parents:
a54bd98
Message:

Bithenge: various fixes and style.

File:
1 edited

Legend:

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

    ra54bd98 r743ce51  
    3535 */
    3636
     37#include <assert.h>
    3738#include <bool.h>
    3839#include <errno.h>
     
    4546 * @memberof bithenge_blob_t
    4647 * @param[out] blob The blob to initialize.
    47  * @param[in] ops Operations provided random access support. This pointer must
     48 * @param[in] ops Operations providing random access support. This pointer must
    4849 * be valid until the blob is destroyed.
    4950 * @return EOK on success or an error code from errno.h.
    5051 */
    51 int bithenge_new_random_access_blob(
    52                 bithenge_blob_t *blob,
    53                 const bithenge_random_access_blob_ops_t *ops) {
     52int bithenge_new_random_access_blob(bithenge_blob_t *blob,
     53    const bithenge_random_access_blob_ops_t *ops)
     54{
     55        assert(blob);
     56        assert(ops);
     57        assert(ops->destroy);
     58        assert(ops->read);
     59        assert(ops->size);
     60
    5461        blob->ops = ops;
    5562        return EOK;
    5663}
    5764
    58 static int sequential_buffer(bithenge_sequential_blob_t *blob, aoff64_t end) {
     65static int sequential_buffer(bithenge_sequential_blob_t *blob, aoff64_t end)
     66{
    5967        bool need_realloc = false;
    6068        while (end > blob->buffer_size) {
     
    6573                char *buffer = realloc(blob->buffer, blob->buffer_size);
    6674                if (!buffer)
    67                         return errno;
     75                        return ENOMEM;
    6876                blob->buffer = buffer;
    6977        }
    70         size_t size = end - blob->data_size;
     78        aoff64_t size = end - blob->data_size;
    7179        int rc = blob->ops->read(blob, blob->buffer + blob->data_size, &size);
    7280        if (rc != EOK)
     
    7684}
    7785
    78 static int sequential_size(bithenge_blob_t *base, aoff64_t *size) {
     86static int sequential_size(bithenge_blob_t *base, aoff64_t *size)
     87{
    7988        bithenge_sequential_blob_t *blob = (bithenge_sequential_blob_t *)base;
    80         int rc = blob->ops->size(blob, size);
    81         if (rc != EOK) {
    82                 rc = sequential_buffer(blob, blob->buffer_size);
     89        int rc;
     90        if (blob->ops->size) {
     91                rc = blob->ops->size(blob, size);
     92                if (rc == EOK)
     93                        return EOK;
     94        }
     95        rc = sequential_buffer(blob, blob->buffer_size);
     96        if (rc != EOK)
     97                return rc;
     98        while (blob->data_size == blob->buffer_size) {
     99                rc = sequential_buffer(blob, 2 * blob->buffer_size);
    83100                if (rc != EOK)
    84101                        return rc;
    85                 while (blob->data_size == blob->buffer_size) {
    86                         rc = sequential_buffer(blob, 2 * blob->buffer_size);
    87                         if (rc != EOK)
    88                                 return rc;
    89                 }
    90                 *size = blob->data_size;
    91102        }
     103        *size = blob->data_size;
    92104        return EOK;
    93105}
    94106
    95 static int sequential_read(bithenge_blob_t *base, aoff64_t offset, char *buffer, aoff64_t *size) {
     107static int sequential_read(bithenge_blob_t *base, aoff64_t offset,
     108    char *buffer, aoff64_t *size)
     109{
    96110        bithenge_sequential_blob_t *blob = (bithenge_sequential_blob_t *)base;
    97111        aoff64_t end = offset + *size;
     
    108122}
    109123
    110 static int sequential_destroy(bithenge_blob_t *base) {
     124static int sequential_destroy(bithenge_blob_t *base)
     125{
    111126        bithenge_sequential_blob_t *blob = (bithenge_sequential_blob_t *)base;
    112127        free(blob->buffer);
     
    127142 * @return EOK on success or an error code from errno.h.
    128143 */
    129 int bithenge_new_sequential_blob(
    130                 bithenge_sequential_blob_t *blob,
    131                 const bithenge_sequential_blob_ops_t *ops) {
     144int bithenge_new_sequential_blob(bithenge_sequential_blob_t *blob,
     145    const bithenge_sequential_blob_ops_t *ops)
     146{
     147        assert(blob);
     148        assert(ops);
     149        assert(ops->destroy);
     150        assert(ops->read);
     151        // ops->size is optional
     152
    132153        int rc = bithenge_new_random_access_blob(&blob->base, &sequential_ops);
    133154        if (rc != EOK)
Note: See TracChangeset for help on using the changeset viewer.