Skip to content

Commit

Permalink
contrib/elf2dmp: Use g_malloc(), g_new() and g_free()
Browse files Browse the repository at this point in the history
QEMU coding style uses the glib memory allocation APIs, not
the raw libc malloc/free. Switch the allocation and free
calls in elf2dmp to use these functions (dropping the now-unneeded
checks for failure).

Signed-off-by: Suraj Shirvankar <surajshirvankar@gmail.com>
Message-id: 169753938460.23804.11418813007617535750-1@git.sr.ht
[PMM: also remove NULL checks from g_malloc() calls;
 beef up commit message]
Reviewed-by: Peter Maydell <peter.maydell@linaro.org>
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
  • Loading branch information
h0lyalg0rithm authored and pm215 committed Oct 19, 2023
1 parent 9ef2629 commit 2a052b4
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 27 deletions.
7 changes: 2 additions & 5 deletions contrib/elf2dmp/addrspace.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,7 @@ int pa_space_create(struct pa_space *ps, QEMU_Elf *qemu_elf)
}
}

ps->block = malloc(sizeof(*ps->block) * ps->block_nr);
if (!ps->block) {
return 1;
}
ps->block = g_new(struct pa_block, ps->block_nr);

for (i = 0; i < phdr_nr; i++) {
if (phdr[i].p_type == PT_LOAD) {
Expand All @@ -97,7 +94,7 @@ int pa_space_create(struct pa_space *ps, QEMU_Elf *qemu_elf)
void pa_space_destroy(struct pa_space *ps)
{
ps->block_nr = 0;
free(ps->block);
g_free(ps->block);
}

void va_space_set_dtb(struct va_space *vs, uint64_t dtb)
Expand Down
9 changes: 3 additions & 6 deletions contrib/elf2dmp/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,11 @@ static KDDEBUGGER_DATA64 *get_kdbg(uint64_t KernBase, struct pdb_reader *pdb,
}
}

kdbg = malloc(kdbg_hdr.Size);
if (!kdbg) {
return NULL;
}
kdbg = g_malloc(kdbg_hdr.Size);

if (va_space_rw(vs, KdDebuggerDataBlock, kdbg, kdbg_hdr.Size, 0)) {
eprintf("Failed to extract entire KDBG\n");
free(kdbg);
g_free(kdbg);
return NULL;
}

Expand Down Expand Up @@ -643,7 +640,7 @@ int main(int argc, char *argv[])
}

out_kdbg:
free(kdbg);
g_free(kdbg);
out_pdb:
pdb_exit(&pdb);
out_pdb_file:
Expand Down
19 changes: 8 additions & 11 deletions contrib/elf2dmp/pdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,18 @@ uint64_t pdb_resolve(uint64_t img_base, struct pdb_reader *r, const char *name)

static void pdb_reader_ds_exit(struct pdb_reader *r)
{
free(r->ds.toc);
g_free(r->ds.toc);
}

static void pdb_exit_symbols(struct pdb_reader *r)
{
free(r->modimage);
free(r->symbols);
g_free(r->modimage);
g_free(r->symbols);
}

static void pdb_exit_segments(struct pdb_reader *r)
{
free(r->segs);
g_free(r->segs);
}

static void *pdb_ds_read(const PDB_DS_HEADER *header,
Expand All @@ -120,10 +120,7 @@ static void *pdb_ds_read(const PDB_DS_HEADER *header,

nBlocks = (size + header->block_size - 1) / header->block_size;

buffer = malloc(nBlocks * header->block_size);
if (!buffer) {
return NULL;
}
buffer = g_malloc(nBlocks * header->block_size);

for (i = 0; i < nBlocks; i++) {
memcpy(buffer + i * header->block_size, (const char *)header +
Expand Down Expand Up @@ -206,7 +203,7 @@ static int pdb_init_symbols(struct pdb_reader *r)
return 0;

out_symbols:
free(symbols);
g_free(symbols);

return err;
}
Expand Down Expand Up @@ -263,7 +260,7 @@ static int pdb_reader_init(struct pdb_reader *r, void *data)
out_sym:
pdb_exit_symbols(r);
out_root:
free(r->ds.root);
g_free(r->ds.root);
out_ds:
pdb_reader_ds_exit(r);

Expand All @@ -274,7 +271,7 @@ static void pdb_reader_exit(struct pdb_reader *r)
{
pdb_exit_segments(r);
pdb_exit_symbols(r);
free(r->ds.root);
g_free(r->ds.root);
pdb_reader_ds_exit(r);
}

Expand Down
7 changes: 2 additions & 5 deletions contrib/elf2dmp/qemu_elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,7 @@ static int init_states(QEMU_Elf *qe)

printf("%zu CPU states has been found\n", cpu_nr);

qe->state = malloc(sizeof(*qe->state) * cpu_nr);
if (!qe->state) {
return 1;
}
qe->state = g_new(QEMUCPUState*, cpu_nr);

cpu_nr = 0;

Expand All @@ -115,7 +112,7 @@ static int init_states(QEMU_Elf *qe)

static void exit_states(QEMU_Elf *qe)
{
free(qe->state);
g_free(qe->state);
}

static bool check_ehdr(QEMU_Elf *qe)
Expand Down

0 comments on commit 2a052b4

Please sign in to comment.