2012-05-20 22:28:05 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2012-04-25 08:07:49 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdint.h>
|
2012-05-20 22:28:05 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "backed_block.h"
|
2012-04-25 08:07:49 +02:00
|
|
|
|
|
|
|
struct backed_block {
|
|
|
|
unsigned int block;
|
|
|
|
unsigned int len;
|
|
|
|
enum backed_block_type type;
|
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
void *data;
|
|
|
|
} data;
|
|
|
|
struct {
|
|
|
|
char *filename;
|
|
|
|
int64_t offset;
|
|
|
|
} file;
|
|
|
|
struct {
|
|
|
|
uint32_t val;
|
|
|
|
} fill;
|
|
|
|
};
|
|
|
|
struct backed_block *next;
|
2012-05-20 22:28:05 +02:00
|
|
|
};
|
|
|
|
|
2012-04-25 03:51:42 +02:00
|
|
|
struct backed_block_list {
|
2012-04-25 08:07:49 +02:00
|
|
|
struct backed_block *data_blocks;
|
|
|
|
struct backed_block *last_used;
|
2012-04-25 03:51:42 +02:00
|
|
|
};
|
|
|
|
|
2012-04-25 08:07:49 +02:00
|
|
|
struct backed_block *backed_block_iter_new(struct backed_block_list *bbl)
|
|
|
|
{
|
|
|
|
return bbl->data_blocks;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct backed_block *backed_block_iter_next(struct backed_block *bb)
|
|
|
|
{
|
|
|
|
return bb->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int backed_block_len(struct backed_block *bb)
|
|
|
|
{
|
|
|
|
return bb->len;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int backed_block_block(struct backed_block *bb)
|
|
|
|
{
|
|
|
|
return bb->block;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *backed_block_data(struct backed_block *bb)
|
|
|
|
{
|
|
|
|
assert(bb->type == BACKED_BLOCK_DATA);
|
|
|
|
return bb->data.data;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *backed_block_filename(struct backed_block *bb)
|
|
|
|
{
|
|
|
|
assert(bb->type == BACKED_BLOCK_FILE);
|
|
|
|
return bb->file.filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t backed_block_file_offset(struct backed_block *bb)
|
|
|
|
{
|
|
|
|
assert(bb->type == BACKED_BLOCK_FILE);
|
|
|
|
return bb->file.offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t backed_block_fill_val(struct backed_block *bb)
|
|
|
|
{
|
|
|
|
assert(bb->type == BACKED_BLOCK_FILL);
|
|
|
|
return bb->fill.val;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum backed_block_type backed_block_type(struct backed_block *bb)
|
|
|
|
{
|
|
|
|
return bb->type;
|
|
|
|
}
|
|
|
|
|
2012-04-25 03:51:42 +02:00
|
|
|
struct backed_block_list *backed_block_list_new(void)
|
|
|
|
{
|
|
|
|
struct backed_block_list *b = calloc(sizeof(struct backed_block_list), 1);
|
|
|
|
|
|
|
|
return b;
|
|
|
|
}
|
2012-05-20 22:28:05 +02:00
|
|
|
|
2012-04-25 08:07:49 +02:00
|
|
|
void backed_block_list_destroy(struct backed_block_list *bbl)
|
2012-04-25 03:51:42 +02:00
|
|
|
{
|
2012-04-25 08:07:49 +02:00
|
|
|
if (bbl->data_blocks) {
|
|
|
|
struct backed_block *bb = bbl->data_blocks;
|
|
|
|
while (bb) {
|
|
|
|
struct backed_block *next = bb->next;
|
|
|
|
if (bb->type == BACKED_BLOCK_FILE) {
|
|
|
|
free(bb->file.filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(bb);
|
|
|
|
bb = next;
|
2012-04-25 03:51:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-25 08:07:49 +02:00
|
|
|
free(bbl);
|
2012-04-25 03:51:42 +02:00
|
|
|
}
|
|
|
|
|
2012-04-25 08:07:49 +02:00
|
|
|
static int queue_bb(struct backed_block_list *bbl, struct backed_block *new_bb)
|
2012-05-20 22:28:05 +02:00
|
|
|
{
|
2012-04-25 08:07:49 +02:00
|
|
|
struct backed_block *bb;
|
2012-05-20 22:28:05 +02:00
|
|
|
|
2012-04-25 08:07:49 +02:00
|
|
|
if (bbl->data_blocks == NULL) {
|
|
|
|
bbl->data_blocks = new_bb;
|
|
|
|
return 0;
|
2012-05-20 22:28:05 +02:00
|
|
|
}
|
|
|
|
|
2012-04-25 08:07:49 +02:00
|
|
|
if (bbl->data_blocks->block > new_bb->block) {
|
|
|
|
new_bb->next = bbl->data_blocks;
|
|
|
|
bbl->data_blocks = new_bb;
|
|
|
|
return 0;
|
2012-05-20 22:28:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Optimization: blocks are mostly queued in sequence, so save the
|
2012-04-25 08:07:49 +02:00
|
|
|
pointer to the last bb that was added, and start searching from
|
2012-05-20 22:28:05 +02:00
|
|
|
there if the next block number is higher */
|
2012-04-25 08:07:49 +02:00
|
|
|
if (bbl->last_used && new_bb->block > bbl->last_used->block)
|
|
|
|
bb = bbl->last_used;
|
2012-05-20 22:28:05 +02:00
|
|
|
else
|
2012-04-25 08:07:49 +02:00
|
|
|
bb = bbl->data_blocks;
|
|
|
|
bbl->last_used = new_bb;
|
2012-05-20 22:28:05 +02:00
|
|
|
|
2012-04-25 08:07:49 +02:00
|
|
|
for (; bb->next && bb->next->block < new_bb->block; bb = bb->next)
|
2012-05-20 22:28:05 +02:00
|
|
|
;
|
|
|
|
|
2012-04-25 08:07:49 +02:00
|
|
|
if (bb->next == NULL) {
|
|
|
|
bb->next = new_bb;
|
2012-05-20 22:28:05 +02:00
|
|
|
} else {
|
2012-04-25 08:07:49 +02:00
|
|
|
new_bb->next = bb->next;
|
|
|
|
bb->next = new_bb;
|
2012-05-20 22:28:05 +02:00
|
|
|
}
|
2012-04-25 08:07:49 +02:00
|
|
|
|
|
|
|
return 0;
|
2012-05-20 22:28:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Queues a fill block of memory to be written to the specified data blocks */
|
2012-04-25 08:07:49 +02:00
|
|
|
int backed_block_add_fill(struct backed_block_list *bbl, unsigned int fill_val,
|
2012-04-25 03:51:42 +02:00
|
|
|
unsigned int len, unsigned int block)
|
2012-05-20 22:28:05 +02:00
|
|
|
{
|
2012-04-25 08:07:49 +02:00
|
|
|
struct backed_block *bb = calloc(1, sizeof(struct backed_block));
|
|
|
|
if (bb == NULL) {
|
|
|
|
return -ENOMEM;
|
2012-05-20 22:28:05 +02:00
|
|
|
}
|
|
|
|
|
2012-04-25 08:07:49 +02:00
|
|
|
bb->block = block;
|
|
|
|
bb->len = len;
|
|
|
|
bb->type = BACKED_BLOCK_FILL;
|
|
|
|
bb->fill.val = fill_val;
|
|
|
|
bb->next = NULL;
|
2012-05-20 22:28:05 +02:00
|
|
|
|
2012-04-25 08:07:49 +02:00
|
|
|
return queue_bb(bbl, bb);
|
2012-05-20 22:28:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Queues a block of memory to be written to the specified data blocks */
|
2012-04-25 08:07:49 +02:00
|
|
|
int backed_block_add_data(struct backed_block_list *bbl, void *data,
|
|
|
|
unsigned int len, unsigned int block)
|
2012-05-20 22:28:05 +02:00
|
|
|
{
|
2012-04-25 08:07:49 +02:00
|
|
|
struct backed_block *bb = calloc(1, sizeof(struct backed_block));
|
|
|
|
if (bb == NULL) {
|
|
|
|
return -ENOMEM;
|
2012-05-20 22:28:05 +02:00
|
|
|
}
|
|
|
|
|
2012-04-25 08:07:49 +02:00
|
|
|
bb->block = block;
|
|
|
|
bb->len = len;
|
|
|
|
bb->type = BACKED_BLOCK_DATA;
|
|
|
|
bb->data.data = data;
|
|
|
|
bb->next = NULL;
|
2012-05-20 22:28:05 +02:00
|
|
|
|
2012-04-25 08:07:49 +02:00
|
|
|
return queue_bb(bbl, bb);
|
2012-05-20 22:28:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Queues a chunk of a file on disk to be written to the specified data blocks */
|
2012-04-25 08:07:49 +02:00
|
|
|
int backed_block_add_file(struct backed_block_list *bbl, const char *filename,
|
2012-04-25 03:51:42 +02:00
|
|
|
int64_t offset, unsigned int len, unsigned int block)
|
2012-05-20 22:28:05 +02:00
|
|
|
{
|
2012-04-25 08:07:49 +02:00
|
|
|
struct backed_block *bb = calloc(1, sizeof(struct backed_block));
|
|
|
|
if (bb == NULL) {
|
|
|
|
return -ENOMEM;
|
2012-05-20 22:28:05 +02:00
|
|
|
}
|
|
|
|
|
2012-04-25 08:07:49 +02:00
|
|
|
bb->block = block;
|
|
|
|
bb->len = len;
|
|
|
|
bb->type = BACKED_BLOCK_FILE;
|
|
|
|
bb->file.filename = strdup(filename);
|
|
|
|
bb->file.offset = offset;
|
|
|
|
bb->next = NULL;
|
2012-05-20 22:28:05 +02:00
|
|
|
|
2012-04-25 08:07:49 +02:00
|
|
|
return queue_bb(bbl, bb);
|
2012-05-20 22:28:05 +02:00
|
|
|
}
|