[libsparse] Modernize codebase by replacing NULL with nullptr

Fixes -Wzero-as-null-pointer-constant warning.

Test: m
Bug: 68236239
Change-Id: I43dae734817cae7a260ffc7afcd85fbd4451eddf
This commit is contained in:
Yi Kong 2018-07-23 16:31:11 -07:00
parent 7e7cefa2c7
commit 17ba95ed9e
5 changed files with 47 additions and 47 deletions

View file

@ -133,7 +133,7 @@ void backed_block_list_move(struct backed_block_list* from, struct backed_block_
struct backed_block* start, struct backed_block* end) {
struct backed_block* bb;
if (start == NULL) {
if (start == nullptr) {
start = from->data_blocks;
}
@ -142,12 +142,12 @@ void backed_block_list_move(struct backed_block_list* from, struct backed_block_
;
}
if (start == NULL || end == NULL) {
if (start == nullptr || end == nullptr) {
return;
}
from->last_used = NULL;
to->last_used = NULL;
from->last_used = nullptr;
to->last_used = nullptr;
if (from->data_blocks == start) {
from->data_blocks = end->next;
} else {
@ -161,7 +161,7 @@ void backed_block_list_move(struct backed_block_list* from, struct backed_block_
if (!to->data_blocks) {
to->data_blocks = start;
end->next = NULL;
end->next = nullptr;
} else {
for (bb = to->data_blocks; bb; bb = bb->next) {
if (!bb->next || bb->next->block > start->block) {
@ -230,7 +230,7 @@ static int merge_bb(struct backed_block_list* bbl, struct backed_block* a, struc
static int queue_bb(struct backed_block_list* bbl, struct backed_block* new_bb) {
struct backed_block* bb;
if (bbl->data_blocks == NULL) {
if (bbl->data_blocks == nullptr) {
bbl->data_blocks = new_bb;
return 0;
}
@ -253,7 +253,7 @@ static int queue_bb(struct backed_block_list* bbl, struct backed_block* new_bb)
for (; bb->next && bb->next->block < new_bb->block; bb = bb->next)
;
if (bb->next == NULL) {
if (bb->next == nullptr) {
bb->next = new_bb;
} else {
new_bb->next = bb->next;
@ -273,7 +273,7 @@ static int queue_bb(struct backed_block_list* bbl, struct backed_block* new_bb)
int backed_block_add_fill(struct backed_block_list* bbl, unsigned int fill_val, unsigned int len,
unsigned int block) {
struct backed_block* bb = reinterpret_cast<backed_block*>(calloc(1, sizeof(struct backed_block)));
if (bb == NULL) {
if (bb == nullptr) {
return -ENOMEM;
}
@ -281,7 +281,7 @@ int backed_block_add_fill(struct backed_block_list* bbl, unsigned int fill_val,
bb->len = len;
bb->type = BACKED_BLOCK_FILL;
bb->fill.val = fill_val;
bb->next = NULL;
bb->next = nullptr;
return queue_bb(bbl, bb);
}
@ -290,7 +290,7 @@ int backed_block_add_fill(struct backed_block_list* bbl, unsigned int fill_val,
int backed_block_add_data(struct backed_block_list* bbl, void* data, unsigned int len,
unsigned int block) {
struct backed_block* bb = reinterpret_cast<backed_block*>(calloc(1, sizeof(struct backed_block)));
if (bb == NULL) {
if (bb == nullptr) {
return -ENOMEM;
}
@ -298,7 +298,7 @@ int backed_block_add_data(struct backed_block_list* bbl, void* data, unsigned in
bb->len = len;
bb->type = BACKED_BLOCK_DATA;
bb->data.data = data;
bb->next = NULL;
bb->next = nullptr;
return queue_bb(bbl, bb);
}
@ -307,7 +307,7 @@ int backed_block_add_data(struct backed_block_list* bbl, void* data, unsigned in
int backed_block_add_file(struct backed_block_list* bbl, const char* filename, int64_t offset,
unsigned int len, unsigned int block) {
struct backed_block* bb = reinterpret_cast<backed_block*>(calloc(1, sizeof(struct backed_block)));
if (bb == NULL) {
if (bb == nullptr) {
return -ENOMEM;
}
@ -316,7 +316,7 @@ int backed_block_add_file(struct backed_block_list* bbl, const char* filename, i
bb->type = BACKED_BLOCK_FILE;
bb->file.filename = strdup(filename);
bb->file.offset = offset;
bb->next = NULL;
bb->next = nullptr;
return queue_bb(bbl, bb);
}
@ -325,7 +325,7 @@ int backed_block_add_file(struct backed_block_list* bbl, const char* filename, i
int backed_block_add_fd(struct backed_block_list* bbl, int fd, int64_t offset, unsigned int len,
unsigned int block) {
struct backed_block* bb = reinterpret_cast<backed_block*>(calloc(1, sizeof(struct backed_block)));
if (bb == NULL) {
if (bb == nullptr) {
return -ENOMEM;
}
@ -334,7 +334,7 @@ int backed_block_add_fd(struct backed_block_list* bbl, int fd, int64_t offset, u
bb->type = BACKED_BLOCK_FD;
bb->fd.fd = fd;
bb->fd.offset = offset;
bb->next = NULL;
bb->next = nullptr;
return queue_bb(bbl, bb);
}
@ -350,7 +350,7 @@ int backed_block_split(struct backed_block_list* bbl, struct backed_block* bb,
}
new_bb = reinterpret_cast<backed_block*>(malloc(sizeof(struct backed_block)));
if (new_bb == NULL) {
if (new_bb == nullptr) {
return -ENOMEM;
}

View file

@ -233,7 +233,7 @@ static int gz_file_write(struct output_file* out, void* data, size_t len) {
while (len > 0) {
ret = gzwrite(outgz->gz_fd, data, min(len, (unsigned int)INT_MAX));
if (ret == 0) {
error("gzwrite %s", gzerror(outgz->gz_fd, NULL));
error("gzwrite %s", gzerror(outgz->gz_fd, nullptr));
return -1;
}
len -= ret;
@ -269,7 +269,7 @@ static int callback_file_skip(struct output_file* out, int64_t off) {
while (off > 0) {
to_write = min(off, (int64_t)INT_MAX);
ret = outc->write(outc->priv, NULL, to_write);
ret = outc->write(outc->priv, nullptr, to_write);
if (ret < 0) {
return ret;
}
@ -568,7 +568,7 @@ static struct output_file* output_file_new_gz(void) {
reinterpret_cast<struct output_file_gz*>(calloc(1, sizeof(struct output_file_gz)));
if (!outgz) {
error_errno("malloc struct outgz");
return NULL;
return nullptr;
}
outgz->out.ops = &gz_file_ops;
@ -581,7 +581,7 @@ static struct output_file* output_file_new_normal(void) {
reinterpret_cast<struct output_file_normal*>(calloc(1, sizeof(struct output_file_normal)));
if (!outn) {
error_errno("malloc struct outn");
return NULL;
return nullptr;
}
outn->out.ops = &file_ops;
@ -599,7 +599,7 @@ struct output_file* output_file_open_callback(int (*write)(void*, const void*, s
reinterpret_cast<struct output_file_callback*>(calloc(1, sizeof(struct output_file_callback)));
if (!outc) {
error_errno("malloc struct outc");
return NULL;
return nullptr;
}
outc->out.ops = &callback_file_ops;
@ -609,7 +609,7 @@ struct output_file* output_file_open_callback(int (*write)(void*, const void*, s
ret = output_file_init(&outc->out, block_size, len, sparse, chunks, crc);
if (ret < 0) {
free(outc);
return NULL;
return nullptr;
}
return &outc->out;
@ -626,7 +626,7 @@ struct output_file* output_file_open_fd(int fd, unsigned int block_size, int64_t
out = output_file_new_normal();
}
if (!out) {
return NULL;
return nullptr;
}
out->ops->open(out, fd);
@ -634,7 +634,7 @@ struct output_file* output_file_open_fd(int fd, unsigned int block_size, int64_t
ret = output_file_init(out, block_size, len, sparse, chunks, crc);
if (ret < 0) {
free(out);
return NULL;
return nullptr;
}
return out;
@ -664,7 +664,7 @@ int write_fd_chunk(struct output_file* out, unsigned int len, int fd, int64_t of
#ifndef _WIN32
if (buffer_size > SIZE_MAX) return -E2BIG;
char* data =
reinterpret_cast<char*>(mmap64(NULL, buffer_size, PROT_READ, MAP_SHARED, fd, aligned_offset));
reinterpret_cast<char*>(mmap64(nullptr, buffer_size, PROT_READ, MAP_SHARED, fd, aligned_offset));
if (data == MAP_FAILED) {
return -errno;
}

View file

@ -66,7 +66,7 @@ int main(int argc, char* argv[]) {
exit(-1);
}
files = sparse_file_resparse(s, max_size, NULL, 0);
files = sparse_file_resparse(s, max_size, nullptr, 0);
if (files < 0) {
fprintf(stderr, "Failed to resparse\n");
exit(-1);

View file

@ -30,13 +30,13 @@
struct sparse_file* sparse_file_new(unsigned int block_size, int64_t len) {
struct sparse_file* s = reinterpret_cast<sparse_file*>(calloc(sizeof(struct sparse_file), 1));
if (!s) {
return NULL;
return nullptr;
}
s->backed_block_list = backed_block_list_new(block_size);
if (!s->backed_block_list) {
free(s);
return NULL;
return nullptr;
}
s->block_size = block_size;
@ -252,7 +252,7 @@ static struct backed_block* move_chunks_up_to_len(struct sparse_file* from, stru
unsigned int len) {
int64_t count = 0;
struct output_file* out_counter;
struct backed_block* last_bb = NULL;
struct backed_block* last_bb = nullptr;
struct backed_block* bb;
struct backed_block* start;
unsigned int last_block = 0;
@ -270,7 +270,7 @@ static struct backed_block* move_chunks_up_to_len(struct sparse_file* from, stru
out_counter = output_file_open_callback(out_counter_write, &count, to->block_size, to->len, false,
true, 0, false);
if (!out_counter) {
return NULL;
return nullptr;
}
for (bb = start; bb; bb = backed_block_iter_next(bb)) {
@ -281,7 +281,7 @@ static struct backed_block* move_chunks_up_to_len(struct sparse_file* from, stru
/* will call out_counter_write to update count */
ret = sparse_file_write_block(out_counter, bb);
if (ret) {
bb = NULL;
bb = nullptr;
goto out;
}
if (file_len + count > len) {
@ -330,13 +330,13 @@ int sparse_file_resparse(struct sparse_file* in_s, unsigned int max_len, struct
if (c < out_s_count) {
out_s[c] = s;
} else {
backed_block_list_move(s->backed_block_list, tmp->backed_block_list, NULL, NULL);
backed_block_list_move(s->backed_block_list, tmp->backed_block_list, nullptr, nullptr);
sparse_file_destroy(s);
}
c++;
} while (bb);
backed_block_list_move(tmp->backed_block_list, in_s->backed_block_list, NULL, NULL);
backed_block_list_move(tmp->backed_block_list, in_s->backed_block_list, nullptr, nullptr);
sparse_file_destroy(tmp);

View file

@ -276,7 +276,7 @@ static int process_crc32_chunk(SparseFileSource* source, unsigned int chunk_size
return ret;
}
if (crc32 != NULL && file_crc32 != *crc32) {
if (crc32 != nullptr && file_crc32 != *crc32) {
return -EINVAL;
}
@ -339,7 +339,7 @@ static int sparse_file_read_sparse(struct sparse_file* s, SparseFileSource* sour
sparse_header_t sparse_header;
chunk_header_t chunk_header;
uint32_t crc32 = 0;
uint32_t* crc_ptr = 0;
uint32_t* crc_ptr = nullptr;
unsigned int cur_block = 0;
if (!copybuf) {
@ -489,39 +489,39 @@ static struct sparse_file* sparse_file_import_source(SparseFileSource* source, b
ret = source->ReadValue(&sparse_header, sizeof(sparse_header));
if (ret < 0) {
verbose_error(verbose, ret, "header");
return NULL;
return nullptr;
}
if (sparse_header.magic != SPARSE_HEADER_MAGIC) {
verbose_error(verbose, -EINVAL, "header magic");
return NULL;
return nullptr;
}
if (sparse_header.major_version != SPARSE_HEADER_MAJOR_VER) {
verbose_error(verbose, -EINVAL, "header major version");
return NULL;
return nullptr;
}
if (sparse_header.file_hdr_sz < SPARSE_HEADER_LEN) {
return NULL;
return nullptr;
}
if (sparse_header.chunk_hdr_sz < sizeof(chunk_header_t)) {
return NULL;
return nullptr;
}
len = (int64_t)sparse_header.total_blks * sparse_header.blk_sz;
s = sparse_file_new(sparse_header.blk_sz, len);
if (!s) {
verbose_error(verbose, -EINVAL, NULL);
return NULL;
verbose_error(verbose, -EINVAL, nullptr);
return nullptr;
}
ret = source->SetOffset(0);
if (ret < 0) {
verbose_error(verbose, ret, "seeking");
sparse_file_destroy(s);
return NULL;
return nullptr;
}
s->verbose = verbose;
@ -529,7 +529,7 @@ static struct sparse_file* sparse_file_import_source(SparseFileSource* source, b
ret = sparse_file_read_sparse(s, source, crc);
if (ret < 0) {
sparse_file_destroy(s);
return NULL;
return nullptr;
}
return s;
@ -557,20 +557,20 @@ struct sparse_file* sparse_file_import_auto(int fd, bool crc, bool verbose) {
len = lseek64(fd, 0, SEEK_END);
if (len < 0) {
return NULL;
return nullptr;
}
lseek64(fd, 0, SEEK_SET);
s = sparse_file_new(4096, len);
if (!s) {
return NULL;
return nullptr;
}
ret = sparse_file_read_normal(s, fd);
if (ret < 0) {
sparse_file_destroy(s);
return NULL;
return nullptr;
}
return s;