~ubuntu-branches/ubuntu/quantal/nspr/quantal-security

« back to all changes in this revision

Viewing changes to mozilla/nsprpub/lib/msgc/tests/thrashgc.c

  • Committer: Package Import Robot
  • Author(s): Jamie Strandboge
  • Date: 2013-01-10 10:56:12 UTC
  • mfrom: (1.3.4)
  • Revision ID: package-import@ubuntu.com-20130110105612-zo087hhaexilklvu
Tags: 4.9.4-0ubuntu0.12.10.1
* New upstream release to support security fixes in nss. Dropped the
  following patches:
  - debian/patches/30_pkgconfig.patch (included upstream)
  - debian/patches/38_hurd.patch (included upstream)
  - debian/patches/99_configure.patch (no longer required)
  - debian/patches/sonames.patch (no longer required)
* debian/libnsp4.symbols: added PR_GetThreadName@Base and
  PR_SetCurrentThreadName@Base

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
 
/* ***** BEGIN LICENSE BLOCK *****
3
 
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4
 
 *
5
 
 * The contents of this file are subject to the Mozilla Public License Version
6
 
 * 1.1 (the "License"); you may not use this file except in compliance with
7
 
 * the License. You may obtain a copy of the License at
8
 
 * http://www.mozilla.org/MPL/
9
 
 *
10
 
 * Software distributed under the License is distributed on an "AS IS" basis,
11
 
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
 
 * for the specific language governing rights and limitations under the
13
 
 * License.
14
 
 *
15
 
 * The Original Code is the Netscape Portable Runtime (NSPR).
16
 
 *
17
 
 * The Initial Developer of the Original Code is
18
 
 * Netscape Communications Corporation.
19
 
 * Portions created by the Initial Developer are Copyright (C) 1998-2000
20
 
 * the Initial Developer. All Rights Reserved.
21
 
 *
22
 
 * Contributor(s):
23
 
 *
24
 
 * Alternatively, the contents of this file may be used under the terms of
25
 
 * either the GNU General Public License Version 2 or later (the "GPL"), or
26
 
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27
 
 * in which case the provisions of the GPL or the LGPL are applicable instead
28
 
 * of those above. If you wish to allow use of your version of this file only
29
 
 * under the terms of either the GPL or the LGPL, and not to allow others to
30
 
 * use your version of this file under the terms of the MPL, indicate your
31
 
 * decision by deleting the provisions above and replace them with the notice
32
 
 * and other provisions required by the GPL or the LGPL. If you do not delete
33
 
 * the provisions above, a recipient may use your version of this file under
34
 
 * the terms of any one of the MPL, the GPL or the LGPL.
35
 
 *
36
 
 * ***** END LICENSE BLOCK ***** */
37
 
 
38
 
/***********************************************************************
39
 
** Name: thrashgc
40
 
**
41
 
** Description: test garbace collection functions.
42
 
**
43
 
** Modification History:
44
 
** 19-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
45
 
**               The debug mode will print all of the printfs associated with this test.
46
 
**                       The regress mode will be the default mode. Since the regress tool limits
47
 
**           the output to a one line status:PASS or FAIL,all of the printf statements
48
 
**                       have been handled with an if (debug_mode) statement.
49
 
** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
50
 
**                      recognize the return code from tha main program.
51
 
***********************************************************************/
52
 
/***********************************************************************
53
 
** Includes
54
 
***********************************************************************/
55
 
#include "prthread.h"
56
 
#include "prgc.h"
57
 
#include "prprf.h"
58
 
#include "prinrval.h"
59
 
#include "prlock.h"
60
 
#include "prinit.h"
61
 
#include "prcvar.h"
62
 
 
63
 
#include "private/pprthred.h"
64
 
 
65
 
#include <stdio.h>
66
 
#include <memory.h>
67
 
#include <string.h>
68
 
 
69
 
 
70
 
PRIntn failed_already=0;
71
 
PRIntn debug_mode;
72
 
 
73
 
static char* progname;
74
 
static PRInt32 loops = 1000;
75
 
static int tix1, tix2, tix3;
76
 
static GCInfo* gcInfo;
77
 
static PRLock* stderrLock;
78
 
 
79
 
typedef struct Type1 Type1;
80
 
typedef struct Type2 Type2;
81
 
 
82
 
struct Type1 {
83
 
  Type2* atwo;
84
 
  Type1* next;
85
 
};
86
 
 
87
 
struct Type2 {
88
 
  void* buf;
89
 
};
90
 
 
91
 
static void PR_CALLBACK ScanType1(void *obj) {
92
 
  gcInfo->livePointer(((Type1 *)obj)->atwo);
93
 
  gcInfo->livePointer(((Type1 *)obj)->next);
94
 
}
95
 
 
96
 
static void PR_CALLBACK ScanType2(void *obj) {
97
 
  gcInfo->livePointer(((Type2 *)obj)->buf);
98
 
}
99
 
 
100
 
static GCType type1 = {
101
 
    ScanType1
102
 
};
103
 
 
104
 
static GCType type2 = {
105
 
    ScanType2
106
 
/*  (void (*)(void*)) ScanType2 */
107
 
};
108
 
 
109
 
static GCType type3 = {
110
 
  0
111
 
};
112
 
 
113
 
Type1* NewType1(void) {
114
 
    Type1* p = (Type1*) PR_AllocMemory(sizeof(Type1), tix1, PR_ALLOC_DOUBLE);
115
 
    PR_ASSERT(p != NULL);
116
 
    return p;
117
 
}
118
 
 
119
 
Type2* NewType2(void) {
120
 
    Type2* p = (Type2*) PR_AllocMemory(sizeof(Type2), tix2, PR_ALLOC_DOUBLE);
121
 
    PR_ASSERT(p != NULL);
122
 
    return p;
123
 
}
124
 
 
125
 
void* NewBuffer(PRInt32 size) {
126
 
    void* p = PR_AllocMemory(size, tix3, PR_ALLOC_DOUBLE);
127
 
    PR_ASSERT(p != NULL);
128
 
    return p;
129
 
}
130
 
 
131
 
/* Allocate alot of garbage */
132
 
static void PR_CALLBACK AllocStuff(void *unused) {
133
 
  PRInt32 i;
134
 
  void* danglingRefs[50];
135
 
  PRIntervalTime start, end;
136
 
  char msg[100];
137
 
 
138
 
  start = PR_IntervalNow();
139
 
  for (i = 0; i < loops; i++) {
140
 
    void* p;
141
 
    if (i & 1) {
142
 
      Type1* t1 = NewType1();
143
 
      t1->atwo = NewType2();
144
 
      t1->next = NewType1();
145
 
      t1->atwo->buf = NewBuffer(100);
146
 
      p = t1;
147
 
    } else {
148
 
      Type2* t2 = NewType2();
149
 
      t2->buf = NewBuffer(i & 16383);
150
 
      p = t2;
151
 
    }
152
 
    if ((i % 10) == 0) {
153
 
      memmove(&danglingRefs[0], &danglingRefs[1], 49*sizeof(void*));
154
 
      danglingRefs[49] = p;
155
 
    }
156
 
  }
157
 
  end = PR_IntervalNow();
158
 
  if (debug_mode) PR_snprintf(msg, sizeof(msg), "Thread %p: %ld allocations took %ld ms",
159
 
              PR_GetCurrentThread(), loops,
160
 
              PR_IntervalToMilliseconds((PRIntervalTime) (end - start)));
161
 
  PR_Lock(stderrLock);
162
 
  fprintf(stderr, "%s\n", msg);
163
 
  PR_Unlock(stderrLock);
164
 
  }
165
 
 
166
 
static void usage(char *progname) {
167
 
  fprintf(stderr, "Usage: %s [-t threads] [-l loops]\n", progname);
168
 
  exit(-1);
169
 
}
170
 
 
171
 
static int realMain(int argc, char **argv, char *notused) {
172
 
  int i;
173
 
  int threads = 0;
174
 
 
175
 
  progname = strrchr(argv[0], '/');
176
 
  if (progname == 0) progname = argv[0];
177
 
  for (i = 1; i < argc; i++) {
178
 
    if (strcmp(argv[i], "-t") == 0) {
179
 
      if (i == argc - 1) {
180
 
        usage(progname);
181
 
      }
182
 
      threads = atoi(argv[++i]);
183
 
      if (threads < 0) threads = 0;
184
 
      if (threads > 10000) threads = 10000;
185
 
      continue;
186
 
    }
187
 
    if (strcmp(argv[i], "-l") == 0) {
188
 
      if (i == argc - 1) {
189
 
        usage(progname);
190
 
      }
191
 
      loops = atoi(argv[++i]);
192
 
      continue;
193
 
    }
194
 
    usage(progname);
195
 
  }
196
 
 
197
 
  for (i = 0; i < threads; i++) {
198
 
    PRThread* thread;
199
 
 
200
 
    /* XXXXX */
201
 
    thread = PR_CreateThreadGCAble(PR_USER_THREAD,  /* thread type */
202
 
                             AllocStuff,  /* start function */
203
 
                             NULL,  /* arg */
204
 
                             PR_PRIORITY_NORMAL,  /* priority */
205
 
                             PR_LOCAL_THREAD,  /* thread scope */
206
 
                             PR_UNJOINABLE_THREAD,  /* thread state */
207
 
                             0);   /* stack size */
208
 
    if (thread == 0) {
209
 
      fprintf(stderr, "%s: no more threads (only %d were created)\n",
210
 
              progname, i);
211
 
      break;
212
 
    }
213
 
  }
214
 
  AllocStuff(NULL);
215
 
  return 0;
216
 
}
217
 
 
218
 
static int padMain(int argc, char **argv) {
219
 
  char pad[512];
220
 
  return realMain(argc, argv, pad);
221
 
}
222
 
 
223
 
int main(int argc, char **argv) {
224
 
  int rv;
225
 
 
226
 
  debug_mode = 1;
227
 
  
228
 
  PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
229
 
  PR_SetThreadGCAble();
230
 
 
231
 
  PR_InitGC(0, 0, 0, PR_GLOBAL_THREAD);
232
 
  PR_STDIO_INIT();
233
 
  stderrLock = PR_NewLock();
234
 
  tix1 = PR_RegisterType(&type1);
235
 
  tix2 = PR_RegisterType(&type2);
236
 
  tix3 = PR_RegisterType(&type3);
237
 
  gcInfo = PR_GetGCInfo();
238
 
  rv = padMain(argc, argv);
239
 
  printf("PASS\n");
240
 
  PR_Cleanup();
241
 
  return rv;
242
 
}