Changeset 743ce51 in mainline
- Timestamp:
- 2012-05-25T03:49:21Z (12 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 1923501
- Parents:
- a54bd98
- Location:
- uspace/app/bithenge
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/bithenge/Makefile
ra54bd98 r743ce51 1 1 # 2 # Copyright (c) 201 1 Vojtech Horky2 # Copyright (c) 2012 Sean Bartell 3 3 # All rights reserved. 4 4 # -
uspace/app/bithenge/blob.c
ra54bd98 r743ce51 35 35 */ 36 36 37 #include <assert.h> 37 38 #include <bool.h> 38 39 #include <errno.h> … … 45 46 * @memberof bithenge_blob_t 46 47 * @param[out] blob The blob to initialize. 47 * @param[in] ops Operations provid edrandom access support. This pointer must48 * @param[in] ops Operations providing random access support. This pointer must 48 49 * be valid until the blob is destroyed. 49 50 * @return EOK on success or an error code from errno.h. 50 51 */ 51 int bithenge_new_random_access_blob( 52 bithenge_blob_t *blob, 53 const bithenge_random_access_blob_ops_t *ops) { 52 int 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 54 61 blob->ops = ops; 55 62 return EOK; 56 63 } 57 64 58 static int sequential_buffer(bithenge_sequential_blob_t *blob, aoff64_t end) { 65 static int sequential_buffer(bithenge_sequential_blob_t *blob, aoff64_t end) 66 { 59 67 bool need_realloc = false; 60 68 while (end > blob->buffer_size) { … … 65 73 char *buffer = realloc(blob->buffer, blob->buffer_size); 66 74 if (!buffer) 67 return errno;75 return ENOMEM; 68 76 blob->buffer = buffer; 69 77 } 70 size_t size = end - blob->data_size;78 aoff64_t size = end - blob->data_size; 71 79 int rc = blob->ops->read(blob, blob->buffer + blob->data_size, &size); 72 80 if (rc != EOK) … … 76 84 } 77 85 78 static int sequential_size(bithenge_blob_t *base, aoff64_t *size) { 86 static int sequential_size(bithenge_blob_t *base, aoff64_t *size) 87 { 79 88 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); 83 100 if (rc != EOK) 84 101 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;91 102 } 103 *size = blob->data_size; 92 104 return EOK; 93 105 } 94 106 95 static int sequential_read(bithenge_blob_t *base, aoff64_t offset, char *buffer, aoff64_t *size) { 107 static int sequential_read(bithenge_blob_t *base, aoff64_t offset, 108 char *buffer, aoff64_t *size) 109 { 96 110 bithenge_sequential_blob_t *blob = (bithenge_sequential_blob_t *)base; 97 111 aoff64_t end = offset + *size; … … 108 122 } 109 123 110 static int sequential_destroy(bithenge_blob_t *base) { 124 static int sequential_destroy(bithenge_blob_t *base) 125 { 111 126 bithenge_sequential_blob_t *blob = (bithenge_sequential_blob_t *)base; 112 127 free(blob->buffer); … … 127 142 * @return EOK on success or an error code from errno.h. 128 143 */ 129 int bithenge_new_sequential_blob( 130 bithenge_sequential_blob_t *blob, 131 const bithenge_sequential_blob_ops_t *ops) { 144 int 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 132 153 int rc = bithenge_new_random_access_blob(&blob->base, &sequential_ops); 133 154 if (rc != EOK) -
uspace/app/bithenge/blob.h
ra54bd98 r743ce51 78 78 * @memberof bithenge_sequential_blob_t */ 79 79 typedef 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 */ 80 90 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 */ 81 104 int (*read)(bithenge_sequential_blob_t *blob, char *buffer, 82 105 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. */ 83 110 int (*destroy)(bithenge_sequential_blob_t *blob); 84 111 } bithenge_sequential_blob_ops_t; … … 91 118 * @return EOK on success or an error code from errno.h. 92 119 */ 93 static inline int bithenge_blob_size(bithenge_blob_t *blob, aoff64_t *size) { 120 static inline int bithenge_blob_size(bithenge_blob_t *blob, aoff64_t *size) 121 { 122 assert(blob); 123 assert(blob->ops); 94 124 return blob->ops->size(blob, size); 95 125 } … … 110 140 * @return EOK on success or an error code from errno.h. 111 141 */ 112 static inline int bithenge_blob_read(bithenge_blob_t *blob, aoff64_t offset, char *buffer, aoff64_t *size) { 142 static 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); 113 147 return blob->ops->read(blob, offset, buffer, size); 114 148 } … … 119 153 * @return EOK on success or an error code from errno.h. 120 154 */ 121 static inline int bithenge_blob_destroy(bithenge_blob_t *blob) { 155 static inline int bithenge_blob_destroy(bithenge_blob_t *blob) 156 { 157 assert(blob); 158 assert(blob->ops); 122 159 return blob->ops->destroy(blob); 123 160 } -
uspace/app/bithenge/block.c
ra54bd98 r743ce51 36 36 */ 37 37 38 #include <assert.h> 38 39 #include <errno.h> 39 40 #include <libblock.h> … … 50 51 } block_blob_t; 51 52 52 static int block_size(bithenge_blob_t *base, aoff64_t *size) { 53 static int block_size(bithenge_blob_t *base, aoff64_t *size) 54 { 53 55 block_blob_t *blob = (block_blob_t *)base; 54 56 *size = blob->size; … … 56 58 } 57 59 58 static int block_read(bithenge_blob_t *base, aoff64_t offset, char *buffer, aoff64_t *size) { 60 static int block_read(bithenge_blob_t *base, aoff64_t offset, char *buffer, 61 aoff64_t *size) 62 { 59 63 block_blob_t *blob = (block_blob_t *)base; 60 64 if (offset > blob->size) … … 64 68 } 65 69 66 static int block_destroy(bithenge_blob_t *base) { 70 static int block_destroy(bithenge_blob_t *base) 71 { 67 72 block_blob_t *blob = (block_blob_t *)base; 68 73 block_fini(blob->service_id); … … 82 87 * @param service_id The service ID of the block device. 83 88 * @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) { 89 int bithenge_new_block_blob(bithenge_blob_t **out, service_id_t service_id) 90 { 91 assert(out); 92 85 93 // Initialize libblock 86 94 int rc; … … 104 112 block_blob_t *blob = malloc(sizeof(*blob)); 105 113 if (!blob) 106 return errno;114 return ENOMEM; 107 115 rc = bithenge_new_random_access_blob(&blob->base, &block_ops); 108 116 if (rc != EOK) { -
uspace/app/bithenge/block.h
ra54bd98 r743ce51 38 38 #define BITHENGE_BLOCK_H_ 39 39 40 #include <loc.h> 40 41 #include "blob.h" 41 #include "loc.h"42 42 43 43 int bithenge_new_block_blob(bithenge_blob_t **, service_id_t); -
uspace/app/bithenge/test.c
ra54bd98 r743ce51 42 42 43 43 static void 44 print_data(const char *data, size_t len) { 44 print_data(const char *data, size_t len) 45 { 45 46 while (len--) 46 47 printf("%02x ", (uint8_t)(*data++)); … … 49 50 50 51 static void 51 print_blob(bithenge_blob_t *blob) { 52 print_blob(bithenge_blob_t *blob) 53 { 52 54 aoff64_t size; 53 55 bithenge_blob_size(blob, &size); … … 59 61 } 60 62 61 int main(int argc, char *argv[]) { 63 int main(int argc, char *argv[]) 64 { 62 65 service_id_t service_id; 63 66 loc_service_get_id("bd/initrd", &service_id, 0);
Note:
See TracChangeset
for help on using the changeset viewer.