aboutsummaryrefslogtreecommitdiffstats
path: root/mps/code/mpslibcb.c
blob: 41bf5bcb6c728be7f5ead67455a25045043a0e79 (plain) (blame)
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/* mpslibcb.c: RAVENBROOK MEMORY POOL SYSTEM LIBRARY INTERFACE (CALLBACK)
 *
 * $Header$
 * Copyright (c) 2005 Ravenbrook Limited.  See end of file for license.
 *
 * .purpose: The purpose of this code is
 *   1. permit the MPS Library Interface to be used conveniently when
 *      the MPS is packaged as a dynamic library (and in particular a
 *      Windows DLL).
 *
 * .readership: For MPS client application developers and MPS developers.
 * .sources: <design/lib/>
 *
 * .freestanding: This is designed to be deployed in a freestanding
 * environment, so we can't use strcmp from <string.h>, so we have to
 * roll our own (in fact we only ever need equality so we define a
 * simpler interface).
 *
 * .mpm.not: This module occupies a halfway house between the MPM and
 * the client.  Let's make it clearer: this module should not use any
 * services of the MPM.  That is, it should be written as if the client
 * could have, in principle, written it.  .mpm.not.why: Perhaps the most
 * compelling reason is that if config.h is included (via mpm.h) then
 * the compile breaks on platform.w3i3mv because of "#define
 * mps_lib_memset memset" in config.h.
 */

#include "mpslibcb.h"
#include "mpslib.h"
#include "mps.h"

/* Forward declarations. */

int mps_lib_callback_default_get_EOF(void);
mps_lib_FILE *mps_lib_callback_default_get_stderr(void);
mps_lib_FILE *mps_lib_callback_default_get_stdout(void);
int mps_lib_callback_default_fputc(int c_, mps_lib_FILE *f_);
int mps_lib_callback_default_fputs(const char *s_, mps_lib_FILE *f_);
void *mps_lib_callback_default_memset(void *p_, int c_, size_t n_);
void *mps_lib_callback_default_memcpy(void *p_, const void *q_, size_t n_);
int mps_lib_callback_default_memcmp(const void *p_, const void *q_, size_t n_);
mps_clock_t mps_lib_callback_default_clock(void);
mps_clock_t mps_lib_callback_default_clocks_per_sec(void);
unsigned long mps_lib_callback_default_telemetry_control(void);
int mps_lib_callback_streq(const char *, const char *);

/* Macros */

/* See .freestanding */
#define EQ(p, q) (mps_lib_callback_streq((p), (q)))
/* We use this to call mps_lib_asssert_fail (which we only ever do
 * unconditionally).  See .mpm.not on why we cannot use ASSERT from
 * mpm.h */
#define AFAIL mps_lib_assert_fail
/* Replaced UNUSED from mpm.h, see .mpm.not */
#define UNUSED(x) ((void)(x))

/* Structures and Types */

struct mps_lib_callback_s
{
  int (*lib_get_EOF)(void);
  mps_lib_FILE * (*lib_get_stderr)(void);
  mps_lib_FILE * (*lib_get_stdout)(void);
  int (*lib_fputc)(int, mps_lib_FILE *);
  int (*lib_fputs)(const char *, mps_lib_FILE *);
  void (*lib_assert_fail)(const char *);
  void * (*lib_memset)(void *, int, size_t);
  void * (*lib_memcpy)(void *, const void *, size_t);
  int (*lib_memcmp)(const void *, const void *, size_t);
  mps_clock_t (*clock)(void);
  mps_clock_t (*clocks_per_sec)(void);
  unsigned long (*lib_telemetry_control)(void);
};

/* Globals */

/* .global.why: A global is necessary so that we can store the function
 * pointers that the client gives us.  The functions in the mpslib.h
 * interface _are_ global.  There is no scope for having one memset
 * function for one Arena and a different memset function for another.
 * */

/* The default functions are stubs that assert.  Except for the
 * assert_fail function (which is called when assertions fail) which
 * will be NULL.  This means: if you provide assert_fail and forget
 * something else, you'll know about it.  If you do not provide
 * assert_fail then it will probably stop anyway.
 *
 * These functions really do need to fail, so they subvert the checking
 * mechanism (which is in mpm.h and not available to us, see .mpm.not)
 */
  
struct mps_lib_callback_s mps_lib_callback_global = {
  mps_lib_callback_default_get_EOF,
  mps_lib_callback_default_get_stderr,
  mps_lib_callback_default_get_stdout,
  mps_lib_callback_default_fputc,
  mps_lib_callback_default_fputs,
  NULL, /* assert_fail */
  mps_lib_callback_default_memset,
  mps_lib_callback_default_memcpy,
  mps_lib_callback_default_memcmp,
  mps_lib_callback_default_clock,
  mps_lib_callback_default_clocks_per_sec,
  mps_lib_callback_default_telemetry_control
};

/* Functions */

int mps_lib_callback_register(const char *name, mps_lib_function_t f)
{
  if(NULL == name) {
    return MPS_RES_FAIL;
  }
  if(EQ(name, "mps_lib_get_EOF")) {
    mps_lib_callback_global.lib_get_EOF = (int(*)(void))f;
  } else if(EQ(name, "mps_lib_get_stderr")) {
    mps_lib_callback_global.lib_get_stderr = (mps_lib_FILE *(*)(void))f;
  } else if(EQ(name, "mps_lib_get_stdout")) {
    mps_lib_callback_global.lib_get_stdout = (mps_lib_FILE *(*)(void))f;
  } else if(EQ(name, "mps_lib_fputc")) {
    mps_lib_callback_global.lib_fputc = (int(*)(int, mps_lib_FILE *))f;
  } else if(EQ(name, "mps_lib_fputs")) {
    mps_lib_callback_global.lib_fputs =
      (int(*)(const char *, mps_lib_FILE *))f;
  } else if(EQ(name, "mps_lib_assert_fail")) {
    mps_lib_callback_global.lib_assert_fail = (void(*)(const char *))f;
  } else if(EQ(name, "mps_lib_memset")) {
    mps_lib_callback_global.lib_memset = (void *(*)(void *, int, size_t))f;
  } else if(EQ(name, "mps_lib_memcpy")) {
    mps_lib_callback_global.lib_memcpy =
      (void *(*)(void *, const void *, size_t))f;
  } else if(EQ(name, "mps_lib_memcmp")) {
    mps_lib_callback_global.lib_memcmp =
      (int(*)(const void *, const void *, size_t))f;
  } else if(EQ(name, "mps_clock")) {
    mps_lib_callback_global.clock = (mps_clock_t(*)(void))f;
  } else if(EQ(name, "mps_clocks_per_sec")) {
    mps_lib_callback_global.clocks_per_sec = (mps_clock_t(*)(void))f;
  } else if(EQ(name, "mps_lib_telemetry_control")) {
    mps_lib_callback_global.lib_telemetry_control =
      (unsigned long(*)(void))f;
  } else {
    return MPS_RES_UNIMPL;
  }
  return MPS_RES_OK;
}

/* Return non-zero if and only if string p equals string q. */
int mps_lib_callback_streq(const char *p, const char *q)
{
  do {
    if(*p == '\0' && *q == '\0') {
      return 1;
    }
  } while(*p++ == *q++);
  return 0;
}

int mps_lib_callback_default_get_EOF(void)
{
  AFAIL("mps_lib_get_EOF needs to be provided");
  return 0;
}

mps_lib_FILE *mps_lib_callback_default_get_stderr(void)
{
  AFAIL("mps_lib_get_stderr needs to be provided");
  return NULL;
}

mps_lib_FILE *mps_lib_callback_default_get_stdout(void)
{
  AFAIL("mps_lib_get_stdout needs to be provided");
  return NULL;
}

int mps_lib_callback_default_fputc(int c_, mps_lib_FILE *f_)
{
  UNUSED(c_);
  UNUSED(f_);
  AFAIL("mps_lib_fputc needs to be provided");
  return 0;
}

int mps_lib_callback_default_fputs(const char *s_, mps_lib_FILE *f_)
{
  UNUSED(s_);
  UNUSED(f_);
  AFAIL("mps_lib_fputs needs to be provided");
  return 0;
}

/* No default implementation for mps_lib_assert_fail */

void *mps_lib_callback_default_memset(void *p_, int c_, size_t n_)
{
  UNUSED(p_);
  UNUSED(c_);
  UNUSED(n_);
  AFAIL("mps_lib_memset needs to be provided");
  return NULL;
}

void *mps_lib_callback_default_memcpy(void *p_, const void *q_, size_t n_)
{
  UNUSED(p_);
  UNUSED(q_);
  UNUSED(n_);
  AFAIL("mps_lib_memcpy needs to be provided");
  return NULL;
}

int mps_lib_callback_default_memcmp(const void *p_, const void *q_, size_t n_)
{
  UNUSED(p_);
  UNUSED(q_);
  UNUSED(n_);
  AFAIL("mps_lib_memcmp needs to be provided");
  return 0;
}

mps_clock_t mps_lib_callback_default_clock(void)
{
  AFAIL("mps_clock needs to be provided");
  return 0;
}

mps_clock_t mps_lib_callback_default_clocks_per_sec(void)
{
  AFAIL("mps_clocks_per_sec needs to be provided");
  return 0;
}

unsigned long mps_lib_callback_default_telemetry_control(void)
{
  AFAIL("mps_lib_telemetry_control needs to be provided");
  return 0;
}

int mps_lib_get_EOF(void)
{
  return mps_lib_callback_global.lib_get_EOF();
}

mps_lib_FILE *mps_lib_get_stderr(void)
{
  return mps_lib_callback_global.lib_get_stderr();
}

mps_lib_FILE *mps_lib_get_stdout(void)
{
  return mps_lib_callback_global.lib_get_stdout();
}

int mps_lib_fputc(int c, mps_lib_FILE *f)
{
  return mps_lib_callback_global.lib_fputc(c, f);
}

int mps_lib_fputs(const char *s, mps_lib_FILE *f)
{
  return mps_lib_callback_global.lib_fputs(s, f);
}

void mps_lib_assert_fail(const char *m)
{
  mps_lib_callback_global.lib_assert_fail(m);
}

void *(mps_lib_memset)(void *p, int c, size_t n)
{
  return mps_lib_callback_global.lib_memset(p, c, n);
}

void *(mps_lib_memcpy)(void *p, const void *q, size_t n)
{
  return mps_lib_callback_global.lib_memcpy(p, q, n);
}

int (mps_lib_memcmp)(const void *p, const void *q, size_t n)
{
  return mps_lib_callback_global.lib_memcmp(p, q, n);
}

mps_clock_t mps_clock(void)
{
  return mps_lib_callback_global.clock();
}

mps_clock_t mps_clocks_per_sec(void)
{
  return mps_lib_callback_global.clocks_per_sec();
}

unsigned long mps_lib_telemetry_control(void)
{
  return mps_lib_callback_global.lib_telemetry_control();
}



/* C. COPYRIGHT AND LICENSE
 *
 * Copyright (C) 2005 Ravenbrook Limited <http://www.ravenbrook.com/>.
 * All rights reserved.  This is an open source license.  Contact
 * Ravenbrook for commercial licensing options.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * 3. Redistributions in any form must be accompanied by information on how
 * to obtain complete source code for this software and any accompanying
 * software that uses this software.  The source code must either be
 * included in the distribution or be available for no more than the cost
 * of distribution plus a nominal fee, and must be freely redistributable
 * under reasonable conditions.  For an executable file, complete source
 * code means the source code for all modules it contains. It does not
 * include source code for modules or files that typically accompany the
 * major components of the operating system on which the executable file
 * runs.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 * PURPOSE, OR NON-INFRINGEMENT, ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */