~ubuntu-branches/debian/squeeze/ffcall/squeeze

« back to all changes in this revision

Viewing changes to ffcall/trampoline/test1.c

  • Committer: Bazaar Package Importer
  • Author(s): Christoph Egger
  • Date: 2010-06-26 15:29:30 UTC
  • mfrom: (5.1.1 experimental)
  • Revision ID: james.westby@ubuntu.com-20100626152930-c09y01gk3szcnykn
Tags: 1.10+cvs20100619-2
Ship to unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Trampoline test */
2
 
 
3
 
/*
4
 
 * Copyright 1995-1999, 2002, 2005 Bruno Haible, <bruno@clisp.org>
5
 
 *
6
 
 * This is free software distributed under the GNU General Public Licence
7
 
 * described in the file COPYING. Contact the author if you don't have this
8
 
 * or can't live with it. There is ABSOLUTELY NO WARRANTY, explicit or implied,
9
 
 * on this software.
10
 
 */
11
 
 
12
 
#include <stdio.h>
13
 
#include <stdlib.h>
14
 
 
15
 
#include "trampoline.h"
16
 
 
17
 
#define MAGIC1  0x9db9af42
18
 
#define MAGIC2  0x614a13c9
19
 
 
20
 
static int magic = MAGIC1;
21
 
 
22
 
#ifdef __cplusplus
23
 
typedef int (*function)(...);
24
 
#else
25
 
typedef int (*function)();
26
 
#endif
27
 
 
28
 
void* function_data;
29
 
 
30
 
int f (int x)
31
 
{ return *(int*)function_data + x; }
32
 
 
33
 
int main ()
34
 
{
35
 
  function cf = alloc_trampoline((function)&f, &function_data, &magic);
36
 
  /* calling cf shall set  function_data = &magic  and then call f(x),
37
 
   * thus returning  magic + x.
38
 
   */
39
 
  if (((*cf)(MAGIC2) == MAGIC1+MAGIC2) && (function_data == &magic))
40
 
    { free_trampoline(cf); printf("Works, test1 passed.\n"); exit(0); }
41
 
  else
42
 
    { printf("Doesn't work!\n"); exit(1); }
43
 
}