Thu, 15 Jan 2015 21:03:48 +0100
Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "prstrms.h" |
michael@0 | 7 | |
michael@0 | 8 | #include "prinit.h" |
michael@0 | 9 | #include "prio.h" |
michael@0 | 10 | #include "prthread.h" |
michael@0 | 11 | |
michael@0 | 12 | #include <cstring> |
michael@0 | 13 | #include <iostream> |
michael@0 | 14 | |
michael@0 | 15 | #ifdef XP_UNIX |
michael@0 | 16 | #include <sys/stat.h> |
michael@0 | 17 | #endif |
michael@0 | 18 | |
michael@0 | 19 | using std::cout; |
michael@0 | 20 | using std::endl; |
michael@0 | 21 | using std::ios; |
michael@0 | 22 | |
michael@0 | 23 | const unsigned int MaxCnt = 1; |
michael@0 | 24 | |
michael@0 | 25 | typedef struct threadarg { |
michael@0 | 26 | const char *mytag; |
michael@0 | 27 | } threadarg; |
michael@0 | 28 | |
michael@0 | 29 | void threadwork(threadarg *arg); |
michael@0 | 30 | |
michael@0 | 31 | void |
michael@0 | 32 | threadmain(void *mytag) |
michael@0 | 33 | { |
michael@0 | 34 | threadarg arg; |
michael@0 | 35 | |
michael@0 | 36 | arg.mytag = static_cast<const char *>(mytag); |
michael@0 | 37 | |
michael@0 | 38 | threadwork(&arg); |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | void |
michael@0 | 42 | threadwork(threadarg *arg) |
michael@0 | 43 | { |
michael@0 | 44 | unsigned int i; |
michael@0 | 45 | |
michael@0 | 46 | char fname1[256]; |
michael@0 | 47 | char fname2[256]; |
michael@0 | 48 | |
michael@0 | 49 | strcpy(fname1, arg->mytag); |
michael@0 | 50 | strcpy(fname2, arg->mytag); |
michael@0 | 51 | strcat(fname2, "2"); |
michael@0 | 52 | PR_Delete(fname1); |
michael@0 | 53 | PR_Delete(fname2); |
michael@0 | 54 | |
michael@0 | 55 | PRfilebuf *fb[MaxCnt]; |
michael@0 | 56 | PRifstream *ifs[MaxCnt]; |
michael@0 | 57 | PRofstream *ofs[MaxCnt]; |
michael@0 | 58 | int mode = 0; |
michael@0 | 59 | #ifdef XP_UNIX |
michael@0 | 60 | mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH; |
michael@0 | 61 | #endif |
michael@0 | 62 | |
michael@0 | 63 | // |
michael@0 | 64 | // Allocate a bunch |
michael@0 | 65 | cout << "Testing unused filebufs ----------------" << endl; |
michael@0 | 66 | for (i=0; i < MaxCnt; i++){ |
michael@0 | 67 | fb[i] = new PRfilebuf; |
michael@0 | 68 | } |
michael@0 | 69 | // Delete them |
michael@0 | 70 | for (i=0; i < MaxCnt; i++){ |
michael@0 | 71 | delete fb[i]; |
michael@0 | 72 | } |
michael@0 | 73 | cout << "Unused filebufs complete ---------------" << endl; |
michael@0 | 74 | |
michael@0 | 75 | // |
michael@0 | 76 | // Allocate a bunch |
michael@0 | 77 | cout << "Testing unused ifstream -----------------" << endl; |
michael@0 | 78 | for (i=0; i < MaxCnt; i++){ |
michael@0 | 79 | ifs[i] = new PRifstream; |
michael@0 | 80 | } |
michael@0 | 81 | // |
michael@0 | 82 | // Delete them |
michael@0 | 83 | for (i=0; i < MaxCnt; i++){ |
michael@0 | 84 | delete ifs[i]; |
michael@0 | 85 | } |
michael@0 | 86 | cout << "Unused ifstream complete ----------------" << endl; |
michael@0 | 87 | // |
michael@0 | 88 | // Allocate a bunch |
michael@0 | 89 | cout << "Testing unused ofstream -----------------" << endl; |
michael@0 | 90 | for (i=0; i < MaxCnt; i++){ |
michael@0 | 91 | ofs[i] = new PRofstream; |
michael@0 | 92 | } |
michael@0 | 93 | for (i=0; i < MaxCnt; i++){ |
michael@0 | 94 | *(ofs[i]) << "A"; // Write a bit |
michael@0 | 95 | delete ofs[i]; // Delete it. |
michael@0 | 96 | } |
michael@0 | 97 | cout << "Unused ofstream complete ----------------" << endl; |
michael@0 | 98 | |
michael@0 | 99 | cout << "Testing use of ofstream 1 (extra filebuf allocated) ---------" << endl; |
michael@0 | 100 | PRofstream *aos = new PRofstream(fname1, ios::out|ios::ate, mode); |
michael@0 | 101 | for (i=0; i < MaxCnt; i++){ |
michael@0 | 102 | for (int j=0; j < 8192; j++) |
michael@0 | 103 | *aos << "AaBbCcDdEeFfGg" << endl; |
michael@0 | 104 | fb[i] = new PRfilebuf; // Allocate as we go to hack at the heap |
michael@0 | 105 | } |
michael@0 | 106 | // |
michael@0 | 107 | // Delete the extra foo we allocated |
michael@0 | 108 | for (i=0; i < MaxCnt; i++){ |
michael@0 | 109 | delete fb[i]; |
michael@0 | 110 | } |
michael@0 | 111 | aos->flush(); // Explicit flush |
michael@0 | 112 | delete aos; |
michael@0 | 113 | cout << "Testing use of ofstream 1 complete (extra filebuf deleted) --" << endl; |
michael@0 | 114 | cout << "Testing use of ofstream 2 (extra filebuf allocated) ---------" << endl; |
michael@0 | 115 | PRofstream *aos2 = new PRofstream(fname2, ios::out, mode); |
michael@0 | 116 | |
michael@0 | 117 | for (i=0; i < MaxCnt; i++){ |
michael@0 | 118 | *aos2 << "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"; |
michael@0 | 119 | } |
michael@0 | 120 | // Force flushing in the dtor |
michael@0 | 121 | delete aos2; |
michael@0 | 122 | cout << "Testing use of ofstream 2 complete (extra filebuf deleted) --" << endl; |
michael@0 | 123 | char line[1024]; |
michael@0 | 124 | cout << "Testing use of ifstream 1 (stack allocation) -------------" << endl; |
michael@0 | 125 | PRifstream ais(fname1); |
michael@0 | 126 | for (i=0; i < MaxCnt; i++){ |
michael@0 | 127 | ais >> line; |
michael@0 | 128 | } |
michael@0 | 129 | cout << "Testing use of ifstream 1 complete -----------------------" << endl; |
michael@0 | 130 | cout << "Testing use of ifstream 2 ----------------------" << endl; |
michael@0 | 131 | PRifstream *ais2 = new PRifstream(fname2); |
michael@0 | 132 | char achar; |
michael@0 | 133 | for (i=0; i < MaxCnt*10; i++){ |
michael@0 | 134 | *ais2 >> achar; |
michael@0 | 135 | } |
michael@0 | 136 | delete ais2; |
michael@0 | 137 | cout << "Testing use of ifstream 2 complete -------------" << endl; |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | #define STACKSIZE 1024*1024 |
michael@0 | 141 | int |
michael@0 | 142 | main() |
michael@0 | 143 | { |
michael@0 | 144 | PR_Init(PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 256); |
michael@0 | 145 | threadmain(const_cast<char *>("TestFile")); |
michael@0 | 146 | PRThread *thr1 = PR_CreateThread(PR_SYSTEM_THREAD, |
michael@0 | 147 | threadmain, |
michael@0 | 148 | const_cast<char *>("TestFile1"), |
michael@0 | 149 | PR_PRIORITY_NORMAL, |
michael@0 | 150 | PR_GLOBAL_THREAD, |
michael@0 | 151 | PR_JOINABLE_THREAD, |
michael@0 | 152 | STACKSIZE); |
michael@0 | 153 | PRThread *thr2 = PR_CreateThread(PR_SYSTEM_THREAD, |
michael@0 | 154 | threadmain, |
michael@0 | 155 | const_cast<char *>("TestFile2"), |
michael@0 | 156 | PR_PRIORITY_NORMAL, |
michael@0 | 157 | PR_GLOBAL_THREAD, |
michael@0 | 158 | PR_JOINABLE_THREAD, |
michael@0 | 159 | STACKSIZE); |
michael@0 | 160 | PRThread *thr3 = PR_CreateThread(PR_SYSTEM_THREAD, |
michael@0 | 161 | threadmain, |
michael@0 | 162 | const_cast<char *>("TestFile3"), |
michael@0 | 163 | PR_PRIORITY_NORMAL, |
michael@0 | 164 | PR_GLOBAL_THREAD, |
michael@0 | 165 | PR_JOINABLE_THREAD, |
michael@0 | 166 | STACKSIZE); |
michael@0 | 167 | PR_JoinThread(thr1); |
michael@0 | 168 | PR_JoinThread(thr2); |
michael@0 | 169 | PR_JoinThread(thr3); |
michael@0 | 170 | return 0; |
michael@0 | 171 | } |