Changeset 743ce51 in mainline for uspace/app/bithenge/blob.c
- Timestamp:
- 2012-05-25T03:49:21Z (13 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 1923501
- Parents:
- a54bd98
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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)
Note:
See TracChangeset
for help on using the changeset viewer.