Mercurial > pub > dyncall > dyncall
comparison test/thunk/test_thunk.c @ 0:3e629dc19168
initial from svn dyncall-1745
author | Daniel Adler |
---|---|
date | Thu, 19 Mar 2015 22:24:28 +0100 |
parents | |
children | f533c5ad1b6e |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3e629dc19168 |
---|---|
1 /* | |
2 | |
3 Package: dyncall | |
4 Library: test | |
5 File: test/thunk/test_thunk.c | |
6 Description: | |
7 License: | |
8 | |
9 Copyright (c) 2011-2015 Daniel Adler <dadler@uni-goettingen.de>, | |
10 Tassilo Philipp <tphilipp@potion-studios.com> | |
11 | |
12 Permission to use, copy, modify, and distribute this software for any | |
13 purpose with or without fee is hereby granted, provided that the above | |
14 copyright notice and this permission notice appear in all copies. | |
15 | |
16 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
17 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
18 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
19 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
20 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
21 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
22 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
23 | |
24 */ | |
25 | |
26 #include "../../dyncall/dyncall.h" | |
27 #include "../../dyncallback/dyncall_thunk.h" | |
28 #include "../../dyncallback/dyncall_alloc_wx.h" | |
29 #include "../common/platformInit.h" | |
30 #include "../common/platformInit.c" /* Impl. for functions only used in this translation unit */ | |
31 | |
32 | |
33 /** | |
34 ** test: thunks with several allocation modes | |
35 ** such as on stack, on heap and explicit wx | |
36 ** | |
37 **/ | |
38 | |
39 #include <assert.h> | |
40 #include <errno.h> | |
41 | |
42 void my_entry(const char* text) | |
43 { | |
44 printf("%s: 1\n", text); | |
45 } | |
46 | |
47 typedef void (printfun)(const char*); | |
48 | |
49 void test_stack() | |
50 { | |
51 DCThunk t; | |
52 printfun* fp; | |
53 dcbInitThunk(&t, &my_entry); | |
54 fp = (printfun*)&t; | |
55 fp("stack"); | |
56 } | |
57 | |
58 #include <stdlib.h> | |
59 | |
60 void test_heap() | |
61 { | |
62 printfun* fp; | |
63 DCThunk* p = (DCThunk*)malloc(sizeof(DCThunk)); | |
64 if(!p) { | |
65 printf("0\n"); | |
66 return; | |
67 } | |
68 dcbInitThunk(p, &my_entry); | |
69 fp = (printfun*)p; | |
70 fp("heap"); | |
71 free(p); | |
72 } | |
73 | |
74 void test_wx() | |
75 { | |
76 DCThunk* p; | |
77 printfun* fp; | |
78 int err = dcAllocWX(sizeof(DCThunk), (void**)&p); | |
79 if(err || !p) { | |
80 printf("0\n"); | |
81 return; | |
82 } | |
83 dcbInitThunk(p, &my_entry); | |
84 fp = (printfun*)p; | |
85 fp("wx"); | |
86 dcFreeWX((void*)p, sizeof(DCThunk)); | |
87 } | |
88 | |
89 int main() | |
90 { | |
91 dcTest_initPlatform(); | |
92 | |
93 printf("Allocating ...\n"); | |
94 printf("... W^X memory: "); | |
95 test_wx(); | |
96 printf("... heap memory: "); | |
97 test_heap(); | |
98 printf("... stack memory: "); | |
99 test_stack(); | |
100 | |
101 dcTest_deInitPlatform(); | |
102 | |
103 return 0; | |
104 } | |
105 |