blackmagic/src/target.c

85 lines
2.0 KiB
C

/*
* This file is part of the Black Magic Debug project.
*
* Copyright (C) 2012 Black Sphere Technologies Ltd.
* Written by Gareth McMullin <gareth@blacksphere.co.nz>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "general.h"
#include "target.h"
#include <stdlib.h>
target *target_list = NULL;
bool connect_assert_srst;
target *target_new(unsigned size)
{
target *t = (void*)calloc(1, size);
t->next = target_list;
target_list = t;
return t;
}
void target_list_free(void)
{
struct target_command_s *tc;
while(target_list) {
target *t = target_list->next;
if (target_list->destroy_callback)
target_list->destroy_callback(target_list);
if (target_list->priv)
target_list->priv_free(target_list->priv);
while (target_list->commands) {
tc = target_list->commands->next;
free(target_list->commands);
target_list->commands = tc;
}
free(target_list);
target_list = t;
}
}
void target_add_commands(target *t, const struct command_s *cmds, const char *name)
{
struct target_command_s *tc;
if (t->commands) {
for (tc = t->commands; tc->next; tc = tc->next);
tc = tc->next = malloc(sizeof(*tc));
} else {
t->commands = tc = malloc(sizeof(*tc));
}
tc->specific_name = name;
tc->cmds = cmds;
tc->next = NULL;
}
target *target_attach(target *t, target_destroy_callback destroy_cb)
{
if (t->destroy_callback)
t->destroy_callback(t);
t->destroy_callback = destroy_cb;
if (!t->attach(t))
return NULL;
return t;
}