diff options
-rw-r--r-- | TAO/performance-tests/Demux/CodeGen/gen_names.cpp | 233 | ||||
-rw-r--r-- | ace/SString.cpp | 112 |
2 files changed, 185 insertions, 160 deletions
diff --git a/TAO/performance-tests/Demux/CodeGen/gen_names.cpp b/TAO/performance-tests/Demux/CodeGen/gen_names.cpp index 77e7cdc8f54..79d452627b0 100644 --- a/TAO/performance-tests/Demux/CodeGen/gen_names.cpp +++ b/TAO/performance-tests/Demux/CodeGen/gen_names.cpp @@ -26,42 +26,40 @@ int Demux_Test_CodeGenerator::gen_poa_names (void) { this->state_ = Demux_Test_CodeGenerator::CG_POA; + if (this->gen_names (this->poa_array_, this->num_POAs_) == -1) - { - ACE_ERROR_RETURN ((LM_ERROR, - "(%n:%l) Demux_Test_CodeGenerator::gen_poa_names - " - "Failed to generate poa names\n"), - -1); - } + ACE_ERROR_RETURN ((LM_ERROR, + "(%n:%l) Demux_Test_CodeGenerator::gen_poa_names - " + "Failed to generate poa names\n"), + -1); // We store the generated names in a file - FILE *fp; char poa_file [128]; - ACE_OS::sprintf (poa_file, "poa_names_%d.dat", this->num_POAs_); - if ((fp = ACE_OS::fopen (poa_file, "w")) == 0) - { - ACE_ERROR_RETURN ((LM_ERROR, - "(%n:%l) Demux_Test_CodeGenerator::gen_poa_names - " - "Failed to open file %s\n", poa_file), - -1); - } + FILE *fp = ACE_OS::fopen (poa_file, "w"); + + if (fp == 0) + ACE_ERROR_RETURN ((LM_ERROR, + "(%n:%l) Demux_Test_CodeGenerator::gen_poa_names - " + "Failed to open file %s\n", poa_file), + -1); - // now output the names + // Now output the names ACE_CString *str; ACE_Unbounded_Queue_Iterator <ACE_CString> iter (this->poa_array_); - // start with the first element - (void)iter.first (); - while (!iter.done ()) + // Start with the first element. + for (iter.first (); + !iter.done (); + (void) iter.advance ()) { - // grab the next element waiting to be grabbed + // grab the next element waiting to be grabbed. (void) iter.next (str); - ACE_OS::fprintf (fp, "%s\n", str->c_str ()); - - (void) iter.advance (); + ACE_OS::fprintf (fp, + "%s\n", + str->c_str ()); } ACE_OS::fclose (fp); @@ -69,48 +67,47 @@ Demux_Test_CodeGenerator::gen_poa_names (void) return 0; } -// object table generator +// Object table generator. + int Demux_Test_CodeGenerator::gen_object_names (void) { this->state_ = Demux_Test_CodeGenerator::CG_OBJ; - if (this->gen_names (this->obj_array_, this->num_objs_) == -1) - { - ACE_ERROR_RETURN ((LM_ERROR, - "(%n:%l) Demux_Test_CodeGenerator::gen_object_names - " - "Failed to generate object names\n"), - -1); - } + + if (this->gen_names (this->obj_array_, + this->num_objs_) == -1) + ACE_ERROR_RETURN ((LM_ERROR, + "(%n:%l) Demux_Test_CodeGenerator::gen_object_names - " + "Failed to generate object names\n"), + -1); return 0; } -// method table generator +// Method table generator. + int Demux_Test_CodeGenerator::gen_operation_names (void) { this->state_ = Demux_Test_CodeGenerator::CG_OP; - if (this->gen_names (this->op_array_, this->num_ops_) == -1) - { - ACE_ERROR_RETURN ((LM_ERROR, - "(%n:%l) Demux_Test_CodeGenerator::gen_object_names - " - "Failed to generate object names\n"), - -1); - } - - // save it at this location + if (this->gen_names (this->op_array_, + this->num_ops_) == -1) + ACE_ERROR_RETURN ((LM_ERROR, + "(%n:%l) Demux_Test_CodeGenerator::gen_object_names - " + "Failed to generate object names\n"), + -1); + + // Save it at this location. if (this->op_array_.enqueue_tail (ACE_CString ("shutdown")) == -1) - { - ACE_ERROR_RETURN ((LM_ERROR, - "(%n:%l) Demux_Test_CodeGenerator::gen_operation_names - " - "Failed to enqueue the shutdown operation\n"), - -1); - } - + ACE_ERROR_RETURN ((LM_ERROR, + "(%n:%l) Demux_Test_CodeGenerator::gen_operation_names - " + "Failed to enqueue the shutdown operation\n"), + -1); return 0; } -// generic unique name generator +// Generic unique name generator. + int Demux_Test_CodeGenerator::gen_names (ACE_Unbounded_Queue<ACE_CString> &arr, int limit) @@ -118,22 +115,19 @@ Demux_Test_CodeGenerator::gen_names (ACE_Unbounded_Queue<ACE_CString> &arr, int i; // loop index // Generate "limit" number of distinct strings + for (i = 0; i < limit; i++) - { - // inserts a new string at the tail in array "arr" - if (this->insert_unique_string (arr) == -1) - { - ACE_ERROR_RETURN ((LM_ERROR, - "(%n:%l) Demux_Test_CodeGenerator::gen_names - " - "Failed to insert unique name\n"), - -1); - } - } - + // inserts a new string at the tail in array "arr" + if (this->insert_unique_string (arr) == -1) + ACE_ERROR_RETURN ((LM_ERROR, + "(%n:%l) Demux_Test_CodeGenerator::gen_names - " + "Failed to insert unique name\n"), + -1); return 0; } -// generate a distinct string and insert it at the tail +// Generate a distinct string and insert it at the tail. + int Demux_Test_CodeGenerator::insert_unique_string (ACE_Unbounded_Queue<ACE_CString> &arr) { @@ -141,64 +135,62 @@ Demux_Test_CodeGenerator::insert_unique_string (ACE_Unbounded_Queue<ACE_CString> ACE_CString str; int exists = 1; - // get a random number between 3 and 32 - rnd = this->gen_rand (3,32); + // Get a random number between 3 and 32. + rnd = this->gen_rand (3, 32); - // This random number is used as a string length of the distinct string to be - // generated. + // This random number is used as a string length of the distinct + // string to be generated. do { if (this->create_string (rnd, str) == -1) - { - ACE_ERROR_RETURN ((LM_ERROR, - "(%n:%l) Demux_Test_CodeGenerator::insert_unique_string - " - "Failed to create string\n"), - -1); - } + ACE_ERROR_RETURN ((LM_ERROR, + "(%n:%l) Demux_Test_CodeGenerator::insert_unique_string - " + "Failed to create string\n"), + -1); - // if we are generating operations, we do not want to generate the - // operation "shutdown" that has special semantics - if ((this->state_ == Demux_Test_CodeGenerator::CG_OP) && - (!ACE_OS::strcmp (str.c_str (), "shutdown"))) - { - // repeat the loop since we cannot accept this string - continue; - } + // If we are generating operations, we do not want to generate + // the operation "shutdown" that has special semantics + + if (this->state_ == Demux_Test_CodeGenerator::CG_OP + && ACE_OS::strcmp (str.c_str (), "shutdown") == 0) + // repeat the loop since we cannot accept this string + continue; exists = this->is_present (arr, str); + if (exists == -1) - { - ACE_ERROR_RETURN ((LM_ERROR, - "(%n:%l) Demux_Test_CodeGenerator::insert_unique_string - " - "is_present check failed\n"), - -1); - } + ACE_ERROR_RETURN ((LM_ERROR, + "(%n:%l) Demux_Test_CodeGenerator::insert_unique_string - " + "is_present check failed\n"), + -1); } while (exists > 0); - // we are here implies that the generated string did not exist in the queue - // that has been created so far + // We are here implies that the generated string did not exist in + // the queue that has been created so far. - // save it at this location + // Save it at this location. if (arr.enqueue_tail (str) == -1) - { - ACE_ERROR_RETURN ((LM_ERROR, - "(%n:%l) Demux_Test_CodeGenerator::insert_unique_string - " - "Failed to enqueue\n"), - -1); - } + ACE_ERROR_RETURN ((LM_ERROR, + "(%n:%l) Demux_Test_CodeGenerator::insert_unique_string - " + "Failed to enqueue\n"), + -1); return 0; } -// generate a random number in the given limits +// Generate a random number in the given limits. + long Demux_Test_CodeGenerator::gen_rand (int low, int up) { + // @@ Vishal, this call to lrand48 isn't portable. Can you please + // try to replace it with a call from ACE_OS, e.g., ACE_OS::rand()? return (lrand48 () % (up - low + 1)) + low; } -// check if the given string is present in the array +// Check if the given string is present in the array + int Demux_Test_CodeGenerator:: is_present (ACE_Unbounded_Queue<ACE_CString> &arr, ACE_CString &s) @@ -208,56 +200,61 @@ Demux_Test_CodeGenerator:: is_present (ACE_Unbounded_Queue<ACE_CString> &arr, // start with the first element if (!iter.first ()) + // queue is empty + return 0; + + for (; + !iter.done (); + iter.advance (); { - // queue is empty - return 0; - } - while (!iter.done ()) - { - // grab the next element waiting to be grabbed + // Grab the next element waiting to be grabbed. (void) iter.next (str); // check if they match if (!str->compare (s)) return 1; // they match - - (void) iter.advance (); } + return 0; // not present } -// create a string of length = len +// Create a string of length = len. + int -Demux_Test_CodeGenerator::create_string (int len, ACE_CString &s) +Demux_Test_CodeGenerator::create_string (int len, + ACE_CString &s) { - int i; - char ch; - - for (i = 0; i < len; i++) + for (int i = 0; i < len; i++) { - ch = this->gen_valid_char (i); - s += ACE_CString (&ch); + char ch = this->gen_valid_char (i); + s += ACE_CString (ch); } + return 0; } -// Generate a valid character for a C++ variable +// Generate a valid character for a C++ variable. + char Demux_Test_CodeGenerator::gen_valid_char (int posn) { - // for posn == 0, we cannot return a digit or an _ as the first character as - // that does not constitute a valid IDL identifier + // For posn == 0, we cannot return a digit or an _ as the first + // character as that does not constitute a valid IDL identifier + char ch; - while (1) + + for (;;) { ch = this->gen_rand (48, 122); + if (isdigit (ch) || isupper (ch) || islower (ch) || (ch == '_')) { - if ((posn == 0) && (isdigit (ch) || (ch == '_'))) + if (posn == 0 && (isdigit (ch) || (ch == '_'))) continue; - break; //otherwise + break; // otherwise } } + return ch; } diff --git a/ace/SString.cpp b/ace/SString.cpp index b630b082d1a..226e5ca13e4 100644 --- a/ace/SString.cpp +++ b/ace/SString.cpp @@ -109,7 +109,7 @@ ACE_Tokenizer::next (void) LPTSTR next_token; // Skip all leading delimiters. - while (1) + for (;;) { // Check for end of string. if (buffer_[index_] == '\0') @@ -119,7 +119,9 @@ ACE_Tokenizer::next (void) return 0; } - if (this->is_delimiter (buffer_[index_], replace, replacement)) + if (this->is_delimiter (buffer_[index_], + replace, + replacement)) index_++; else break; @@ -132,7 +134,9 @@ ACE_Tokenizer::next (void) // A preserved region is it's own token. TCHAR stop; int strip; - if (this->is_preserve_designator (buffer_[index_], stop, strip)) + if (this->is_preserve_designator (buffer_[index_], + stop, + strip)) { while (++index_) { @@ -162,13 +166,15 @@ ACE_Tokenizer::next (void) } // Step through finding the next delimiter or EOS. - while (1) + for (;;) { // Advance pointer. index_++; // Check for delimiter. - if (this->is_delimiter (buffer_[index_], replace, replacement)) + if (this->is_delimiter (buffer_[index_], + replace, + replacement)) { // Replace the delimiter. if (replace != 0) @@ -180,7 +186,9 @@ ACE_Tokenizer::next (void) } // A preserve designator signifies the end of this token. - if (this->is_preserve_designator (buffer_[index_], stop, strip)) + if (this->is_preserve_designator (buffer_[index_], + stop, + strip)) goto EXIT_LABEL; // Check for end of string. @@ -258,6 +266,7 @@ ACE_CString::ACE_CString (const ACE_USHORT16 *s, // string. for (size_t i = 0; i < this->len_; i++) this->rep_[i] = char (s[i]); + this->rep_[this->len_] = '\0'; } } @@ -267,18 +276,16 @@ ACE_CString::set (const char *s, size_t len, int release) { - // // Free memory if necessary - // // Going from memory to no memory - if ((!release || s == 0 || s[0] == '\0') && - this->release_) + if ((!release || s == 0 || s[0] == '\0') + && this->release_) this->allocator_->free (this->rep_); // Going from memory to more memory - else if (this->len_ < len && - this->release_) + else if (this->len_ < len + && this->release_) this->allocator_->free (this->rep_); // Figure out future ownership @@ -287,28 +294,24 @@ ACE_CString::set (const char *s, else this->release_ = 1; - // // Allocate memory if owner and necessary - // - // len is greather than 0, so must allocate space for it. + // Len is greater than 0, so must allocate space for it. if (this->release_ && this->len_ < len) this->rep_ = (char *) this->allocator_->malloc (len + 1); // set new length this->len_ = len; - // If no string or null string is specified by the user + // If no string or null string is specified by the user. if (s == 0 || s[0] == '\0') this->rep_ = &ACE_CString::NULL_CString_; - // If we don't own the string + // If we don't own the string. else if (!this->release_) - { - this->rep_ = (char *) s; - } + this->rep_ = (char *) s; - // We own the string + // We own the string. else { ACE_OS::memcpy (this->rep_, s, len); @@ -337,7 +340,9 @@ ACE_CString::substring (size_t offset, if (length == -1) count = this->len_ - offset; - return ACE_CString (&this->rep_[offset], count, this->allocator_); + return ACE_CString (&this->rep_[offset], + count, + this->allocator_); } // Concat operator (does copy memory). @@ -359,9 +364,13 @@ ACE_CString::operator+= (const ACE_CString &s) (char *) this->allocator_->malloc (newlen), *this); // Copy memory from old string into new string. - ACE_OS::memcpy (t, this->rep_, oldlen); + ACE_OS::memcpy (t, + this->rep_, + oldlen); - ACE_OS::memcpy (t + oldlen, s.rep_, s.len_); + ACE_OS::memcpy (t + oldlen, + s.rep_, + s.len_); t[this->len_] = '\0'; if (this->release_) @@ -394,7 +403,9 @@ ACE_SString::ACE_SString (const ACE_SString &s) this->allocator_ = ACE_Allocator::instance (); this->rep_ = (char *) this->allocator_->malloc (s.len_ + 1); - ACE_OS::memcpy ((void *) this->rep_, (const void *) s.rep_, this->len_); + ACE_OS::memcpy ((void *) this->rep_, + (const void *) s.rep_, + this->len_); this->rep_[this->len_] = '\0'; } @@ -565,7 +576,8 @@ ACE_WString::ACE_WString (ACE_Allocator *alloc) this->allocator_ = ACE_Allocator::instance (); this->len_ = 0; - this->rep_ = (ACE_USHORT16 *) this->allocator_->malloc ((this->len_ + 1) * sizeof (ACE_USHORT16)); + this->rep_ = (ACE_USHORT16 *) this->allocator_->malloc + ((this->len_ + 1) * sizeof (ACE_USHORT16)); this->rep_[this->len_] = 0; } @@ -620,13 +632,15 @@ ACE_WString::ACE_WString (const char *s, if (s == 0) { this->len_ = 0; - this->rep_ = (ACE_USHORT16 *) this->allocator_->malloc ((this->len_ + 1) * sizeof (ACE_USHORT16)); + this->rep_ = (ACE_USHORT16 *) this->allocator_->malloc + ((this->len_ + 1) * sizeof (ACE_USHORT16)); this->rep_[this->len_] = 0; } else { this->len_ = ACE_OS::strlen (s); - this->rep_ = (ACE_USHORT16 *) this->allocator_->malloc ((this->len_ + 1) * sizeof (ACE_USHORT16)); + this->rep_ = (ACE_USHORT16 *) this->allocator_->malloc + ((this->len_ + 1) * sizeof (ACE_USHORT16)); // Copy the char * string byte-by-byte into the ACE_USHORT16 * // string. @@ -652,13 +666,15 @@ ACE_WString::ACE_WString (const ACE_USHORT16 *s, if (s == 0) { this->len_ = 0; - this->rep_ = (ACE_USHORT16 *) this->allocator_->malloc ((this->len_ + 1) * sizeof (ACE_USHORT16)); + this->rep_ = (ACE_USHORT16 *) this->allocator_->malloc + ((this->len_ + 1) * sizeof (ACE_USHORT16)); this->rep_[this->len_] = 0; } else { this->len_ = ACE_WString::strlen (s); - this->rep_ = (ACE_USHORT16 *) this->allocator_->malloc ((this->len_ + 1) * sizeof (ACE_USHORT16)); + this->rep_ = (ACE_USHORT16 *) this->allocator_->malloc + ((this->len_ + 1) * sizeof (ACE_USHORT16)); ACE_OS::memcpy (this->rep_, s, @@ -679,7 +695,8 @@ ACE_WString::ACE_WString (ACE_USHORT16 c, this->allocator_ = ACE_Allocator::instance (); this->len_ = 1; - this->rep_ = (ACE_USHORT16 *) this->allocator_->malloc ((this->len_ + 1) * sizeof (ACE_USHORT16)); + this->rep_ = (ACE_USHORT16 *) this->allocator_->malloc + ((this->len_ + 1) * sizeof (ACE_USHORT16)); this->rep_[0] = c; this->rep_[this->len_] = 0; } @@ -699,15 +716,19 @@ ACE_WString::ACE_WString (const ACE_USHORT16 *s, if (s == 0) { this->len_ = 0; - this->rep_ = (ACE_USHORT16 *) this->allocator_->malloc ((this->len_ + 1) * sizeof (ACE_USHORT16)); + this->rep_ = (ACE_USHORT16 *) this->allocator_->malloc + ((this->len_ + 1) * sizeof (ACE_USHORT16)); this->rep_[this->len_] = 0; } else { this->len_ = len; - this->rep_ = (ACE_USHORT16 *) this->allocator_->malloc ((this->len_ + 1) * sizeof (ACE_USHORT16)); + this->rep_ = (ACE_USHORT16 *) this->allocator_->malloc + ((this->len_ + 1) * sizeof (ACE_USHORT16)); - ACE_OS::memcpy (this->rep_, s, len * sizeof (ACE_USHORT16)); + ACE_OS::memcpy (this->rep_, + s, + len * sizeof (ACE_USHORT16)); // null terminate this->rep_[this->len_] = 0; @@ -726,12 +747,12 @@ ACE_WString::ACE_WString (size_t len, this->allocator_ = ACE_Allocator::instance (); this->len_ = len; - this->rep_ = (ACE_USHORT16 *) this->allocator_->malloc ((this->len_ + 1) * sizeof (ACE_USHORT16)); + this->rep_ = (ACE_USHORT16 *) this->allocator_->malloc + ((this->len_ + 1) * sizeof (ACE_USHORT16)); ACE_OS::memset (this->rep_, 0, len * sizeof (ACE_USHORT16)); - // NUL terminate. this->rep_[this->len_] = 0; } @@ -747,7 +768,8 @@ ACE_WString::ACE_WString (const ACE_WString &s) if (this->allocator_ == 0) this->allocator_ = ACE_Allocator::instance (); - this->rep_ = (ACE_USHORT16 *) this->allocator_->malloc ((s.len_ + 1) * sizeof (ACE_USHORT16)); + this->rep_ = (ACE_USHORT16 *) this->allocator_->malloc + ((s.len_ + 1) * sizeof (ACE_USHORT16)); ACE_OS::memcpy ((void *) this->rep_, (const void *) s.rep_, this->len_ * sizeof (ACE_USHORT16)); @@ -781,11 +803,14 @@ ACE_WString::set (const ACE_USHORT16 *s, size_t len) if (this->len_ < len) { this->allocator_->free (this->rep_); - this->rep_ = (ACE_USHORT16 *) this->allocator_->malloc ((len + 1) * sizeof (ACE_USHORT16)); + this->rep_ = (ACE_USHORT16 *) this->allocator_->malloc + ((len + 1) * sizeof (ACE_USHORT16)); } this->len_ = len; - ACE_OS::memcpy (this->rep_, s, len * sizeof (ACE_USHORT16)); + ACE_OS::memcpy (this->rep_, + s, + len * sizeof (ACE_USHORT16)); // NUL terminate. this->rep_[len] = 0; } @@ -824,7 +849,8 @@ ACE_WString::resize (size_t len) if (this->len_ < len) { this->allocator_->free (this->rep_); - this->rep_ = (ACE_USHORT16 *) this->allocator_->malloc ((len + 1) * sizeof (ACE_USHORT16)); + this->rep_ = (ACE_USHORT16 *) this->allocator_->malloc + ((len + 1) * sizeof (ACE_USHORT16)); } this->len_ = len; @@ -840,7 +866,8 @@ ACE_WString & ACE_WString::operator+= (const ACE_WString &s) { ACE_TRACE ("ACE_WString::operator+="); - ACE_USHORT16 *t = (ACE_USHORT16 *) this->allocator_->malloc ((this->len_ + s.len_ + 1) * sizeof (ACE_USHORT16)); + ACE_USHORT16 *t = (ACE_USHORT16 *) this->allocator_->malloc + ((this->len_ + s.len_ + 1) * sizeof (ACE_USHORT16)); ACE_OS::memcpy ((void *) t, (const void *) this->rep_, @@ -867,7 +894,8 @@ ACE_WString::~ACE_WString (void) /* static */ const ACE_USHORT16 * -ACE_WString::strstr (const ACE_USHORT16 *s1, const ACE_USHORT16 *s2) +ACE_WString::strstr (const ACE_USHORT16 *s1, + const ACE_USHORT16 *s2) { ACE_TRACE ("ACE_WString::strstr"); |