Пример #1
0
int main() 
{
  struct list          *l;
  struct data          *storage;
  storage = calloc(LEN, sizeof (struct data));
  l = build_int_list(LEN, storage);
  print_list(l);
  l = reverse(l);
  print_list(l);
  free(l);
  free(storage);
  return 0;
}
Пример #2
0
static PyObject *CS_COMMAND_ct_res_info(CS_COMMANDObj *self, PyObject *args)
{
    int type;
    CS_RETCODE status;
    CS_INT int_val;
    CS_BOOL bool_val;
    char *type_str = NULL;

    if (!PyArg_ParseTuple(args, "i", &type))
	return NULL;

    if (self->cmd == NULL) {
	PyErr_SetString(PyExc_TypeError, "CS_COMMAND has been dropped");
	return NULL;
    }

    switch (type) {
    case CS_BROWSE_INFO:
	/* ct_res_info(CS_BROWSE_INFO) -> status, bool */
	/* PyErr_Clear(); */

	SY_CONN_BEGIN_THREADS(self->conn);
	status = ct_res_info(self->cmd, type, &bool_val, CS_UNUSED, NULL);
	SY_CONN_END_THREADS(self->conn);

	if (self->debug)
	    debug_msg("ct_res_info(cmd%d, CS_BROWSE_INFO, &value, CS_UNUSED,"
		      " NULL) -> %s, %d\n",
		      self->serial,
		      value_str(VAL_STATUS, status), (int)bool_val);
	if (PyErr_Occurred())
	    return NULL;

	return Py_BuildValue("ii", status, bool_val);

#ifdef CS_MSGTYPE
    case CS_MSGTYPE:
	/* ct_res_info(CS_MSGTYPE) -> status, int */
    {
	CS_USHORT ushort_val;

	/* PyErr_Clear(); */

	SY_CONN_BEGIN_THREADS(self->conn);
	status = ct_res_info(self->cmd, type, &ushort_val, CS_UNUSED, NULL);
	SY_CONN_END_THREADS(self->conn);

	if (self->debug)
	    debug_msg("ct_res_info(cmd%d, CS_MSGTYPE, &value, CS_UNUSED, NULL)"
		      " -> %s, %d\n",
		      self->serial, value_str(VAL_STATUS, status), ushort_val);
	if (PyErr_Occurred())
	    return NULL;

	return Py_BuildValue("ii", status, ushort_val);
    }
#endif

    case CS_CMD_NUMBER:
	/* ct_res_info(CS_CMD_NUMBER) -> status, int */
	type_str = "CS_CMD_NUMBER";
    case CS_NUM_COMPUTES:
	/* ct_res_info(CS_NUM_COMPUTES) -> status, int */
	if (type_str == NULL)
	    type_str = "CS_NUM_COMPUTES";
    case CS_NUMDATA:
	/* ct_res_info(CS_NUMDATA) -> status, int */
	if (type_str == NULL)
	    type_str = "CS_NUMDATA";
    case CS_NUMORDERCOLS:
	/* ct_res_info(CS_NUMORDER_COLS) -> status, int */
	if (type_str == NULL)
	    type_str = "CS_NUMORDER_COLS";
    case CS_ROW_COUNT:
	/* ct_res_info(CS_ROW_COUNT) -> status, int */
	if (type_str == NULL)
	    type_str = "CS_ROW_COUNT";
    case CS_TRANS_STATE:
	/* ct_res_info(CS_TRANS_STATE) -> status, int */
	if (type_str == NULL)
	    type_str = "CS_TRANS_STATE";
	/* PyErr_Clear(); */

	SY_CONN_BEGIN_THREADS(self->conn);
	status = ct_res_info(self->cmd, type, &int_val, CS_UNUSED, NULL);
	SY_CONN_END_THREADS(self->conn);

	if (self->debug)
	    debug_msg("ct_res_info(cmd%d, %s, &value, CS_UNUSED, NULL)"
		      " -> %s, %d\n",
		      self->serial, type_str,
		      value_str(VAL_STATUS, status), (int)int_val);
	if (PyErr_Occurred())
	    return NULL;

	return Py_BuildValue("ii", status, int_val);

#ifdef CS_ORDERBY_COLS
    case CS_ORDERBY_COLS:
	/* ct_res_info(CS_ORDERBY_COLS) -> status, list of int */
    {
	PyObject *list;
	CS_INT *col_nums;

	/* PyErr_Clear(); */

	SY_CONN_BEGIN_THREADS(self->conn);
	status = ct_res_info(self->cmd, CS_NUMORDERCOLS, &int_val, CS_UNUSED, NULL);
	SY_CONN_END_THREADS(self->conn);

	if (self->debug)
	    debug_msg("ct_res_info(cmd%d, CS_NUMORDERCOLS, &value, CS_UNUSED,"
		      " NULL) -> %s, %d\n",
		      self->serial,
		      value_str(VAL_STATUS, status), (int)int_val);
	if (PyErr_Occurred())
	    return NULL;

	if (status != CS_SUCCEED)
	    return Py_BuildValue("iO", status, Py_None);

	if (int_val <= 0)
	    return Py_BuildValue("i[]", status);

	col_nums = malloc(sizeof(*col_nums) * int_val);
	if (col_nums == NULL)
	    return PyErr_NoMemory();

	/* PyErr_Clear(); */

	SY_CONN_BEGIN_THREADS(self->conn);
	status = ct_res_info(self->cmd, CS_ORDERBY_COLS,
			     col_nums, sizeof(*col_nums) * int_val, NULL);
	SY_CONN_END_THREADS(self->conn);

	if (self->debug) {
	    int i;

	    debug_msg("ct_res_info(cmd%d, CS_ORDERBY_COLS, &col_nums, %d,"
		      " NULL) -> %s, [",
		      self->serial, (int)(sizeof(*col_nums) * int_val),
		      value_str(VAL_STATUS, status));
	    for (i = 0; i < int_val; i++) {
		if (i > 0)
		    debug_msg(",");
		debug_msg("%d", (int)col_nums[i]);
	    }
	    debug_msg("]\n");
	}
	if (PyErr_Occurred()) {
	    free(col_nums);
	    return NULL;
	}

	list = build_int_list(col_nums, int_val);
	free(col_nums);
	if (list == NULL)
	    return NULL;
	return Py_BuildValue("iN", status, list);
    }
#endif

    default:
	PyErr_SetString(PyExc_TypeError, "unknown command");
	return NULL;
    }
}
Пример #3
0
return_val_t run_adverb(
  char* adverb_name, 
  int fn_id, 
  host_val* fixed, int num_fixed,
  int combine_fn_id, int combine_provided,
  host_val* combine_fixed, int num_combine_fixed,
  host_val* init, int num_init, 
  int axes_given, int* axes, int num_axes, 
  host_val* array_positional, int num_array_positional,
  char** array_keyword_names, host_val* array_keyword_values,
  int num_array_keyword_values) {
   
  CAMLparam0();
  CAMLlocal1(fn_id_val);
  CAMLlocal1(combine_id_val_opt);
  CAMLlocal1(adverb);
  CAMLlocal2(fixed_actuals, combine_fixed_actuals);
  CAMLlocal1(array_actuals);
  CAMLlocal2(init_list, axes_list_option); 
  CAMLlocal1(ocaml_result);
  
  fn_id_val = Val_int(fn_id);
  if (combine_provided) {
    combine_id_val_opt = caml_alloc_tuple(1);
    Store_field(combine_id_val_opt, 0, Val_int(combine_fn_id));
  } else {
    combine_id_val_opt = Val_int(0);
  }

  printf("Making fixed args from %d fixed values and %d fixed kwds\n", 
    num_fixed, num_fixed_keywords); 
  fixed_actuals = mk_actual_args(fixed, num_fixed, 0, 0, 0);

  printf("Making fixed args for combiner\n");
  combine_fixed_actuals = mk_actual_args(combine_fixed,
    num_combine_fixed, 0, 0, 0);

  printf("Making array args from %d arrays and %d kwds\n", 
    num_array_positional, num_array_keyword_values);

  array_actuals = mk_actual_args(array_positional, num_array_positional, \
    array_keyword_names, array_keyword_values, num_array_keyword_values); 
  printf("Building list of %d init args\n", num_init); 
  init_list = build_host_val_list(init, num_init);
  printf("Axes given? %d\n", axes_given);
  if (axes_given) {
    printf("Building %d axes\n", num_axes); 
    axes_list_option = caml_alloc(1, 0);
    Store_field( axes_list_option, 0,  build_int_list(axes, num_axes) );
  } else {
    axes_list_option = Val_int(0);
  }
  printf("Calling into OCaml\n");  
  adverb = get_adverb(adverb_name); 
  value func_args[8] = {
    adverb,
    fn_id_val,
    fixed_actuals,
    combine_id_val_opt,
    combine_fixed_actuals,
    init_list, 
    axes_list_option, 
    array_actuals
  };
  ocaml_result = caml_callbackN(*ocaml_run_adverb, 7, func_args);
  printf("Returned from OCaml\n"); 
  CAMLreturnT(return_val_t, translate_return_value(ocaml_result));

}