Clean up const usage to avoid excessive casting

Don't use const on pointers that hold heap allocated data, because that
means functions that free them must cast away the const.

Do use const on pointer parameters or fields that needn't be modified.

Remove pointer casts that are no longer needed after fixing the constness.

Change-Id: I5d206f5019982fd1950bc6d6d07b6062dc24e886
Signed-off-by: Andreas Fritiofson <andreas.fritiofson@gmail.com>
Reviewed-on: http://openocd.zylin.com/1668
Tested-by: jenkins
Reviewed-by: Mathias Küster <kesmtp@freenet.de>
Reviewed-by: Spencer Oliver <spen@spen-soft.co.uk>
__archive__
Andreas Fritiofson 2013-09-30 23:16:20 +02:00 committed by Spencer Oliver
parent c044c60121
commit 517ba0690d
24 changed files with 86 additions and 95 deletions

View File

@ -146,17 +146,17 @@ void script_debug(Jim_Interp *interp, const char *name,
free(dbg);
}
static void script_command_args_free(const char **words, unsigned nwords)
static void script_command_args_free(char **words, unsigned nwords)
{
for (unsigned i = 0; i < nwords; i++)
free((void *)words[i]);
free(words[i]);
free(words);
}
static const char **script_command_args_alloc(
static char **script_command_args_alloc(
unsigned argc, Jim_Obj * const *argv, unsigned *nwords)
{
const char **words = malloc(argc * sizeof(char *));
char **words = malloc(argc * sizeof(char *));
if (NULL == words)
return NULL;
@ -198,7 +198,7 @@ static int script_command_run(Jim_Interp *interp,
LOG_USER_N("%s", ""); /* Keep GDB connection alive*/
unsigned nwords;
const char **words = script_command_args_alloc(argc, argv, &nwords);
char **words = script_command_args_alloc(argc, argv, &nwords);
if (NULL == words)
return JIM_ERR;
@ -299,12 +299,9 @@ static void command_free(struct command *c)
command_free(tmp);
}
if (c->name)
free((void *)c->name);
if (c->help)
free((void *)c->help);
if (c->usage)
free((void *)c->usage);
free(c->name);
free(c->help);
free(c->usage);
free(c);
}
@ -362,7 +359,7 @@ static int register_command_handler(struct command_context *cmd_ctx,
struct command *c)
{
Jim_Interp *interp = cmd_ctx->interp;
const char *ocd_name = alloc_printf("ocd_%s", c->name);
char *ocd_name = alloc_printf("ocd_%s", c->name);
if (NULL == ocd_name)
return JIM_ERR;
@ -370,19 +367,19 @@ static int register_command_handler(struct command_context *cmd_ctx,
Jim_CmdProc func = c->handler ? &script_command : &command_unknown;
int retval = Jim_CreateCommand(interp, ocd_name, func, c, NULL);
free((void *)ocd_name);
free(ocd_name);
if (JIM_OK != retval)
return retval;
/* we now need to add an overrideable proc */
const char *override_name = alloc_printf(
char *override_name = alloc_printf(
"proc %s {args} {eval ocd_bouncer %s $args}",
c->name, c->name);
if (NULL == override_name)
return JIM_ERR;
retval = Jim_Eval_Named(interp, override_name, 0, 0);
free((void *)override_name);
free(override_name);
return retval;
}
@ -1103,7 +1100,7 @@ int help_add_command(struct command_context *cmd_ctx, struct command *parent,
if (help_text) {
bool replaced = false;
if (nc->help) {
free((void *)nc->help);
free(nc->help);
replaced = true;
}
nc->help = strdup(help_text);
@ -1115,7 +1112,7 @@ int help_add_command(struct command_context *cmd_ctx, struct command *parent,
if (usage) {
bool replaced = false;
if (nc->usage) {
free((void *)nc->usage);
free(nc->usage);
replaced = true;
}
nc->usage = strdup(usage);

View File

@ -162,9 +162,9 @@ struct command_invocation {
typedef __COMMAND_HANDLER((*command_handler_t));
struct command {
const char *name;
const char *help;
const char *usage;
char *name;
char *help;
char *usage;
struct command *parent;
struct command *children;
command_handler_t handler;

View File

@ -33,7 +33,7 @@
#include "fileio.h"
struct fileio_internal {
const char *url;
char *url;
ssize_t size;
enum fileio_type type;
enum fileio_access access;
@ -141,7 +141,7 @@ int fileio_close(struct fileio *fileio_p)
retval = fileio_close_local(fileio);
free((void *)fileio->url);
free(fileio->url);
fileio->url = NULL;
free(fileio);

View File

@ -109,7 +109,7 @@ static int jim_aice_newtap_cmd(Jim_GetOptInfo *goi)
e = Jim_GetOpt_Nvp(goi, opts, &n);
if (e != JIM_OK) {
Jim_GetOpt_NvpUnknown(goi, opts, 0);
free((void *)pTap->dotted_name);
free(cp);
free(pTap);
return e;
}
@ -118,7 +118,7 @@ static int jim_aice_newtap_cmd(Jim_GetOptInfo *goi)
case NTAP_OPT_EXPECTED_ID:
e = jim_newtap_expected_id(n, goi, pTap);
if (JIM_OK != e) {
free((void *)pTap->dotted_name);
free(cp);
free(pTap);
return e;
}

View File

@ -1326,11 +1326,11 @@ void jtag_tap_free(struct jtag_tap *tap)
jtag_unregister_event_callback(&jtag_reset_callback, tap);
/** @todo is anything missing? no memory leaks please */
free((void *)tap->expected);
free((void *)tap->expected_ids);
free((void *)tap->chip);
free((void *)tap->tapname);
free((void *)tap->dotted_name);
free(tap->expected);
free(tap->expected_ids);
free(tap->chip);
free(tap->tapname);
free(tap->dotted_name);
free(tap);
}

View File

@ -169,9 +169,9 @@ int ulink_usb_close(struct ulink **device);
/* ULINK MCU (Cypress EZ-USB) specific functions */
int ulink_cpu_reset(struct ulink *device, unsigned char reset_bit);
int ulink_load_firmware_and_renumerate(struct ulink **device, char *filename,
int ulink_load_firmware_and_renumerate(struct ulink **device, const char *filename,
uint32_t delay);
int ulink_load_firmware(struct ulink *device, char *filename);
int ulink_load_firmware(struct ulink *device, const char *filename);
int ulink_write_firmware_section(struct ulink *device,
struct image *firmware_image, int section_index);
@ -362,7 +362,7 @@ int ulink_cpu_reset(struct ulink *device, unsigned char reset_bit)
* @return on failure: ERROR_FAIL
*/
int ulink_load_firmware_and_renumerate(struct ulink **device,
char *filename, uint32_t delay)
const char *filename, uint32_t delay)
{
int ret;
@ -397,7 +397,7 @@ int ulink_load_firmware_and_renumerate(struct ulink **device,
* @return on success: ERROR_OK
* @return on failure: ERROR_FAIL
*/
int ulink_load_firmware(struct ulink *device, char *filename)
int ulink_load_firmware(struct ulink *device, const char *filename)
{
struct image ulink_firmware_image;
int ret, i;
@ -2284,7 +2284,7 @@ COMMAND_HANDLER(ulink_download_firmware_handler)
LOG_INFO("Downloading ULINK firmware image %s", CMD_ARGV[0]);
/* Download firmware image in CMD_ARGV[0] */
ret = ulink_load_firmware_and_renumerate(&ulink_handle, (char *)CMD_ARGV[0],
ret = ulink_load_firmware_and_renumerate(&ulink_handle, CMD_ARGV[0],
ULINK_RENUMERATION_DELAY);
return ret;

View File

@ -107,7 +107,7 @@ static int jim_hl_newtap_cmd(Jim_GetOptInfo *goi)
e = Jim_GetOpt_Nvp(goi, opts, &n);
if (e != JIM_OK) {
Jim_GetOpt_NvpUnknown(goi, opts, 0);
free((void *)pTap->dotted_name);
free(cp);
free(pTap);
return e;
}
@ -116,7 +116,7 @@ static int jim_hl_newtap_cmd(Jim_GetOptInfo *goi)
case NTAP_OPT_EXPECTED_ID:
e = jim_newtap_expected_id(n, goi, pTap);
if (JIM_OK != e) {
free((void *)pTap->dotted_name);
free(cp);
free(pTap);
return e;
}

View File

@ -121,9 +121,9 @@ struct scan_field {
};
struct jtag_tap {
const char *chip;
const char *tapname;
const char *dotted_name;
char *chip;
char *tapname;
char *dotted_name;
int abs_chain_position;
/** Is this TAP disabled after JTAG reset? */
bool disabled_after_reset;

View File

@ -563,7 +563,7 @@ static int jim_newtap_cmd(Jim_GetOptInfo *goi)
e = Jim_GetOpt_Nvp(goi, opts, &n);
if (e != JIM_OK) {
Jim_GetOpt_NvpUnknown(goi, opts, 0);
free((void *)pTap->dotted_name);
free(cp);
free(pTap);
return e;
}
@ -578,7 +578,7 @@ static int jim_newtap_cmd(Jim_GetOptInfo *goi)
case NTAP_OPT_EXPECTED_ID:
e = jim_newtap_expected_id(n, goi, pTap);
if (JIM_OK != e) {
free((void *)pTap->dotted_name);
free(cp);
free(pTap);
return e;
}
@ -588,7 +588,7 @@ static int jim_newtap_cmd(Jim_GetOptInfo *goi)
case NTAP_OPT_IRCAPTURE:
e = jim_newtap_ir_param(n, goi, pTap);
if (JIM_OK != e) {
free((void *)pTap->dotted_name);
free(cp);
free(pTap);
return e;
}
@ -1133,14 +1133,14 @@ COMMAND_HANDLER(handle_irscan_command)
}
int field_size = tap->ir_length;
fields[i].num_bits = field_size;
fields[i].out_value = malloc(DIV_ROUND_UP(field_size, 8));
uint8_t *v = malloc(DIV_ROUND_UP(field_size, 8));
uint64_t value;
retval = parse_u64(CMD_ARGV[i * 2 + 1], &value);
if (ERROR_OK != retval)
goto error_return;
void *v = (void *)fields[i].out_value;
buf_set_u64(v, 0, field_size, value);
fields[i].out_value = v;
fields[i].in_value = NULL;
}

View File

@ -552,6 +552,6 @@ static int ChibiOS_create(struct target *target)
return -1;
}
target->rtos->rtos_specific_params = (void *) &ChibiOS_params_list[i];
target->rtos->rtos_specific_params = &ChibiOS_params_list[i];
return 0;
}

View File

@ -131,7 +131,7 @@ static int embKernel_create(struct target *target)
return -1;
}
target->rtos->rtos_specific_params = (void *) &embKernel_params_list[i];
target->rtos->rtos_specific_params = &embKernel_params_list[i];
return 0;
}

View File

@ -1524,7 +1524,7 @@ static int linux_os_create(struct target *target)
os_linux->threads_needs_update = 0;
os_linux->threadid_count = 1;
os_linux->current_threads = NULL;
target->rtos->rtos_specific_params = (void *)os_linux;
target->rtos->rtos_specific_params = os_linux;
ct->core_id = target->coreid;
ct->threadid = -1;
ct->TS = 0xdeadbeef;

View File

@ -104,8 +104,8 @@ static int gdb_breakpoint_override;
static enum breakpoint_type gdb_breakpoint_override_type;
static int gdb_error(struct connection *connection, int retval);
static const char *gdb_port;
static const char *gdb_port_next;
static char *gdb_port;
static char *gdb_port_next;
static void gdb_log_callback(void *priv, const char *file, unsigned line,
const char *function, const char *string);
@ -2900,7 +2900,7 @@ static int gdb_target_add_one(struct target *target)
portnumber = strtol(gdb_port_next, &end, 0);
if (!*end) {
if (parse_long(gdb_port_next, &portnumber) == ERROR_OK) {
free((void *)gdb_port_next);
free(gdb_port_next);
gdb_port_next = alloc_printf("%d", portnumber+1);
}
}
@ -2947,7 +2947,7 @@ COMMAND_HANDLER(handle_gdb_port_command)
{
int retval = CALL_COMMAND_HANDLER(server_pipe_command, &gdb_port);
if (ERROR_OK == retval) {
free((void *)gdb_port_next);
free(gdb_port_next);
gdb_port_next = strdup(gdb_port);
}
return retval;

View File

@ -303,14 +303,14 @@ static int remove_services(void)
struct service *next = c->next;
if (c->name)
free((void *)c->name);
free(c->name);
if (c->type == CONNECTION_PIPE) {
if (c->fd != -1)
close(c->fd);
}
if (c->port)
free((void *)c->port);
free(c->port);
if (c->priv)
free(c->priv);
@ -612,7 +612,7 @@ int server_register_commands(struct command_context *cmd_ctx)
return register_commands(cmd_ctx, NULL, server_command_handlers);
}
SERVER_PORT_COMMAND()
COMMAND_HELPER(server_port_command, unsigned short *out)
{
switch (CMD_ARGC) {
case 0:
@ -631,7 +631,7 @@ SERVER_PORT_COMMAND()
return ERROR_OK;
}
SERVER_PIPE_COMMAND()
COMMAND_HELPER(server_pipe_command, char **out)
{
switch (CMD_ARGC) {
case 0:
@ -643,9 +643,8 @@ SERVER_PIPE_COMMAND()
LOG_WARNING("unable to change server port after init");
return ERROR_COMMAND_ARGUMENT_INVALID;
}
const char *t = strdup(CMD_ARGV[0]);
free((void *)*out);
*out = t;
free(*out);
*out = strdup(CMD_ARGV[0]);
break;
}
default:

View File

@ -55,9 +55,9 @@ typedef int (*input_handler_t)(struct connection *connection);
typedef int (*connection_closed_handler_t)(struct connection *connection);
struct service {
const char *name;
char *name;
enum connection_type type;
const char *port;
char *port;
unsigned short portnumber;
int fd;
struct sockaddr_in sin;
@ -101,14 +101,9 @@ void openocd_sleep_postlude(void);
* Call server_port like a normal COMMAND_HANDLER with an extra @a out parameter
* to receive the specified port number.
*/
#define SERVER_PIPE_COMMAND() \
COMMAND_HELPER(server_pipe_command, const char **out)
SERVER_PIPE_COMMAND();
COMMAND_HELPER(server_pipe_command, char **out);
#define SERVER_PORT_COMMAND() \
COMMAND_HELPER(server_port_command, unsigned short *out)
SERVER_PORT_COMMAND();
COMMAND_HELPER(server_port_command, unsigned short *out);
#define ERROR_SERVER_REMOTE_CLOSED (-400)
#define ERROR_CONNECTION_REJECTED (-401)

View File

@ -34,7 +34,7 @@ struct tcl_connection {
int tc_outerror;/* flag an output error */
};
static const char *tcl_port;
static char *tcl_port;
/* handlers */
static int tcl_new_connection(struct connection *connection);

View File

@ -32,7 +32,7 @@
#include <target/target_request.h>
#include <helper/configuration.h>
static const char *telnet_port;
static char *telnet_port;
static char *negotiate =
"\xFF\xFB\x03" /* IAC WILL Suppress Go Ahead */

View File

@ -38,11 +38,11 @@ static int mips32_dmaacc_read_mem32(struct mips_ejtag *ejtag_info,
uint32_t addr, int count, uint32_t *buf);
static int mips32_dmaacc_write_mem8(struct mips_ejtag *ejtag_info,
uint32_t addr, int count, uint8_t *buf);
uint32_t addr, int count, const uint8_t *buf);
static int mips32_dmaacc_write_mem16(struct mips_ejtag *ejtag_info,
uint32_t addr, int count, uint16_t *buf);
uint32_t addr, int count, const uint16_t *buf);
static int mips32_dmaacc_write_mem32(struct mips_ejtag *ejtag_info,
uint32_t addr, int count, uint32_t *buf);
uint32_t addr, int count, const uint32_t *buf);
/*
* The following logic shamelessly cloned from HairyDairyMaid's wrt54g_debrick
@ -407,21 +407,21 @@ static int mips32_dmaacc_read_mem8(struct mips_ejtag *ejtag_info, uint32_t addr,
return ERROR_OK;
}
int mips32_dmaacc_write_mem(struct mips_ejtag *ejtag_info, uint32_t addr, int size, int count, void *buf)
int mips32_dmaacc_write_mem(struct mips_ejtag *ejtag_info, uint32_t addr, int size, int count, const void *buf)
{
switch (size) {
case 1:
return mips32_dmaacc_write_mem8(ejtag_info, addr, count, (uint8_t *)buf);
return mips32_dmaacc_write_mem8(ejtag_info, addr, count, buf);
case 2:
return mips32_dmaacc_write_mem16(ejtag_info, addr, count, (uint16_t *)buf);
return mips32_dmaacc_write_mem16(ejtag_info, addr, count, buf);
case 4:
return mips32_dmaacc_write_mem32(ejtag_info, addr, count, (uint32_t *)buf);
return mips32_dmaacc_write_mem32(ejtag_info, addr, count, buf);
}
return ERROR_OK;
}
static int mips32_dmaacc_write_mem32(struct mips_ejtag *ejtag_info, uint32_t addr, int count, uint32_t *buf)
static int mips32_dmaacc_write_mem32(struct mips_ejtag *ejtag_info, uint32_t addr, int count, const uint32_t *buf)
{
int i;
int retval;
@ -435,7 +435,7 @@ static int mips32_dmaacc_write_mem32(struct mips_ejtag *ejtag_info, uint32_t add
return ERROR_OK;
}
static int mips32_dmaacc_write_mem16(struct mips_ejtag *ejtag_info, uint32_t addr, int count, uint16_t *buf)
static int mips32_dmaacc_write_mem16(struct mips_ejtag *ejtag_info, uint32_t addr, int count, const uint16_t *buf)
{
int i;
int retval;
@ -449,7 +449,7 @@ static int mips32_dmaacc_write_mem16(struct mips_ejtag *ejtag_info, uint32_t add
return ERROR_OK;
}
static int mips32_dmaacc_write_mem8(struct mips_ejtag *ejtag_info, uint32_t addr, int count, uint8_t *buf)
static int mips32_dmaacc_write_mem8(struct mips_ejtag *ejtag_info, uint32_t addr, int count, const uint8_t *buf)
{
int i;
int retval;

View File

@ -38,6 +38,6 @@
int mips32_dmaacc_read_mem(struct mips_ejtag *ejtag_info,
uint32_t addr, int size, int count, void *buf);
int mips32_dmaacc_write_mem(struct mips_ejtag *ejtag_info,
uint32_t addr, int size, int count, void *buf);
uint32_t addr, int size, int count, const void *buf);
#endif

View File

@ -730,16 +730,17 @@ static int mips32_pracc_clean_invalidate_cache(struct mips_ejtag *ejtag_info,
return retval;
}
static int mips32_pracc_write_mem_generic(struct mips_ejtag *ejtag_info, uint32_t addr, int size, int count, void *buf)
static int mips32_pracc_write_mem_generic(struct mips_ejtag *ejtag_info,
uint32_t addr, int size, int count, const void *buf)
{
struct pracc_queue_info ctx = {.max_code = 128 * 3 + 6 + 1}; /* alloc memory for the worst case */
pracc_queue_init(&ctx);
if (ctx.retval != ERROR_OK)
goto exit;
uint32_t *buf32 = buf;
uint16_t *buf16 = buf;
uint8_t *buf8 = buf;
const uint32_t *buf32 = buf;
const uint16_t *buf16 = buf;
const uint8_t *buf8 = buf;
while (count) {
ctx.code_count = 0;
@ -798,7 +799,7 @@ exit:
return ctx.retval;
}
int mips32_pracc_write_mem(struct mips_ejtag *ejtag_info, uint32_t addr, int size, int count, void *buf)
int mips32_pracc_write_mem(struct mips_ejtag *ejtag_info, uint32_t addr, int size, int count, const void *buf)
{
int retval = mips32_pracc_write_mem_generic(ejtag_info, addr, size, count, buf);
if (retval != ERROR_OK)

View File

@ -67,7 +67,7 @@ int mips32_pracc_queue_exec(struct mips_ejtag *ejtag_info,
int mips32_pracc_read_mem(struct mips_ejtag *ejtag_info,
uint32_t addr, int size, int count, void *buf);
int mips32_pracc_write_mem(struct mips_ejtag *ejtag_info,
uint32_t addr, int size, int count, void *buf);
uint32_t addr, int size, int count, const void *buf);
int mips32_pracc_fastdata_xfer(struct mips_ejtag *ejtag_info, struct working_area *source,
int write_t, uint32_t addr, int count, uint32_t *buf);

View File

@ -1048,9 +1048,9 @@ static int mips_m4k_write_memory(struct target *target, uint32_t address,
/* if noDMA off, use DMAACC mode for memory write */
int retval;
if (ejtag_info->impcode & EJTAG_IMP_NODMA)
retval = mips32_pracc_write_mem(ejtag_info, address, size, count, (void *)buffer);
retval = mips32_pracc_write_mem(ejtag_info, address, size, count, buffer);
else
retval = mips32_dmaacc_write_mem(ejtag_info, address, size, count, (void *)buffer);
retval = mips32_dmaacc_write_mem(ejtag_info, address, size, count, buffer);
if (t != NULL)
free(t);

View File

@ -377,7 +377,7 @@ void target_buffer_get_u16_array(struct target *target, const uint8_t *buffer, u
}
/* write a uint32_t array to a buffer in target memory endianness */
void target_buffer_set_u32_array(struct target *target, uint8_t *buffer, uint32_t count, uint32_t *srcbuf)
void target_buffer_set_u32_array(struct target *target, uint8_t *buffer, uint32_t count, const uint32_t *srcbuf)
{
uint32_t i;
for (i = 0; i < count; i++)
@ -385,7 +385,7 @@ void target_buffer_set_u32_array(struct target *target, uint8_t *buffer, uint32_
}
/* write a uint16_t array to a buffer in target memory endianness */
void target_buffer_set_u16_array(struct target *target, uint8_t *buffer, uint32_t count, uint16_t *srcbuf)
void target_buffer_set_u16_array(struct target *target, uint8_t *buffer, uint32_t count, const uint16_t *srcbuf)
{
uint32_t i;
for (i = 0; i < count; i++)
@ -4260,11 +4260,10 @@ no_params:
n->name);
return JIM_ERR;
}
if (target->variant)
free((void *)(target->variant));
e = Jim_GetOpt_String(goi, &cp, NULL);
if (e != JIM_OK)
return e;
free(target->variant);
target->variant = strdup(cp);
} else {
if (goi->argc != 0)

View File

@ -129,7 +129,7 @@ struct target {
int target_number; /* DO NOT USE! field to be removed in 2010 */
struct jtag_tap *tap; /* where on the jtag chain is this */
int32_t coreid; /* which device on the TAP? */
const char *variant; /* what variant of this chip is it? */
char *variant; /* what variant of this chip is it? */
/**
* Indicates whether this target has been examined.
@ -602,8 +602,8 @@ void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t valu
void target_buffer_get_u32_array(struct target *target, const uint8_t *buffer, uint32_t count, uint32_t *dstbuf);
void target_buffer_get_u16_array(struct target *target, const uint8_t *buffer, uint32_t count, uint16_t *dstbuf);
void target_buffer_set_u32_array(struct target *target, uint8_t *buffer, uint32_t count, uint32_t *srcbuf);
void target_buffer_set_u16_array(struct target *target, uint8_t *buffer, uint32_t count, uint16_t *srcbuf);
void target_buffer_set_u32_array(struct target *target, uint8_t *buffer, uint32_t count, const uint32_t *srcbuf);
void target_buffer_set_u16_array(struct target *target, uint8_t *buffer, uint32_t count, const uint16_t *srcbuf);
int target_read_u32(struct target *target, uint32_t address, uint32_t *value);
int target_read_u16(struct target *target, uint32_t address, uint16_t *value);