Fork us on GitHub Follow us on Facebook Follow us on Twitter

Changeset 743ce51 in mainline


Ignore:
Timestamp:
2012-05-25T03:49:21Z (9 years ago)
Author:
Sean Bartell <wingedtachikoma@…>
Branches:
master
Children:
1923501
Parents:
a54bd98
Message:

Bithenge: various fixes and style.

Location:
uspace/app/bithenge
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/bithenge/Makefile

    ra54bd98 r743ce51  
    11#
    2 # Copyright (c) 2011 Vojtech Horky
     2# Copyright (c) 2012 Sean Bartell
    33# All rights reserved.
    44#
  • 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)
  • uspace/app/bithenge/blob.h

    ra54bd98 r743ce51  
    7878 * @memberof bithenge_sequential_blob_t */
    7979typedef struct bithenge_sequential_blob_ops_t {
     80
     81        /** Get the total size of the blob. If the total size cannot be
     82         * determined easily, this field may be null or return an error,
     83         * forcing the entire blob to be read to determine its size.
     84         *
     85         * @memberof bithenge_blob_t
     86         * @param blob The blob.
     87         * @param[out] size Total size of the blob.
     88         * @return EOK on success or an error code from errno.h.
     89         */
    8090        int (*size)(bithenge_sequential_blob_t *blob, aoff64_t *size);
     91
     92        /** Read the next part of the blob. If the requested data extends
     93         * beyond the end of the blob, the data up until the end of the blob
     94         * will be read.
     95         *
     96         * @param blob The blob.
     97         * @param[out] buffer Buffer to read into. If an error occurs, the contents are
     98         * undefined.
     99         * @param[in,out] size Number of bytes to read; may be 0. If not enough
     100         * data is left in the blob, the actual number of bytes read should be
     101         * stored here. If an error occurs, the contents are undefined.
     102         * @return EOK on success or an error code from errno.h.
     103         */
    81104        int (*read)(bithenge_sequential_blob_t *blob, char *buffer,
    82105            aoff64_t *size);
     106
     107        /** Destroy the blob.
     108         * @param blob The blob.
     109         * @return EOK on success or an error code from errno.h. */
    83110        int (*destroy)(bithenge_sequential_blob_t *blob);
    84111} bithenge_sequential_blob_ops_t;
     
    91118 * @return EOK on success or an error code from errno.h.
    92119 */
    93 static inline int bithenge_blob_size(bithenge_blob_t *blob, aoff64_t *size) {
     120static inline int bithenge_blob_size(bithenge_blob_t *blob, aoff64_t *size)
     121{
     122        assert(blob);
     123        assert(blob->ops);
    94124        return blob->ops->size(blob, size);
    95125}
     
    110140 * @return EOK on success or an error code from errno.h.
    111141 */
    112 static inline int bithenge_blob_read(bithenge_blob_t *blob, aoff64_t offset, char *buffer, aoff64_t *size) {
     142static inline int bithenge_blob_read(bithenge_blob_t *blob, aoff64_t offset,
     143    char *buffer, aoff64_t *size)
     144{
     145        assert(blob);
     146        assert(blob->ops);
    113147        return blob->ops->read(blob, offset, buffer, size);
    114148}
     
    119153 * @return EOK on success or an error code from errno.h.
    120154 */
    121 static inline int bithenge_blob_destroy(bithenge_blob_t *blob) {
     155static inline int bithenge_blob_destroy(bithenge_blob_t *blob)
     156{
     157        assert(blob);
     158        assert(blob->ops);
    122159        return blob->ops->destroy(blob);
    123160}
  • uspace/app/bithenge/block.c

    ra54bd98 r743ce51  
    3636 */
    3737
     38#include <assert.h>
    3839#include <errno.h>
    3940#include <libblock.h>
     
    5051} block_blob_t;
    5152
    52 static int block_size(bithenge_blob_t *base, aoff64_t *size) {
     53static int block_size(bithenge_blob_t *base, aoff64_t *size)
     54{
    5355        block_blob_t *blob = (block_blob_t *)base;
    5456        *size = blob->size;
     
    5658}
    5759
    58 static int block_read(bithenge_blob_t *base, aoff64_t offset, char *buffer, aoff64_t *size) {
     60static int block_read(bithenge_blob_t *base, aoff64_t offset, char *buffer,
     61    aoff64_t *size)
     62{
    5963        block_blob_t *blob = (block_blob_t *)base;
    6064        if (offset > blob->size)
     
    6468}
    6569
    66 static int block_destroy(bithenge_blob_t *base) {
     70static int block_destroy(bithenge_blob_t *base)
     71{
    6772        block_blob_t *blob = (block_blob_t *)base;
    6873        block_fini(blob->service_id);
     
    8287 * @param service_id The service ID of the block device.
    8388 * @return EOK on success or an error code from errno.h. */
    84 int bithenge_new_block_blob(bithenge_blob_t **out, service_id_t service_id) {
     89int bithenge_new_block_blob(bithenge_blob_t **out, service_id_t service_id)
     90{
     91        assert(out);
     92
    8593        // Initialize libblock
    8694        int rc;
     
    104112        block_blob_t *blob = malloc(sizeof(*blob));
    105113        if (!blob)
    106                 return errno;
     114                return ENOMEM;
    107115        rc = bithenge_new_random_access_blob(&blob->base, &block_ops);
    108116        if (rc != EOK) {
  • uspace/app/bithenge/block.h

    ra54bd98 r743ce51  
    3838#define BITHENGE_BLOCK_H_
    3939
     40#include <loc.h>
    4041#include "blob.h"
    41 #include "loc.h"
    4242
    4343int bithenge_new_block_blob(bithenge_blob_t **, service_id_t);
  • uspace/app/bithenge/test.c

    ra54bd98 r743ce51  
    4242
    4343static void
    44 print_data(const char *data, size_t len) {
     44print_data(const char *data, size_t len)
     45{
    4546        while (len--)
    4647                printf("%02x ", (uint8_t)(*data++));
     
    4950
    5051static void
    51 print_blob(bithenge_blob_t *blob) {
     52print_blob(bithenge_blob_t *blob)
     53{
    5254        aoff64_t size;
    5355        bithenge_blob_size(blob, &size);
     
    5961}
    6062
    61 int main(int argc, char *argv[]) {
     63int main(int argc, char *argv[])
     64{
    6265        service_id_t service_id;
    6366        loc_service_get_id("bd/initrd", &service_id, 0);
Note: See TracChangeset for help on using the changeset viewer.