source: trunk/ORBit2-2.14.0/test/test-mem.c

Last change on this file was 92, checked in by cinc, 19 years ago

Orbit2 modified for use with NOM

File size: 6.5 KB
Line 
1#include <math.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <signal.h>
6
7#if defined(_POSIX_SOURCE)
8#include <sys/wait.h>
9#include <sys/time.h>
10#include <sys/resource.h>
11#include <unistd.h>
12#endif
13
14#include <orbit/orbit.h>
15
16#include "test1.h"
17
18#define ORBIT_EX(ev) ((ev)->_major != CORBA_NO_EXCEPTION && (g_warning("%s", CORBA_exception_id(ev)),TRUE))
19#define ORBIT_EX_IS_A(ev,id) (((ev)->_major != CORBA_NO_EXCEPTION && strcmp(id, CORBA_exception_id(ev)) == 0) || (g_warning("%s", CORBA_exception_id(ev)),FALSE))
20
21/*** App-specific servant structures ***/
22
23typedef struct
24{
25 POA_Test servant;
26
27 /* ------ add private attributes here ------ */
28 CORBA_octet buf[1024]; /* big enough to see mem-leaks fast*/
29 /* ------ ---------- end ------------ ------ */
30} impl_POA_Test;
31
32/*** Implementation stub prototypes ***/
33static void impl_Test__destroy(impl_POA_Test * servant,
34 CORBA_Environment * ev);
35
36static CORBA_string
37impl_Test_op(impl_POA_Test * servant,
38 const CORBA_char * astr, CORBA_Environment * ev);
39
40/*** epv structures ***/
41
42static PortableServer_ServantBase__epv impl_Test_base_epv = {
43 NULL, /* _private data */
44 (gpointer) & impl_Test__destroy, /* finalize routine */
45 NULL, /* default_POA routine */
46};
47static POA_Test__epv impl_Test_epv = {
48 NULL, /* _private */
49 (gpointer) & impl_Test_op,
50
51};
52
53/*** vepv structures ***/
54
55static POA_Test__vepv impl_Test_vepv = {
56 &impl_Test_base_epv,
57 &impl_Test_epv,
58};
59
60/*** Stub implementations ***/
61static void
62impl_Test__destroy(impl_POA_Test * servant, CORBA_Environment * ev)
63{
64 POA_Test__fini((PortableServer_Servant) servant, ev);
65 g_free(servant);
66}
67
68
69
70static CORBA_string
71impl_Test_op(impl_POA_Test * servant,
72 const CORBA_char * astr, CORBA_Environment * ev)
73{
74 CORBA_string retval;
75
76 /* ------ insert method code here ------ */
77 retval = CORBA_string_dup ("blahblah");
78 /* ------ ---------- end ------------ ------ */
79
80 return retval;
81}
82
83static void
84test_ORBit_alloc (void)
85{
86 gpointer p;
87 int i;
88
89 p = ORBit_alloc_string (100);
90 g_assert ((gulong)p & 0x1);
91 for (i = 0; i < 100; i++)
92 ((guchar *)p) [i] = i;
93 CORBA_free (p);
94
95 p = CORBA_string_dup ("Foo");
96 g_assert (((gulong)p & 0x1));
97 CORBA_free (p);
98
99 p = ORBit_alloc_simple (100);
100 g_assert (!((gulong)p & 0x1));
101 for (i = 0; i < 100; i++)
102 ((guchar *)p) [i] = i;
103 CORBA_free (p);
104
105 p = ORBit_alloc_tcval (TC_CORBA_sequence_CORBA_octet, 1);
106 g_assert (!((gulong)p & 0x1));
107 CORBA_free (p);
108
109 p = ORBit_alloc_tcval (TC_ORBit_IInterface, 8);
110 g_assert (!((gulong)p & 0x1));
111 CORBA_free (p);
112}
113
114
115static void
116test_ORBit_sequence (void) {
117 gpointer seq
118 = ORBit_sequence_alloc (TC_CORBA_sequence_CORBA_octet,
119 1000);
120 CORBA_free (seq);
121}
122
123static void
124test_activate_deactivate (PortableServer_POA poa, CORBA_Environment * ev)
125{
126 impl_POA_Test *newservant;
127 PortableServer_ObjectId *objid;
128
129 newservant = g_new0(impl_POA_Test, 1);
130 newservant->servant.vepv = &impl_Test_vepv;
131
132 POA_Test__init((PortableServer_Servant) newservant, ev);
133
134 objid = PortableServer_POA_activate_object(poa, newservant, ev);
135 g_assert (!ORBIT_EX (ev));
136
137 PortableServer_POA_deactivate_object (poa, objid, ev);
138 g_assert (!ORBIT_EX (ev));
139
140 CORBA_free(objid);
141}
142
143static CORBA_ORB global_orb = CORBA_OBJECT_NIL; /* global orb */
144
145static void
146server_shutdown (int sig) {
147 CORBA_Environment local_ev[1];
148 CORBA_exception_init(local_ev);
149
150 if (global_orb != CORBA_OBJECT_NIL) {
151 CORBA_ORB_shutdown (global_orb, FALSE, local_ev);
152 g_assert (!ORBIT_EX (local_ev));
153 }
154}
155
156static long
157get_procmem (void) {
158#if defined(_POSIX_SOURCE)
159 long ret=0;
160 char cmd[255];
161 FILE *handle = NULL;
162
163 sprintf (cmd, "ps --no-headers -o \"rss\" -p %d 2>/dev/null", getpid());
164 handle = popen (cmd, "r");
165 if (!handle)
166 return 0;
167
168 switch (fscanf (handle, "%ld", &ret)) {
169 case EOF:
170 g_warning ("EOF, executing \"%s\" failed", cmd);
171 break;
172 case 0:
173 g_warning ("parse error, executing \"%s\" failed", cmd);
174 break;
175 default:
176 break;
177 }
178 pclose (handle);
179 return ret;
180#else
181 return 0;
182#endif
183}
184
185#define LEAK_DETECT_WITH_TOLERANCE(N,CALL,TOLERANCE) do { \
186 long mem_usage_start = 0; \
187 long mem_usage_end = 0; \
188 long i = 0; \
189 mem_usage_start = get_procmem (); \
190 for (i = 0; i < (N); ++i) { \
191 (CALL); \
192 } \
193 mem_usage_end = get_procmem (); \
194 g_print ("mem usage prev/post: " \
195 "%5ldKB / %5ldKB -- %5dx " #CALL "\n", \
196 mem_usage_start, \
197 mem_usage_end, \
198 N); \
199 g_assert ( mem_usage_end - mem_usage_start < TOLERANCE); \
200} while(FALSE)
201
202static
203void
204main_func(int argc, char *argv[]) {
205 PortableServer_POA poa = CORBA_OBJECT_NIL;
206 PortableServer_POAManager poa_manager = CORBA_OBJECT_NIL;
207 CORBA_Environment ev[1];
208 CORBA_exception_init(ev);
209
210 /* init signal handling */
211
212 signal(SIGINT, server_shutdown);
213 signal(SIGTERM, server_shutdown);
214
215 /* create Object Request Broker (ORB) */
216
217 global_orb = CORBA_ORB_init(&argc,
218 argv,
219 "orbit-local-treaded-orb",
220 ev);
221 g_assert (!ORBIT_EX (ev));
222
223 /* get Portable Object Adaptor (POA) */
224
225 poa = (PortableServer_POA)
226 CORBA_ORB_resolve_initial_references(global_orb,
227 "RootPOA",
228 ev);
229 g_assert (!ORBIT_EX (ev));
230
231 /* activate POA Manager */
232
233 poa_manager = PortableServer_POA__get_the_POAManager(poa, ev);
234 g_assert (!ORBIT_EX (ev));
235
236 PortableServer_POAManager_activate(poa_manager, ev);
237 g_assert (!ORBIT_EX (ev));
238
239 LEAK_DETECT_WITH_TOLERANCE (1000, test_ORBit_alloc (), 50);
240 LEAK_DETECT_WITH_TOLERANCE (1000, test_ORBit_sequence (), 50);
241 LEAK_DETECT_WITH_TOLERANCE (1000, test_activate_deactivate (poa, ev), 50);
242
243 /* tear down the ORB */
244 CORBA_Object_release ((CORBA_Object) poa_manager, ev);
245 g_assert (!ORBIT_EX (ev));
246
247 CORBA_Object_release ((CORBA_Object) poa, ev);
248 g_assert (!ORBIT_EX (ev));
249
250 if (global_orb != CORBA_OBJECT_NIL) {
251 /* going to destroy orb.. */
252 CORBA_ORB_destroy(global_orb, ev);
253 g_assert (!ORBIT_EX (ev));
254 }
255}
256
257int
258main(int argc, char *argv[]) {
259 /* ORBit2-2.7.6 leaks on shutdown with about 700KB */
260 /* LEAK_DETECT_WITH_TOLERANCE (1, main_func (argc,argv), 20); */
261 main_func (argc, argv);
262 exit (0);
263}
264
Note: See TracBrowser for help on using the repository browser.