Skip to content

Commit

Permalink
Rename make_callback_data and simplify typedefs
Browse files Browse the repository at this point in the history
Since this is now C++ code, we no longer need typedefs around our structs.
  • Loading branch information
eric-wieser committed Apr 17, 2020
1 parent f3a93c8 commit ed4d6dc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
52 changes: 26 additions & 26 deletions cocotb/share/lib/simulator/simulatormodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,14 @@ int handle_gpi_callback(void *user_data)
{
int ret = 0;
to_python();
p_callback_data callback_data_p = (p_callback_data)user_data;
callback_data *cb_data = (callback_data *)user_data;

if (callback_data_p->id_value != COCOTB_ACTIVE_ID) {
if (cb_data->id_value != COCOTB_ACTIVE_ID) {
fprintf(stderr, "Userdata corrupted!\n");
ret = 1;
goto err;
}
callback_data_p->id_value = COCOTB_INACTIVE_ID;
cb_data->id_value = COCOTB_INACTIVE_ID;

/* Cache the sim time */
gpi_get_sim_time(&cache_time.high, &cache_time.low);
Expand All @@ -201,15 +201,15 @@ int handle_gpi_callback(void *user_data)

// Python allowed

if (!PyCallable_Check(callback_data_p->function)) {
if (!PyCallable_Check(cb_data->function)) {
fprintf(stderr, "Callback fired but function isn't callable?!\n");
ret = 1;
goto out;
}

{
// Call the callback
PyObject *pValue = PyObject_Call(callback_data_p->function, callback_data_p->args, callback_data_p->kwargs);
PyObject *pValue = PyObject_Call(cb_data->function, cb_data->args, cb_data->kwargs);

// If the return value is NULL a Python exception has occurred
// The best thing to do here is shutdown as any subsequent
Expand All @@ -229,12 +229,12 @@ int handle_gpi_callback(void *user_data)
}

// Callbacks may have been re-enabled
if (callback_data_p->id_value == COCOTB_INACTIVE_ID) {
Py_DECREF(callback_data_p->function);
Py_DECREF(callback_data_p->args);
if (cb_data->id_value == COCOTB_INACTIVE_ID) {
Py_DECREF(cb_data->function);
Py_DECREF(cb_data->args);

// Free the callback data
free(callback_data_p);
free(cb_data);
}

out:
Expand Down Expand Up @@ -270,9 +270,9 @@ static PyObject *log_msg(PyObject *self, PyObject *args)
}


static p_callback_data make_callback_data(PyObject *func, PyObject *args, PyObject *kwargs)
static callback_data *callback_data_new(PyObject *func, PyObject *args, PyObject *kwargs)
{
p_callback_data data = (p_callback_data)malloc(sizeof(s_callback_data));
callback_data *data = (callback_data *)malloc(sizeof(callback_data));
if (data == NULL) {
PyErr_NoMemory();
return NULL;
Expand Down Expand Up @@ -317,12 +317,12 @@ static PyObject *register_readonly_callback(PyObject *self, PyObject *args)
return NULL;
}

p_callback_data callback_data_p = make_callback_data(function, fArgs, NULL);
if (callback_data_p == NULL) {
callback_data *cb_data = callback_data_new(function, fArgs, NULL);
if (cb_data == NULL) {
return NULL;
}

gpi_cb_hdl hdl = gpi_register_readonly_callback((gpi_function_t)handle_gpi_callback, callback_data_p);
gpi_cb_hdl hdl = gpi_register_readonly_callback((gpi_function_t)handle_gpi_callback, cb_data);

PyObject *rv = gpi_hdl_New(hdl);
FEXIT
Expand Down Expand Up @@ -357,13 +357,13 @@ static PyObject *register_rwsynch_callback(PyObject *self, PyObject *args)
return NULL;
}

p_callback_data callback_data_p = make_callback_data(function, fArgs, NULL);
if (callback_data_p == NULL) {
callback_data *cb_data = callback_data_new(function, fArgs, NULL);
if (cb_data == NULL) {
return NULL;
}

gpi_cb_hdl hdl = gpi_register_readwrite_callback(
(gpi_function_t)handle_gpi_callback, callback_data_p);
(gpi_function_t)handle_gpi_callback, cb_data);

PyObject *rv = gpi_hdl_New(hdl);
FEXIT
Expand Down Expand Up @@ -398,13 +398,13 @@ static PyObject *register_nextstep_callback(PyObject *self, PyObject *args)
return NULL;
}

p_callback_data callback_data_p = make_callback_data(function, fArgs, NULL);
if (callback_data_p == NULL) {
callback_data *cb_data = callback_data_new(function, fArgs, NULL);
if (cb_data == NULL) {
return NULL;
}

gpi_cb_hdl hdl = gpi_register_nexttime_callback(
(gpi_function_t)handle_gpi_callback, callback_data_p);
(gpi_function_t)handle_gpi_callback, cb_data);

PyObject *rv = gpi_hdl_New(hdl);
FEXIT
Expand Down Expand Up @@ -457,13 +457,13 @@ static PyObject *register_timed_callback(PyObject *self, PyObject *args)
return NULL;
}

p_callback_data callback_data_p = make_callback_data(function, fArgs, NULL);
if (callback_data_p == NULL) {
callback_data *cb_data = callback_data_new(function, fArgs, NULL);
if (cb_data == NULL) {
return NULL;
}

gpi_cb_hdl hdl = gpi_register_timed_callback(
(gpi_function_t)handle_gpi_callback, callback_data_p, time_ps);
(gpi_function_t)handle_gpi_callback, cb_data, time_ps);

// Check success
PyObject *rv = gpi_hdl_New(hdl);
Expand Down Expand Up @@ -513,13 +513,13 @@ static PyObject *register_value_change_callback(PyObject *self, PyObject *args)
return NULL;
}

p_callback_data callback_data_p = make_callback_data(function, fArgs, NULL);
if (callback_data_p == NULL) {
callback_data *cb_data = callback_data_new(function, fArgs, NULL);
if (cb_data == NULL) {
return NULL;
}

gpi_cb_hdl hdl = gpi_register_value_change_callback(
(gpi_function_t)handle_gpi_callback, callback_data_p, sig_hdl, edge);
(gpi_function_t)handle_gpi_callback, cb_data, sig_hdl, edge);

// Check success
PyObject *rv = gpi_hdl_New(hdl);
Expand Down
4 changes: 2 additions & 2 deletions cocotb/share/lib/simulator/simulatormodule.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@
#define MODULE_NAME "simulator"

// callback user data
typedef struct t_callback_data {
struct callback_data {
PyThreadState *_saved_thread_state; // Thread state of the calling thread FIXME is this required?
uint32_t id_value; // COCOTB_ACTIVE_ID or COCOTB_INACTIVE_ID
PyObject *function; // Function to call when the callback fires
PyObject *args; // The arguments to call the function with
PyObject *kwargs; // Keyword arguments to call the function with
gpi_sim_hdl cb_hdl;
} s_callback_data, *p_callback_data;
};

static PyObject *log_msg(PyObject *self, PyObject *args);

Expand Down

0 comments on commit ed4d6dc

Please sign in to comment.