diff --git a/CHANGES b/CHANGES index 59b35511d5..44c6a83484 100644 --- a/CHANGES +++ b/CHANGES @@ -1,4 +1,35 @@ +2.6-612 | 2019-07-15 19:46:04 -0700 + + * Fix a potential usage of List::remove_nth(-1) (Jon Siwek, Corelight) + + * Change List::remote(const T&) to return a bool (Jon Siwek, Corelight) + + It now indicates whether the removal took place or not, depending + on whether a matching element was found in the list. + + * Fix debug build due to old int_list usage within assert (Jon Siwek, Corelight) + + * Convert uses of loop_over_list to ranged-for loops (Tim Wojtulewicz, Corelight) + + * Remove loop_over_queue (as an example for later removing loop_over_list) (Tim Wojtulewicz, Corelight) + + * Change int_list in CCL.h to be a vector, fix uses of int_list to match (Tim Wojtulewicz, Corelight) + + * Remove List<> usage from strings.bif (Tim Wojtulewicz, Corelight) + + * Replace uses of the old Queue/PQueue generation code with new template versions (Tim Wojtulewicz, Corelight) + + * Convert BaseQueue/Queue/PQueue into templates, including iterator support (Tim Wojtulewicz, Corelight) + + * Replace uses of the old Dict generation code with new template versions (Tim Wojtulewicz, Corelight) + + * Convert PDict into template (Tim Wojtulewicz, Corelight) + + * Replace uses of the old List generation code with new template versions (Tim Wojtulewicz, Corelight) + + * Convert BaseList/List/PList into templates, including iterator support (Tim Wojtulewicz, Corelight) + 2.6-598 | 2019-07-12 18:20:12 -0700 * Fix canonification of timestamps with a decisecond multiple (Jon Siwek, Corelight) diff --git a/NEWS b/NEWS index a4e19ab60e..892be5e160 100644 --- a/NEWS +++ b/NEWS @@ -357,6 +357,27 @@ Changed Functionality are small changes to the output; most visibly double numbers are now rounded slightly differently. The way in which port values are rendered does _not_ change for JSON logs. +- The C++-layer List, Queue, and Dict types have changed from using macros to + templates as well as some other API changes. + + - Range-based for-loops are now supported + + - The loop_over_queue macro is now removed + + - PList is now a template instead of a macro, so any "PList(T)" usages in + external code should now use "PList" + + - PDict is now a template instead of a macro, so any "PDict(T)" usages in + external code should now use "PDict" + + - Generally some methods used to assume containers were only using integer + or pointer types, so semantics may now be slightly different to + either avoid copying or unsafely returning arbitrary T types by value. + E.g. List::remove_nth and List::get can no longer return a "null" value + when the provided index is out of range, so they assert instead, and + Queue::pop methods do not return a value at all (one must check for + a non-empty container before removing an element). + Removed Functionality --------------------- diff --git a/VERSION b/VERSION index eec6d0de55..e6aa2f840a 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.6-598 +2.6-612 diff --git a/src/Attr.cc b/src/Attr.cc index ce93061cef..3001438bc3 100644 --- a/src/Attr.cc +++ b/src/Attr.cc @@ -143,16 +143,16 @@ Attributes::Attributes(attr_list* a, BroType* t, bool arg_in_record, bool is_glo // rather than just taking over 'a' for ourselves, so that // the necessary checking gets done. - loop_over_list(*a, i) - AddAttr((*a)[i]); + for ( const auto& attr : *a ) + AddAttr(attr); delete a; } Attributes::~Attributes() { - loop_over_list(*attrs, i) - Unref((*attrs)[i]); + for ( const auto& attr : *attrs ) + Unref(attr); delete attrs; @@ -189,8 +189,8 @@ void Attributes::AddAttr(Attr* attr) void Attributes::AddAttrs(Attributes* a) { attr_list* as = a->Attrs(); - loop_over_list(*as, i) - AddAttr((*as)[i]); + for ( const auto& attr : *as ) + AddAttr(attr); Unref(a); } @@ -200,9 +200,8 @@ Attr* Attributes::FindAttr(attr_tag t) const if ( ! attrs ) return 0; - loop_over_list(*attrs, i) + for ( const auto& a : *attrs ) { - Attr* a = (*attrs)[i]; if ( a->Tag() == t ) return a; } @@ -404,9 +403,8 @@ void Attributes::CheckAttr(Attr* a) int num_expires = 0; if ( attrs ) { - loop_over_list(*attrs, i) + for ( const auto& a : *attrs ) { - Attr* a = (*attrs)[i]; if ( a->Tag() == ATTR_EXPIRE_READ || a->Tag() == ATTR_EXPIRE_WRITE || a->Tag() == ATTR_EXPIRE_CREATE ) @@ -519,9 +517,8 @@ bool Attributes::operator==(const Attributes& other) const if ( ! other.attrs ) return false; - loop_over_list(*attrs, i) + for ( const auto& a : *attrs ) { - Attr* a = (*attrs)[i]; Attr* o = other.FindAttr(a->Tag()); if ( ! o ) @@ -531,9 +528,8 @@ bool Attributes::operator==(const Attributes& other) const return false; } - loop_over_list(*other.attrs, j) + for ( const auto& o : *other.attrs ) { - Attr* o = (*other.attrs)[j]; Attr* a = FindAttr(o->Tag()); if ( ! a ) diff --git a/src/BroList.h b/src/BroList.h index 0aa94d55ec..d4d3451a24 100644 --- a/src/BroList.h +++ b/src/BroList.h @@ -6,31 +6,24 @@ #include "List.h" class Expr; -declare(PList,Expr); -typedef PList(Expr) expr_list; +typedef PList expr_list; class ID; -declare(PList,ID); -typedef PList(ID) id_list; +typedef PList id_list; class Val; -declare(PList,Val); -typedef PList(Val) val_list; +typedef PList val_list; class Stmt; -declare(PList,Stmt); -typedef PList(Stmt) stmt_list; +typedef PList stmt_list; class BroType; -declare(PList,BroType); -typedef PList(BroType) type_list; +typedef PList type_list; class Attr; -declare(PList,Attr); -typedef PList(Attr) attr_list; +typedef PList attr_list; class Timer; -declare(PList,Timer); -typedef PList(Timer) timer_list; +typedef PList timer_list; #endif diff --git a/src/CCL.cc b/src/CCL.cc index 86ca2a03da..34809843ef 100644 --- a/src/CCL.cc +++ b/src/CCL.cc @@ -2,6 +2,8 @@ #include "zeek-config.h" +#include + #include "CCL.h" #include "RE.h" #include "DFA.h" @@ -30,14 +32,14 @@ void CCL::Add(int sym) ptr_compat_int sym_p = ptr_compat_int(sym); // Check to see if the character is already in the ccl. - for ( int i = 0; i < syms->length(); ++i ) - if ( (*syms)[i] == sym_p ) + for ( auto sym : *syms ) + if ( sym == sym_p ) return; - syms->append(sym_p); + syms->push_back(sym_p); } void CCL::Sort() { - syms->sort(int_list_cmp); + std::sort(syms->begin(), syms->end()); } diff --git a/src/CCL.h b/src/CCL.h index 2870acf53a..a842b15fe6 100644 --- a/src/CCL.h +++ b/src/CCL.h @@ -3,10 +3,10 @@ #ifndef ccl_h #define ccl_h +#include #include "List.h" -declare(List,ptr_compat_int); -typedef List(ptr_compat_int) int_list; +typedef std::vector int_list; class CCL { public: @@ -26,7 +26,7 @@ public: { delete syms; syms = new_syms; } unsigned int MemoryAllocation() const - { return padded_sizeof(*this) + syms->MemoryAllocation(); } + { return padded_sizeof(*this) + padded_sizeof(*syms) + pad_size(syms->size() * sizeof(int_list::value_type)); } protected: int_list* syms; diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a9d5501718..bc236a1ffb 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -261,7 +261,6 @@ set(MAIN_SRCS IntSet.cc IP.cc IPAddr.cc - List.cc Reporter.cc NFA.cc Net.cc @@ -273,7 +272,6 @@ set(MAIN_SRCS PolicyFile.cc PrefixTable.cc PriorityQueue.cc - Queue.cc RandTest.cc RE.cc Reassem.cc diff --git a/src/CompHash.cc b/src/CompHash.cc index 4e5366edde..74186f2615 100644 --- a/src/CompHash.cc +++ b/src/CompHash.cc @@ -675,10 +675,10 @@ ListVal* CompositeHash::RecoverVals(const HashKey* k) const const char* kp = (const char*) k->Key(); const char* const k_end = kp + k->Size(); - loop_over_list(*tl, i) + for ( const auto& type : *tl ) { Val* v = nullptr; - kp = RecoverOneVal(k, kp, k_end, (*tl)[i], v, false); + kp = RecoverOneVal(k, kp, k_end, type, v, false); ASSERT(v); l->Append(v); } diff --git a/src/Conn.cc b/src/Conn.cc index dc510038f0..a6247e378a 100644 --- a/src/Conn.cc +++ b/src/Conn.cc @@ -481,8 +481,8 @@ void Connection::ConnectionEvent(EventHandlerPtr f, analyzer::Analyzer* a, val_l { // This may actually happen if there is no local handler // and a previously existing remote handler went away. - loop_over_list(vl, i) - Unref(vl[i]); + for ( const auto& v : vl) + Unref(v); return; } @@ -540,11 +540,10 @@ void Connection::CancelTimers() // traversing. Thus, we first make a copy of the list which we then // iterate through. timer_list tmp(timers.length()); - loop_over_list(timers, j) - tmp.append(timers[j]); + std::copy(timers.begin(), timers.end(), std::back_inserter(tmp)); - loop_over_list(tmp, i) - GetTimerMgr()->Cancel(tmp[i]); + for ( const auto& timer : tmp ) + GetTimerMgr()->Cancel(timer); timers_canceled = 1; timers.clear(); diff --git a/src/Continuation.h b/src/Continuation.h index 009d2a87f3..373e049f98 100644 --- a/src/Continuation.h +++ b/src/Continuation.h @@ -50,8 +50,7 @@ private: int current_level; int suspend_level; - declare(PList, void); - typedef PList(void) voidp_list; + typedef PList voidp_list; voidp_list states; }; diff --git a/src/DFA.cc b/src/DFA.cc index 448307e3fe..26562f8e55 100644 --- a/src/DFA.cc +++ b/src/DFA.cc @@ -108,11 +108,11 @@ DFA_State* DFA_State::ComputeXtion(int sym, DFA_Machine* machine) void DFA_State::AppendIfNew(int sym, int_list* sym_list) { - for ( int i = 0; i < sym_list->length(); ++i ) - if ( (*sym_list)[i] == sym ) + for ( auto value : *sym_list ) + if ( value == sym ) return; - sym_list->append(sym); + sym_list->push_back(sym); } NFA_state_list* DFA_State::SymFollowSet(int ec_sym, const EquivClass* ec) @@ -132,8 +132,8 @@ NFA_state_list* DFA_State::SymFollowSet(int ec_sym, const EquivClass* ec) if ( ccl->IsNegated() ) { - int j; - for ( j = 0; j < syms->length(); ++j ) + size_t j; + for ( j = 0; j < syms->size(); ++j ) { // Loop through (sorted) negated // character class, which has @@ -143,25 +143,25 @@ NFA_state_list* DFA_State::SymFollowSet(int ec_sym, const EquivClass* ec) break; } - if ( j >= syms->length() || (*syms)[j] > ec_sym ) + if ( j >= syms->size() || (*syms)[j] > ec_sym ) // Didn't find ec_sym in ccl. n->AddXtionsTo(ns); continue; } - for ( int j = 0; j < syms->length(); ++j ) + for ( auto sym : *syms ) { - if ( (*syms)[j] > ec_sym ) + if ( sym > ec_sym ) break; - if ( (*syms)[j] == ec_sym ) + if ( sym == ec_sym ) { n->AddXtionsTo(ns); break; } } - } + } else if ( n->TransSym() == SYM_EPSILON ) { // do nothing diff --git a/src/DFA.h b/src/DFA.h index 1b58774da0..7e17f9a1a8 100644 --- a/src/DFA.h +++ b/src/DFA.h @@ -105,10 +105,8 @@ private: int hits; // Statistics int misses; - declare(PDict,CacheEntry); - // Hash indexed by NFA states (MD5s of them, actually). - PDict(CacheEntry) states; + PDict states; }; class DFA_Machine : public BroObj { diff --git a/src/DNS_Mgr.h b/src/DNS_Mgr.h index 39f728c812..96e73779e8 100644 --- a/src/DNS_Mgr.h +++ b/src/DNS_Mgr.h @@ -23,14 +23,11 @@ class EventHandler; class RecordType; class DNS_Mgr_Request; -declare(PList,DNS_Mgr_Request); -typedef PList(DNS_Mgr_Request) DNS_mgr_request_list; +typedef PList DNS_mgr_request_list; struct nb_dns_info; struct nb_dns_result; -declare(PDict,ListVal); - class DNS_Mapping; enum DNS_MgrMode { @@ -144,7 +141,7 @@ protected: DNS_MgrMode mode; - PDict(ListVal) services; + PDict services; HostMap host_mappings; AddrMap addr_mappings; diff --git a/src/Debug.cc b/src/Debug.cc index bf23ff105b..de84471691 100644 --- a/src/Debug.cc +++ b/src/Debug.cc @@ -27,7 +27,7 @@ using namespace std; bool g_policy_debug = false; DebuggerState g_debugger_state; TraceState g_trace_state; -PDict(Filemap) g_dbgfilemaps; +PDict g_dbgfilemaps; // These variables are used only to decide whether or not to print the // current context; you don't want to do it after a step or next @@ -378,10 +378,9 @@ vector parse_location_string(const string& s) } StmtLocMapping* hit = 0; - loop_over_queue(*map, i) + for ( const auto entry : *map ) { - StmtLocMapping* entry = (*map)[i]; - plr.filename = (*map)[i]->Loc().filename; + plr.filename = entry->Loc().filename; if ( entry->Loc().first_line > plr.line ) break; @@ -389,7 +388,7 @@ vector parse_location_string(const string& s) if ( plr.line >= entry->Loc().first_line && plr.line <= entry->Loc().last_line ) { - hit = (*map)[i]; + hit = entry; break; } } diff --git a/src/Debug.h b/src/Debug.h index 89884cfb0b..7e414262c9 100644 --- a/src/Debug.h +++ b/src/Debug.h @@ -29,9 +29,7 @@ struct ParseLocationRec { #include "DbgBreakpoint.h" class StmtLocMapping; -declare(PQueue,StmtLocMapping); -typedef PQueue(StmtLocMapping) Filemap; // mapping for a single file -declare(PDict,Filemap); +typedef PQueue Filemap; // mapping for a single file class DbgBreakpoint; class DbgWatch; @@ -178,7 +176,7 @@ string get_context_description(const Stmt* stmt, const Frame* frame); extern Frame* g_dbg_locals; // variables created within debugger context -extern PDict(Filemap) g_dbgfilemaps; // filename => filemap +extern PDict g_dbgfilemaps; // filename => filemap // Perhaps add a code/priority argument to do selective output. int debug_msg(const char* fmt, ...) __attribute__ ((format (printf, 1, 2))); diff --git a/src/DebugCmds.cc b/src/DebugCmds.cc index d11efb0390..dcba46d195 100644 --- a/src/DebugCmds.cc +++ b/src/DebugCmds.cc @@ -51,7 +51,7 @@ void lookup_global_symbols_regex(const string& orig_regex, vector& matches, } Scope* global = global_scope(); - PDict(ID)* syms = global->Vars(); + PDict* syms = global->Vars(); ID* nextid; IterCookie* cookie = syms->InitForIteration(); @@ -114,7 +114,7 @@ void choose_global_symbols_regex(const string& regex, vector& choices, // DebugCmdInfo implementation // -PQueue(DebugCmdInfo) g_DebugCmdInfos; +PQueue g_DebugCmdInfos; DebugCmdInfo::DebugCmdInfo(const DebugCmdInfo& info) : cmd(info.cmd), helpstring(0) diff --git a/src/DebugCmds.h b/src/DebugCmds.h index e7b9c6a4c1..1c6c243786 100644 --- a/src/DebugCmds.h +++ b/src/DebugCmds.h @@ -12,9 +12,6 @@ using namespace std; #include "Queue.h" #include "DebugCmdConstants.h" -class DebugCmdInfo; -declare(PQueue,DebugCmdInfo); - class DebugCmdInfo { public: DebugCmdInfo(const DebugCmdInfo& info); @@ -47,7 +44,7 @@ protected: bool repeatable; }; -extern PQueue(DebugCmdInfo) g_DebugCmdInfos; +extern PQueue g_DebugCmdInfos; void init_global_dbg_constants (); diff --git a/src/Dict.cc b/src/Dict.cc index 02886c6d5d..d3eea6591f 100644 --- a/src/Dict.cc +++ b/src/Dict.cc @@ -50,9 +50,9 @@ public: } int bucket, offset; - PList(DictEntry)** ttbl; + PList** ttbl; const int* num_buckets_p; - PList(DictEntry) inserted; // inserted while iterating + PList inserted; // inserted while iterating }; Dictionary::Dictionary(dict_order ordering, int initial_size) @@ -61,7 +61,7 @@ Dictionary::Dictionary(dict_order ordering, int initial_size) tbl2 = 0; if ( ordering == ORDERED ) - order = new PList(DictEntry); + order = new PList; else order = 0; @@ -99,10 +99,9 @@ void Dictionary::DeInit() for ( int i = 0; i < num_buckets; ++i ) if ( tbl[i] ) { - PList(DictEntry)* chain = tbl[i]; - loop_over_list(*chain, j) + PList* chain = tbl[i]; + for ( const auto& e : *chain ) { - DictEntry* e = (*chain)[j]; if ( delete_func ) delete_func(e->value); delete e; @@ -119,10 +118,9 @@ void Dictionary::DeInit() for ( int i = 0; i < num_buckets2; ++i ) if ( tbl2[i] ) { - PList(DictEntry)* chain = tbl2[i]; - loop_over_list(*chain, j) + PList* chain = tbl2[i]; + for ( const auto& e : *chain ) { - DictEntry* e = (*chain)[j]; if ( delete_func ) delete_func(e->value); delete e; @@ -141,7 +139,7 @@ void* Dictionary::Lookup(const void* key, int key_size, hash_t hash) const return 0; hash_t h; - PList(DictEntry)* chain; + PList* chain; // Figure out which hash table to look in. h = hash % num_buckets; @@ -199,7 +197,7 @@ void* Dictionary::Remove(const void* key, int key_size, hash_t hash, return 0; hash_t h; - PList(DictEntry)* chain; + PList* chain; int* num_entries_ptr; // Figure out which hash table to look in @@ -240,7 +238,7 @@ void* Dictionary::Remove(const void* key, int key_size, hash_t hash, } void* Dictionary::DoRemove(DictEntry* entry, hash_t h, - PList(DictEntry)* chain, int chain_offset) + PList* chain, int chain_offset) { void* entry_value = entry->value; @@ -249,10 +247,8 @@ void* Dictionary::DoRemove(DictEntry* entry, hash_t h, order->remove(entry); // Adjust existing cookies. - loop_over_list(cookies, i) + for ( const auto& c : cookies ) { - IterCookie* c = cookies[i]; - // Is the affected bucket the current one? if ( (unsigned int) c->bucket == h ) { @@ -301,7 +297,7 @@ void* Dictionary::NextEntry(HashKey*& h, IterCookie*& cookie, int return_hash) c { if ( ! tbl && ! tbl2 ) { - const_cast(&cookies)->remove(cookie); + const_cast*>(&cookies)->remove(cookie); delete cookie; cookie = 0; return 0; @@ -326,7 +322,7 @@ void* Dictionary::NextEntry(HashKey*& h, IterCookie*& cookie, int return_hash) c int b = cookie->bucket; int o = cookie->offset; - PList(DictEntry)** ttbl; + PList** ttbl; const int* num_buckets_p; if ( ! cookie->ttbl ) @@ -368,7 +364,7 @@ void* Dictionary::NextEntry(HashKey*& h, IterCookie*& cookie, int return_hash) c // FIXME: I don't like removing the const here. But is there // a better way? - const_cast(&cookies)->remove(cookie); + const_cast*>(&cookies)->remove(cookie); delete cookie; cookie = 0; return 0; @@ -387,7 +383,7 @@ void* Dictionary::NextEntry(HashKey*& h, IterCookie*& cookie, int return_hash) c void Dictionary::Init(int size) { num_buckets = NextPrime(size); - tbl = new PList(DictEntry)*[num_buckets]; + tbl = new PList*[num_buckets]; for ( int i = 0; i < num_buckets; ++i ) tbl[i] = 0; @@ -399,7 +395,7 @@ void Dictionary::Init(int size) void Dictionary::Init2(int size) { num_buckets2 = NextPrime(size); - tbl2 = new PList(DictEntry)*[num_buckets2]; + tbl2 = new PList*[num_buckets2]; for ( int i = 0; i < num_buckets2; ++i ) tbl2[i] = 0; @@ -413,7 +409,7 @@ void* Dictionary::Insert(DictEntry* new_entry, int copy_key) if ( ! tbl ) Init(DEFAULT_DICT_SIZE); - PList(DictEntry)** ttbl; + PList** ttbl; int* num_entries_ptr; int* max_num_entries_ptr; hash_t h = new_entry->hash % num_buckets; @@ -438,7 +434,7 @@ void* Dictionary::Insert(DictEntry* new_entry, int copy_key) max_num_entries_ptr = &max_num_entries2; } - PList(DictEntry)* chain = ttbl[h]; + PList* chain = ttbl[h]; int n = new_entry->len; @@ -460,7 +456,7 @@ void* Dictionary::Insert(DictEntry* new_entry, int copy_key) } else // Create new chain. - chain = ttbl[h] = new PList(DictEntry); + chain = ttbl[h] = new PList; // If we got this far, then we couldn't use an existing copy // of the key, so make a new one if necessary. @@ -482,9 +478,8 @@ void* Dictionary::Insert(DictEntry* new_entry, int copy_key) // For ongoing iterations: If we already passed the bucket where this // entry was put, add it to the cookie's list of inserted entries. - loop_over_list(cookies, i) + for ( const auto& c : cookies ) { - IterCookie* c = cookies[i]; if ( h < (unsigned int) c->bucket ) c->inserted.append(new_entry); } @@ -545,7 +540,7 @@ void Dictionary::MoveChains() do { - PList(DictEntry)* chain = tbl[tbl_next_ind++]; + PList* chain = tbl[tbl_next_ind++]; if ( ! chain ) continue; @@ -605,13 +600,13 @@ unsigned int Dictionary::MemoryAllocation() const for ( int i = 0; i < num_buckets; ++i ) if ( tbl[i] ) { - PList(DictEntry)* chain = tbl[i]; - loop_over_list(*chain, j) - size += padded_sizeof(DictEntry) + pad_size((*chain)[j]->len); + PList* chain = tbl[i]; + for ( const auto& c : *chain ) + size += padded_sizeof(DictEntry) + pad_size(c->len); size += chain->MemoryAllocation(); } - size += pad_size(num_buckets * sizeof(PList(DictEntry)*)); + size += pad_size(num_buckets * sizeof(PList*)); if ( order ) size += order->MemoryAllocation(); @@ -621,13 +616,13 @@ unsigned int Dictionary::MemoryAllocation() const for ( int i = 0; i < num_buckets2; ++i ) if ( tbl2[i] ) { - PList(DictEntry)* chain = tbl2[i]; - loop_over_list(*chain, j) - size += padded_sizeof(DictEntry) + pad_size((*chain)[j]->len); + PList* chain = tbl2[i]; + for ( const auto& c : *chain ) + size += padded_sizeof(DictEntry) + pad_size(c->len); size += chain->MemoryAllocation(); } - size += pad_size(num_buckets2 * sizeof(PList(DictEntry)*)); + size += pad_size(num_buckets2 * sizeof(PList*)); } return size; diff --git a/src/Dict.h b/src/Dict.h index 75eb34abe9..17f3061655 100644 --- a/src/Dict.h +++ b/src/Dict.h @@ -10,9 +10,6 @@ class Dictionary; class DictEntry; class IterCookie; -declare(PList,DictEntry); -declare(PList,IterCookie); - // Type indicating whether the dictionary should keep track of the order // of insertions. typedef enum { ORDERED, UNORDERED } dict_order; @@ -132,7 +129,7 @@ private: void* Insert(DictEntry* entry, int copy_key); void* DoRemove(DictEntry* entry, hash_t h, - PList(DictEntry)* chain, int chain_offset); + PList* chain, int chain_offset); int NextPrime(int n) const; int IsPrime(int n) const; @@ -162,7 +159,7 @@ private: // When we're resizing, we'll have tbl (old) and tbl2 (new) // tbl_next_ind keeps track of how much we've moved to tbl2 // (it's the next index we're going to move). - PList(DictEntry)** tbl; + PList** tbl; int num_buckets; int num_entries; int max_num_entries; @@ -171,7 +168,7 @@ private: int thresh_entries; // Resizing table (replicates tbl above). - PList(DictEntry)** tbl2; + PList** tbl2; int num_buckets2; int num_entries2; int max_num_entries2; @@ -180,52 +177,47 @@ private: hash_t tbl_next_ind; - PList(DictEntry)* order; + PList* order; dict_delete_func delete_func; - PList(IterCookie) cookies; + PList cookies; }; - -#define PDict(type) type ## PDict -#define PDictdeclare(type) \ -class PDict(type) : public Dictionary { \ -public: \ - explicit PDict(type)(dict_order ordering = UNORDERED, \ - int initial_size = 0) : \ - Dictionary(ordering, initial_size) {} \ - type* Lookup(const char* key) const \ - { \ - HashKey h(key); \ - return (type*) Dictionary::Lookup(&h); \ - } \ - type* Lookup(const HashKey* key) const \ - { return (type*) Dictionary::Lookup(key); } \ - type* Insert(const char* key, type* val) \ - { \ - HashKey h(key); \ - return (type*) Dictionary::Insert(&h, (void*) val); \ - } \ - type* Insert(HashKey* key, type* val) \ - { return (type*) Dictionary::Insert(key, (void*) val); }\ - type* NthEntry(int n) const \ - { return (type*) Dictionary::NthEntry(n); } \ - type* NthEntry(int n, const char*& key) const \ - { \ - int key_len; \ - return (type*) Dictionary::NthEntry(n, (const void*&) key,\ - key_len); \ - } \ - type* NextEntry(IterCookie*& cookie) const \ - { \ - HashKey* h; \ - return (type*) Dictionary::NextEntry(h, cookie, 0); \ - } \ - type* NextEntry(HashKey*& h, IterCookie*& cookie) const \ - { return (type*) Dictionary::NextEntry(h, cookie, 1); } \ - type* RemoveEntry(const HashKey* key) \ - { return (type*) Remove(key->Key(), key->Size(), \ - key->Hash()); } \ -} +template +class PDict : public Dictionary { +public: + explicit PDict(dict_order ordering = UNORDERED, int initial_size = 0) : + Dictionary(ordering, initial_size) {} + T* Lookup(const char* key) const + { + HashKey h(key); + return (T*) Dictionary::Lookup(&h); + } + T* Lookup(const HashKey* key) const + { return (T*) Dictionary::Lookup(key); } + T* Insert(const char* key, T* val) + { + HashKey h(key); + return (T*) Dictionary::Insert(&h, (void*) val); + } + T* Insert(HashKey* key, T* val) + { return (T*) Dictionary::Insert(key, (void*) val); } + T* NthEntry(int n) const + { return (T*) Dictionary::NthEntry(n); } + T* NthEntry(int n, const char*& key) const + { + int key_len; + return (T*) Dictionary::NthEntry(n, (const void*&) key, key_len); + } + T* NextEntry(IterCookie*& cookie) const + { + HashKey* h; + return (T*) Dictionary::NextEntry(h, cookie, 0); + } + T* NextEntry(HashKey*& h, IterCookie*& cookie) const + { return (T*) Dictionary::NextEntry(h, cookie, 1); } + T* RemoveEntry(const HashKey* key) + { return (T*) Remove(key->Key(), key->Size(), key->Hash()); } +}; #endif diff --git a/src/EquivClass.cc b/src/EquivClass.cc index 6b2a7aa593..60cddbf486 100644 --- a/src/EquivClass.cc +++ b/src/EquivClass.cc @@ -52,11 +52,10 @@ void EquivClass::ConvertCCL(CCL* ccl) int_list* c_syms = ccl->Syms(); int_list* new_syms = new int_list; - for ( int i = 0; i < c_syms->length(); ++i ) + for ( auto sym : *c_syms ) { - int sym = (*c_syms)[i]; if ( IsRep(sym) ) - new_syms->append(SymEquivClass(sym)); + new_syms->push_back(SymEquivClass(sym)); } ccl->ReplaceSyms(new_syms); @@ -95,18 +94,18 @@ void EquivClass::CCL_Use(CCL* ccl) } int_list* csyms = ccl->Syms(); - for ( int i = 0; i < csyms->length(); /* no increment */ ) + for ( size_t i = 0; i < csyms->size(); /* no increment */ ) { int sym = (*csyms)[i]; int old_ec = bck[sym]; int new_ec = sym; - int j = i + 1; + size_t j = i + 1; for ( int k = fwd[sym]; k && k < size; k = fwd[k] ) { // look for the symbol in the character class - for ( ; j < csyms->length(); ++j ) + for ( ; j < csyms->size(); ++j ) { if ( (*csyms)[j] > k ) // Since the character class is sorted, @@ -131,7 +130,7 @@ void EquivClass::CCL_Use(CCL* ccl) } } - if ( j < csyms->length() && (*csyms)[j] == k ) + if ( j < csyms->size() && (*csyms)[j] == k ) // We broke out of the above loop by finding // an old companion - go to the next symbol. continue; @@ -154,7 +153,7 @@ void EquivClass::CCL_Use(CCL* ccl) fwd[new_ec] = ec_nil; // Find next ccl member to process. - for ( ++i; i < csyms->length() && ccl_flags[i]; ++i ) + for ( ++i; i < csyms->size() && ccl_flags[i]; ++i ) // Reset "doesn't need processing" flag. ccl_flags[i] = 0; } diff --git a/src/Event.h b/src/Event.h index 475dc5f577..1673c9d41f 100644 --- a/src/Event.h +++ b/src/Event.h @@ -86,8 +86,8 @@ public: QueueEvent(new Event(h, std::move(vl), src, aid, mgr, obj)); else { - loop_over_list(vl, i) - Unref(vl[i]); + for ( const auto& v : vl ) + Unref(v); } } diff --git a/src/EventHandler.cc b/src/EventHandler.cc index 0f06ad50ca..8c1b9f28f2 100644 --- a/src/EventHandler.cc +++ b/src/EventHandler.cc @@ -117,8 +117,8 @@ void EventHandler::Call(val_list* vl, bool no_remote) Unref(local->Call(vl)); else { - loop_over_list(*vl, i) - Unref((*vl)[i]); + for ( auto v : *vl ) + Unref(v); } } diff --git a/src/EventRegistry.h b/src/EventRegistry.h index 8fba8d6762..2a570dd8bc 100644 --- a/src/EventRegistry.h +++ b/src/EventRegistry.h @@ -22,8 +22,7 @@ public: // Returns a list of all local handlers that match the given pattern. // Passes ownership of list. typedef const char constchar; // PList doesn't like "const char" - declare(PList, constchar); - typedef PList(constchar) string_list; + typedef PList string_list; string_list* Match(RE_Matcher* pattern); // Marks a handler as handling errors. Error handler will not be called @@ -38,8 +37,7 @@ public: void PrintDebug(); private: - declare(PDict, EventHandler); - typedef PDict(EventHandler) handler_map; + typedef PDict handler_map; handler_map handlers; }; diff --git a/src/Expr.cc b/src/Expr.cc index 7235b14d35..8d81be851b 100644 --- a/src/Expr.cc +++ b/src/Expr.cc @@ -2113,8 +2113,7 @@ bool AssignExpr::TypeCheck(attr_list* attrs) if ( attrs ) { attr_copy = new attr_list(attrs->length()); - loop_over_list(*attrs, i) - attr_copy->append((*attrs)[i]); + std::copy(attrs->begin(), attrs->end(), std::back_inserter(*attr_copy)); } bool empty_list_assignment = (op2->AsListExpr()->Exprs().length() == 0); @@ -2194,8 +2193,7 @@ bool AssignExpr::TypeCheck(attr_list* attrs) { attr_list* a = sce->Attrs()->Attrs(); attrs = new attr_list(a->length()); - loop_over_list(*a, i) - attrs->append((*a)[i]); + std::copy(a->begin(), a->end(), std::back_inserter(*attrs)); } int errors_before = reporter->Errors(); @@ -3029,10 +3027,8 @@ RecordConstructorExpr::RecordConstructorExpr(ListExpr* constructor_list) const expr_list& exprs = constructor_list->Exprs(); type_decl_list* record_types = new type_decl_list(exprs.length()); - loop_over_list(exprs, i) + for ( const auto& e : exprs ) { - Expr* e = exprs[i]; - if ( e->Tag() != EXPR_FIELD_ASSIGN ) { Error("bad type in record constructor", e); @@ -3139,12 +3135,12 @@ TableConstructorExpr::TableConstructorExpr(ListExpr* constructor_list, const expr_list& cle = constructor_list->Exprs(); // check and promote all index expressions in ctor list - loop_over_list(cle, i) + for ( const auto& expr : cle ) { - if ( cle[i]->Tag() != EXPR_ASSIGN ) + if ( expr->Tag() != EXPR_ASSIGN ) continue; - Expr* idx_expr = cle[i]->AsAssignExpr()->Op1(); + Expr* idx_expr = expr->AsAssignExpr()->Op1(); if ( idx_expr->Tag() != EXPR_LIST ) continue; @@ -3178,8 +3174,8 @@ Val* TableConstructorExpr::Eval(Frame* f) const Val* aggr = new TableVal(Type()->AsTableType(), attrs); const expr_list& exprs = op->AsListExpr()->Exprs(); - loop_over_list(exprs, i) - exprs[i]->EvalIntoAggregate(type, aggr, f); + for ( const auto& expr : exprs ) + expr->EvalIntoAggregate(type, aggr, f); return aggr; } @@ -3193,8 +3189,8 @@ Val* TableConstructorExpr::InitVal(const BroType* t, Val* aggr) const TableVal* tval = aggr ? aggr->AsTableVal() : new TableVal(tt, attrs); const expr_list& exprs = op->AsListExpr()->Exprs(); - loop_over_list(exprs, i) - exprs[i]->EvalIntoAggregate(t, tval, 0); + for ( const auto& expr : exprs ) + expr->EvalIntoAggregate(t, tval, 0); return tval; } @@ -3282,9 +3278,9 @@ Val* SetConstructorExpr::Eval(Frame* f) const TableVal* aggr = new TableVal(type->AsTableType(), attrs); const expr_list& exprs = op->AsListExpr()->Exprs(); - loop_over_list(exprs, i) + for ( const auto& expr : exprs ) { - Val* element = exprs[i]->Eval(f); + Val* element = expr->Eval(f); aggr->Assign(element, 0); Unref(element); } @@ -3302,9 +3298,8 @@ Val* SetConstructorExpr::InitVal(const BroType* t, Val* aggr) const TableVal* tval = aggr ? aggr->AsTableVal() : new TableVal(tt, attrs); const expr_list& exprs = op->AsListExpr()->Exprs(); - loop_over_list(exprs, i) + for ( const auto& e : exprs ) { - Expr* e = exprs[i]; Val* element = check_and_promote(e->Eval(0), index_type, 1); if ( ! element || ! tval->Assign(element, 0) ) @@ -4417,8 +4412,8 @@ ListExpr::ListExpr(Expr* e) : Expr(EXPR_LIST) ListExpr::~ListExpr() { - loop_over_list(exprs, i) - Unref(exprs[i]); + for ( const auto& expr: exprs ) + Unref(expr); } void ListExpr::Append(Expr* e) @@ -4429,8 +4424,8 @@ void ListExpr::Append(Expr* e) int ListExpr::IsPure() const { - loop_over_list(exprs, i) - if ( ! exprs[i]->IsPure() ) + for ( const auto& expr : exprs ) + if ( ! expr->IsPure() ) return 0; return 1; @@ -4438,8 +4433,8 @@ int ListExpr::IsPure() const int ListExpr::AllConst() const { - loop_over_list(exprs, i) - if ( ! exprs[i]->IsConst() ) + for ( const auto& expr : exprs ) + if ( ! expr->IsConst() ) return 0; return 1; @@ -4449,9 +4444,9 @@ Val* ListExpr::Eval(Frame* f) const { ListVal* v = new ListVal(TYPE_ANY); - loop_over_list(exprs, i) + for ( const auto& expr : exprs ) { - Val* ev = exprs[i]->Eval(f); + Val* ev = expr->Eval(f); if ( ! ev ) { RuntimeError("uninitialized list value"); @@ -4476,12 +4471,12 @@ BroType* ListExpr::InitType() const if ( exprs[0]->IsRecordElement(0) ) { type_decl_list* types = new type_decl_list(exprs.length()); - loop_over_list(exprs, i) + for ( const auto& expr : exprs ) { TypeDecl* td = new TypeDecl(0, 0); - if ( ! exprs[i]->IsRecordElement(td) ) + if ( ! expr->IsRecordElement(td) ) { - exprs[i]->Error("record element expected"); + expr->Error("record element expected"); delete td; delete types; return 0; @@ -4497,9 +4492,8 @@ BroType* ListExpr::InitType() const else { TypeList* tl = new TypeList(); - loop_over_list(exprs, i) + for ( const auto& e : exprs ) { - Expr* e = exprs[i]; BroType* ti = e->Type(); // Collapse any embedded sets or lists. @@ -4633,10 +4627,8 @@ Val* ListExpr::InitVal(const BroType* t, Val* aggr) const // know how to add themselves to a table or record. Another // possibility is an expression that evaluates itself to a // table, which we can then add to the aggregate. - loop_over_list(exprs, i) + for ( const auto& e : exprs ) { - Expr* e = exprs[i]; - if ( e->Tag() == EXPR_ASSIGN || e->Tag() == EXPR_FIELD_ASSIGN ) { if ( ! e->InitVal(t, aggr) ) @@ -4674,17 +4666,17 @@ Val* ListExpr::AddSetInit(const BroType* t, Val* aggr) const const TableType* tt = tv->Type()->AsTableType(); const TypeList* it = tt->Indices(); - loop_over_list(exprs, i) + for ( const auto& expr : exprs ) { Val* element; - if ( exprs[i]->Type()->IsSet() ) + if ( expr->Type()->IsSet() ) // A set to flatten. - element = exprs[i]->Eval(0); - else if ( exprs[i]->Type()->Tag() == TYPE_LIST ) - element = exprs[i]->InitVal(it, 0); + element = expr->Eval(0); + else if ( expr->Type()->Tag() == TYPE_LIST ) + element = expr->InitVal(it, 0); else - element = exprs[i]->InitVal((*it->Types())[0], 0); + element = expr->InitVal((*it->Types())[0], 0); if ( ! element ) return 0; @@ -4703,7 +4695,7 @@ Val* ListExpr::AddSetInit(const BroType* t, Val* aggr) const continue; } - if ( exprs[i]->Type()->Tag() == TYPE_LIST ) + if ( expr->Type()->Tag() == TYPE_LIST ) element = check_and_promote(element, it, 1); else element = check_and_promote(element, (*it->Types())[0], 1); @@ -4739,8 +4731,8 @@ void ListExpr::ExprDescribe(ODesc* d) const Expr* ListExpr::MakeLvalue() { - loop_over_list(exprs, i) - if ( exprs[i]->Tag() != EXPR_NAME ) + for ( const auto & expr : exprs ) + if ( expr->Tag() != EXPR_NAME ) ExprError("can only assign to list of identifiers"); return new RefExpr(this); @@ -4764,9 +4756,9 @@ TraversalCode ListExpr::Traverse(TraversalCallback* cb) const TraversalCode tc = cb->PreExpr(this); HANDLE_TC_EXPR_PRE(tc); - loop_over_list(exprs, i) + for ( const auto& expr : exprs ) { - tc = exprs[i]->Traverse(cb); + tc = expr->Traverse(cb); HANDLE_TC_EXPR_PRE(tc); } @@ -4785,11 +4777,11 @@ RecordAssignExpr::RecordAssignExpr(Expr* record, Expr* init_list, int is_init) // 2) a string indicating the field name, then (as the next element) // the value to use for that field. - for ( int i = 0; i < inits.length(); ++i ) + for ( const auto& init : inits ) { - if ( inits[i]->Type()->Tag() == TYPE_RECORD ) + if ( init->Type()->Tag() == TYPE_RECORD ) { - RecordType* t = inits[i]->Type()->AsRecordType(); + RecordType* t = init->Type()->AsRecordType(); for ( int j = 0; j < t->NumFields(); ++j ) { @@ -4800,15 +4792,15 @@ RecordAssignExpr::RecordAssignExpr(Expr* record, Expr* init_list, int is_init) same_type(lhs->FieldType(field), t->FieldType(j)) ) { FieldExpr* fe_lhs = new FieldExpr(record, field_name); - FieldExpr* fe_rhs = new FieldExpr(inits[i], field_name); + FieldExpr* fe_rhs = new FieldExpr(init, field_name); Append(get_assign_expr(fe_lhs->Ref(), fe_rhs->Ref(), is_init)); } } } - else if ( inits[i]->Tag() == EXPR_FIELD_ASSIGN ) + else if ( init->Tag() == EXPR_FIELD_ASSIGN ) { - FieldAssignExpr* rf = (FieldAssignExpr*) inits[i]; + FieldAssignExpr* rf = (FieldAssignExpr*) init; rf->Ref(); const char* field_name = ""; // rf->FieldName(); @@ -5072,8 +5064,8 @@ int check_and_promote_args(ListExpr*& args, RecordType* types) def_elements.insert(def_attr->AttrExpr()); } - loop_over_list(def_elements, i) - el.append(def_elements[i]->Ref()); + for ( const auto& elem : def_elements ) + el.append(elem->Ref()); } TypeList* tl = new TypeList(); @@ -5115,18 +5107,22 @@ val_list* eval_list(Frame* f, const ListExpr* l) const expr_list& e = l->Exprs(); val_list* v = new val_list(e.length()); - loop_over_list(e, i) + bool success = true; + for ( const auto& expr : e ) { - Val* ev = e[i]->Eval(f); + Val* ev = expr->Eval(f); if ( ! ev ) + { + success = false; break; + } v->append(ev); } - if ( i < e.length() ) + if ( ! success ) { // Failure. - loop_over_list(*v, j) - Unref((*v)[j]); + for ( const auto& val : *v ) + Unref(val); delete v; return 0; } diff --git a/src/Func.cc b/src/Func.cc index ccd9793291..8f0fa7f5c8 100644 --- a/src/Func.cc +++ b/src/Func.cc @@ -73,11 +73,11 @@ std::string render_call_stack() if ( ci.args ) { - loop_over_list(*ci.args, i) + for ( const auto& arg : *ci.args ) { ODesc d; d.SetShort(); - (*ci.args)[i]->Describe(&d); + arg->Describe(&d); if ( ! arg_desc.empty() ) arg_desc += ", "; @@ -237,8 +237,8 @@ std::pair Func::HandlePluginResult(std::pair plugin_resu } } - loop_over_list(*args, i) - Unref((*args)[i]); + for ( const auto& arg : *args ) + Unref(arg); return plugin_result; } @@ -300,8 +300,8 @@ Val* BroFunc::Call(val_list* args, Frame* parent) const { // Can only happen for events and hooks. assert(Flavor() == FUNC_FLAVOR_EVENT || Flavor() == FUNC_FLAVOR_HOOK); - loop_over_list(*args, i) - Unref((*args)[i]); + for ( const auto& arg : *args ) + Unref(arg); return Flavor() == FUNC_FLAVOR_HOOK ? val_mgr->GetTrue() : 0; } @@ -331,11 +331,11 @@ Val* BroFunc::Call(val_list* args, Frame* parent) const stmt_flow_type flow = FLOW_NEXT; Val* result = 0; - for ( size_t i = 0; i < bodies.size(); ++i ) + for ( const auto& body : bodies ) { if ( sample_logger ) sample_logger->LocationSeen( - bodies[i].stmts->GetLocationInfo()); + body.stmts->GetLocationInfo()); Unref(result); @@ -356,7 +356,7 @@ Val* BroFunc::Call(val_list* args, Frame* parent) const try { - result = bodies[i].stmts->Exec(f, flow); + result = body.stmts->Exec(f, flow); } catch ( InterpreterException& e ) @@ -397,8 +397,8 @@ Val* BroFunc::Call(val_list* args, Frame* parent) const // We have an extra Ref for each argument (so that they don't get // deleted between bodies), release that. - loop_over_list(*args, k) - Unref((*args)[k]); + for ( const auto& arg : *args ) + Unref(arg); if ( Flavor() == FUNC_FLAVOR_HOOK ) { @@ -441,8 +441,8 @@ void BroFunc::AddBody(Stmt* new_body, id_list* new_inits, int new_frame_size, { // For functions, we replace the old body with the new one. assert(bodies.size() <= 1); - for ( unsigned int i = 0; i < bodies.size(); ++i ) - Unref(bodies[i].stmts); + for ( const auto& body : bodies ) + Unref(body.stmts); bodies.clear(); } @@ -540,8 +540,8 @@ Val* BuiltinFunc::Call(val_list* args, Frame* parent) const Val* result = func(parent, args); call_stack.pop_back(); - loop_over_list(*args, i) - Unref((*args)[i]); + for ( const auto& arg : *args ) + Unref(arg); // Don't Unref() args, that's the caller's responsibility. if ( result && g_trace_state.DoTrace() ) diff --git a/src/List.cc b/src/List.cc deleted file mode 100644 index 1b8c2fd5e5..0000000000 --- a/src/List.cc +++ /dev/null @@ -1,246 +0,0 @@ -#include "zeek-config.h" - -#include -#include - -#include "List.h" -#include "util.h" - -#define DEFAULT_LIST_SIZE 10 -#define GROWTH_FACTOR 2 - -BaseList::BaseList(int size) - { - num_entries = 0; - - if ( size <= 0 ) - { - max_entries = 0; - entry = 0; - return; - } - - max_entries = size; - - entry = (ent *) safe_malloc(max_entries * sizeof(ent)); - } - - -BaseList::BaseList(const BaseList& b) - { - max_entries = b.max_entries; - num_entries = b.num_entries; - - if ( max_entries ) - entry = (ent *) safe_malloc(max_entries * sizeof(ent)); - else - entry = 0; - - for ( int i = 0; i < num_entries; ++i ) - entry[i] = b.entry[i]; - } - -BaseList::BaseList(BaseList&& b) - { - entry = b.entry; - num_entries = b.num_entries; - max_entries = b.max_entries; - - b.entry = 0; - b.num_entries = b.max_entries = 0; - } - -BaseList::BaseList(const ent* arr, int n) - { - num_entries = max_entries = n; - entry = (ent*) safe_malloc(max_entries * sizeof(ent)); - memcpy(entry, arr, n * sizeof(ent)); - } - -void BaseList::sort(list_cmp_func cmp_func) - { - qsort(entry, num_entries, sizeof(ent), cmp_func); - } - -BaseList& BaseList::operator=(const BaseList& b) - { - if ( this == &b ) - return *this; - - free(entry); - - max_entries = b.max_entries; - num_entries = b.num_entries; - - if ( max_entries ) - entry = (ent *) safe_malloc(max_entries * sizeof(ent)); - else - entry = 0; - - for ( int i = 0; i < num_entries; ++i ) - entry[i] = b.entry[i]; - - return *this; - } - -BaseList& BaseList::operator=(BaseList&& b) - { - if ( this == &b ) - return *this; - - free(entry); - entry = b.entry; - num_entries = b.num_entries; - max_entries = b.max_entries; - - b.entry = 0; - b.num_entries = b.max_entries = 0; - return *this; - } - -void BaseList::insert(ent a) - { - if ( num_entries == max_entries ) - resize(max_entries ? max_entries * GROWTH_FACTOR : DEFAULT_LIST_SIZE); - - for ( int i = num_entries; i > 0; --i ) - entry[i] = entry[i-1]; // move all pointers up one - - ++num_entries; - entry[0] = a; - } - -#include - -void BaseList::sortedinsert(ent a, list_cmp_func cmp_func) - { - // We optimize for the case that the new element is - // larger than most of the current entries. - - // First append element. - if ( num_entries == max_entries ) - resize(max_entries ? max_entries * GROWTH_FACTOR : DEFAULT_LIST_SIZE); - - entry[num_entries++] = a; - - // Then move it to the correct place. - ent tmp; - for ( int i = num_entries - 1; i > 0; --i ) - { - if ( cmp_func(entry[i],entry[i-1]) <= 0 ) - break; - - tmp = entry[i]; - entry[i] = entry[i-1]; - entry[i-1] = tmp; - } - } - -ent BaseList::remove(ent a) - { - int i; - for ( i = 0; i < num_entries && a != entry[i]; ++i ) - ; - - return remove_nth(i); - } - -ent BaseList::remove_nth(int n) - { - if ( n < 0 || n >= num_entries ) - return 0; - - ent old_ent = entry[n]; - --num_entries; - - for ( ; n < num_entries; ++n ) - entry[n] = entry[n+1]; - - entry[n] = 0; // for debugging - return old_ent; - } - -void BaseList::append(ent a) - { - if ( num_entries == max_entries ) - resize(max_entries ? max_entries * GROWTH_FACTOR : DEFAULT_LIST_SIZE); - - entry[num_entries++] = a; - } - -// Get and remove from the end of the list. -ent BaseList::get() - { - if ( num_entries == 0 ) - return 0; - - return entry[--num_entries]; - } - - -void BaseList::clear() - { - free(entry); - entry = 0; - num_entries = max_entries = 0; - } - -ent BaseList::replace(int ent_index, ent new_ent) - { - if ( ent_index < 0 ) - return 0; - - ent old_ent; - - if ( ent_index > num_entries - 1 ) - { // replacement beyond the end of the list - resize(ent_index + 1); - - for ( int i = num_entries; i < max_entries; ++i ) - entry[i] = 0; - num_entries = max_entries; - - old_ent = 0; - } - else - old_ent = entry[ent_index]; - - entry[ent_index] = new_ent; - - return old_ent; - } - -int BaseList::resize(int new_size) - { - if ( new_size < num_entries ) - new_size = num_entries; // do not lose any entries - - if ( new_size != max_entries ) - { - entry = (ent*) safe_realloc((void*) entry, sizeof(ent) * new_size); - if ( entry ) - max_entries = new_size; - else - max_entries = 0; - } - - return max_entries; - } - -ent BaseList::is_member(ent e) const - { - int i; - for ( i = 0; i < length() && e != entry[i]; ++i ) - ; - - return (i == length()) ? 0 : e; - } - -int BaseList::member_pos(ent e) const - { - int i; - for ( i = 0; i < length() && e != entry[i]; ++i ) - ; - - return (i == length()) ? -1 : i; - } diff --git a/src/List.h b/src/List.h index 15e99eb0dd..fdc23ec529 100644 --- a/src/List.h +++ b/src/List.h @@ -20,67 +20,362 @@ // Entries must be either a pointer to the data or nonzero data with // sizeof(data) <= sizeof(void*). -#include -#include #include +#include +#include +#include +#include #include "util.h" -typedef void* ent; typedef int (*list_cmp_func)(const void* v1, const void* v2); -class BaseList { +template +class ListIterator + { public: - void clear(); // remove all entries - int length() const { return num_entries; } - int max() const { return max_entries; } - int resize(int = 0); // 0 => size to fit current number of entries + ListIterator(T* entries, int offset, int num_entries) : + entries(entries), offset(offset), num_entries(num_entries), endptr() {} + bool operator==(const ListIterator& rhs) { return entries == rhs.entries && offset == rhs.offset; } + bool operator!=(const ListIterator& rhs) { return entries != rhs.entries || offset != rhs.offset; } + ListIterator & operator++() { offset++; return *this; } + ListIterator operator++(int) { auto t = *this; offset++; return t; } + ListIterator & operator--() { offset--; return *this; } + ListIterator operator--(int) { auto t = *this; offset--; return t; } + std::ptrdiff_t operator-(ListIterator const& sibling) const { return offset - sibling.offset; } + ListIterator & operator+=(int amount) { offset += amount; return *this; } + ListIterator & operator-=(int amount) { offset -= amount; return *this; } + bool operator<(ListIterator const&sibling) const { return offset < sibling.offset;} + bool operator<=(ListIterator const&sibling) const { return offset <= sibling.offset; } + bool operator>(ListIterator const&sibling) const { return offset > sibling.offset; } + bool operator>=(ListIterator const&sibling) const { return offset >= sibling.offset; } + T& operator[](int index) + { + if (index < num_entries) + return entries[index]; + else + return endptr; + } + T& operator*() + { + if ( offset < num_entries ) + return entries[offset]; + else + return endptr; + } - void sort(list_cmp_func cmp_func); +private: + T* const entries; + int offset; + int num_entries; + T endptr; // let this get set to some random value on purpose. It's only used + // for the operator[] and operator* cases where you pass something + // off the end of the collection, which is undefined behavior anyways. + }; - int MemoryAllocation() const - { return padded_sizeof(*this) + pad_size(max_entries * sizeof(ent)); } -protected: - ~BaseList() { free(entry); } - explicit BaseList(int = 0); - BaseList(const BaseList&); - BaseList(BaseList&&); - BaseList(const ent* arr, int n); +namespace std { + template + class iterator_traits > + { + public: + using difference_type = std::ptrdiff_t; + using size_type = std::size_t; + using value_type = T; + using pointer = T*; + using reference = T&; + using iterator_category = std::random_access_iterator_tag; + }; +} - BaseList& operator=(const BaseList&); - BaseList& operator=(BaseList&&); - void insert(ent); // add at head of list +template +class List { +public: - // Assumes that the list is sorted and inserts at correct position. - void sortedinsert(ent, list_cmp_func cmp_func); + const int DEFAULT_LIST_SIZE = 10; + const int LIST_GROWTH_FACTOR = 2; - void append(ent); // add to end of list - ent remove(ent); // delete entry from list - ent remove_nth(int); // delete nth entry from list - ent get(); // return and remove ent at end of list - ent last() // return at end of list - { return entry[num_entries-1]; } + ~List() { free(entries); } + explicit List(int size = 0) + { + num_entries = 0; - // Return 0 if ent is not in the list, ent otherwise. - ent is_member(ent) const; + if ( size <= 0 ) + { + max_entries = 0; + entries = nullptr; + return; + } - // Returns -1 if ent is not in the list, otherwise its position. - int member_pos(ent) const; + max_entries = size; - ent replace(int, ent); // replace entry #i with a new value + entries = (T*) safe_malloc(max_entries * sizeof(T)); + } + + List(const List& b) + { + max_entries = b.max_entries; + num_entries = b.num_entries; + + if ( max_entries ) + entries = (T*) safe_malloc(max_entries * sizeof(T)); + else + entries = nullptr; + + for ( int i = 0; i < num_entries; ++i ) + entries[i] = b.entries[i]; + } + + List(List&& b) + { + entries = b.entries; + num_entries = b.num_entries; + max_entries = b.max_entries; + + b.entries = nullptr; + b.num_entries = b.max_entries = 0; + } + + List(const T* arr, int n) + { + num_entries = max_entries = n; + entries = (T*) safe_malloc(max_entries * sizeof(T)); + memcpy(entries, arr, n * sizeof(T)); + } + + List(std::initializer_list il) : List(il.begin(), il.size()) {} + + List& operator=(const List& b) + { + if ( this == &b ) + return *this; + + free(entries); + + max_entries = b.max_entries; + num_entries = b.num_entries; + + if ( max_entries ) + entries = (T *) safe_malloc(max_entries * sizeof(T)); + else + entries = nullptr; + + for ( int i = 0; i < num_entries; ++i ) + entries[i] = b.entries[i]; + + return *this; + } + + List& operator=(List&& b) + { + if ( this == &b ) + return *this; + + free(entries); + entries = b.entries; + num_entries = b.num_entries; + max_entries = b.max_entries; + + b.entries = nullptr; + b.num_entries = b.max_entries = 0; + return *this; + } // Return nth ent of list (do not remove). - ent operator[](int i) const + T& operator[](int i) const { -#ifdef SAFE_LISTS - if ( i < 0 || i > num_entries-1 ) - return 0; - else -#endif - return entry[i]; + return entries[i]; } + void clear() // remove all entries + { + free(entries); + entries = nullptr; + num_entries = max_entries = 0; + } + + int length() const { return num_entries; } + int max() const { return max_entries; } + int resize(int new_size = 0) // 0 => size to fit current number of entries + { + if ( new_size < num_entries ) + new_size = num_entries; // do not lose any entries + + if ( new_size != max_entries ) + { + entries = (T*) safe_realloc((void*) entries, sizeof(T) * new_size); + if ( entries ) + max_entries = new_size; + else + max_entries = 0; + } + + return max_entries; + } + + void sort(list_cmp_func cmp_func) + { + qsort(entries, num_entries, sizeof(T), cmp_func); + } + + int MemoryAllocation() const + { return padded_sizeof(*this) + pad_size(max_entries * sizeof(T)); } + + void insert(const T& a) // add at head of list + { + if ( num_entries == max_entries ) + resize(max_entries ? max_entries * LIST_GROWTH_FACTOR : DEFAULT_LIST_SIZE); + + for ( int i = num_entries; i > 0; --i ) + entries[i] = entries[i-1]; // move all pointers up one + + ++num_entries; + entries[0] = a; + } + + // Assumes that the list is sorted and inserts at correct position. + void sortedinsert(const T& a, list_cmp_func cmp_func) + { + // We optimize for the case that the new element is + // larger than most of the current entries. + + // First append element. + if ( num_entries == max_entries ) + resize(max_entries ? max_entries * LIST_GROWTH_FACTOR : DEFAULT_LIST_SIZE); + + entries[num_entries++] = a; + + // Then move it to the correct place. + T tmp; + for ( int i = num_entries - 1; i > 0; --i ) + { + if ( cmp_func(entries[i],entries[i-1]) <= 0 ) + break; + + tmp = entries[i]; + entries[i] = entries[i-1]; + entries[i-1] = tmp; + } + } + + void push_back(const T& a) { append(a); } + void push_front(const T& a) { insert(a); } + void pop_front() { remove_nth(0); } + void pop_back() { remove_nth(num_entries-1); } + + T& front() { return entries[0]; } + T& back() { return entries[num_entries-1]; } + + void append(const T& a) // add to end of list + { + if ( num_entries == max_entries ) + resize(max_entries ? max_entries * LIST_GROWTH_FACTOR : DEFAULT_LIST_SIZE); + + entries[num_entries++] = a; + } + + bool remove(const T& a) // delete entry from list + { + for ( int i = 0; i < num_entries; ++i ) + { + if ( a == entries[i] ) + { + remove_nth(i); + return true; + } + } + + return false; + } + + T remove_nth(int n) // delete nth entry from list + { + assert(n >=0 && n < num_entries); + + T old_ent = entries[n]; + --num_entries; + + for ( ; n < num_entries; ++n ) + entries[n] = entries[n+1]; + + return old_ent; + } + + ZEEK_DEPRECATED("Remove in v3.1: Use back()/pop_back() instead") + T get() // return and remove ent at end of list + { + assert(num_entries > 0); + return entries[--num_entries]; + } + + ZEEK_DEPRECATED("Remove in v3.1: Use back() instead") + T& last() { return back(); } + + // Return 0 if ent is not in the list, ent otherwise. + bool is_member(const T& a) const + { + int pos = member_pos(a); + return pos != -1; + } + + // Returns -1 if ent is not in the list, otherwise its position. + int member_pos(const T& e) const + { + int i; + for ( i = 0; i < length() && e != entries[i]; ++i ) + ; + + return (i == length()) ? -1 : i; + } + + T replace(int ent_index, const T& new_ent) // replace entry #i with a new value + { + if ( ent_index < 0 ) + return 0; + + T old_ent = nullptr; + + if ( ent_index > num_entries - 1 ) + { // replacement beyond the end of the list + resize(ent_index + 1); + + for ( int i = num_entries; i < max_entries; ++i ) + entries[i] = nullptr; + num_entries = max_entries; + } + else + old_ent = entries[ent_index]; + + entries[ent_index] = new_ent; + + return old_ent; + } + + // Type traits needed for some of the std algorithms to work + using value_type = T; + + // Iterator support + using iterator = ListIterator; + using const_iterator = ListIterator; + using reverse_iterator = std::reverse_iterator; + using const_reverse_iterator = std::reverse_iterator; + + iterator begin() { return { entries, 0, num_entries }; } + iterator end() { return { entries, num_entries, num_entries }; } + const_iterator begin() const { return { entries, 0, num_entries }; } + const_iterator end() const { return { entries, num_entries, num_entries }; } + const_iterator cbegin() const { return { entries, 0, num_entries }; } + const_iterator cend() const { return { entries, num_entries, num_entries }; } + + reverse_iterator rbegin() { return reverse_iterator{end()}; } + reverse_iterator rend() { return reverse_iterator{begin()}; } + const_reverse_iterator rbegin() const { return const_reverse_iterator{end()}; } + const_reverse_iterator rend() const { return const_reverse_iterator{begin()}; } + const_reverse_iterator crbegin() const { return rbegin(); } + const_reverse_iterator crend() const { return rend(); } + +protected: + // This could essentially be an std::vector if we wanted. Some // reasons to maybe not refactor to use std::vector ? // @@ -105,120 +400,18 @@ protected: // advantage of realloc's ability to contract in-place, it would // allocate-and-copy. - ent* entry; + T* entries; int max_entries; int num_entries; }; -// List.h -- interface for class List -// Use: to get a list of pointers to class foo you should: -// 1) typedef foo* Pfoo; (the macros don't like explicit pointers) -// 2) declare(List,Pfoo); (declare an interest in lists of Pfoo's) -// 3) variables are declared like: -// List(Pfoo) bar; (bar is of type list of Pfoo's) - -// For lists of "type". - -#define List(type) type ## List - -// For lists of pointers to "type" -#define PList(type) type ## PList - -#define Listdeclare(type) \ -struct List(type) : BaseList \ - { \ - explicit List(type)(type ...); \ - List(type)() : BaseList(0) {} \ - explicit List(type)(int sz) : BaseList(sz) {} \ - List(type)(const List(type)& l) : BaseList(l) {} \ - List(type)(List(type)&& l) : BaseList(std::move(l)) {} \ - \ - List(type)& operator=(const List(type)& l) \ - { return (List(type)&) BaseList::operator=(l); } \ - List(type)& operator=(List(type)&& l) \ - { return (List(type)&) BaseList::operator=(std::move(l)); } \ - void insert(type a) { BaseList::insert(ent(a)); } \ - void sortedinsert(type a, list_cmp_func cmp_func) \ - { BaseList::sortedinsert(ent(a), cmp_func); } \ - void append(type a) { BaseList::append(ent(a)); } \ - type remove(type a) \ - { return type(BaseList::remove(ent(a))); } \ - type remove_nth(int n) { return type(BaseList::remove_nth(n)); }\ - type get() { return type(BaseList::get()); } \ - type last() { return type(BaseList::last()); } \ - type replace(int i, type new_type) \ - { return type(BaseList::replace(i,ent(new_type))); } \ - type is_member(type e) const \ - { return type(BaseList::is_member(ent(e))); } \ - int member_pos(type e) const \ - { return BaseList::member_pos(ent(e)); } \ - \ - type operator[](int i) const \ - { return type(BaseList::operator[](i)); } \ - }; \ - -#define Listimplement(type) \ -List(type)::List(type)(type e1 ...) : BaseList() \ - { \ - append(e1); \ - va_list ap; \ - va_start(ap,e1); \ - for ( type e = va_arg(ap,type); e != 0; e = va_arg(ap,type) ) \ - append(e); \ - resize(); \ - } - -#define PListdeclare(type) \ -struct PList(type) : BaseList \ - { \ - explicit PList(type)(type* ...); \ - PList(type)() : BaseList(0) {} \ - explicit PList(type)(int sz) : BaseList(sz) {} \ - PList(type)(const PList(type)& l) : BaseList(l) {} \ - PList(type)(PList(type)&& l) : BaseList(std::move(l)) {} \ - PList(type)(std::initializer_list il) : BaseList((const ent*)il.begin(), il.size()) {} \ - \ - PList(type)& operator=(const PList(type)& l) \ - { return (PList(type)&) BaseList::operator=(l); } \ - PList(type)& operator=(PList(type)&& l) \ - { return (PList(type)&) BaseList::operator=(std::move(l)); } \ - void insert(type* a) { BaseList::insert(ent(a)); } \ - void sortedinsert(type* a, list_cmp_func cmp_func) \ - { BaseList::sortedinsert(ent(a), cmp_func); } \ - void append(type* a) { BaseList::append(ent(a)); } \ - type* remove(type* a) \ - { return (type*)BaseList::remove(ent(a)); } \ - type* remove_nth(int n) { return (type*)(BaseList::remove_nth(n)); }\ - type* get() { return (type*)BaseList::get(); } \ - type* operator[](int i) const \ - { return (type*)(BaseList::operator[](i)); } \ - type* replace(int i, type* new_type) \ - { return (type*)BaseList::replace(i,ent(new_type)); } \ - type* is_member(type* e) \ - { return (type*)BaseList::is_member(ent(e)); } \ - int member_pos(type* e) \ - { return BaseList::member_pos(ent(e)); } \ - }; \ - -#define PListimplement(type) \ -PList(type)::PList(type)(type* ep1 ...) : BaseList() \ - { \ - append(ep1); \ - va_list ap; \ - va_start(ap,ep1); \ - for ( type* ep = va_arg(ap,type*); ep != 0; \ - ep = va_arg(ap,type*) ) \ - append(ep); \ - resize(); \ - } - - -#define declare(metatype,type) metatype ## declare (type) +// Specialization of the List class to store pointers of a type. +template +using PList = List; // Popular type of list: list of strings. -declare(PList,char); -typedef PList(char) name_list; +typedef PList name_list; // Macro to visit each list element in turn. #define loop_over_list(list, iterator) \ diff --git a/src/NFA.h b/src/NFA.h index 79c3961dd5..d1efc212ad 100644 --- a/src/NFA.h +++ b/src/NFA.h @@ -9,8 +9,7 @@ class NFA_State; class EquivClass; -declare(PList,NFA_State); -typedef PList(NFA_State) NFA_state_list; +typedef PList NFA_state_list; #define NO_ACCEPT 0 diff --git a/src/OpaqueVal.cc b/src/OpaqueVal.cc index d372f525a5..595102e8a2 100644 --- a/src/OpaqueVal.cc +++ b/src/OpaqueVal.cc @@ -233,9 +233,8 @@ void MD5Val::digest(val_list& vlist, u_char result[MD5_DIGEST_LENGTH]) { EVP_MD_CTX* h = hash_init(Hash_MD5); - loop_over_list(vlist, i) + for ( const auto& v : vlist ) { - Val* v = vlist[i]; if ( v->Type()->Tag() == TYPE_STRING ) { const BroString* str = v->AsString(); @@ -385,9 +384,8 @@ void SHA1Val::digest(val_list& vlist, u_char result[SHA_DIGEST_LENGTH]) { EVP_MD_CTX* h = hash_init(Hash_SHA1); - loop_over_list(vlist, i) + for ( const auto& v : vlist ) { - Val* v = vlist[i]; if ( v->Type()->Tag() == TYPE_STRING ) { const BroString* str = v->AsString(); @@ -529,9 +527,8 @@ void SHA256Val::digest(val_list& vlist, u_char result[SHA256_DIGEST_LENGTH]) { EVP_MD_CTX* h = hash_init(Hash_SHA256); - loop_over_list(vlist, i) + for ( const auto& v : vlist ) { - Val* v = vlist[i]; if ( v->Type()->Tag() == TYPE_STRING ) { const BroString* str = v->AsString(); diff --git a/src/Queue.cc b/src/Queue.cc deleted file mode 100644 index 90f63a85be..0000000000 --- a/src/Queue.cc +++ /dev/null @@ -1,137 +0,0 @@ -// See the file "COPYING" in the main distribution directory for copyright. - -#include "zeek-config.h" - -#include - -#include "Queue.h" - -BaseQueue::BaseQueue(int size) - { - const int DEFAULT_CHUNK_SIZE = 10; - - chunk_size = DEFAULT_CHUNK_SIZE; - - head = tail = num_entries = 0; - - if ( size < 0 ) - { - entry = new ent[1]; - max_entries = 0; - } - else - { - if ( (entry = new ent[chunk_size+1]) ) - max_entries = chunk_size; - else - { - entry = new ent[1]; - max_entries = 0; - } - } - } - -void BaseQueue::push_front(ent a) - { - if ( num_entries == max_entries ) - { - resize(max_entries+chunk_size); // make more room - chunk_size *= 2; - } - - ++num_entries; - if ( head ) - entry[--head] = a; - else - { - head = max_entries; - entry[head] = a; - } - } - -void BaseQueue::push_back(ent a) - { - if ( num_entries == max_entries ) - { - resize(max_entries+chunk_size); // make more room - chunk_size *= 2; - } - - ++num_entries; - if ( tail < max_entries ) - entry[tail++] = a; - else - { - entry[tail] = a; - tail = 0; - } - } - -ent BaseQueue::pop_front() - { - if ( ! num_entries ) - return 0; - - --num_entries; - if ( head < max_entries ) - return entry[head++]; - else - { - head = 0; - return entry[max_entries]; - } - } - -ent BaseQueue::pop_back() - { - if ( ! num_entries ) - return 0; - - --num_entries; - if ( tail ) - return entry[--tail]; - else - { - tail = max_entries; - return entry[tail]; - } - } - -int BaseQueue::resize(int new_size) - { - if ( new_size < num_entries ) - new_size = num_entries; // do not lose any entries - - if ( new_size != max_entries ) - { - // Note, allocate extra space, so that we can always - // use the [max_entries] element. - // ### Yin, why not use realloc()? - ent* new_entry = new ent[new_size+1]; - - if ( new_entry ) - { - if ( head <= tail ) - memcpy( new_entry, entry + head, - sizeof(ent) * num_entries ); - else - { - int len = num_entries - tail; - memcpy( new_entry, entry + head, - sizeof(ent) * len ); - memcpy( new_entry + len, entry, - sizeof(ent) * tail ); - } - delete [] entry; - entry = new_entry; - max_entries = new_size; - head = 0; - tail = num_entries; - } - else - { // out of memory - } - } - - return max_entries; - } diff --git a/src/Queue.h b/src/Queue.h index 691b5b9908..4a1a2c05e2 100644 --- a/src/Queue.h +++ b/src/Queue.h @@ -3,8 +3,8 @@ #ifndef queue_h #define queue_h -// BaseQueue.h -- -// Interface for class BaseQueue, current implementation is as an +// Queue.h -- +// Interface for class Queue, current implementation is as an // array of ent's. This implementation was chosen to optimize // getting to the ent's rather than inserting and deleting. // Also push's and pop's from the front or the end of the queue @@ -21,35 +21,214 @@ // Entries must be either a pointer to the data or nonzero data with // sizeof(data) <= sizeof(void*). -#include "List.h" - -class BaseQueue { +template +class QueueIterator + { + T* const entries; + int offset; + int num_entries; public: - ~BaseQueue() { delete[] entry; } + QueueIterator(T* entries, int offset, int num_entries) : + entries(entries), offset(offset), num_entries(num_entries) {} + bool operator==(const QueueIterator& rhs) { return entries == rhs.entries && offset == rhs.offset; } + bool operator!=(const QueueIterator& rhs) { return entries != rhs.entries || offset != rhs.offset; } + QueueIterator & operator++() { offset++; return *this; } + QueueIterator operator++(int) { auto t = *this; offset++; return t; } + QueueIterator & operator--() { offset--; return *this; } + QueueIterator operator--(int) { auto t = *this; offset--; return t; } + std::ptrdiff_t operator-(QueueIterator const& sibling) const { return offset - sibling.offset; } + QueueIterator & operator+=(int amount) { offset += amount; return *this; } + QueueIterator & operator-=(int amount) { offset -= amount; return *this; } + bool operator<(QueueIterator const&sibling) const { return offset < sibling.offset;} + bool operator<=(QueueIterator const&sibling) const { return offset <= sibling.offset; } + bool operator>(QueueIterator const&sibling) const { return offset > sibling.offset; } + bool operator>=(QueueIterator const&sibling) const { return offset >= sibling.offset; } + T& operator[](int index) + { + return entries[index]; + } + T& operator*() + { + return entries[offset]; + } + }; + +namespace std { + template + class iterator_traits > + { + public: + using difference_type = std::ptrdiff_t; + using size_type = std::size_t; + using value_type = T; + using pointer = T*; + using reference = T&; + using iterator_category = std::random_access_iterator_tag; + }; +} + + +template +class Queue { +public: + explicit Queue(int size = 0) + { + const int DEFAULT_CHUNK_SIZE = 10; + chunk_size = DEFAULT_CHUNK_SIZE; + + head = tail = num_entries = 0; + + if ( size < 0 ) + { + entries = new T[1]; + max_entries = 0; + } + else + { + if ( (entries = new T[chunk_size+1]) ) + max_entries = chunk_size; + else + { + entries = new T[1]; + max_entries = 0; + } + } + } + + ~Queue() { delete[] entries; } int length() const { return num_entries; } - int resize(int = 0); // 0 => size to fit current number of entries + int resize(int new_size = 0) // 0 => size to fit current number of entries + { + if ( new_size < num_entries ) + new_size = num_entries; // do not lose any entries + + if ( new_size != max_entries ) + { + // Note, allocate extra space, so that we can always + // use the [max_entries] element. + // ### Yin, why not use realloc()? + T* new_entries = new T[new_size+1]; + + if ( new_entries ) + { + if ( head <= tail ) + memcpy( new_entries, entries + head, + sizeof(T) * num_entries ); + else + { + int len = num_entries - tail; + memcpy( new_entries, entries + head, + sizeof(T) * len ); + memcpy( new_entries + len, entries, + sizeof(T) * tail ); + } + delete [] entries; + entries = new_entries; + max_entries = new_size; + head = 0; + tail = num_entries; + } + else + { // out of memory + } + } + + return max_entries; + } // remove all entries without delete[] entry void clear() { head = tail = num_entries = 0; } // helper functions for iterating over queue - int front() const { return head; } - int back() const { return tail; } - void incr(int& index) { index < max_entries ? ++index : index = 0; } + T& front() { return entries[head]; } + T& back() { return entries[tail]; } + const T& front() const { return entries[head]; } + const T& back() const { return entries[tail]; } -protected: - explicit BaseQueue(int = 0); + void push_front(const T& a) // add in front of queue + { + if ( num_entries == max_entries ) + { + resize(max_entries+chunk_size); // make more room + chunk_size *= 2; + } - void push_front(ent); // add in front of queue - void push_back(ent); // add at end of queue - ent pop_front(); // return and remove the front of queue - ent pop_back(); // return and remove the end of queue + ++num_entries; + if ( head ) + entries[--head] = a; + else + { + head = max_entries; + entries[head] = a; + } + } + + void push_back(const T& a) // add at end of queue + { + if ( num_entries == max_entries ) + { + resize(max_entries+chunk_size); // make more room + chunk_size *= 2; + } + + ++num_entries; + if ( tail < max_entries ) + entries[tail++] = a; + else + { + entries[tail] = a; + tail = 0; + } + } + + void pop_front() + { + --num_entries; + if ( head < max_entries ) + head++; + else + head = 0; + } + + void pop_back() + { + --num_entries; + if ( tail ) + --tail; + else + tail = max_entries; + } // return nth *PHYSICAL* entry of queue (do not remove) - ent operator[](int i) const { return entry[i]; } + T& operator[](int i) const { return entries[i]; } - ent* entry; + // Type traits needed for some of the std algorithms to work + using value_type = T; + + // Iterator support + using iterator = QueueIterator; + using const_iterator = QueueIterator; + using reverse_iterator = std::reverse_iterator; + using const_reverse_iterator = std::reverse_iterator; + + iterator begin() { return { entries, 0, num_entries }; } + iterator end() { return { entries, num_entries, num_entries }; } + const_iterator begin() const { return { entries, 0, num_entries }; } + const_iterator end() const { return { entries, num_entries, num_entries }; } + const_iterator cbegin() const { return { entries, 0, num_entries }; } + const_iterator cend() const { return { entries, num_entries, num_entries }; } + + reverse_iterator rbegin() { return reverse_iterator{end()}; } + reverse_iterator rend() { return reverse_iterator{begin()}; } + const_reverse_iterator rbegin() const { return const_reverse_iterator{end()}; } + const_reverse_iterator rend() const { return const_reverse_iterator{begin()}; } + const_reverse_iterator crbegin() const { return rbegin(); } + const_reverse_iterator crend() const { return rend(); } + +protected: + + T* entries; int chunk_size; // increase size by this amount when necessary int max_entries; // entry's index range: 0 .. max_entries int num_entries; @@ -57,54 +236,8 @@ protected: int tail; // just beyond the end of the queue in the ring }; -// Queue.h -- interface for class Queue -// Use: to get a list of pointers to class foo you should: -// 1) declare(PQueue,foo); (declare interest in lists of foo*'s) -// 2) variables are declared like: -// PQueue(foo) bar; (bar is of type list of foo*'s) -// For queues of "type" -#define Queue(type) type ## Queue - -// For queues of pointers to "type" -#define PQueue(type) type ## PQueue - -#define Queuedeclare(type) \ -struct Queue(type) : BaseQueue \ - { \ - Queue(type)() : BaseQueue(0) {} \ - explicit Queue(type)(int sz) : BaseQueue(sz) {} \ - \ - void push_front(type a) { BaseQueue::push_front(ent(a)); } \ - void push_back(type a) { BaseQueue::push_back(ent(a)); } \ - type pop_front() { return type(BaseQueue::pop_front()); }\ - type pop_back() { return type(BaseQueue::pop_back()); } \ - \ - type operator[](int i) const \ - { return type(BaseQueue::operator[](i)); } \ - }; \ - -#define PQueuedeclare(type) \ -struct PQueue(type) : BaseQueue \ - { \ - PQueue(type)() : BaseQueue(0) {} \ - explicit PQueue(type)(int sz) : BaseQueue(sz) {} \ - \ - void push_front(type* a){ BaseQueue::push_front(ent(a)); } \ - void push_back(type* a) { BaseQueue::push_back(ent(a)); } \ - type* pop_front() \ - { return (type*)BaseQueue::pop_front(); } \ - type* pop_back() \ - { return (type*)BaseQueue::pop_back(); } \ - \ - type* operator[](int i) const \ - { return (type*)BaseQueue::operator[](i); } \ - }; \ - -// Macro to visit each queue element in turn. -#define loop_over_queue(queue, iterator) \ - int iterator; \ - for ( iterator = (queue).front(); iterator != (queue).back(); \ - (queue).incr(iterator) ) \ +template +using PQueue = Queue; #endif /* queue_h */ diff --git a/src/RE.cc b/src/RE.cc index 98f120efe0..95d5e1786b 100644 --- a/src/RE.cc +++ b/src/RE.cc @@ -149,7 +149,7 @@ int Specific_RE_Matcher::Compile(int lazy) int Specific_RE_Matcher::CompileSet(const string_list& set, const int_list& idx) { - if ( set.length() != idx.length() ) + if ( (size_t)set.length() != idx.size() ) reporter->InternalError("compileset: lengths of sets differ"); rem = this; diff --git a/src/RE.h b/src/RE.h index 9386aa6f5f..3ea84636b4 100644 --- a/src/RE.h +++ b/src/RE.h @@ -22,10 +22,6 @@ class Specific_RE_Matcher; class RE_Matcher; class DFA_State; -declare(PDict,char); -declare(PDict,CCL); -declare(PList,CCL); - extern int case_insensitive; extern CCL* curr_ccl; extern NFA_Machine* nfa; @@ -123,9 +119,9 @@ protected: int multiline; char* pattern_text; - PDict(char) defs; - PDict(CCL) ccl_dict; - PList(CCL) ccl_list; + PDict defs; + PDict ccl_dict; + PList ccl_list; EquivClass equiv_class; int* ecs; DFA_Machine* dfa; diff --git a/src/Reporter.cc b/src/Reporter.cc index d44eff556b..d4095a4c9f 100644 --- a/src/Reporter.cc +++ b/src/Reporter.cc @@ -502,10 +502,7 @@ void Reporter::DoLog(const char* prefix, EventHandlerPtr event, FILE* out, vl.append(conn->BuildConnVal()); if ( addl ) - { - loop_over_list(*addl, i) - vl.append((*addl)[i]); - } + std::copy(addl->begin(), addl->end(), std::back_inserter(vl)); if ( conn ) conn->ConnectionEventFast(event, 0, std::move(vl)); @@ -516,8 +513,8 @@ void Reporter::DoLog(const char* prefix, EventHandlerPtr event, FILE* out, { if ( addl ) { - loop_over_list(*addl, i) - Unref((*addl)[i]); + for ( const auto& av : *addl ) + Unref(av); } } diff --git a/src/Rule.cc b/src/Rule.cc index 57cb82f65e..c9f3734692 100644 --- a/src/Rule.cc +++ b/src/Rule.cc @@ -13,25 +13,25 @@ Rule::~Rule() { delete [] id; - loop_over_list(patterns, i) + for ( const auto& p : patterns ) { - delete [] patterns[i]->pattern; - delete patterns[i]; + delete [] p->pattern; + delete p; } - loop_over_list(hdr_tests, j) - delete hdr_tests[j]; + for ( const auto& test : hdr_tests ) + delete test; - loop_over_list(conditions, k) - delete conditions[k]; + for ( const auto& cond : conditions ) + delete cond; - loop_over_list(actions, l) - delete actions[l]; + for ( const auto& action : actions ) + delete action; - loop_over_list(preconds, m) + for ( const auto& prec : preconds ) { - delete [] preconds[m]->id; - delete preconds[m]; + delete [] prec->id; + delete prec; } } @@ -49,21 +49,20 @@ void Rule::PrintDebug() { fprintf(stderr, "Rule %s (%d) %s\n", id, idx, active ? "[active]" : "[disabled]"); - loop_over_list(patterns, i) + for ( const auto& p : patterns ) { fprintf(stderr, " %-8s |%s| (%d) \n", - TypeToString(patterns[i]->type), patterns[i]->pattern, - patterns[i]->id); + TypeToString(p->type), p->pattern, p->id); } - loop_over_list(hdr_tests, j) - hdr_tests[j]->PrintDebug(); + for ( const auto& h : hdr_tests ) + h->PrintDebug(); - loop_over_list(conditions, k) - conditions[k]->PrintDebug(); + for ( const auto& c : conditions ) + c->PrintDebug(); - loop_over_list(actions, l) - actions[l]->PrintDebug(); + for ( const auto& a : actions ) + a->PrintDebug(); fputs("\n", stderr); } diff --git a/src/Rule.h b/src/Rule.h index 8cfc3835dd..0ac29d07fc 100644 --- a/src/Rule.h +++ b/src/Rule.h @@ -14,11 +14,8 @@ class RuleHdrTest; class Rule; -declare(PList, Rule); -typedef PList(Rule) rule_list; - -declare(PDict, Rule); -typedef PDict(Rule) rule_dict; +typedef PList rule_list; +typedef PDict rule_dict; class Rule { public: @@ -61,14 +58,9 @@ private: void SortHdrTests(); - declare(PList, RuleAction); - typedef PList(RuleAction) rule_action_list; - - declare(PList, RuleCondition); - typedef PList(RuleCondition) rule_condition_list; - - declare(PList, RuleHdrTest); - typedef PList(RuleHdrTest) rule_hdr_test_list; + typedef PList rule_action_list; + typedef PList rule_condition_list; + typedef PList rule_hdr_test_list; rule_hdr_test_list hdr_tests; rule_condition_list conditions; @@ -82,8 +74,7 @@ private: bool negate; // negate test }; - declare(PList, Precond); - typedef PList(Precond) precond_list; + typedef PList precond_list; precond_list preconds; rule_list dependents; // rules w/ us as a precondition @@ -101,8 +92,7 @@ private: uint32 depth; }; - declare(PList, Pattern); - typedef PList(Pattern) pattern_list; + typedef PList pattern_list; pattern_list patterns; Rule* next; // Linkage within RuleHdrTest tree: diff --git a/src/RuleMatcher.cc b/src/RuleMatcher.cc index 6fd13d2db7..2e0fcfd98e 100644 --- a/src/RuleMatcher.cc +++ b/src/RuleMatcher.cc @@ -25,6 +25,11 @@ uint32 RuleHdrTest::idcounter = 0; +static bool is_member_of(const int_list& l, int_list::value_type v) + { + return std::find(l.begin(), l.end(), v) != l.end(); + } + RuleHdrTest::RuleHdrTest(Prot arg_prot, uint32 arg_offset, uint32 arg_size, Comp arg_comp, maskedvalue_list* arg_vals) { @@ -78,21 +83,20 @@ RuleHdrTest::RuleHdrTest(RuleHdrTest& h) comp = h.comp; vals = new maskedvalue_list; - loop_over_list(*h.vals, i) - vals->append(new MaskedValue(*(*h.vals)[i])); + for ( const auto& val : *h.vals ) + vals->append(new MaskedValue(*val)); prefix_vals = h.prefix_vals; for ( int j = 0; j < Rule::TYPES; ++j ) { - loop_over_list(h.psets[j], k) + for ( PatternSet* orig_set : h.psets[j] ) { - PatternSet* orig_set = h.psets[j][k]; PatternSet* copied_set = new PatternSet; copied_set->re = 0; copied_set->ids = orig_set->ids; - loop_over_list(orig_set->patterns, l) - copied_set->patterns.append(copy_string(orig_set->patterns[l])); + for ( const auto& pattern : orig_set->patterns ) + copied_set->patterns.append(copy_string(pattern)); delete copied_set; // TODO: Why do we create copied_set only to then // never use it? @@ -110,14 +114,14 @@ RuleHdrTest::RuleHdrTest(RuleHdrTest& h) RuleHdrTest::~RuleHdrTest() { - loop_over_list(*vals, i) - delete (*vals)[i]; + for ( auto val : *vals ) + delete val; delete vals; for ( int i = 0; i < Rule::TYPES; ++i ) { - loop_over_list(psets[i], j) - delete psets[i][j]->re; + for ( auto pset : psets[i] ) + delete pset->re; } delete ruleset; @@ -149,12 +153,12 @@ void RuleHdrTest::PrintDebug() fprintf(stderr, " RuleHdrTest %s[%d:%d] %s", str_prot[prot], offset, size, str_comp[comp]); - loop_over_list(*vals, i) + for ( const auto& val : *vals ) fprintf(stderr, " 0x%08x/0x%08x", - (*vals)[i]->val, (*vals)[i]->mask); + val->val, val->mask); - for ( size_t i = 0; i < prefix_vals.size(); ++i ) - fprintf(stderr, " %s", prefix_vals[i].AsString().c_str()); + for ( const auto& prefix : prefix_vals ) + fprintf(stderr, " %s", prefix.AsString().c_str()); fprintf(stderr, "\n"); } @@ -176,22 +180,22 @@ RuleEndpointState::RuleEndpointState(analyzer::Analyzer* arg_analyzer, bool arg_ RuleEndpointState::~RuleEndpointState() { - loop_over_list(matchers, i) + for ( auto matcher : matchers ) { - delete matchers[i]->state; - delete matchers[i]; + delete matcher->state; + delete matcher; } - loop_over_list(matched_text, j) - delete matched_text[j]; + for ( auto text : matched_text ) + delete text; } RuleFileMagicState::~RuleFileMagicState() { - loop_over_list(matchers, i) + for ( auto matcher : matchers ) { - delete matchers[i]->state; - delete matchers[i]; + delete matcher->state; + delete matcher; } } @@ -209,8 +213,8 @@ RuleMatcher::~RuleMatcher() #endif Delete(root); - loop_over_list(rules, i) - delete rules[i]; + for ( auto rule : rules ) + delete rule; } void RuleMatcher::Delete(RuleHdrTest* node) @@ -275,13 +279,13 @@ void RuleMatcher::AddRule(Rule* rule) void RuleMatcher::BuildRulesTree() { - loop_over_list(rules, r) + for ( const auto& rule : rules ) { - if ( ! rules[r]->Active() ) + if ( ! rule->Active() ) continue; - rules[r]->SortHdrTests(); - InsertRuleIntoTree(rules[r], 0, root, 0); + rule->SortHdrTests(); + InsertRuleIntoTree(rule, 0, root, 0); } } @@ -289,10 +293,8 @@ void RuleMatcher::InsertRuleIntoTree(Rule* r, int testnr, RuleHdrTest* dest, int level) { // Initiliaze the preconditions - loop_over_list(r->preconds, i) + for ( const auto& pc : r->preconds ) { - Rule::Precond* pc = r->preconds[i]; - Rule* pc_rule = rules_by_id.Lookup(pc->id); if ( ! pc_rule ) { @@ -345,11 +347,10 @@ void RuleMatcher::BuildRegEx(RuleHdrTest* hdr_test, string_list* exprs, // For each type, get all patterns on this node. for ( Rule* r = hdr_test->pattern_rules; r; r = r->next ) { - loop_over_list(r->patterns, j) + for ( const auto& p : r->patterns ) { - Rule::Pattern* p = r->patterns[j]; exprs[p->type].append(p->pattern); - ids[p->type].append(p->id); + ids[p->type].push_back(p->id); } } @@ -374,7 +375,7 @@ void RuleMatcher::BuildRegEx(RuleHdrTest* hdr_test, string_list* exprs, loop_over_list(child_exprs[i], j) { exprs[i].append(child_exprs[i][j]); - ids[i].append(child_ids[i][j]); + ids[i].push_back(child_ids[i][j]); } } } @@ -394,7 +395,7 @@ void RuleMatcher::BuildRegEx(RuleHdrTest* hdr_test, string_list* exprs, void RuleMatcher::BuildPatternSets(RuleHdrTest::pattern_set_list* dst, const string_list& exprs, const int_list& ids) { - assert(exprs.length() == ids.length()); + assert(static_cast(exprs.length()) == ids.size()); // We build groups of at most sig_max_group_size regexps. @@ -406,7 +407,7 @@ void RuleMatcher::BuildPatternSets(RuleHdrTest::pattern_set_list* dst, if ( i < exprs.length() ) { group_exprs.append(exprs[i]); - group_ids.append(ids[i]); + group_ids.push_back(ids[i]); } if ( group_exprs.length() > sig_max_group_size || @@ -452,9 +453,10 @@ static inline uint32 getval(const u_char* data, int size) template static inline bool match_or(const maskedvalue_list& mvals, uint32 v, FuncT comp) { - loop_over_list(mvals, i) + // TODO: this could be a find_if + for ( const auto& val : mvals ) { - if ( comp(v & mvals[i]->mask, mvals[i]->val) ) + if ( comp(v & val->mask, val->val) ) return true; } return false; @@ -480,9 +482,10 @@ template static inline bool match_not_and(const maskedvalue_list& mvals, uint32 v, FuncT comp) { - loop_over_list(mvals, i) + // TODO: this could be a find_if + for ( const auto& val : mvals ) { - if ( comp(v & mvals[i]->mask, mvals[i]->val) ) + if ( comp(v & val->mask, val->val) ) return false; } return true; @@ -577,9 +580,8 @@ RuleFileMagicState* RuleMatcher::InitFileMagic() const { RuleFileMagicState* state = new RuleFileMagicState(); - loop_over_list(root->psets[Rule::FILE_MAGIC], i) + for ( const auto& set : root->psets[Rule::FILE_MAGIC] ) { - RuleHdrTest::PatternSet* set = root->psets[Rule::FILE_MAGIC][i]; assert(set->re); RuleFileMagicState::Matcher* m = new RuleFileMagicState::Matcher; m->state = new RE_Match_State(set->re); @@ -597,13 +599,13 @@ bool RuleMatcher::AllRulePatternsMatched(const Rule* r, MatchPos matchpos, DBG_LOG(DBG_RULES, "Checking rule: %s", r->id); // Check whether all patterns of the rule have matched. - loop_over_list(r->patterns, j) + for ( const auto& pattern : r->patterns ) { - if ( ams.find(r->patterns[j]->id) == ams.end() ) + if ( ams.find(pattern->id) == ams.end() ) return false; // See if depth is satisfied. - if ( matchpos > r->patterns[j]->offset + r->patterns[j]->depth ) + if ( matchpos > pattern->offset + pattern->depth ) return false; // FIXME: How to check for offset ??? ### @@ -640,10 +642,8 @@ RuleMatcher::MIME_Matches* RuleMatcher::Match(RuleFileMagicState* state, bool newmatch = false; - loop_over_list(state->matchers, x) + for ( const auto& m : state->matchers ) { - RuleFileMagicState::Matcher* m = state->matchers[x]; - if ( m->state->Match(data, len, true, false, true) ) newmatch = true; } @@ -655,9 +655,8 @@ RuleMatcher::MIME_Matches* RuleMatcher::Match(RuleFileMagicState* state, AcceptingMatchSet accepted_matches; - loop_over_list(state->matchers, y) + for ( const auto& m : state->matchers ) { - RuleFileMagicState::Matcher* m = state->matchers[y]; const AcceptingMatchSet& ams = m->state->AcceptedMatches(); accepted_matches.insert(ams.begin(), ams.end()); } @@ -682,10 +681,10 @@ RuleMatcher::MIME_Matches* RuleMatcher::Match(RuleFileMagicState* state, { Rule* r = *it; - loop_over_list(r->actions, rai) + for ( const auto& action : r->actions ) { const RuleActionMIME* ram = - dynamic_cast(r->actions[rai]); + dynamic_cast(action); if ( ! ram ) continue; @@ -734,11 +733,8 @@ RuleEndpointState* RuleMatcher::InitEndpoint(analyzer::Analyzer* analyzer, { for ( int i = 0; i < Rule::TYPES; ++i ) { - loop_over_list(hdr_test->psets[i], j) + for ( const auto& set : hdr_test->psets[i] ) { - RuleHdrTest::PatternSet* set = - hdr_test->psets[i][j]; - assert(set->re); RuleEndpointState::Matcher* m = @@ -855,9 +851,8 @@ void RuleMatcher::Match(RuleEndpointState* state, Rule::PatternType type, } // Feed data into all relevant matchers. - loop_over_list(state->matchers, x) + for ( const auto& m : state->matchers ) { - RuleEndpointState::Matcher* m = state->matchers[x]; if ( m->type == type && m->state->Match((const u_char*) data, data_len, bol, eol, clear) ) @@ -872,9 +867,8 @@ void RuleMatcher::Match(RuleEndpointState* state, Rule::PatternType type, AcceptingMatchSet accepted_matches; - loop_over_list(state->matchers, y ) + for ( const auto& m : state->matchers ) { - RuleEndpointState::Matcher* m = state->matchers[y]; const AcceptingMatchSet& ams = m->state->AcceptedMatches(); accepted_matches.insert(ams.begin(), ams.end()); } @@ -907,10 +901,8 @@ void RuleMatcher::Match(RuleEndpointState* state, Rule::PatternType type, DBG_LOG(DBG_RULES, "Accepted rule: %s", r->id); - loop_over_list(state->hdr_tests, k) + for ( const auto& h : state->hdr_tests ) { - RuleHdrTest* h = state->hdr_tests[k]; - DBG_LOG(DBG_RULES, "Checking for accepted rule on HdrTest %d", h->id); // Skip if rule does not belong to this node. @@ -920,7 +912,7 @@ void RuleMatcher::Match(RuleEndpointState* state, Rule::PatternType type, DBG_LOG(DBG_RULES, "On current node"); // Skip if rule already fired for this connection. - if ( state->matched_rules.is_member(r->Index()) ) + if ( is_member_of(state->matched_rules, r->Index()) ) continue; // Remember that all patterns have matched. @@ -960,9 +952,8 @@ void RuleMatcher::FinishEndpoint(RuleEndpointState* state) void RuleMatcher::ExecPureRules(RuleEndpointState* state, bool eos) { - loop_over_list(state->hdr_tests, i) + for ( const auto& hdr_test : state->hdr_tests ) { - RuleHdrTest* hdr_test = state->hdr_tests[i]; for ( Rule* r = hdr_test->pure_rules; r; r = r->next ) ExecRulePurely(r, 0, state, eos); } @@ -971,7 +962,7 @@ void RuleMatcher::ExecPureRules(RuleEndpointState* state, bool eos) bool RuleMatcher::ExecRulePurely(Rule* r, BroString* s, RuleEndpointState* state, bool eos) { - if ( state->matched_rules.is_member(r->Index()) ) + if ( is_member_of(state->matched_rules, r->Index()) ) return false; DBG_LOG(DBG_RULES, "Checking rule %s purely", r->ID()); @@ -997,10 +988,8 @@ bool RuleMatcher::EvalRuleConditions(Rule* r, RuleEndpointState* state, DBG_LOG(DBG_RULES, "Evaluating conditions for rule %s", r->ID()); // Check for other rules which have to match first. - loop_over_list(r->preconds, i) + for ( const auto& pc : r->preconds ) { - Rule::Precond* pc = r->preconds[i]; - RuleEndpointState* pc_state = state; if ( pc->opposite_dir ) @@ -1014,7 +1003,7 @@ bool RuleMatcher::EvalRuleConditions(Rule* r, RuleEndpointState* state, if ( ! pc->negate ) { - if ( ! pc_state->matched_rules.is_member(pc->rule->Index()) ) + if ( ! is_member_of(pc_state->matched_rules, pc->rule->Index()) ) // Precond rule has not matched yet. return false; } @@ -1024,13 +1013,13 @@ bool RuleMatcher::EvalRuleConditions(Rule* r, RuleEndpointState* state, if ( ! eos ) return false; - if ( pc_state->matched_rules.is_member(pc->rule->Index()) ) + if ( is_member_of(pc_state->matched_rules, pc->rule->Index()) ) return false; } } - loop_over_list(r->conditions, l) - if ( ! r->conditions[l]->DoMatch(r, state, data, len) ) + for ( const auto& cond : r->conditions ) + if ( ! cond->DoMatch(r, state, data, len) ) return false; DBG_LOG(DBG_RULES, "Conditions met: MATCH! %s", r->ID()); @@ -1041,19 +1030,18 @@ void RuleMatcher::ExecRuleActions(Rule* r, RuleEndpointState* state, const u_char* data, int len, bool eos) { if ( state->opposite && - state->opposite->matched_rules.is_member(r->Index()) ) + is_member_of(state->opposite->matched_rules, r->Index()) ) // We have already executed the actions. return; - state->matched_rules.append(r->Index()); + state->matched_rules.push_back(r->Index()); - loop_over_list(r->actions, i) - r->actions[i]->DoAction(r, state, data, len); + for ( const auto& action : r->actions ) + action->DoAction(r, state, data, len); // This rule may trigger some other rules; check them. - loop_over_list(r->dependents, j) + for ( const auto& dep : r->dependents ) { - Rule* dep = (r->dependents)[j]; ExecRule(dep, state, eos); if ( state->opposite ) ExecRule(dep, state->opposite, eos); @@ -1063,13 +1051,11 @@ void RuleMatcher::ExecRuleActions(Rule* r, RuleEndpointState* state, void RuleMatcher::ExecRule(Rule* rule, RuleEndpointState* state, bool eos) { // Nothing to do if it has already matched. - if ( state->matched_rules.is_member(rule->Index()) ) + if ( is_member_of(state->matched_rules, rule->Index()) ) return; - loop_over_list(state->hdr_tests, i) + for ( const auto& h : state->hdr_tests ) { - RuleHdrTest* h = state->hdr_tests[i]; - // Is it on this HdrTest at all? if ( ! h->ruleset->Contains(rule->Index()) ) continue; @@ -1099,20 +1085,20 @@ void RuleMatcher::ClearEndpointState(RuleEndpointState* state) state->payload_size = -1; - loop_over_list(state->matchers, j) - state->matchers[j]->state->Clear(); + for ( const auto& matcher : state->matchers ) + matcher->state->Clear(); } void RuleMatcher::ClearFileMagicState(RuleFileMagicState* state) const { - loop_over_list(state->matchers, j) - state->matchers[j]->state->Clear(); + for ( const auto& matcher : state->matchers ) + matcher->state->Clear(); } void RuleMatcher::PrintDebug() { - loop_over_list(rules, i) - rules[i]->PrintDebug(); + for ( const auto& rule : rules ) + rule->PrintDebug(); fprintf(stderr, "\n---------------\n"); @@ -1135,10 +1121,10 @@ void RuleMatcher::PrintTreeDebug(RuleHdrTest* node) RuleHdrTest::PatternSet* set = node->psets[i][j]; fprintf(stderr, - "[%d patterns in %s group %d from %d rules]\n", + "[%d patterns in %s group %d from %zu rules]\n", set->patterns.length(), Rule::TypeToString((Rule::PatternType) i), j, - set->ids.length()); + set->ids.size()); } } @@ -1182,9 +1168,8 @@ void RuleMatcher::GetStats(Stats* stats, RuleHdrTest* hdr_test) for ( int i = 0; i < Rule::TYPES; ++i ) { - loop_over_list(hdr_test->psets[i], j) + for ( const auto& set : hdr_test->psets[i] ) { - RuleHdrTest::PatternSet* set = hdr_test->psets[i][j]; assert(set->re); ++stats->matchers; @@ -1234,9 +1219,9 @@ void RuleMatcher::DumpStateStats(BroFile* f, RuleHdrTest* hdr_test) set->re->DFA()->NumStates(), Rule::TypeToString((Rule::PatternType)i), j)); - loop_over_list(set->ids, k) + for ( const auto& id : set->ids ) { - Rule* r = Rule::rule_table[set->ids[k] - 1]; + Rule* r = Rule::rule_table[id - 1]; f->Write(fmt("%s ", r->ID())); } @@ -1345,8 +1330,8 @@ void id_to_maskedvallist(const char* id, maskedvalue_list* append_to, { ListVal* lv = v->AsTableVal()->ConvertToPureList(); val_list* vals = lv->Vals(); - loop_over_list(*vals, i ) - if ( ! val_to_maskedval((*vals)[i], append_to, prefix_vector) ) + for ( const auto& val : *vals ) + if ( ! val_to_maskedval(val, append_to, prefix_vector) ) { Unref(lv); return; diff --git a/src/RuleMatcher.h b/src/RuleMatcher.h index 23b7e6d731..c02fe9aadd 100644 --- a/src/RuleMatcher.h +++ b/src/RuleMatcher.h @@ -56,13 +56,9 @@ struct MaskedValue { uint32 mask; }; -declare(PList, MaskedValue); -typedef PList(MaskedValue) maskedvalue_list; - -typedef PList(char) string_list; - -declare(PList, BroString); -typedef PList(BroString) bstr_list; +typedef PList maskedvalue_list; +typedef PList string_list; +typedef PList bstr_list; // Get values from Bro's script-level variables. extern void id_to_maskedvallist(const char* id, maskedvalue_list* append_to, @@ -119,8 +115,7 @@ private: int_list ids; // (only needed for debugging) }; - declare(PList, PatternSet); - typedef PList(PatternSet) pattern_set_list; + typedef PList pattern_set_list; pattern_set_list psets[Rule::TYPES]; // List of rules belonging to this node. @@ -136,8 +131,7 @@ private: int level; // level within the tree }; -declare(PList, RuleHdrTest); -typedef PList(RuleHdrTest) rule_hdr_test_list; +typedef PList rule_hdr_test_list; // RuleEndpointState keeps the per-stream matching state of one // connection endpoint. @@ -172,8 +166,7 @@ private: Rule::PatternType type; }; - declare(PList, Matcher); - typedef PList(Matcher) matcher_list; + typedef PList matcher_list; bool is_orig; analyzer::Analyzer* analyzer; @@ -212,8 +205,7 @@ private: RE_Match_State* state; }; - declare(PList, Matcher); - typedef PList(Matcher) matcher_list; + typedef PList matcher_list; matcher_list matchers; }; diff --git a/src/Scope.cc b/src/Scope.cc index 5107bd8e9a..56a9968417 100644 --- a/src/Scope.cc +++ b/src/Scope.cc @@ -7,8 +7,7 @@ #include "Scope.h" #include "Reporter.h" -declare(PList,Scope); -typedef PList(Scope) scope_list; +typedef PList scope_list; static scope_list scopes; static Scope* top_scope; @@ -20,7 +19,7 @@ Scope::Scope(ID* id, attr_list* al) attrs = al; return_type = 0; - local = new PDict(ID)(ORDERED); + local = new PDict(ORDERED); inits = new id_list; if ( id ) @@ -48,8 +47,8 @@ Scope::~Scope() if ( attrs ) { - loop_over_list(*attrs, i) - Unref((*attrs)[i]); + for ( const auto& attr : *attrs ) + Unref(attr); delete attrs; } @@ -109,7 +108,7 @@ void Scope::Describe(ODesc* d) const TraversalCode Scope::Traverse(TraversalCallback* cb) const { - PDict(ID)* ids = GetIDs(); + PDict* ids = GetIDs(); IterCookie* iter = ids->InitForIteration(); HashKey* key; diff --git a/src/Scope.h b/src/Scope.h index 5fcd31fd8b..4dfb9bb0e5 100644 --- a/src/Scope.h +++ b/src/Scope.h @@ -15,8 +15,6 @@ class ID; class BroType; class ListVal; -declare(PDict,ID); - class Scope : public BroObj { public: explicit Scope(ID* id, attr_list* al); @@ -35,11 +33,11 @@ public: BroType* ReturnType() const { return return_type; } int Length() const { return local->Length(); } - PDict(ID)* Vars() const { return local; } + PDict* Vars() const { return local; } ID* GenerateTemporary(const char* name); - PDict(ID)* GetIDs() const { return local; } + PDict* GetIDs() const { return local; } // Returns the list of variables needing initialization, and // removes it from this Scope. @@ -56,7 +54,7 @@ protected: ID* scope_id; attr_list* attrs; BroType* return_type; - PDict(ID)* local; + PDict* local; id_list* inits; }; diff --git a/src/Sessions.h b/src/Sessions.h index 27b0ab474c..74a4d2c992 100644 --- a/src/Sessions.h +++ b/src/Sessions.h @@ -20,9 +20,6 @@ class Connection; class ConnCompressor; struct ConnID; -declare(PDict,Connection); -declare(PDict,FragReassembler); - class Discarder; class PacketFilter; @@ -216,10 +213,10 @@ protected: const Packet *pkt, const EncapsulationStack* encap); CompositeHash* ch; - PDict(Connection) tcp_conns; - PDict(Connection) udp_conns; - PDict(Connection) icmp_conns; - PDict(FragReassembler) fragments; + PDict tcp_conns; + PDict udp_conns; + PDict icmp_conns; + PDict fragments; typedef pair IPPair; typedef pair TunnelActivity; diff --git a/src/Stats.cc b/src/Stats.cc index 9489f12f93..674c2fb50c 100644 --- a/src/Stats.cc +++ b/src/Stats.cc @@ -239,7 +239,7 @@ void ProfileLogger::Log() // Script-level state. unsigned int size, mem = 0; - PDict(ID)* globals = global_scope()->Vars(); + PDict* globals = global_scope()->Vars(); if ( expensive ) { diff --git a/src/Stmt.cc b/src/Stmt.cc index 913fe31573..a452f9cd82 100644 --- a/src/Stmt.cc +++ b/src/Stmt.cc @@ -123,9 +123,9 @@ ExprListStmt::ExprListStmt(BroStmtTag t, ListExpr* arg_l) l = arg_l; const expr_list& e = l->Exprs(); - loop_over_list(e, i) + for ( const auto& expr : e ) { - const BroType* t = e[i]->Type(); + const BroType* t = expr->Type(); if ( ! t || t->Tag() == TYPE_VOID ) Error("value of type void illegal"); } @@ -172,9 +172,9 @@ TraversalCode ExprListStmt::Traverse(TraversalCallback* cb) const HANDLE_TC_STMT_PRE(tc); const expr_list& e = l->Exprs(); - loop_over_list(e, i) + for ( const auto& expr : e ) { - tc = e[i]->Traverse(cb); + tc = expr->Traverse(cb); HANDLE_TC_STMT_PRE(tc); } @@ -430,8 +430,8 @@ Case::~Case() Unref(expr_cases); Unref(s); - loop_over_list((*type_cases), i) - Unref((*type_cases)[i]); + for ( const auto& id : *type_cases ) + Unref(id); delete type_cases; } @@ -631,9 +631,9 @@ SwitchStmt::SwitchStmt(Expr* index, case_list* arg_cases) : { have_types = true; - loop_over_list((*tl), j) + for ( const auto& t : *tl ) { - BroType* ct = (*tl)[j]->Type(); + BroType* ct = t->Type(); if ( ! can_cast_value_to_type(e->Type(), ct) ) { @@ -641,7 +641,7 @@ SwitchStmt::SwitchStmt(Expr* index, case_list* arg_cases) : continue; } - if ( ! AddCaseLabelTypeMapping((*tl)[j], i) ) + if ( ! AddCaseLabelTypeMapping(t, i) ) { c->Error("duplicate case label"); continue; @@ -665,8 +665,8 @@ SwitchStmt::SwitchStmt(Expr* index, case_list* arg_cases) : SwitchStmt::~SwitchStmt() { - loop_over_list(*cases, i) - Unref((*cases)[i]); + for ( const auto& c : *cases ) + Unref(c); delete cases; delete comp_hash; @@ -793,9 +793,8 @@ int SwitchStmt::IsPure() const if ( ! e->IsPure() ) return 0; - loop_over_list(*cases, i) + for ( const auto& c : *cases ) { - Case* c = (*cases)[i]; if ( ! c->ExprCases()->IsPure() || ! c->Body()->IsPure() ) return 0; } @@ -812,8 +811,8 @@ void SwitchStmt::Describe(ODesc* d) const d->PushIndent(); d->AddCount(cases->length()); - loop_over_list(*cases, i) - (*cases)[i]->Describe(d); + for ( const auto& c : *cases ) + c->Describe(d); d->PopIndent(); if ( ! d->IsBinary() ) @@ -830,9 +829,9 @@ TraversalCode SwitchStmt::Traverse(TraversalCallback* cb) const tc = e->Traverse(cb); HANDLE_TC_STMT_PRE(tc); - loop_over_list(*cases, i) + for ( const auto& c : *cases ) { - tc = (*cases)[i]->Traverse(cb); + tc = c->Traverse(cb); HANDLE_TC_STMT_PRE(tc); } @@ -1132,8 +1131,8 @@ ForStmt::ForStmt(id_list* arg_loop_vars, Expr* loop_expr, ID* val_var) ForStmt::~ForStmt() { - loop_over_list(*loop_vars, i) - Unref((*loop_vars)[i]); + for ( const auto& var : *loop_vars ) + Unref(var); delete loop_vars; Unref(value_var); @@ -1147,7 +1146,7 @@ Val* ForStmt::DoExec(Frame* f, Val* v, stmt_flow_type& flow) const if ( v->Type()->Tag() == TYPE_TABLE ) { TableVal* tv = v->AsTableVal(); - const PDict(TableEntryVal)* loop_vals = tv->AsTable(); + const PDict* loop_vals = tv->AsTable(); if ( ! loop_vals->Length() ) return 0; @@ -1275,9 +1274,9 @@ TraversalCode ForStmt::Traverse(TraversalCallback* cb) const TraversalCode tc = cb->PreStmt(this); HANDLE_TC_STMT_PRE(tc); - loop_over_list(*loop_vars, i) + for ( const auto& var : *loop_vars ) { - tc = (*loop_vars)[i]->Traverse(cb); + tc = var->Traverse(cb); HANDLE_TC_STMT_PRE(tc); } @@ -1445,8 +1444,8 @@ StmtList::StmtList() : Stmt(STMT_LIST) StmtList::~StmtList() { - loop_over_list(stmts, i) - Unref(stmts[i]); + for ( const auto& stmt : stmts ) + Unref(stmt); } Val* StmtList::Exec(Frame* f, stmt_flow_type& flow) const @@ -1454,17 +1453,17 @@ Val* StmtList::Exec(Frame* f, stmt_flow_type& flow) const RegisterAccess(); flow = FLOW_NEXT; - loop_over_list(stmts, i) + for ( const auto& stmt : stmts ) { - f->SetNextStmt(stmts[i]); + f->SetNextStmt(stmt); - if ( ! pre_execute_stmt(stmts[i], f) ) + if ( ! pre_execute_stmt(stmt, f) ) { // ### Abort or something } - Val* result = stmts[i]->Exec(f, flow); + Val* result = stmt->Exec(f, flow); - if ( ! post_execute_stmt(stmts[i], f, result, &flow) ) + if ( ! post_execute_stmt(stmt, f, result, &flow) ) { // ### Abort or something } @@ -1477,8 +1476,8 @@ Val* StmtList::Exec(Frame* f, stmt_flow_type& flow) const int StmtList::IsPure() const { - loop_over_list(stmts, i) - if ( ! stmts[i]->IsPure() ) + for ( const auto& stmt : stmts ) + if ( ! stmt->IsPure() ) return 0; return 1; } @@ -1502,9 +1501,9 @@ void StmtList::Describe(ODesc* d) const d->NL(); } - loop_over_list(stmts, i) + for ( const auto& stmt : stmts ) { - stmts[i]->Describe(d); + stmt->Describe(d); d->NL(); } @@ -1518,9 +1517,9 @@ TraversalCode StmtList::Traverse(TraversalCallback* cb) const TraversalCode tc = cb->PreStmt(this); HANDLE_TC_STMT_PRE(tc); - loop_over_list(stmts, i) + for ( const auto& stmt : stmts ) { - tc = stmts[i]->Traverse(cb); + tc = stmt->Traverse(cb); HANDLE_TC_STMT_PRE(tc); } @@ -1533,21 +1532,21 @@ Val* EventBodyList::Exec(Frame* f, stmt_flow_type& flow) const RegisterAccess(); flow = FLOW_NEXT; - loop_over_list(stmts, i) + for ( const auto& stmt : stmts ) { - f->SetNextStmt(stmts[i]); + f->SetNextStmt(stmt); // Ignore the return value, since there shouldn't be // any; and ignore the flow, since we still execute // all of the event bodies even if one of them does // a FLOW_RETURN. - if ( ! pre_execute_stmt(stmts[i], f) ) + if ( ! pre_execute_stmt(stmt, f) ) { // ### Abort or something } - Val* result = stmts[i]->Exec(f, flow); + Val* result = stmt->Exec(f, flow); - if ( ! post_execute_stmt(stmts[i], f, result, &flow) ) + if ( ! post_execute_stmt(stmt, f, result, &flow) ) { // ### Abort or something } } @@ -1563,16 +1562,16 @@ void EventBodyList::Describe(ODesc* d) const { if ( d->IsReadable() && stmts.length() > 0 ) { - loop_over_list(stmts, i) + for ( const auto& stmt : stmts ) { if ( ! d->IsBinary() ) { d->Add("{"); d->PushIndent(); - stmts[i]->AccessStats(d); + stmt->AccessStats(d); } - stmts[i]->Describe(d); + stmt->Describe(d); if ( ! d->IsBinary() ) { @@ -1588,8 +1587,8 @@ void EventBodyList::Describe(ODesc* d) const InitStmt::~InitStmt() { - loop_over_list(*inits, i) - Unref((*inits)[i]); + for ( const auto& init : *inits ) + Unref(init); delete inits; } @@ -1599,9 +1598,8 @@ Val* InitStmt::Exec(Frame* f, stmt_flow_type& flow) const RegisterAccess(); flow = FLOW_NEXT; - loop_over_list(*inits, i) + for ( const auto& aggr : *inits ) { - ID* aggr = (*inits)[i]; BroType* t = aggr->Type(); Val* v = 0; @@ -1649,9 +1647,9 @@ TraversalCode InitStmt::Traverse(TraversalCallback* cb) const TraversalCode tc = cb->PreStmt(this); HANDLE_TC_STMT_PRE(tc); - loop_over_list(*inits, i) + for ( const auto& init : *inits ) { - tc = (*inits)[i]->Traverse(cb); + tc = init->Traverse(cb); HANDLE_TC_STMT_PRE(tc); } diff --git a/src/Stmt.h b/src/Stmt.h index 7136ffe306..0fc6b437c1 100644 --- a/src/Stmt.h +++ b/src/Stmt.h @@ -17,8 +17,6 @@ class StmtList; class ForStmt; -declare(PDict, int); - class Stmt : public BroObj { public: BroStmtTag Tag() const { return tag; } @@ -195,8 +193,7 @@ protected: Stmt* s; }; -declare(PList,Case); -typedef PList(Case) case_list; +typedef PList case_list; class SwitchStmt : public ExprStmt { public: @@ -238,7 +235,7 @@ protected: case_list* cases; int default_case_idx; CompositeHash* comp_hash; - PDict(int) case_label_value_map; + PDict case_label_value_map; std::vector> case_label_type_list; }; diff --git a/src/Type.cc b/src/Type.cc index efe3d254c5..f0605c9a06 100644 --- a/src/Type.cc +++ b/src/Type.cc @@ -207,16 +207,16 @@ unsigned int BroType::MemoryAllocation() const TypeList::~TypeList() { - loop_over_list(types, i) - Unref(types[i]); + for ( const auto& type : types ) + Unref(type); Unref(pure_type); } int TypeList::AllMatch(const BroType* t, int is_init) const { - loop_over_list(types, i) - if ( ! same_type(types[i], t, is_init) ) + for ( const auto& type : types ) + if ( ! same_type(type, t, is_init) ) return 0; return 1; } @@ -381,9 +381,8 @@ TableType::TableType(TypeList* ind, BroType* yield) type_list* tl = indices->Types(); - loop_over_list(*tl, i) + for ( const auto& tli : *tl ) { - BroType* tli = (*tl)[i]; InternalTypeTag t = tli->InternalType(); if ( t == TYPE_INTERNAL_ERROR ) @@ -704,8 +703,8 @@ RecordType::RecordType(type_decl_list* arg_types) : BroType(TYPE_RECORD) RecordType* RecordType::ShallowClone() { auto pass = new type_decl_list(); - loop_over_list(*types, i) - pass->append(new TypeDecl(*(*types)[i])); + for ( const auto& type : *types ) + pass->append(new TypeDecl(*type)); return new RecordType(pass); } @@ -713,8 +712,8 @@ RecordType::~RecordType() { if ( types ) { - loop_over_list(*types, i) - delete (*types)[i]; + for ( auto type : *types ) + delete type; delete types; } @@ -823,17 +822,15 @@ const char* RecordType::AddFields(type_decl_list* others, attr_list* attr) if ( attr ) { - loop_over_list(*attr, j) + for ( const auto& at : *attr ) { - if ( (*attr)[j]->Tag() == ATTR_LOG ) + if ( at->Tag() == ATTR_LOG ) log = true; } } - loop_over_list(*others, i) + for ( const auto& td : *others ) { - TypeDecl* td = (*others)[i]; - if ( ! td->FindAttr(ATTR_DEFAULT) && ! td->FindAttr(ATTR_OPTIONAL) ) return "extension field must be &optional or have &default"; @@ -883,11 +880,11 @@ void RecordType::DescribeFields(ODesc* d) const { d->AddCount(0); d->AddCount(types->length()); - loop_over_list(*types, i) + for ( const auto& type : *types ) { - (*types)[i]->type->Describe(d); + type->type->Describe(d); d->SP(); - d->Add((*types)[i]->id); + d->Add(type->id); d->SP(); } } diff --git a/src/Type.h b/src/Type.h index 19fad4b2ce..17a74476ab 100644 --- a/src/Type.h +++ b/src/Type.h @@ -455,8 +455,7 @@ public: const char* id; }; -declare(PList,TypeDecl); -typedef PList(TypeDecl) type_decl_list; +typedef PList type_decl_list; class RecordType : public BroType { public: diff --git a/src/Val.cc b/src/Val.cc index d0f11a2062..7372c12f66 100644 --- a/src/Val.cc +++ b/src/Val.cc @@ -1035,7 +1035,7 @@ Val* StringVal::Substitute(RE_Matcher* re, StringVal* repl, bool do_all) // cut_points is a set of pairs of indices in str that should // be removed/replaced. A pair means "delete starting // at offset x, up to but not including offset y". - List(ptr_compat_int) cut_points; // where RE matches pieces of str + vector> cut_points; int size = 0; // size of result @@ -1058,8 +1058,7 @@ Val* StringVal::Substitute(RE_Matcher* re, StringVal* repl, bool do_all) break; // s[offset .. offset+end_of_match-1] matches re. - cut_points.append(offset); - cut_points.append(offset + end_of_match); + cut_points.push_back({offset, offset + end_of_match}); offset += end_of_match; n -= end_of_match; @@ -1075,8 +1074,7 @@ Val* StringVal::Substitute(RE_Matcher* re, StringVal* repl, bool do_all) // size now reflects amount of space copied. Factor in amount // of space for replacement text. - int num_cut_points = cut_points.length() / 2; - size += num_cut_points * repl->Len(); + size += cut_points.size() * repl->Len(); // And a final NUL for good health. ++size; @@ -1086,13 +1084,13 @@ Val* StringVal::Substitute(RE_Matcher* re, StringVal* repl, bool do_all) // Copy it all over. int start_offset = 0; - for ( int i = 0; i < cut_points.length(); i += 2 /* loop over pairs */ ) + for ( const auto& point : cut_points ) { - int num_to_copy = cut_points[i] - start_offset; + int num_to_copy = point.first - start_offset; memcpy(r, s + start_offset, num_to_copy); r += num_to_copy; - start_offset = cut_points[i+1]; + start_offset = point.second; // Now add in replacement text. memcpy(r, repl->Bytes(), repl->Len()); @@ -1189,8 +1187,8 @@ ListVal::ListVal(TypeTag t) ListVal::~ListVal() { - loop_over_list(vals, i) - Unref(vals[i]); + for ( const auto& val : vals ) + Unref(val); Unref(type); } @@ -1200,9 +1198,9 @@ RE_Matcher* ListVal::BuildRE() const Internal("non-string list in ListVal::IncludedInString"); RE_Matcher* re = new RE_Matcher(); - loop_over_list(vals, i) + for ( const auto& val : vals ) { - const char* vs = (const char*) (vals[i]->AsString()->Bytes()); + const char* vs = (const char*) (val->AsString()->Bytes()); re->AddPat(vs); } @@ -1231,8 +1229,8 @@ TableVal* ListVal::ConvertToSet() const SetType* s = new SetType(set_index, 0); TableVal* t = new TableVal(s); - loop_over_list(vals, i) - t->Assign(vals[i], 0); + for ( const auto& val : vals ) + t->Assign(val, 0); Unref(s); return t; @@ -1269,8 +1267,8 @@ Val* ListVal::DoClone(CloneState* state) lv->vals.resize(vals.length()); state->NewClone(this, lv); - loop_over_list(vals, i) - lv->Append(vals[i]->Clone(state)); + for ( const auto& val : vals ) + lv->Append(val->Clone(state)); return lv; } @@ -1278,8 +1276,8 @@ Val* ListVal::DoClone(CloneState* state) unsigned int ListVal::MemoryAllocation() const { unsigned int size = 0; - loop_over_list(vals, i) - size += vals[i]->MemoryAllocation(); + for ( const auto& val : vals ) + size += val->MemoryAllocation(); return size + padded_sizeof(*this) + vals.MemoryAllocation() - padded_sizeof(vals) + type->MemoryAllocation(); @@ -1334,7 +1332,7 @@ void TableVal::Init(TableType* t) subnets = 0; table_hash = new CompositeHash(table_type->Indices()); - val.table_val = new PDict(TableEntryVal); + val.table_val = new PDict; val.table_val->SetDeleteFunc(table_entry_val_delete_func); } @@ -1357,7 +1355,7 @@ void TableVal::RemoveAll() { // Here we take the brute force approach. delete AsTable(); - val.table_val = new PDict(TableEntryVal); + val.table_val = new PDict; val.table_val->SetDeleteFunc(table_entry_val_delete_func); } @@ -1370,7 +1368,7 @@ int TableVal::RecursiveSize() const != TYPE_TABLE ) return n; - PDict(TableEntryVal)* v = val.table_val; + PDict* v = val.table_val; IterCookie* c = v->InitForIteration(); TableEntryVal* tv; @@ -1508,7 +1506,7 @@ int TableVal::AddTo(Val* val, int is_first_init, bool propagate_ops) const return 0; } - const PDict(TableEntryVal)* tbl = AsTable(); + const PDict* tbl = AsTable(); IterCookie* c = tbl->InitForIteration(); HashKey* k; @@ -1556,7 +1554,7 @@ int TableVal::RemoveFrom(Val* val) const return 0; } - const PDict(TableEntryVal)* tbl = AsTable(); + const PDict* tbl = AsTable(); IterCookie* c = tbl->InitForIteration(); HashKey* k; @@ -1578,14 +1576,14 @@ TableVal* TableVal::Intersect(const TableVal* tv) const { TableVal* result = new TableVal(table_type); - const PDict(TableEntryVal)* t0 = AsTable(); - const PDict(TableEntryVal)* t1 = tv->AsTable(); - PDict(TableEntryVal)* t2 = result->AsNonConstTable(); + const PDict* t0 = AsTable(); + const PDict* t1 = tv->AsTable(); + PDict* t2 = result->AsNonConstTable(); // Figure out which is smaller; assign it to t1. if ( t1->Length() > t0->Length() ) { // Swap. - const PDict(TableEntryVal)* tmp = t1; + const PDict* tmp = t1; t1 = t0; t0 = tmp; } @@ -1607,8 +1605,8 @@ TableVal* TableVal::Intersect(const TableVal* tv) const bool TableVal::EqualTo(const TableVal* tv) const { - const PDict(TableEntryVal)* t0 = AsTable(); - const PDict(TableEntryVal)* t1 = tv->AsTable(); + const PDict* t0 = AsTable(); + const PDict* t1 = tv->AsTable(); if ( t0->Length() != t1->Length() ) return false; @@ -1634,8 +1632,8 @@ bool TableVal::EqualTo(const TableVal* tv) const bool TableVal::IsSubsetOf(const TableVal* tv) const { - const PDict(TableEntryVal)* t0 = AsTable(); - const PDict(TableEntryVal)* t1 = tv->AsTable(); + const PDict* t0 = AsTable(); + const PDict* t1 = tv->AsTable(); if ( t0->Length() > t1->Length() ) return false; @@ -1771,8 +1769,8 @@ Val* TableVal::Default(Val* index) { const val_list* vl0 = index->AsListVal()->Vals(); vl = val_list(vl0->length()); - loop_over_list(*vl0, i) - vl.append((*vl0)[i]->Ref()); + for ( const auto& v : *vl0 ) + vl.append(v->Ref()); } else { @@ -1828,7 +1826,7 @@ Val* TableVal::Lookup(Val* index, bool use_default_val) return def; } - const PDict(TableEntryVal)* tbl = AsTable(); + const PDict* tbl = AsTable(); if ( tbl->Length() > 0 ) { @@ -1973,7 +1971,7 @@ ListVal* TableVal::ConvertToList(TypeTag t) const { ListVal* l = new ListVal(t); - const PDict(TableEntryVal)* tbl = AsTable(); + const PDict* tbl = AsTable(); IterCookie* c = tbl->InitForIteration(); HashKey* k; @@ -2014,7 +2012,7 @@ ListVal* TableVal::ConvertToPureList() const void TableVal::Describe(ODesc* d) const { - const PDict(TableEntryVal)* tbl = AsTable(); + const PDict* tbl = AsTable(); int n = tbl->Length(); if ( d->IsBinary() || d->IsPortable() ) @@ -2158,7 +2156,7 @@ void TableVal::DoExpire(double t) if ( ! type ) return; // FIX ME ### - PDict(TableEntryVal)* tbl = AsNonConstTable(); + PDict* tbl = AsNonConstTable(); double timeout = GetExpireTime(); @@ -2343,7 +2341,7 @@ Val* TableVal::DoClone(CloneState* state) auto tv = new TableVal(table_type); state->NewClone(this, tv); - const PDict(TableEntryVal)* tbl = AsTable(); + const PDict* tbl = AsTable(); IterCookie* cookie = tbl->InitForIteration(); HashKey* key; @@ -2392,7 +2390,7 @@ unsigned int TableVal::MemoryAllocation() const { unsigned int size = 0; - PDict(TableEntryVal)* v = val.table_val; + PDict* v = val.table_val; IterCookie* c = v->InitForIteration(); TableEntryVal* tv; @@ -2675,9 +2673,9 @@ Val* RecordVal::DoClone(CloneState* state) rv->origin = nullptr; state->NewClone(this, rv); - loop_over_list(*val.val_list_val, i) + for ( const auto& vlv : *val.val_list_val ) { - Val* v = (*val.val_list_val)[i] ? (*val.val_list_val)[i]->Clone(state) : nullptr; + Val* v = vlv ? vlv->Clone(state) : nullptr; rv->val.val_list_val->append(v); } @@ -2687,12 +2685,10 @@ Val* RecordVal::DoClone(CloneState* state) unsigned int RecordVal::MemoryAllocation() const { unsigned int size = 0; - const val_list* vl = AsRecord(); - loop_over_list(*vl, i) + for ( const auto& v : *vl ) { - Val* v = (*vl)[i]; if ( v ) size += v->MemoryAllocation(); } @@ -3070,8 +3066,8 @@ void delete_vals(val_list* vals) { if ( vals ) { - loop_over_list(*vals, i) - Unref((*vals)[i]); + for ( const auto& val : *vals ) + Unref(val); delete vals; } } diff --git a/src/Val.h b/src/Val.h index fb85ce2794..b13747c58a 100644 --- a/src/Val.h +++ b/src/Val.h @@ -55,7 +55,6 @@ class StateAccess; class VectorVal; class TableEntryVal; -declare(PDict,TableEntryVal); typedef union { // Used for bool, int, enum. @@ -77,7 +76,7 @@ typedef union { Func* func_val; BroFile* file_val; RE_Matcher* re_val; - PDict(TableEntryVal)* table_val; + PDict* table_val; val_list* val_list_val; vector* vector_val; @@ -228,7 +227,7 @@ public: CONST_ACCESSOR2(TYPE_ENUM, int, int_val, AsEnum) CONST_ACCESSOR(TYPE_STRING, BroString*, string_val, AsString) CONST_ACCESSOR(TYPE_FUNC, Func*, func_val, AsFunc) - CONST_ACCESSOR(TYPE_TABLE, PDict(TableEntryVal)*, table_val, AsTable) + CONST_ACCESSOR(TYPE_TABLE, PDict*, table_val, AsTable) CONST_ACCESSOR(TYPE_RECORD, val_list*, val_list_val, AsRecord) CONST_ACCESSOR(TYPE_FILE, BroFile*, file_val, AsFile) CONST_ACCESSOR(TYPE_PATTERN, RE_Matcher*, re_val, AsPattern) @@ -400,7 +399,7 @@ protected: #endif } - ACCESSOR(TYPE_TABLE, PDict(TableEntryVal)*, table_val, AsNonConstTable) + ACCESSOR(TYPE_TABLE, PDict*, table_val, AsNonConstTable) ACCESSOR(TYPE_RECORD, val_list*, val_list_val, AsNonConstRecord) // For internal use by the Val::Clone() methods. diff --git a/src/Var.cc b/src/Var.cc index 3f8f411dc3..d0cce1309c 100644 --- a/src/Var.cc +++ b/src/Var.cc @@ -448,10 +448,8 @@ void end_func(Stmt* body) if ( attrs ) { - loop_over_list(*attrs, i) + for ( const auto& a : *attrs ) { - Attr* a = (*attrs)[i]; - if ( a->Tag() == ATTR_DEPRECATED ) continue; diff --git a/src/analyzer/Analyzer.cc b/src/analyzer/Analyzer.cc index 9977d44e70..95d0322459 100644 --- a/src/analyzer/Analyzer.cc +++ b/src/analyzer/Analyzer.cc @@ -728,11 +728,11 @@ void Analyzer::CancelTimers() // traversing. Thus, we first make a copy of the list which we then // iterate through. timer_list tmp(timers.length()); - loop_over_list(timers, j) - tmp.append(timers[j]); + std::copy(timers.begin(), timers.end(), back_inserter(tmp)); - loop_over_list(tmp, i) - Conn()->GetTimerMgr()->Cancel(tmp[i]); + // TODO: could be a for_each + for ( auto timer : tmp ) + Conn()->GetTimerMgr()->Cancel(timer); timers_canceled = 1; timers.clear(); diff --git a/src/analyzer/protocol/mime/MIME.h b/src/analyzer/protocol/mime/MIME.h index 9fcf2ea468..fa85747626 100644 --- a/src/analyzer/protocol/mime/MIME.h +++ b/src/analyzer/protocol/mime/MIME.h @@ -84,7 +84,6 @@ protected: }; -// declare(PList, MIME_Header); typedef vector MIME_HeaderList; class MIME_Entity { diff --git a/src/analyzer/protocol/rpc/RPC.h b/src/analyzer/protocol/rpc/RPC.h index 40c65a00d4..225c2c8cd4 100644 --- a/src/analyzer/protocol/rpc/RPC.h +++ b/src/analyzer/protocol/rpc/RPC.h @@ -99,8 +99,6 @@ protected: Val* v; // single (perhaps compound) value corresponding to call }; -declare(PDict,RPC_CallInfo); - class RPC_Interpreter { public: explicit RPC_Interpreter(analyzer::Analyzer* analyzer); @@ -125,7 +123,7 @@ protected: void Weird(const char* name, const char* addl = ""); - PDict(RPC_CallInfo) calls; + PDict calls; analyzer::Analyzer* analyzer; }; diff --git a/src/analyzer/protocol/stepping-stone/SteppingStone.cc b/src/analyzer/protocol/stepping-stone/SteppingStone.cc index d3844846b9..d47a0b6745 100644 --- a/src/analyzer/protocol/stepping-stone/SteppingStone.cc +++ b/src/analyzer/protocol/stepping-stone/SteppingStone.cc @@ -77,12 +77,11 @@ int SteppingStoneEndpoint::DataSent(double t, uint64 seq, int len, int caplen, while ( stp_manager->OrderedEndpoints().length() > 0 ) { - int f = stp_manager->OrderedEndpoints().front(); + auto e = stp_manager->OrderedEndpoints().front(); - if ( stp_manager->OrderedEndpoints()[f]->stp_resume_time < tmin ) + if ( e->stp_resume_time < tmin ) { - SteppingStoneEndpoint* e = - stp_manager->OrderedEndpoints().pop_front(); + stp_manager->OrderedEndpoints().pop_front(); e->Done(); Unref(e); } @@ -109,9 +108,8 @@ int SteppingStoneEndpoint::DataSent(double t, uint64 seq, int len, int caplen, stp_last_time = stp_resume_time = t; Event(stp_resume_endp, stp_id); - loop_over_queue(stp_manager->OrderedEndpoints(), i) + for ( auto ep : stp_manager->OrderedEndpoints() ) { - SteppingStoneEndpoint* ep = stp_manager->OrderedEndpoints()[i]; if ( ep->endp->TCP() != endp->TCP() ) { Ref(ep); diff --git a/src/analyzer/protocol/stepping-stone/SteppingStone.h b/src/analyzer/protocol/stepping-stone/SteppingStone.h index b9ebd1ff8b..502e3caa7e 100644 --- a/src/analyzer/protocol/stepping-stone/SteppingStone.h +++ b/src/analyzer/protocol/stepping-stone/SteppingStone.h @@ -13,9 +13,6 @@ namespace analyzer { namespace stepping_stone { class SteppingStoneEndpoint; class SteppingStoneManager; -declare(PQueue,SteppingStoneEndpoint); -declare(PDict,SteppingStoneEndpoint); - class SteppingStoneEndpoint : public BroObj { public: SteppingStoneEndpoint(tcp::TCP_Endpoint* e, SteppingStoneManager* m); @@ -41,8 +38,8 @@ protected: // no LOOP in Bro language. int stp_id; HashKey* stp_key; - PDict(SteppingStoneEndpoint) stp_inbound_endps; - PDict(SteppingStoneEndpoint) stp_outbound_endps; + PDict stp_inbound_endps; + PDict stp_outbound_endps; }; class SteppingStone_Analyzer : public tcp::TCP_ApplicationAnalyzer { @@ -76,14 +73,14 @@ class SteppingStoneManager { public: SteppingStoneManager() { endp_cnt = 0; } - PQueue(SteppingStoneEndpoint)& OrderedEndpoints() + PQueue& OrderedEndpoints() { return ordered_endps; } // Use postfix ++, since the first ID needs to be even. int NextID() { return endp_cnt++; } protected: - PQueue(SteppingStoneEndpoint) ordered_endps; + PQueue ordered_endps; int endp_cnt; }; diff --git a/src/broker/Manager.cc b/src/broker/Manager.cc index 0a6ae8fa65..4f7d7c3aae 100644 --- a/src/broker/Manager.cc +++ b/src/broker/Manager.cc @@ -1054,8 +1054,8 @@ void Manager::ProcessEvent(const broker::topic& topic, broker::zeek::Event ev) mgr.QueueEventFast(handler, std::move(vl), SOURCE_BROKER); else { - loop_over_list(vl, i) - Unref(vl[i]); + for ( const auto& v : vl ) + Unref(v); } } diff --git a/src/broker/messaging.bif b/src/broker/messaging.bif index 6c873c863a..06eae5068b 100644 --- a/src/broker/messaging.bif +++ b/src/broker/messaging.bif @@ -28,7 +28,7 @@ std::set val_to_topic_set(Val* val) rval.emplace(val->AsString()->CheckString()); else { - const PDict(TableEntryVal)* tbl = val->AsTable(); + const PDict* tbl = val->AsTable(); if ( tbl->Length() == 0 ) return rval; diff --git a/src/file_analysis/AnalyzerSet.h b/src/file_analysis/AnalyzerSet.h index 3cbe5b1898..032257f29e 100644 --- a/src/file_analysis/AnalyzerSet.h +++ b/src/file_analysis/AnalyzerSet.h @@ -14,7 +14,6 @@ namespace file_analysis { class File; -declare(PDict,Analyzer); /** * A set of file analysis analyzers indexed by an \c AnalyzerArgs (script-layer @@ -139,7 +138,7 @@ private: File* file; /**< File which owns the set */ CompositeHash* analyzer_hash; /**< AnalyzerArgs hashes. */ - PDict(file_analysis::Analyzer) analyzer_map; /**< Indexed by AnalyzerArgs. */ + PDict analyzer_map; /**< Indexed by AnalyzerArgs. */ /** * Abstract base class for analyzer set modifications. diff --git a/src/file_analysis/Manager.h b/src/file_analysis/Manager.h index fe3328b679..67f84134ec 100644 --- a/src/file_analysis/Manager.h +++ b/src/file_analysis/Manager.h @@ -26,9 +26,6 @@ namespace file_analysis { -declare(PDict,bool); -declare(PDict,File); - /** * Main entry point for interacting with file analysis. */ @@ -339,8 +336,8 @@ public: protected: friend class FileTimer; - typedef PDict(bool) IDSet; - typedef PDict(File) IDMap; + typedef PDict IDSet; + typedef PDict IDMap; /** * Create a new file to be analyzed or retrieve an existing one. @@ -410,8 +407,8 @@ private: TagSet* LookupMIMEType(const string& mtype, bool add_if_not_found); - PDict(File) id_map; /**< Map file ID to file_analysis::File records. */ - PDict(bool) ignored; /**< Ignored files. Will be finally removed on EOF. */ + PDict id_map; /**< Map file ID to file_analysis::File records. */ + PDict ignored; /**< Ignored files. Will be finally removed on EOF. */ string current_file_id; /**< Hash of what get_file_handle event sets. */ RuleFileMagicState* magic_state; /**< File magic signature match state. */ MIMEMap mime_types;/**< Mapping of MIME types to analyzers. */ diff --git a/src/input/Manager.cc b/src/input/Manager.cc index 34e8960193..59d8445aa9 100644 --- a/src/input/Manager.cc +++ b/src/input/Manager.cc @@ -52,8 +52,6 @@ static void input_hash_delete_func(void* val) delete h; } -declare(PDict, InputHash); - /** * Base stuff that every stream can do. */ @@ -109,8 +107,8 @@ public: RecordType* rtype; RecordType* itype; - PDict(InputHash)* currDict; - PDict(InputHash)* lastDict; + PDict* currDict; + PDict* lastDict; Func* pred; @@ -703,9 +701,9 @@ bool Manager::CreateTableStream(RecordVal* fval) stream->itype = idx->AsRecordType(); stream->event = event ? event_registry->Lookup(event->Name()) : 0; stream->error_event = error_event ? event_registry->Lookup(error_event->Name()) : nullptr; - stream->currDict = new PDict(InputHash); + stream->currDict = new PDict; stream->currDict->SetDeleteFunc(input_hash_delete_func); - stream->lastDict = new PDict(InputHash); + stream->lastDict = new PDict; stream->lastDict->SetDeleteFunc(input_hash_delete_func); stream->want_record = ( want_record->InternalInt() == 1 ); @@ -1425,7 +1423,7 @@ void Manager::EndCurrentSend(ReaderFrontend* reader) delete(stream->lastDict); stream->lastDict = stream->currDict; - stream->currDict = new PDict(InputHash); + stream->currDict = new PDict; stream->currDict->SetDeleteFunc(input_hash_delete_func); #ifdef DEBUG @@ -1895,8 +1893,8 @@ bool Manager::SendEvent(ReaderFrontend* reader, const string& name, const int nu if ( convert_error ) { - loop_over_list(vl, i) - Unref(vl[i]); + for ( const auto& v : vl ) + Unref(v); return false; } diff --git a/src/iosource/PktSrc.h b/src/iosource/PktSrc.h index 7efecfb0bf..927ff3be90 100644 --- a/src/iosource/PktSrc.h +++ b/src/iosource/PktSrc.h @@ -10,8 +10,6 @@ #include "Dict.h" #include "Packet.h" -declare(PDict,BPF_Program); - namespace iosource { /** diff --git a/src/parse.y b/src/parse.y index 820e958a98..4af53cbfdd 100644 --- a/src/parse.y +++ b/src/parse.y @@ -164,11 +164,8 @@ static type_decl_list* copy_type_decl_list(type_decl_list* tdl) type_decl_list* rval = new type_decl_list(); - loop_over_list(*tdl, i) - { - TypeDecl* td = (*tdl)[i]; + for ( const auto& td : *tdl ) rval->append(new TypeDecl(*td)); - } return rval; } @@ -180,9 +177,8 @@ static attr_list* copy_attr_list(attr_list* al) attr_list* rval = new attr_list(); - loop_over_list(*al, i) + for ( const auto& a : *al ) { - Attr* a = (*al)[i]; ::Ref(a); rval->append(a); } diff --git a/src/probabilistic/Topk.cc b/src/probabilistic/Topk.cc index 29f47ec753..541167470c 100644 --- a/src/probabilistic/Topk.cc +++ b/src/probabilistic/Topk.cc @@ -40,7 +40,7 @@ HashKey* TopkVal::GetHash(Val* v) const TopkVal::TopkVal(uint64 arg_size) : OpaqueVal(topk_type) { - elementDict = new PDict(Element); + elementDict = new PDict; elementDict->SetDeleteFunc(topk_element_hash_delete_func); size = arg_size; type = 0; @@ -51,7 +51,7 @@ TopkVal::TopkVal(uint64 arg_size) : OpaqueVal(topk_type) TopkVal::TopkVal() : OpaqueVal(topk_type) { - elementDict = new PDict(Element); + elementDict = new PDict; elementDict->SetDeleteFunc(topk_element_hash_delete_func); size = 0; type = 0; diff --git a/src/probabilistic/Topk.h b/src/probabilistic/Topk.h index 24d05e12af..cf79b6292c 100644 --- a/src/probabilistic/Topk.h +++ b/src/probabilistic/Topk.h @@ -33,8 +33,6 @@ struct Element { ~Element(); }; -declare(PDict, Element); - class TopkVal : public OpaqueVal { public: @@ -168,7 +166,7 @@ private: BroType* type; CompositeHash* hash; std::list buckets; - PDict(Element)* elementDict; + PDict* elementDict; uint64 size; // how many elements are we tracking? uint64 numElements; // how many elements do we have at the moment bool pruned; // was this data structure pruned? diff --git a/src/scan.l b/src/scan.l index 9216f8d67a..926826fdd4 100644 --- a/src/scan.l +++ b/src/scan.l @@ -126,8 +126,7 @@ public: // A stack of input buffers we're scanning. file_stack[len-1] is the // top of the stack. -declare(PList,FileInfo); -static PList(FileInfo) file_stack; +static PList file_stack; #define RET_CONST(v) \ { \ @@ -757,7 +756,7 @@ void do_atif(Expr* expr) if ( ! val->AsBool() ) { - if_stack.append(current_depth); + if_stack.push_back(current_depth); BEGIN(IGNORE); } } @@ -770,7 +769,7 @@ void do_atifdef(const char* id) if ( ! (i = lookup_ID(id, current_module.c_str())) ) { - if_stack.append(current_depth); + if_stack.push_back(current_depth); BEGIN(IGNORE); } @@ -785,7 +784,7 @@ void do_atifndef(const char *id) if ( (i = lookup_ID(id, current_module.c_str())) ) { - if_stack.append(current_depth); + if_stack.push_back(current_depth); BEGIN(IGNORE); } @@ -797,17 +796,17 @@ void do_atelse() if ( current_depth == 0 ) reporter->Error("@else without @if..."); - if ( if_stack.length() && current_depth > if_stack.last() ) + if ( ! if_stack.empty() && current_depth > if_stack.back() ) return; if ( YY_START == INITIAL ) { - if_stack.append(current_depth); + if_stack.push_back(current_depth); BEGIN(IGNORE); } else { - if_stack.get(); + if_stack.pop_back(); BEGIN(INITIAL); } } @@ -817,10 +816,10 @@ void do_atendif() if ( current_depth == 0 ) reporter->Error("unbalanced @if... @endif"); - if ( current_depth == if_stack.last() ) + if ( current_depth == if_stack.back() ) { BEGIN(INITIAL); - if_stack.get(); + if_stack.pop_back(); } --current_depth; @@ -901,7 +900,8 @@ int yywrap() yy_delete_buffer(YY_CURRENT_BUFFER); - delete file_stack.remove_nth(file_stack.length() - 1); + if ( file_stack.length() > 0 ) + delete file_stack.remove_nth(file_stack.length() - 1); if ( YY_CURRENT_BUFFER ) // There's more on the stack to scan. diff --git a/src/strings.bif b/src/strings.bif index 42630e4b6b..89173ead77 100644 --- a/src/strings.bif +++ b/src/strings.bif @@ -61,15 +61,15 @@ function levenshtein_distance%(s1: string, s2: string%): count function string_cat%(...%): string %{ int n = 0; - loop_over_list(@ARG@, i) - n += @ARG@[i]->AsString()->Len(); + for ( const auto& a : @ARG@ ) + n += a->AsString()->Len(); u_char* b = new u_char[n+1]; BroString* s = new BroString(1, b, n); - loop_over_list(@ARG@, j) + for ( const auto& a : @ARG@ ) { - const BroString* s = @ARG@[j]->AsString(); + const BroString* s = a->AsString(); memcpy(b, s->Bytes(), s->Len()); b += s->Len(); } @@ -350,7 +350,6 @@ Val* do_split(StringVal* str_val, RE_Matcher* re, int incl_sep, int max_num_sep) return a; } - %%} ## Splits a string into an array of strings according to a pattern. diff --git a/src/util.cc b/src/util.cc index e7d989660b..f111c3f2b3 100644 --- a/src/util.cc +++ b/src/util.cc @@ -1001,11 +1001,12 @@ string bro_prefixes() { string rval; - loop_over_list(prefixes, j) - if ( j == 0 ) - rval.append(prefixes[j]); - else - rval.append(":").append(prefixes[j]); + for ( const auto& prefix : prefixes ) + { + if ( ! rval.empty() ) + rval.append(":"); + rval.append(prefix); + } return rval; } diff --git a/src/zeek.bif b/src/zeek.bif index 8b7a047189..9e934b0d4b 100644 --- a/src/zeek.bif +++ b/src/zeek.bif @@ -1566,8 +1566,8 @@ function cat%(...%): string ODesc d; d.SetStyle(RAW_STYLE); - loop_over_list(@ARG@, i) - @ARG@[i]->Describe(&d); + for ( const auto& a : @ARG@ ) + a->Describe(&d); BroString* s = new BroString(1, d.TakeBytes(), d.Len()); s->SetUseFreeToDelete(true); @@ -1898,7 +1898,7 @@ function reading_traces%(%): bool function global_sizes%(%): var_sizes %{ TableVal* sizes = new TableVal(var_sizes); - PDict(ID)* globals = global_scope()->Vars(); + PDict* globals = global_scope()->Vars(); IterCookie* c = globals->InitForIteration(); ID* id; @@ -1925,7 +1925,7 @@ function global_sizes%(%): var_sizes function global_ids%(%): id_table %{ TableVal* ids = new TableVal(id_table); - PDict(ID)* globals = global_scope()->Vars(); + PDict* globals = global_scope()->Vars(); IterCookie* c = globals->InitForIteration(); ID* id;