File: mswdynld.c

package info (click to toggle)
xlispstat 3.52.14-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 7,560 kB
  • ctags: 12,676
  • sloc: ansic: 91,357; lisp: 21,759; sh: 1,525; makefile: 521; csh: 1
file content (257 lines) | stat: -rw-r--r-- 7,244 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/* mswdynld - Dynamic loading and C function calling routines.         */
/* XLISP-STAT 2.1 Copyright (c) 1990, by Luke Tierney                  */
/* Additions to Xlisp 2.1, Copyright (c) 1989 by David Michael Betz    */
/* You may give out copies of this software; for conditions see the    */
/* file COPYING included with this distribution.                       */

/* Calling conventions are based on the conventions given in the New S */
/* book.                                                               */

#include "xlisp.h"
#include "xlstat.h"
#include "xlsx.h"

extern LVAL s_dll_list;

/************************************************************************/
/**                                                                    **/
/**                      DLL Handling Functions                        **/
/**                                                                    **/
/************************************************************************/

LVAL xsload_dll()
{
  char *dllname;
  HANDLE hdll;
  LVAL dllhandle, dll_list;
#ifdef DODO
{
  HANDLE h1,h2;
  h1 = LoadLibrary("foo.dll");
  h2 = LoadLibrary("simplex.dll");
  FreeLibrary(h1);
  FreeLibrary(h2);
  return(NIL);
}
#endif DODO

  dllname = (char *) getstring(xlgastring());
  xllastarg();
  hdll = LoadLibrary(dllname);

  if ((UINT) hdll >= 32) {
    xlsave1(dllhandle);
    dllhandle = cvfixnum((FIXTYPE) hdll);
    dll_list = consp(getvalue(s_dll_list)) ? getvalue(s_dll_list) : NIL;
    setvalue(s_dll_list, cons(dllhandle, dll_list));
    xlpop();
#ifdef MULVALS
    xlnumresults = 1;
    xlresults[0] = dllhandle;
#endif
    return(dllhandle);
  }
  else {
#ifdef MULVALS
    xlnumresults = 2;
    xlresults[0] = NIL;
    xlresults[1] = cvfixnum((FIXTYPE) hdll);
#endif
    return NIL;
  }
}

LVAL xsfree_dll()
{
  HANDLE hdll;
  LVAL last, list;

  hdll = (HANDLE) getfixnum(xlgafixnum());
  xllastarg();

//  SysBeep(10);
//  return(NIL);

  if ((UINT) hdll >= 32) {
    for (last = NIL, list = getvalue(s_dll_list);
	 consp(list);
	 last = list, list = cdr(list)) {
      //*** because of some bug, on a 386SX20 at least, frees MUST be done
      //*** on a last in-first out basis. So only the head of the list
      //*** can be freed. Hence the following line:
      if (consp(last)) break;
      if (hdll == (HANDLE) getfixnum(car(list))) {
	if (consp(last)) cdr(last) = cdr(list);
	else setvalue(s_dll_list, cdr(list));
	FreeLibrary(hdll);
	break;
      }
    }
  }
  return(NIL);
}

void MSWDLLCleanup()
{
  LVAL list;

  if (s_dll_list != NULL)
    for (list = getvalue(s_dll_list); consp(list); list = cdr(list))
      FreeLibrary((HANDLE) getfixnum(car(list)));
}

/************************************************************************/
/**                                                                    **/
/**               Allocation and Error Signalling Functions            **/
/**                                                                    **/
/************************************************************************/

static LVAL current_allocs = NULL;
#define fixup_current_allocs \
  { if (current_allocs == NULL) current_allocs = NIL; }

/* allocate space that will be garbage collected after return */
static char *xscall_alloc(int n, int m)
{
  LVAL adata;
  char *p;

  fixup_current_allocs;

  adata = newadata(n, m, FALSE);
  if (adata == NIL || (p = getadaddr(adata)) == NULL)
    xlfail("allocation failed");
  current_allocs = cons(adata, current_allocs);
  return(p);
}

/************************************************************************/
/**                                                                    **/
/**                Lisp to C/FORTRAN Data Conversion                   **/
/**                                                                    **/
/************************************************************************/

#define IN 0
#define RE 1

typedef struct {
  int type, size;
  char *addr;
} call_arg;

/* convert lisp argument to allocated pointer */
static call_arg lisp2arg(LVAL x)
{
  call_arg a;
  LVAL elem, data;
  int i;

  xlprot1(x);

  /* make sure x is a sequence and find its length */
  if (! seqp(x)) x = consa(x);
  a.size = seqlen(x);

  /* determine the mode of the data */
  for (i = 0, a.type = IN, data = x; i < a.size; i++) {
    elem = getnextelement(&data, i);
    if (floatp(elem)) a.type = RE;
#ifdef BIGNUMS
    else if (ratiop(elem)) a.type = RE;
#endif
    else if (! integerp(elem)) xlerror("not a real number", elem);
  }

  /* allocate space for the data */
  a.addr = xscall_alloc(a.size, (a.type == IN) ? sizeof(int) : sizeof(double));

  /* fill the space */
  for (i = 0, data = x; i < a.size; i++) {
    elem = getnextelement(&data, i);
    if (a.type == IN) ((int *) a.addr)[i] = getfixnum(elem);
    else ((double *) a.addr)[i] = makefloat(elem);
  }

  xlpop();
  return(a);
}

/* copy allocated pointer back to new lisp list */
static LVAL arg2lisp(call_arg a)
{
  LVAL x, next;
  int i;

  xlsave1(x);
  x = mklist(a.size, NIL);
  for (i = 0, next = x; i < a.size; i++, next = cdr(next)) {
    if (a.type == IN) rplaca(next, cvfixnum((FIXTYPE) ((int *) a.addr)[i]));
    else rplaca(next, cvflonum((FLOTYPE) ((double *) a.addr)[i]));
  }
  xlpop();

  return(x);
}

/************************************************************************/
/**                                                                    **/
/**                 Foreign Function Call Function                     **/
/**                                                                    **/
/************************************************************************/

typedef void VFUN(XLSXblock *);
typedef VFUN *VFPTR;

LVAL xscall_cfun()
{
  LVAL result, Lfun, old_allocs, next;
  call_arg *args, *pargs;
  int nargs, i;
  VFPTR routine;
  HANDLE hdll;
  char *name;
  XLSXblock params;

  fixup_current_allocs;

  // ### patch this to handle errors properly (reset allocs, etc) -- use dynamic scoping
  // ### also use allocation with free as in (new) linalg?
  xlstkcheck(3);
  xlsave(old_allocs);
  xlprotect(current_allocs);
  xlsave(result);
  old_allocs = current_allocs;
  current_allocs = NIL;

  /* get the routine pointer */
  hdll = (HANDLE) getfixnum(xlgafixnum());
  Lfun = xlgetarg();
  if (stringp(Lfun)) name = getstring(Lfun);
  else if (fixp(Lfun)) name = (char *) MAKEINTRESOURCE((int) getfixnum(Lfun));
  routine = (VFPTR) GetProcAddress(hdll, name);
  if (! routine) xlerror("can't find function address", Lfun);

  /* convert the arguments to allocated pointers */
  nargs = xlargc;
  if (nargs == 0) xlfail("too few arguments");
  args = (call_arg *) xscall_alloc(nargs, sizeof(call_arg));
  params.argc = nargs;
  params.argv = (char **) xscall_alloc(nargs, sizeof(char *));
  for (i = 0; i < nargs; i++) {
    args[i] = lisp2arg(xlgetarg());
    params.argv[i] = args[i].addr;
  }

  /* make the call */
  (*routine)(&params);

  /* convert the pointers back to lists, grouped in a list */
  result = (nargs > 0) ? mklist(nargs, NIL) : NIL;
  for (next = result, pargs = args; consp(next); next = cdr(next), pargs++)
    rplaca(next, arg2lisp(*pargs));

  current_allocs = old_allocs;
  xlpopn(3);

  return(result);
}