Merge branch 'topic/jsiwek/template-containers-merge'

* topic/jsiwek/template-containers-merge:
  Fix a potential usage of List::remove_nth(-1)
  Change List::remote(const T&) to return a bool
  Fix debug build due to old int_list usage within assert
  Convert uses of loop_over_list to ranged-for loops
  Remove loop_over_queue (as an example for later removing loop_over_list)
  Change int_list in CCL.h to be a vector, fix uses of int_list to match
  Remove List<> usage from strings.bif
  Replace uses of the old Queue/PQueue generation code with new template versions
  Convert BaseQueue/Queue/PQueue into templates, including iterator support
  Replace uses of the old Dict generation code with new template versions
  Convert PDict into template
  Replace uses of the old List generation code with new template versions
  Convert BaseList/List/PList into templates, including iterator support

* Generally squashed fixups from topic/timw/template-containers

* Add missing include file in List.h: <cassert>
This commit is contained in:
Jon Siwek 2019-07-15 19:46:04 -07:00
commit 8c45937798
68 changed files with 1111 additions and 1249 deletions

31
CHANGES
View file

@ -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 2.6-598 | 2019-07-12 18:20:12 -0700
* Fix canonification of timestamps with a decisecond multiple (Jon Siwek, Corelight) * Fix canonification of timestamps with a decisecond multiple (Jon Siwek, Corelight)

21
NEWS
View file

@ -357,6 +357,27 @@ Changed Functionality
are small changes to the output; most visibly double numbers are now rounded slightly 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. 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<T>"
- PDict is now a template instead of a macro, so any "PDict(T)" usages in
external code should now use "PDict<T>"
- 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 Removed Functionality
--------------------- ---------------------

View file

@ -1 +1 @@
2.6-598 2.6-612

View file

@ -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 // rather than just taking over 'a' for ourselves, so that
// the necessary checking gets done. // the necessary checking gets done.
loop_over_list(*a, i) for ( const auto& attr : *a )
AddAttr((*a)[i]); AddAttr(attr);
delete a; delete a;
} }
Attributes::~Attributes() Attributes::~Attributes()
{ {
loop_over_list(*attrs, i) for ( const auto& attr : *attrs )
Unref((*attrs)[i]); Unref(attr);
delete attrs; delete attrs;
@ -189,8 +189,8 @@ void Attributes::AddAttr(Attr* attr)
void Attributes::AddAttrs(Attributes* a) void Attributes::AddAttrs(Attributes* a)
{ {
attr_list* as = a->Attrs(); attr_list* as = a->Attrs();
loop_over_list(*as, i) for ( const auto& attr : *as )
AddAttr((*as)[i]); AddAttr(attr);
Unref(a); Unref(a);
} }
@ -200,9 +200,8 @@ Attr* Attributes::FindAttr(attr_tag t) const
if ( ! attrs ) if ( ! attrs )
return 0; return 0;
loop_over_list(*attrs, i) for ( const auto& a : *attrs )
{ {
Attr* a = (*attrs)[i];
if ( a->Tag() == t ) if ( a->Tag() == t )
return a; return a;
} }
@ -404,9 +403,8 @@ void Attributes::CheckAttr(Attr* a)
int num_expires = 0; int num_expires = 0;
if ( attrs ) if ( attrs )
{ {
loop_over_list(*attrs, i) for ( const auto& a : *attrs )
{ {
Attr* a = (*attrs)[i];
if ( a->Tag() == ATTR_EXPIRE_READ || if ( a->Tag() == ATTR_EXPIRE_READ ||
a->Tag() == ATTR_EXPIRE_WRITE || a->Tag() == ATTR_EXPIRE_WRITE ||
a->Tag() == ATTR_EXPIRE_CREATE ) a->Tag() == ATTR_EXPIRE_CREATE )
@ -519,9 +517,8 @@ bool Attributes::operator==(const Attributes& other) const
if ( ! other.attrs ) if ( ! other.attrs )
return false; return false;
loop_over_list(*attrs, i) for ( const auto& a : *attrs )
{ {
Attr* a = (*attrs)[i];
Attr* o = other.FindAttr(a->Tag()); Attr* o = other.FindAttr(a->Tag());
if ( ! o ) if ( ! o )
@ -531,9 +528,8 @@ bool Attributes::operator==(const Attributes& other) const
return false; return false;
} }
loop_over_list(*other.attrs, j) for ( const auto& o : *other.attrs )
{ {
Attr* o = (*other.attrs)[j];
Attr* a = FindAttr(o->Tag()); Attr* a = FindAttr(o->Tag());
if ( ! a ) if ( ! a )

View file

@ -6,31 +6,24 @@
#include "List.h" #include "List.h"
class Expr; class Expr;
declare(PList,Expr); typedef PList<Expr> expr_list;
typedef PList(Expr) expr_list;
class ID; class ID;
declare(PList,ID); typedef PList<ID> id_list;
typedef PList(ID) id_list;
class Val; class Val;
declare(PList,Val); typedef PList<Val> val_list;
typedef PList(Val) val_list;
class Stmt; class Stmt;
declare(PList,Stmt); typedef PList<Stmt> stmt_list;
typedef PList(Stmt) stmt_list;
class BroType; class BroType;
declare(PList,BroType); typedef PList<BroType> type_list;
typedef PList(BroType) type_list;
class Attr; class Attr;
declare(PList,Attr); typedef PList<Attr> attr_list;
typedef PList(Attr) attr_list;
class Timer; class Timer;
declare(PList,Timer); typedef PList<Timer> timer_list;
typedef PList(Timer) timer_list;
#endif #endif

View file

@ -2,6 +2,8 @@
#include "zeek-config.h" #include "zeek-config.h"
#include <algorithm>
#include "CCL.h" #include "CCL.h"
#include "RE.h" #include "RE.h"
#include "DFA.h" #include "DFA.h"
@ -30,14 +32,14 @@ void CCL::Add(int sym)
ptr_compat_int sym_p = ptr_compat_int(sym); ptr_compat_int sym_p = ptr_compat_int(sym);
// Check to see if the character is already in the ccl. // Check to see if the character is already in the ccl.
for ( int i = 0; i < syms->length(); ++i ) for ( auto sym : *syms )
if ( (*syms)[i] == sym_p ) if ( sym == sym_p )
return; return;
syms->append(sym_p); syms->push_back(sym_p);
} }
void CCL::Sort() void CCL::Sort()
{ {
syms->sort(int_list_cmp); std::sort(syms->begin(), syms->end());
} }

View file

@ -3,10 +3,10 @@
#ifndef ccl_h #ifndef ccl_h
#define ccl_h #define ccl_h
#include <vector>
#include "List.h" #include "List.h"
declare(List,ptr_compat_int); typedef std::vector<ptr_compat_int> int_list;
typedef List(ptr_compat_int) int_list;
class CCL { class CCL {
public: public:
@ -26,7 +26,7 @@ public:
{ delete syms; syms = new_syms; } { delete syms; syms = new_syms; }
unsigned int MemoryAllocation() const 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: protected:
int_list* syms; int_list* syms;

View file

@ -261,7 +261,6 @@ set(MAIN_SRCS
IntSet.cc IntSet.cc
IP.cc IP.cc
IPAddr.cc IPAddr.cc
List.cc
Reporter.cc Reporter.cc
NFA.cc NFA.cc
Net.cc Net.cc
@ -273,7 +272,6 @@ set(MAIN_SRCS
PolicyFile.cc PolicyFile.cc
PrefixTable.cc PrefixTable.cc
PriorityQueue.cc PriorityQueue.cc
Queue.cc
RandTest.cc RandTest.cc
RE.cc RE.cc
Reassem.cc Reassem.cc

View file

@ -675,10 +675,10 @@ ListVal* CompositeHash::RecoverVals(const HashKey* k) const
const char* kp = (const char*) k->Key(); const char* kp = (const char*) k->Key();
const char* const k_end = kp + k->Size(); const char* const k_end = kp + k->Size();
loop_over_list(*tl, i) for ( const auto& type : *tl )
{ {
Val* v = nullptr; Val* v = nullptr;
kp = RecoverOneVal(k, kp, k_end, (*tl)[i], v, false); kp = RecoverOneVal(k, kp, k_end, type, v, false);
ASSERT(v); ASSERT(v);
l->Append(v); l->Append(v);
} }

View file

@ -481,8 +481,8 @@ void Connection::ConnectionEvent(EventHandlerPtr f, analyzer::Analyzer* a, val_l
{ {
// This may actually happen if there is no local handler // This may actually happen if there is no local handler
// and a previously existing remote handler went away. // and a previously existing remote handler went away.
loop_over_list(vl, i) for ( const auto& v : vl)
Unref(vl[i]); Unref(v);
return; return;
} }
@ -540,11 +540,10 @@ void Connection::CancelTimers()
// traversing. Thus, we first make a copy of the list which we then // traversing. Thus, we first make a copy of the list which we then
// iterate through. // iterate through.
timer_list tmp(timers.length()); timer_list tmp(timers.length());
loop_over_list(timers, j) std::copy(timers.begin(), timers.end(), std::back_inserter(tmp));
tmp.append(timers[j]);
loop_over_list(tmp, i) for ( const auto& timer : tmp )
GetTimerMgr()->Cancel(tmp[i]); GetTimerMgr()->Cancel(timer);
timers_canceled = 1; timers_canceled = 1;
timers.clear(); timers.clear();

View file

@ -50,8 +50,7 @@ private:
int current_level; int current_level;
int suspend_level; int suspend_level;
declare(PList, void); typedef PList<void> voidp_list;
typedef PList(void) voidp_list;
voidp_list states; voidp_list states;
}; };

View file

@ -108,11 +108,11 @@ DFA_State* DFA_State::ComputeXtion(int sym, DFA_Machine* machine)
void DFA_State::AppendIfNew(int sym, int_list* sym_list) void DFA_State::AppendIfNew(int sym, int_list* sym_list)
{ {
for ( int i = 0; i < sym_list->length(); ++i ) for ( auto value : *sym_list )
if ( (*sym_list)[i] == sym ) if ( value == sym )
return; return;
sym_list->append(sym); sym_list->push_back(sym);
} }
NFA_state_list* DFA_State::SymFollowSet(int ec_sym, const EquivClass* ec) 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() ) if ( ccl->IsNegated() )
{ {
int j; size_t j;
for ( j = 0; j < syms->length(); ++j ) for ( j = 0; j < syms->size(); ++j )
{ {
// Loop through (sorted) negated // Loop through (sorted) negated
// character class, which has // character class, which has
@ -143,19 +143,19 @@ NFA_state_list* DFA_State::SymFollowSet(int ec_sym, const EquivClass* ec)
break; break;
} }
if ( j >= syms->length() || (*syms)[j] > ec_sym ) if ( j >= syms->size() || (*syms)[j] > ec_sym )
// Didn't find ec_sym in ccl. // Didn't find ec_sym in ccl.
n->AddXtionsTo(ns); n->AddXtionsTo(ns);
continue; continue;
} }
for ( int j = 0; j < syms->length(); ++j ) for ( auto sym : *syms )
{ {
if ( (*syms)[j] > ec_sym ) if ( sym > ec_sym )
break; break;
if ( (*syms)[j] == ec_sym ) if ( sym == ec_sym )
{ {
n->AddXtionsTo(ns); n->AddXtionsTo(ns);
break; break;

View file

@ -105,10 +105,8 @@ private:
int hits; // Statistics int hits; // Statistics
int misses; int misses;
declare(PDict,CacheEntry);
// Hash indexed by NFA states (MD5s of them, actually). // Hash indexed by NFA states (MD5s of them, actually).
PDict(CacheEntry) states; PDict<CacheEntry> states;
}; };
class DFA_Machine : public BroObj { class DFA_Machine : public BroObj {

View file

@ -23,14 +23,11 @@ class EventHandler;
class RecordType; class RecordType;
class DNS_Mgr_Request; class DNS_Mgr_Request;
declare(PList,DNS_Mgr_Request); typedef PList<DNS_Mgr_Request> DNS_mgr_request_list;
typedef PList(DNS_Mgr_Request) DNS_mgr_request_list;
struct nb_dns_info; struct nb_dns_info;
struct nb_dns_result; struct nb_dns_result;
declare(PDict,ListVal);
class DNS_Mapping; class DNS_Mapping;
enum DNS_MgrMode { enum DNS_MgrMode {
@ -144,7 +141,7 @@ protected:
DNS_MgrMode mode; DNS_MgrMode mode;
PDict(ListVal) services; PDict<ListVal> services;
HostMap host_mappings; HostMap host_mappings;
AddrMap addr_mappings; AddrMap addr_mappings;

View file

@ -27,7 +27,7 @@ using namespace std;
bool g_policy_debug = false; bool g_policy_debug = false;
DebuggerState g_debugger_state; DebuggerState g_debugger_state;
TraceState g_trace_state; TraceState g_trace_state;
PDict(Filemap) g_dbgfilemaps; PDict<Filemap> g_dbgfilemaps;
// These variables are used only to decide whether or not to print the // 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 // current context; you don't want to do it after a step or next
@ -378,10 +378,9 @@ vector<ParseLocationRec> parse_location_string(const string& s)
} }
StmtLocMapping* hit = 0; StmtLocMapping* hit = 0;
loop_over_queue(*map, i) for ( const auto entry : *map )
{ {
StmtLocMapping* entry = (*map)[i]; plr.filename = entry->Loc().filename;
plr.filename = (*map)[i]->Loc().filename;
if ( entry->Loc().first_line > plr.line ) if ( entry->Loc().first_line > plr.line )
break; break;
@ -389,7 +388,7 @@ vector<ParseLocationRec> parse_location_string(const string& s)
if ( plr.line >= entry->Loc().first_line && if ( plr.line >= entry->Loc().first_line &&
plr.line <= entry->Loc().last_line ) plr.line <= entry->Loc().last_line )
{ {
hit = (*map)[i]; hit = entry;
break; break;
} }
} }

View file

@ -29,9 +29,7 @@ struct ParseLocationRec {
#include "DbgBreakpoint.h" #include "DbgBreakpoint.h"
class StmtLocMapping; class StmtLocMapping;
declare(PQueue,StmtLocMapping); typedef PQueue<StmtLocMapping> Filemap; // mapping for a single file
typedef PQueue(StmtLocMapping) Filemap; // mapping for a single file
declare(PDict,Filemap);
class DbgBreakpoint; class DbgBreakpoint;
class DbgWatch; 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 Frame* g_dbg_locals; // variables created within debugger context
extern PDict(Filemap) g_dbgfilemaps; // filename => filemap extern PDict<Filemap> g_dbgfilemaps; // filename => filemap
// Perhaps add a code/priority argument to do selective output. // Perhaps add a code/priority argument to do selective output.
int debug_msg(const char* fmt, ...) __attribute__ ((format (printf, 1, 2))); int debug_msg(const char* fmt, ...) __attribute__ ((format (printf, 1, 2)));

View file

@ -51,7 +51,7 @@ void lookup_global_symbols_regex(const string& orig_regex, vector<ID*>& matches,
} }
Scope* global = global_scope(); Scope* global = global_scope();
PDict(ID)* syms = global->Vars(); PDict<ID>* syms = global->Vars();
ID* nextid; ID* nextid;
IterCookie* cookie = syms->InitForIteration(); IterCookie* cookie = syms->InitForIteration();
@ -114,7 +114,7 @@ void choose_global_symbols_regex(const string& regex, vector<ID*>& choices,
// DebugCmdInfo implementation // DebugCmdInfo implementation
// //
PQueue(DebugCmdInfo) g_DebugCmdInfos; PQueue<DebugCmdInfo> g_DebugCmdInfos;
DebugCmdInfo::DebugCmdInfo(const DebugCmdInfo& info) DebugCmdInfo::DebugCmdInfo(const DebugCmdInfo& info)
: cmd(info.cmd), helpstring(0) : cmd(info.cmd), helpstring(0)

View file

@ -12,9 +12,6 @@ using namespace std;
#include "Queue.h" #include "Queue.h"
#include "DebugCmdConstants.h" #include "DebugCmdConstants.h"
class DebugCmdInfo;
declare(PQueue,DebugCmdInfo);
class DebugCmdInfo { class DebugCmdInfo {
public: public:
DebugCmdInfo(const DebugCmdInfo& info); DebugCmdInfo(const DebugCmdInfo& info);
@ -47,7 +44,7 @@ protected:
bool repeatable; bool repeatable;
}; };
extern PQueue(DebugCmdInfo) g_DebugCmdInfos; extern PQueue<DebugCmdInfo> g_DebugCmdInfos;
void init_global_dbg_constants (); void init_global_dbg_constants ();

View file

@ -50,9 +50,9 @@ public:
} }
int bucket, offset; int bucket, offset;
PList(DictEntry)** ttbl; PList<DictEntry>** ttbl;
const int* num_buckets_p; const int* num_buckets_p;
PList(DictEntry) inserted; // inserted while iterating PList<DictEntry> inserted; // inserted while iterating
}; };
Dictionary::Dictionary(dict_order ordering, int initial_size) Dictionary::Dictionary(dict_order ordering, int initial_size)
@ -61,7 +61,7 @@ Dictionary::Dictionary(dict_order ordering, int initial_size)
tbl2 = 0; tbl2 = 0;
if ( ordering == ORDERED ) if ( ordering == ORDERED )
order = new PList(DictEntry); order = new PList<DictEntry>;
else else
order = 0; order = 0;
@ -99,10 +99,9 @@ void Dictionary::DeInit()
for ( int i = 0; i < num_buckets; ++i ) for ( int i = 0; i < num_buckets; ++i )
if ( tbl[i] ) if ( tbl[i] )
{ {
PList(DictEntry)* chain = tbl[i]; PList<DictEntry>* chain = tbl[i];
loop_over_list(*chain, j) for ( const auto& e : *chain )
{ {
DictEntry* e = (*chain)[j];
if ( delete_func ) if ( delete_func )
delete_func(e->value); delete_func(e->value);
delete e; delete e;
@ -119,10 +118,9 @@ void Dictionary::DeInit()
for ( int i = 0; i < num_buckets2; ++i ) for ( int i = 0; i < num_buckets2; ++i )
if ( tbl2[i] ) if ( tbl2[i] )
{ {
PList(DictEntry)* chain = tbl2[i]; PList<DictEntry>* chain = tbl2[i];
loop_over_list(*chain, j) for ( const auto& e : *chain )
{ {
DictEntry* e = (*chain)[j];
if ( delete_func ) if ( delete_func )
delete_func(e->value); delete_func(e->value);
delete e; delete e;
@ -141,7 +139,7 @@ void* Dictionary::Lookup(const void* key, int key_size, hash_t hash) const
return 0; return 0;
hash_t h; hash_t h;
PList(DictEntry)* chain; PList<DictEntry>* chain;
// Figure out which hash table to look in. // Figure out which hash table to look in.
h = hash % num_buckets; h = hash % num_buckets;
@ -199,7 +197,7 @@ void* Dictionary::Remove(const void* key, int key_size, hash_t hash,
return 0; return 0;
hash_t h; hash_t h;
PList(DictEntry)* chain; PList<DictEntry>* chain;
int* num_entries_ptr; int* num_entries_ptr;
// Figure out which hash table to look in // 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, void* Dictionary::DoRemove(DictEntry* entry, hash_t h,
PList(DictEntry)* chain, int chain_offset) PList<DictEntry>* chain, int chain_offset)
{ {
void* entry_value = entry->value; void* entry_value = entry->value;
@ -249,10 +247,8 @@ void* Dictionary::DoRemove(DictEntry* entry, hash_t h,
order->remove(entry); order->remove(entry);
// Adjust existing cookies. // Adjust existing cookies.
loop_over_list(cookies, i) for ( const auto& c : cookies )
{ {
IterCookie* c = cookies[i];
// Is the affected bucket the current one? // Is the affected bucket the current one?
if ( (unsigned int) c->bucket == h ) if ( (unsigned int) c->bucket == h )
{ {
@ -301,7 +297,7 @@ void* Dictionary::NextEntry(HashKey*& h, IterCookie*& cookie, int return_hash) c
{ {
if ( ! tbl && ! tbl2 ) if ( ! tbl && ! tbl2 )
{ {
const_cast<PList(IterCookie)*>(&cookies)->remove(cookie); const_cast<PList<IterCookie>*>(&cookies)->remove(cookie);
delete cookie; delete cookie;
cookie = 0; cookie = 0;
return 0; return 0;
@ -326,7 +322,7 @@ void* Dictionary::NextEntry(HashKey*& h, IterCookie*& cookie, int return_hash) c
int b = cookie->bucket; int b = cookie->bucket;
int o = cookie->offset; int o = cookie->offset;
PList(DictEntry)** ttbl; PList<DictEntry>** ttbl;
const int* num_buckets_p; const int* num_buckets_p;
if ( ! cookie->ttbl ) 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 // FIXME: I don't like removing the const here. But is there
// a better way? // a better way?
const_cast<PList(IterCookie)*>(&cookies)->remove(cookie); const_cast<PList<IterCookie>*>(&cookies)->remove(cookie);
delete cookie; delete cookie;
cookie = 0; cookie = 0;
return 0; return 0;
@ -387,7 +383,7 @@ void* Dictionary::NextEntry(HashKey*& h, IterCookie*& cookie, int return_hash) c
void Dictionary::Init(int size) void Dictionary::Init(int size)
{ {
num_buckets = NextPrime(size); num_buckets = NextPrime(size);
tbl = new PList(DictEntry)*[num_buckets]; tbl = new PList<DictEntry>*[num_buckets];
for ( int i = 0; i < num_buckets; ++i ) for ( int i = 0; i < num_buckets; ++i )
tbl[i] = 0; tbl[i] = 0;
@ -399,7 +395,7 @@ void Dictionary::Init(int size)
void Dictionary::Init2(int size) void Dictionary::Init2(int size)
{ {
num_buckets2 = NextPrime(size); num_buckets2 = NextPrime(size);
tbl2 = new PList(DictEntry)*[num_buckets2]; tbl2 = new PList<DictEntry>*[num_buckets2];
for ( int i = 0; i < num_buckets2; ++i ) for ( int i = 0; i < num_buckets2; ++i )
tbl2[i] = 0; tbl2[i] = 0;
@ -413,7 +409,7 @@ void* Dictionary::Insert(DictEntry* new_entry, int copy_key)
if ( ! tbl ) if ( ! tbl )
Init(DEFAULT_DICT_SIZE); Init(DEFAULT_DICT_SIZE);
PList(DictEntry)** ttbl; PList<DictEntry>** ttbl;
int* num_entries_ptr; int* num_entries_ptr;
int* max_num_entries_ptr; int* max_num_entries_ptr;
hash_t h = new_entry->hash % num_buckets; 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; max_num_entries_ptr = &max_num_entries2;
} }
PList(DictEntry)* chain = ttbl[h]; PList<DictEntry>* chain = ttbl[h];
int n = new_entry->len; int n = new_entry->len;
@ -460,7 +456,7 @@ void* Dictionary::Insert(DictEntry* new_entry, int copy_key)
} }
else else
// Create new chain. // Create new chain.
chain = ttbl[h] = new PList(DictEntry); chain = ttbl[h] = new PList<DictEntry>;
// If we got this far, then we couldn't use an existing copy // If we got this far, then we couldn't use an existing copy
// of the key, so make a new one if necessary. // 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 // For ongoing iterations: If we already passed the bucket where this
// entry was put, add it to the cookie's list of inserted entries. // 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 ) if ( h < (unsigned int) c->bucket )
c->inserted.append(new_entry); c->inserted.append(new_entry);
} }
@ -545,7 +540,7 @@ void Dictionary::MoveChains()
do do
{ {
PList(DictEntry)* chain = tbl[tbl_next_ind++]; PList<DictEntry>* chain = tbl[tbl_next_ind++];
if ( ! chain ) if ( ! chain )
continue; continue;
@ -605,13 +600,13 @@ unsigned int Dictionary::MemoryAllocation() const
for ( int i = 0; i < num_buckets; ++i ) for ( int i = 0; i < num_buckets; ++i )
if ( tbl[i] ) if ( tbl[i] )
{ {
PList(DictEntry)* chain = tbl[i]; PList<DictEntry>* chain = tbl[i];
loop_over_list(*chain, j) for ( const auto& c : *chain )
size += padded_sizeof(DictEntry) + pad_size((*chain)[j]->len); size += padded_sizeof(DictEntry) + pad_size(c->len);
size += chain->MemoryAllocation(); size += chain->MemoryAllocation();
} }
size += pad_size(num_buckets * sizeof(PList(DictEntry)*)); size += pad_size(num_buckets * sizeof(PList<DictEntry>*));
if ( order ) if ( order )
size += order->MemoryAllocation(); size += order->MemoryAllocation();
@ -621,13 +616,13 @@ unsigned int Dictionary::MemoryAllocation() const
for ( int i = 0; i < num_buckets2; ++i ) for ( int i = 0; i < num_buckets2; ++i )
if ( tbl2[i] ) if ( tbl2[i] )
{ {
PList(DictEntry)* chain = tbl2[i]; PList<DictEntry>* chain = tbl2[i];
loop_over_list(*chain, j) for ( const auto& c : *chain )
size += padded_sizeof(DictEntry) + pad_size((*chain)[j]->len); size += padded_sizeof(DictEntry) + pad_size(c->len);
size += chain->MemoryAllocation(); size += chain->MemoryAllocation();
} }
size += pad_size(num_buckets2 * sizeof(PList(DictEntry)*)); size += pad_size(num_buckets2 * sizeof(PList<DictEntry>*));
} }
return size; return size;

View file

@ -10,9 +10,6 @@ class Dictionary;
class DictEntry; class DictEntry;
class IterCookie; class IterCookie;
declare(PList,DictEntry);
declare(PList,IterCookie);
// Type indicating whether the dictionary should keep track of the order // Type indicating whether the dictionary should keep track of the order
// of insertions. // of insertions.
typedef enum { ORDERED, UNORDERED } dict_order; typedef enum { ORDERED, UNORDERED } dict_order;
@ -132,7 +129,7 @@ private:
void* Insert(DictEntry* entry, int copy_key); void* Insert(DictEntry* entry, int copy_key);
void* DoRemove(DictEntry* entry, hash_t h, void* DoRemove(DictEntry* entry, hash_t h,
PList(DictEntry)* chain, int chain_offset); PList<DictEntry>* chain, int chain_offset);
int NextPrime(int n) const; int NextPrime(int n) const;
int IsPrime(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) // 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 // tbl_next_ind keeps track of how much we've moved to tbl2
// (it's the next index we're going to move). // (it's the next index we're going to move).
PList(DictEntry)** tbl; PList<DictEntry>** tbl;
int num_buckets; int num_buckets;
int num_entries; int num_entries;
int max_num_entries; int max_num_entries;
@ -171,7 +168,7 @@ private:
int thresh_entries; int thresh_entries;
// Resizing table (replicates tbl above). // Resizing table (replicates tbl above).
PList(DictEntry)** tbl2; PList<DictEntry>** tbl2;
int num_buckets2; int num_buckets2;
int num_entries2; int num_entries2;
int max_num_entries2; int max_num_entries2;
@ -180,52 +177,47 @@ private:
hash_t tbl_next_ind; hash_t tbl_next_ind;
PList(DictEntry)* order; PList<DictEntry>* order;
dict_delete_func delete_func; dict_delete_func delete_func;
PList(IterCookie) cookies; PList<IterCookie> cookies;
}; };
template<typename T>
#define PDict(type) type ## PDict class PDict : public Dictionary {
#define PDictdeclare(type) \ public:
class PDict(type) : public Dictionary { \ explicit PDict(dict_order ordering = UNORDERED, int initial_size = 0) :
public: \ Dictionary(ordering, initial_size) {}
explicit PDict(type)(dict_order ordering = UNORDERED, \ T* Lookup(const char* key) const
int initial_size = 0) : \ {
Dictionary(ordering, initial_size) {} \ HashKey h(key);
type* Lookup(const char* key) const \ return (T*) Dictionary::Lookup(&h);
{ \ }
HashKey h(key); \ T* Lookup(const HashKey* key) const
return (type*) Dictionary::Lookup(&h); \ { return (T*) Dictionary::Lookup(key); }
} \ T* Insert(const char* key, T* val)
type* Lookup(const HashKey* key) const \ {
{ return (type*) Dictionary::Lookup(key); } \ HashKey h(key);
type* Insert(const char* key, type* val) \ return (T*) Dictionary::Insert(&h, (void*) val);
{ \ }
HashKey h(key); \ T* Insert(HashKey* key, T* val)
return (type*) Dictionary::Insert(&h, (void*) val); \ { return (T*) Dictionary::Insert(key, (void*) val); }
} \ T* NthEntry(int n) const
type* Insert(HashKey* key, type* val) \ { return (T*) Dictionary::NthEntry(n); }
{ return (type*) Dictionary::Insert(key, (void*) val); }\ T* NthEntry(int n, const char*& key) const
type* NthEntry(int n) const \ {
{ return (type*) Dictionary::NthEntry(n); } \ int key_len;
type* NthEntry(int n, const char*& key) const \ return (T*) Dictionary::NthEntry(n, (const void*&) key, key_len);
{ \ }
int key_len; \ T* NextEntry(IterCookie*& cookie) const
return (type*) Dictionary::NthEntry(n, (const void*&) key,\ {
key_len); \ HashKey* h;
} \ return (T*) Dictionary::NextEntry(h, cookie, 0);
type* NextEntry(IterCookie*& cookie) const \ }
{ \ T* NextEntry(HashKey*& h, IterCookie*& cookie) const
HashKey* h; \ { return (T*) Dictionary::NextEntry(h, cookie, 1); }
return (type*) Dictionary::NextEntry(h, cookie, 0); \ T* RemoveEntry(const HashKey* key)
} \ { return (T*) Remove(key->Key(), key->Size(), key->Hash()); }
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()); } \
}
#endif #endif

View file

@ -52,11 +52,10 @@ void EquivClass::ConvertCCL(CCL* ccl)
int_list* c_syms = ccl->Syms(); int_list* c_syms = ccl->Syms();
int_list* new_syms = new int_list; 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) ) if ( IsRep(sym) )
new_syms->append(SymEquivClass(sym)); new_syms->push_back(SymEquivClass(sym));
} }
ccl->ReplaceSyms(new_syms); ccl->ReplaceSyms(new_syms);
@ -95,18 +94,18 @@ void EquivClass::CCL_Use(CCL* ccl)
} }
int_list* csyms = ccl->Syms(); 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 sym = (*csyms)[i];
int old_ec = bck[sym]; int old_ec = bck[sym];
int new_ec = 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] ) for ( int k = fwd[sym]; k && k < size; k = fwd[k] )
{ // look for the symbol in the character class { // look for the symbol in the character class
for ( ; j < csyms->length(); ++j ) for ( ; j < csyms->size(); ++j )
{ {
if ( (*csyms)[j] > k ) if ( (*csyms)[j] > k )
// Since the character class is sorted, // 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 // We broke out of the above loop by finding
// an old companion - go to the next symbol. // an old companion - go to the next symbol.
continue; continue;
@ -154,7 +153,7 @@ void EquivClass::CCL_Use(CCL* ccl)
fwd[new_ec] = ec_nil; fwd[new_ec] = ec_nil;
// Find next ccl member to process. // 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. // Reset "doesn't need processing" flag.
ccl_flags[i] = 0; ccl_flags[i] = 0;
} }

View file

@ -86,8 +86,8 @@ public:
QueueEvent(new Event(h, std::move(vl), src, aid, mgr, obj)); QueueEvent(new Event(h, std::move(vl), src, aid, mgr, obj));
else else
{ {
loop_over_list(vl, i) for ( const auto& v : vl )
Unref(vl[i]); Unref(v);
} }
} }

View file

@ -117,8 +117,8 @@ void EventHandler::Call(val_list* vl, bool no_remote)
Unref(local->Call(vl)); Unref(local->Call(vl));
else else
{ {
loop_over_list(*vl, i) for ( auto v : *vl )
Unref((*vl)[i]); Unref(v);
} }
} }

View file

@ -22,8 +22,7 @@ public:
// Returns a list of all local handlers that match the given pattern. // Returns a list of all local handlers that match the given pattern.
// Passes ownership of list. // Passes ownership of list.
typedef const char constchar; // PList doesn't like "const char" typedef const char constchar; // PList doesn't like "const char"
declare(PList, constchar); typedef PList<constchar> string_list;
typedef PList(constchar) string_list;
string_list* Match(RE_Matcher* pattern); string_list* Match(RE_Matcher* pattern);
// Marks a handler as handling errors. Error handler will not be called // Marks a handler as handling errors. Error handler will not be called
@ -38,8 +37,7 @@ public:
void PrintDebug(); void PrintDebug();
private: private:
declare(PDict, EventHandler); typedef PDict<EventHandler> handler_map;
typedef PDict(EventHandler) handler_map;
handler_map handlers; handler_map handlers;
}; };

View file

@ -2113,8 +2113,7 @@ bool AssignExpr::TypeCheck(attr_list* attrs)
if ( attrs ) if ( attrs )
{ {
attr_copy = new attr_list(attrs->length()); attr_copy = new attr_list(attrs->length());
loop_over_list(*attrs, i) std::copy(attrs->begin(), attrs->end(), std::back_inserter(*attr_copy));
attr_copy->append((*attrs)[i]);
} }
bool empty_list_assignment = (op2->AsListExpr()->Exprs().length() == 0); 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(); attr_list* a = sce->Attrs()->Attrs();
attrs = new attr_list(a->length()); attrs = new attr_list(a->length());
loop_over_list(*a, i) std::copy(a->begin(), a->end(), std::back_inserter(*attrs));
attrs->append((*a)[i]);
} }
int errors_before = reporter->Errors(); int errors_before = reporter->Errors();
@ -3029,10 +3027,8 @@ RecordConstructorExpr::RecordConstructorExpr(ListExpr* constructor_list)
const expr_list& exprs = constructor_list->Exprs(); const expr_list& exprs = constructor_list->Exprs();
type_decl_list* record_types = new type_decl_list(exprs.length()); 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 ) if ( e->Tag() != EXPR_FIELD_ASSIGN )
{ {
Error("bad type in record constructor", e); Error("bad type in record constructor", e);
@ -3139,12 +3135,12 @@ TableConstructorExpr::TableConstructorExpr(ListExpr* constructor_list,
const expr_list& cle = constructor_list->Exprs(); const expr_list& cle = constructor_list->Exprs();
// check and promote all index expressions in ctor list // 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; continue;
Expr* idx_expr = cle[i]->AsAssignExpr()->Op1(); Expr* idx_expr = expr->AsAssignExpr()->Op1();
if ( idx_expr->Tag() != EXPR_LIST ) if ( idx_expr->Tag() != EXPR_LIST )
continue; continue;
@ -3178,8 +3174,8 @@ Val* TableConstructorExpr::Eval(Frame* f) const
Val* aggr = new TableVal(Type()->AsTableType(), attrs); Val* aggr = new TableVal(Type()->AsTableType(), attrs);
const expr_list& exprs = op->AsListExpr()->Exprs(); const expr_list& exprs = op->AsListExpr()->Exprs();
loop_over_list(exprs, i) for ( const auto& expr : exprs )
exprs[i]->EvalIntoAggregate(type, aggr, f); expr->EvalIntoAggregate(type, aggr, f);
return aggr; return aggr;
} }
@ -3193,8 +3189,8 @@ Val* TableConstructorExpr::InitVal(const BroType* t, Val* aggr) const
TableVal* tval = aggr ? aggr->AsTableVal() : new TableVal(tt, attrs); TableVal* tval = aggr ? aggr->AsTableVal() : new TableVal(tt, attrs);
const expr_list& exprs = op->AsListExpr()->Exprs(); const expr_list& exprs = op->AsListExpr()->Exprs();
loop_over_list(exprs, i) for ( const auto& expr : exprs )
exprs[i]->EvalIntoAggregate(t, tval, 0); expr->EvalIntoAggregate(t, tval, 0);
return tval; return tval;
} }
@ -3282,9 +3278,9 @@ Val* SetConstructorExpr::Eval(Frame* f) const
TableVal* aggr = new TableVal(type->AsTableType(), attrs); TableVal* aggr = new TableVal(type->AsTableType(), attrs);
const expr_list& exprs = op->AsListExpr()->Exprs(); 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); aggr->Assign(element, 0);
Unref(element); Unref(element);
} }
@ -3302,9 +3298,8 @@ Val* SetConstructorExpr::InitVal(const BroType* t, Val* aggr) const
TableVal* tval = aggr ? aggr->AsTableVal() : new TableVal(tt, attrs); TableVal* tval = aggr ? aggr->AsTableVal() : new TableVal(tt, attrs);
const expr_list& exprs = op->AsListExpr()->Exprs(); 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); Val* element = check_and_promote(e->Eval(0), index_type, 1);
if ( ! element || ! tval->Assign(element, 0) ) if ( ! element || ! tval->Assign(element, 0) )
@ -4417,8 +4412,8 @@ ListExpr::ListExpr(Expr* e) : Expr(EXPR_LIST)
ListExpr::~ListExpr() ListExpr::~ListExpr()
{ {
loop_over_list(exprs, i) for ( const auto& expr: exprs )
Unref(exprs[i]); Unref(expr);
} }
void ListExpr::Append(Expr* e) void ListExpr::Append(Expr* e)
@ -4429,8 +4424,8 @@ void ListExpr::Append(Expr* e)
int ListExpr::IsPure() const int ListExpr::IsPure() const
{ {
loop_over_list(exprs, i) for ( const auto& expr : exprs )
if ( ! exprs[i]->IsPure() ) if ( ! expr->IsPure() )
return 0; return 0;
return 1; return 1;
@ -4438,8 +4433,8 @@ int ListExpr::IsPure() const
int ListExpr::AllConst() const int ListExpr::AllConst() const
{ {
loop_over_list(exprs, i) for ( const auto& expr : exprs )
if ( ! exprs[i]->IsConst() ) if ( ! expr->IsConst() )
return 0; return 0;
return 1; return 1;
@ -4449,9 +4444,9 @@ Val* ListExpr::Eval(Frame* f) const
{ {
ListVal* v = new ListVal(TYPE_ANY); 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 ) if ( ! ev )
{ {
RuntimeError("uninitialized list value"); RuntimeError("uninitialized list value");
@ -4476,12 +4471,12 @@ BroType* ListExpr::InitType() const
if ( exprs[0]->IsRecordElement(0) ) if ( exprs[0]->IsRecordElement(0) )
{ {
type_decl_list* types = new type_decl_list(exprs.length()); 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); 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 td;
delete types; delete types;
return 0; return 0;
@ -4497,9 +4492,8 @@ BroType* ListExpr::InitType() const
else else
{ {
TypeList* tl = new TypeList(); TypeList* tl = new TypeList();
loop_over_list(exprs, i) for ( const auto& e : exprs )
{ {
Expr* e = exprs[i];
BroType* ti = e->Type(); BroType* ti = e->Type();
// Collapse any embedded sets or lists. // 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 // know how to add themselves to a table or record. Another
// possibility is an expression that evaluates itself to a // possibility is an expression that evaluates itself to a
// table, which we can then add to the aggregate. // 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->Tag() == EXPR_ASSIGN || e->Tag() == EXPR_FIELD_ASSIGN )
{ {
if ( ! e->InitVal(t, aggr) ) 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 TableType* tt = tv->Type()->AsTableType();
const TypeList* it = tt->Indices(); const TypeList* it = tt->Indices();
loop_over_list(exprs, i) for ( const auto& expr : exprs )
{ {
Val* element; Val* element;
if ( exprs[i]->Type()->IsSet() ) if ( expr->Type()->IsSet() )
// A set to flatten. // A set to flatten.
element = exprs[i]->Eval(0); element = expr->Eval(0);
else if ( exprs[i]->Type()->Tag() == TYPE_LIST ) else if ( expr->Type()->Tag() == TYPE_LIST )
element = exprs[i]->InitVal(it, 0); element = expr->InitVal(it, 0);
else else
element = exprs[i]->InitVal((*it->Types())[0], 0); element = expr->InitVal((*it->Types())[0], 0);
if ( ! element ) if ( ! element )
return 0; return 0;
@ -4703,7 +4695,7 @@ Val* ListExpr::AddSetInit(const BroType* t, Val* aggr) const
continue; continue;
} }
if ( exprs[i]->Type()->Tag() == TYPE_LIST ) if ( expr->Type()->Tag() == TYPE_LIST )
element = check_and_promote(element, it, 1); element = check_and_promote(element, it, 1);
else else
element = check_and_promote(element, (*it->Types())[0], 1); element = check_and_promote(element, (*it->Types())[0], 1);
@ -4739,8 +4731,8 @@ void ListExpr::ExprDescribe(ODesc* d) const
Expr* ListExpr::MakeLvalue() Expr* ListExpr::MakeLvalue()
{ {
loop_over_list(exprs, i) for ( const auto & expr : exprs )
if ( exprs[i]->Tag() != EXPR_NAME ) if ( expr->Tag() != EXPR_NAME )
ExprError("can only assign to list of identifiers"); ExprError("can only assign to list of identifiers");
return new RefExpr(this); return new RefExpr(this);
@ -4764,9 +4756,9 @@ TraversalCode ListExpr::Traverse(TraversalCallback* cb) const
TraversalCode tc = cb->PreExpr(this); TraversalCode tc = cb->PreExpr(this);
HANDLE_TC_EXPR_PRE(tc); 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); 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) // 2) a string indicating the field name, then (as the next element)
// the value to use for that field. // 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 ) 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)) ) same_type(lhs->FieldType(field), t->FieldType(j)) )
{ {
FieldExpr* fe_lhs = new FieldExpr(record, field_name); 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)); 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(); rf->Ref();
const char* field_name = ""; // rf->FieldName(); 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()); def_elements.insert(def_attr->AttrExpr());
} }
loop_over_list(def_elements, i) for ( const auto& elem : def_elements )
el.append(def_elements[i]->Ref()); el.append(elem->Ref());
} }
TypeList* tl = new TypeList(); TypeList* tl = new TypeList();
@ -5115,18 +5107,22 @@ val_list* eval_list(Frame* f, const ListExpr* l)
const expr_list& e = l->Exprs(); const expr_list& e = l->Exprs();
val_list* v = new val_list(e.length()); 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 ) if ( ! ev )
{
success = false;
break; break;
}
v->append(ev); v->append(ev);
} }
if ( i < e.length() ) if ( ! success )
{ // Failure. { // Failure.
loop_over_list(*v, j) for ( const auto& val : *v )
Unref((*v)[j]); Unref(val);
delete v; delete v;
return 0; return 0;
} }

View file

@ -73,11 +73,11 @@ std::string render_call_stack()
if ( ci.args ) if ( ci.args )
{ {
loop_over_list(*ci.args, i) for ( const auto& arg : *ci.args )
{ {
ODesc d; ODesc d;
d.SetShort(); d.SetShort();
(*ci.args)[i]->Describe(&d); arg->Describe(&d);
if ( ! arg_desc.empty() ) if ( ! arg_desc.empty() )
arg_desc += ", "; arg_desc += ", ";
@ -237,8 +237,8 @@ std::pair<bool, Val*> Func::HandlePluginResult(std::pair<bool, Val*> plugin_resu
} }
} }
loop_over_list(*args, i) for ( const auto& arg : *args )
Unref((*args)[i]); Unref(arg);
return plugin_result; return plugin_result;
} }
@ -300,8 +300,8 @@ Val* BroFunc::Call(val_list* args, Frame* parent) const
{ {
// Can only happen for events and hooks. // Can only happen for events and hooks.
assert(Flavor() == FUNC_FLAVOR_EVENT || Flavor() == FUNC_FLAVOR_HOOK); assert(Flavor() == FUNC_FLAVOR_EVENT || Flavor() == FUNC_FLAVOR_HOOK);
loop_over_list(*args, i) for ( const auto& arg : *args )
Unref((*args)[i]); Unref(arg);
return Flavor() == FUNC_FLAVOR_HOOK ? val_mgr->GetTrue() : 0; 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; stmt_flow_type flow = FLOW_NEXT;
Val* result = 0; Val* result = 0;
for ( size_t i = 0; i < bodies.size(); ++i ) for ( const auto& body : bodies )
{ {
if ( sample_logger ) if ( sample_logger )
sample_logger->LocationSeen( sample_logger->LocationSeen(
bodies[i].stmts->GetLocationInfo()); body.stmts->GetLocationInfo());
Unref(result); Unref(result);
@ -356,7 +356,7 @@ Val* BroFunc::Call(val_list* args, Frame* parent) const
try try
{ {
result = bodies[i].stmts->Exec(f, flow); result = body.stmts->Exec(f, flow);
} }
catch ( InterpreterException& e ) 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 // We have an extra Ref for each argument (so that they don't get
// deleted between bodies), release that. // deleted between bodies), release that.
loop_over_list(*args, k) for ( const auto& arg : *args )
Unref((*args)[k]); Unref(arg);
if ( Flavor() == FUNC_FLAVOR_HOOK ) 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. // For functions, we replace the old body with the new one.
assert(bodies.size() <= 1); assert(bodies.size() <= 1);
for ( unsigned int i = 0; i < bodies.size(); ++i ) for ( const auto& body : bodies )
Unref(bodies[i].stmts); Unref(body.stmts);
bodies.clear(); bodies.clear();
} }
@ -540,8 +540,8 @@ Val* BuiltinFunc::Call(val_list* args, Frame* parent) const
Val* result = func(parent, args); Val* result = func(parent, args);
call_stack.pop_back(); call_stack.pop_back();
loop_over_list(*args, i) for ( const auto& arg : *args )
Unref((*args)[i]); Unref(arg);
// Don't Unref() args, that's the caller's responsibility. // Don't Unref() args, that's the caller's responsibility.
if ( result && g_trace_state.DoTrace() ) if ( result && g_trace_state.DoTrace() )

View file

@ -1,246 +0,0 @@
#include "zeek-config.h"
#include <stdio.h>
#include <stdlib.h>
#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 <stdio.h>
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;
}

View file

@ -20,67 +20,362 @@
// Entries must be either a pointer to the data or nonzero data with // Entries must be either a pointer to the data or nonzero data with
// sizeof(data) <= sizeof(void*). // sizeof(data) <= sizeof(void*).
#include <initializer_list>
#include <utility>
#include <stdarg.h> #include <stdarg.h>
#include <initializer_list>
#include <iterator>
#include <utility>
#include <cassert>
#include "util.h" #include "util.h"
typedef void* ent;
typedef int (*list_cmp_func)(const void* v1, const void* v2); typedef int (*list_cmp_func)(const void* v1, const void* v2);
class BaseList { template<typename T>
class ListIterator
{
public: public:
void clear(); // remove all entries ListIterator(T* entries, int offset, int num_entries) :
int length() const { return num_entries; } entries(entries), offset(offset), num_entries(num_entries), endptr() {}
int max() const { return max_entries; } bool operator==(const ListIterator& rhs) { return entries == rhs.entries && offset == rhs.offset; }
int resize(int = 0); // 0 => size to fit current number of entries 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: namespace std {
~BaseList() { free(entry); } template<typename T>
explicit BaseList(int = 0); class iterator_traits<ListIterator<T> >
BaseList(const BaseList&); {
BaseList(BaseList&&); public:
BaseList(const ent* arr, int n); 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<typename T>
class List {
public:
// Assumes that the list is sorted and inserts at correct position. const int DEFAULT_LIST_SIZE = 10;
void sortedinsert(ent, list_cmp_func cmp_func); const int LIST_GROWTH_FACTOR = 2;
void append(ent); // add to end of list ~List() { free(entries); }
ent remove(ent); // delete entry from list explicit List(int size = 0)
ent remove_nth(int); // delete nth entry from list {
ent get(); // return and remove ent at end of list num_entries = 0;
ent last() // return at end of list
{ return entry[num_entries-1]; }
// Return 0 if ent is not in the list, ent otherwise. if ( size <= 0 )
ent is_member(ent) const; {
max_entries = 0;
entries = nullptr;
return;
}
// Returns -1 if ent is not in the list, otherwise its position. max_entries = size;
int member_pos(ent) const;
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<T> 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). // Return nth ent of list (do not remove).
ent operator[](int i) const T& operator[](int i) const
{ {
#ifdef SAFE_LISTS return entries[i];
if ( i < 0 || i > num_entries-1 )
return 0;
else
#endif
return entry[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<T>;
using const_iterator = ListIterator<const T>;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_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 // This could essentially be an std::vector if we wanted. Some
// reasons to maybe not refactor to use std::vector ? // 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 // advantage of realloc's ability to contract in-place, it would
// allocate-and-copy. // allocate-and-copy.
ent* entry; T* entries;
int max_entries; int max_entries;
int num_entries; int num_entries;
}; };
// List.h -- interface for class List // Specialization of the List class to store pointers of a type.
// Use: to get a list of pointers to class foo you should: template<typename T>
// 1) typedef foo* Pfoo; (the macros don't like explicit pointers) using PList = List<T*>;
// 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<type*> 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)
// Popular type of list: list of strings. // Popular type of list: list of strings.
declare(PList,char); typedef PList<char> name_list;
typedef PList(char) name_list;
// Macro to visit each list element in turn. // Macro to visit each list element in turn.
#define loop_over_list(list, iterator) \ #define loop_over_list(list, iterator) \

View file

@ -9,8 +9,7 @@
class NFA_State; class NFA_State;
class EquivClass; class EquivClass;
declare(PList,NFA_State); typedef PList<NFA_State> NFA_state_list;
typedef PList(NFA_State) NFA_state_list;
#define NO_ACCEPT 0 #define NO_ACCEPT 0

View file

@ -233,9 +233,8 @@ void MD5Val::digest(val_list& vlist, u_char result[MD5_DIGEST_LENGTH])
{ {
EVP_MD_CTX* h = hash_init(Hash_MD5); 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 ) if ( v->Type()->Tag() == TYPE_STRING )
{ {
const BroString* str = v->AsString(); 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); 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 ) if ( v->Type()->Tag() == TYPE_STRING )
{ {
const BroString* str = v->AsString(); 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); 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 ) if ( v->Type()->Tag() == TYPE_STRING )
{ {
const BroString* str = v->AsString(); const BroString* str = v->AsString();

View file

@ -1,137 +0,0 @@
// See the file "COPYING" in the main distribution directory for copyright.
#include "zeek-config.h"
#include <string.h>
#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;
}

View file

@ -3,8 +3,8 @@
#ifndef queue_h #ifndef queue_h
#define queue_h #define queue_h
// BaseQueue.h -- // Queue.h --
// Interface for class BaseQueue, current implementation is as an // Interface for class Queue, current implementation is as an
// array of ent's. This implementation was chosen to optimize // array of ent's. This implementation was chosen to optimize
// getting to the ent's rather than inserting and deleting. // 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 // 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 // Entries must be either a pointer to the data or nonzero data with
// sizeof(data) <= sizeof(void*). // sizeof(data) <= sizeof(void*).
#include "List.h" template<typename T>
class QueueIterator
class BaseQueue { {
T* const entries;
int offset;
int num_entries;
public: 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<typename T>
class iterator_traits<QueueIterator<T> >
{
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<typename T>
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 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 // remove all entries without delete[] entry
void clear() { head = tail = num_entries = 0; } void clear() { head = tail = num_entries = 0; }
// helper functions for iterating over queue // helper functions for iterating over queue
int front() const { return head; } T& front() { return entries[head]; }
int back() const { return tail; } T& back() { return entries[tail]; }
void incr(int& index) { index < max_entries ? ++index : index = 0; } const T& front() const { return entries[head]; }
const T& back() const { return entries[tail]; }
protected: void push_front(const T& a) // add in front of queue
explicit BaseQueue(int = 0); {
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 ++num_entries;
void push_back(ent); // add at end of queue if ( head )
ent pop_front(); // return and remove the front of queue entries[--head] = a;
ent pop_back(); // return and remove the end of queue 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) // 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<T>;
using const_iterator = QueueIterator<const T>;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_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 chunk_size; // increase size by this amount when necessary
int max_entries; // entry's index range: 0 .. max_entries int max_entries; // entry's index range: 0 .. max_entries
int num_entries; int num_entries;
@ -57,54 +236,8 @@ protected:
int tail; // just beyond the end of the queue in the ring 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" template<typename T>
#define Queue(type) type ## Queue using PQueue = Queue<T*>;
// 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) ) \
#endif /* queue_h */ #endif /* queue_h */

View file

@ -149,7 +149,7 @@ int Specific_RE_Matcher::Compile(int lazy)
int Specific_RE_Matcher::CompileSet(const string_list& set, const int_list& idx) 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"); reporter->InternalError("compileset: lengths of sets differ");
rem = this; rem = this;

View file

@ -22,10 +22,6 @@ class Specific_RE_Matcher;
class RE_Matcher; class RE_Matcher;
class DFA_State; class DFA_State;
declare(PDict,char);
declare(PDict,CCL);
declare(PList,CCL);
extern int case_insensitive; extern int case_insensitive;
extern CCL* curr_ccl; extern CCL* curr_ccl;
extern NFA_Machine* nfa; extern NFA_Machine* nfa;
@ -123,9 +119,9 @@ protected:
int multiline; int multiline;
char* pattern_text; char* pattern_text;
PDict(char) defs; PDict<char> defs;
PDict(CCL) ccl_dict; PDict<CCL> ccl_dict;
PList(CCL) ccl_list; PList<CCL> ccl_list;
EquivClass equiv_class; EquivClass equiv_class;
int* ecs; int* ecs;
DFA_Machine* dfa; DFA_Machine* dfa;

View file

@ -502,10 +502,7 @@ void Reporter::DoLog(const char* prefix, EventHandlerPtr event, FILE* out,
vl.append(conn->BuildConnVal()); vl.append(conn->BuildConnVal());
if ( addl ) if ( addl )
{ std::copy(addl->begin(), addl->end(), std::back_inserter(vl));
loop_over_list(*addl, i)
vl.append((*addl)[i]);
}
if ( conn ) if ( conn )
conn->ConnectionEventFast(event, 0, std::move(vl)); conn->ConnectionEventFast(event, 0, std::move(vl));
@ -516,8 +513,8 @@ void Reporter::DoLog(const char* prefix, EventHandlerPtr event, FILE* out,
{ {
if ( addl ) if ( addl )
{ {
loop_over_list(*addl, i) for ( const auto& av : *addl )
Unref((*addl)[i]); Unref(av);
} }
} }

View file

@ -13,25 +13,25 @@ Rule::~Rule()
{ {
delete [] id; delete [] id;
loop_over_list(patterns, i) for ( const auto& p : patterns )
{ {
delete [] patterns[i]->pattern; delete [] p->pattern;
delete patterns[i]; delete p;
} }
loop_over_list(hdr_tests, j) for ( const auto& test : hdr_tests )
delete hdr_tests[j]; delete test;
loop_over_list(conditions, k) for ( const auto& cond : conditions )
delete conditions[k]; delete cond;
loop_over_list(actions, l) for ( const auto& action : actions )
delete actions[l]; delete action;
loop_over_list(preconds, m) for ( const auto& prec : preconds )
{ {
delete [] preconds[m]->id; delete [] prec->id;
delete preconds[m]; delete prec;
} }
} }
@ -49,21 +49,20 @@ void Rule::PrintDebug()
{ {
fprintf(stderr, "Rule %s (%d) %s\n", id, idx, active ? "[active]" : "[disabled]"); 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", fprintf(stderr, " %-8s |%s| (%d) \n",
TypeToString(patterns[i]->type), patterns[i]->pattern, TypeToString(p->type), p->pattern, p->id);
patterns[i]->id);
} }
loop_over_list(hdr_tests, j) for ( const auto& h : hdr_tests )
hdr_tests[j]->PrintDebug(); h->PrintDebug();
loop_over_list(conditions, k) for ( const auto& c : conditions )
conditions[k]->PrintDebug(); c->PrintDebug();
loop_over_list(actions, l) for ( const auto& a : actions )
actions[l]->PrintDebug(); a->PrintDebug();
fputs("\n", stderr); fputs("\n", stderr);
} }

View file

@ -14,11 +14,8 @@ class RuleHdrTest;
class Rule; class Rule;
declare(PList, Rule); typedef PList<Rule> rule_list;
typedef PList(Rule) rule_list; typedef PDict<Rule> rule_dict;
declare(PDict, Rule);
typedef PDict(Rule) rule_dict;
class Rule { class Rule {
public: public:
@ -61,14 +58,9 @@ private:
void SortHdrTests(); void SortHdrTests();
declare(PList, RuleAction); typedef PList<RuleAction> rule_action_list;
typedef PList(RuleAction) rule_action_list; typedef PList<RuleCondition> rule_condition_list;
typedef PList<RuleHdrTest> rule_hdr_test_list;
declare(PList, RuleCondition);
typedef PList(RuleCondition) rule_condition_list;
declare(PList, RuleHdrTest);
typedef PList(RuleHdrTest) rule_hdr_test_list;
rule_hdr_test_list hdr_tests; rule_hdr_test_list hdr_tests;
rule_condition_list conditions; rule_condition_list conditions;
@ -82,8 +74,7 @@ private:
bool negate; // negate test bool negate; // negate test
}; };
declare(PList, Precond); typedef PList<Precond> precond_list;
typedef PList(Precond) precond_list;
precond_list preconds; precond_list preconds;
rule_list dependents; // rules w/ us as a precondition rule_list dependents; // rules w/ us as a precondition
@ -101,8 +92,7 @@ private:
uint32 depth; uint32 depth;
}; };
declare(PList, Pattern); typedef PList<Pattern> pattern_list;
typedef PList(Pattern) pattern_list;
pattern_list patterns; pattern_list patterns;
Rule* next; // Linkage within RuleHdrTest tree: Rule* next; // Linkage within RuleHdrTest tree:

View file

@ -25,6 +25,11 @@
uint32 RuleHdrTest::idcounter = 0; 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, RuleHdrTest::RuleHdrTest(Prot arg_prot, uint32 arg_offset, uint32 arg_size,
Comp arg_comp, maskedvalue_list* arg_vals) Comp arg_comp, maskedvalue_list* arg_vals)
{ {
@ -78,21 +83,20 @@ RuleHdrTest::RuleHdrTest(RuleHdrTest& h)
comp = h.comp; comp = h.comp;
vals = new maskedvalue_list; vals = new maskedvalue_list;
loop_over_list(*h.vals, i) for ( const auto& val : *h.vals )
vals->append(new MaskedValue(*(*h.vals)[i])); vals->append(new MaskedValue(*val));
prefix_vals = h.prefix_vals; prefix_vals = h.prefix_vals;
for ( int j = 0; j < Rule::TYPES; ++j ) 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; PatternSet* copied_set = new PatternSet;
copied_set->re = 0; copied_set->re = 0;
copied_set->ids = orig_set->ids; copied_set->ids = orig_set->ids;
loop_over_list(orig_set->patterns, l) for ( const auto& pattern : orig_set->patterns )
copied_set->patterns.append(copy_string(orig_set->patterns[l])); copied_set->patterns.append(copy_string(pattern));
delete copied_set; delete copied_set;
// TODO: Why do we create copied_set only to then // TODO: Why do we create copied_set only to then
// never use it? // never use it?
@ -110,14 +114,14 @@ RuleHdrTest::RuleHdrTest(RuleHdrTest& h)
RuleHdrTest::~RuleHdrTest() RuleHdrTest::~RuleHdrTest()
{ {
loop_over_list(*vals, i) for ( auto val : *vals )
delete (*vals)[i]; delete val;
delete vals; delete vals;
for ( int i = 0; i < Rule::TYPES; ++i ) for ( int i = 0; i < Rule::TYPES; ++i )
{ {
loop_over_list(psets[i], j) for ( auto pset : psets[i] )
delete psets[i][j]->re; delete pset->re;
} }
delete ruleset; delete ruleset;
@ -149,12 +153,12 @@ void RuleHdrTest::PrintDebug()
fprintf(stderr, " RuleHdrTest %s[%d:%d] %s", fprintf(stderr, " RuleHdrTest %s[%d:%d] %s",
str_prot[prot], offset, size, str_comp[comp]); str_prot[prot], offset, size, str_comp[comp]);
loop_over_list(*vals, i) for ( const auto& val : *vals )
fprintf(stderr, " 0x%08x/0x%08x", 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 ) for ( const auto& prefix : prefix_vals )
fprintf(stderr, " %s", prefix_vals[i].AsString().c_str()); fprintf(stderr, " %s", prefix.AsString().c_str());
fprintf(stderr, "\n"); fprintf(stderr, "\n");
} }
@ -176,22 +180,22 @@ RuleEndpointState::RuleEndpointState(analyzer::Analyzer* arg_analyzer, bool arg_
RuleEndpointState::~RuleEndpointState() RuleEndpointState::~RuleEndpointState()
{ {
loop_over_list(matchers, i) for ( auto matcher : matchers )
{ {
delete matchers[i]->state; delete matcher->state;
delete matchers[i]; delete matcher;
} }
loop_over_list(matched_text, j) for ( auto text : matched_text )
delete matched_text[j]; delete text;
} }
RuleFileMagicState::~RuleFileMagicState() RuleFileMagicState::~RuleFileMagicState()
{ {
loop_over_list(matchers, i) for ( auto matcher : matchers )
{ {
delete matchers[i]->state; delete matcher->state;
delete matchers[i]; delete matcher;
} }
} }
@ -209,8 +213,8 @@ RuleMatcher::~RuleMatcher()
#endif #endif
Delete(root); Delete(root);
loop_over_list(rules, i) for ( auto rule : rules )
delete rules[i]; delete rule;
} }
void RuleMatcher::Delete(RuleHdrTest* node) void RuleMatcher::Delete(RuleHdrTest* node)
@ -275,13 +279,13 @@ void RuleMatcher::AddRule(Rule* rule)
void RuleMatcher::BuildRulesTree() void RuleMatcher::BuildRulesTree()
{ {
loop_over_list(rules, r) for ( const auto& rule : rules )
{ {
if ( ! rules[r]->Active() ) if ( ! rule->Active() )
continue; continue;
rules[r]->SortHdrTests(); rule->SortHdrTests();
InsertRuleIntoTree(rules[r], 0, root, 0); InsertRuleIntoTree(rule, 0, root, 0);
} }
} }
@ -289,10 +293,8 @@ void RuleMatcher::InsertRuleIntoTree(Rule* r, int testnr,
RuleHdrTest* dest, int level) RuleHdrTest* dest, int level)
{ {
// Initiliaze the preconditions // 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); Rule* pc_rule = rules_by_id.Lookup(pc->id);
if ( ! pc_rule ) 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 each type, get all patterns on this node.
for ( Rule* r = hdr_test->pattern_rules; r; r = r->next ) 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); 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) loop_over_list(child_exprs[i], j)
{ {
exprs[i].append(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, void RuleMatcher::BuildPatternSets(RuleHdrTest::pattern_set_list* dst,
const string_list& exprs, const int_list& ids) const string_list& exprs, const int_list& ids)
{ {
assert(exprs.length() == ids.length()); assert(static_cast<size_t>(exprs.length()) == ids.size());
// We build groups of at most sig_max_group_size regexps. // 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() ) if ( i < exprs.length() )
{ {
group_exprs.append(exprs[i]); group_exprs.append(exprs[i]);
group_ids.append(ids[i]); group_ids.push_back(ids[i]);
} }
if ( group_exprs.length() > sig_max_group_size || if ( group_exprs.length() > sig_max_group_size ||
@ -452,9 +453,10 @@ static inline uint32 getval(const u_char* data, int size)
template <typename FuncT> template <typename FuncT>
static inline bool match_or(const maskedvalue_list& mvals, uint32 v, FuncT comp) 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 true;
} }
return false; return false;
@ -480,9 +482,10 @@ template <typename FuncT>
static inline bool match_not_and(const maskedvalue_list& mvals, uint32 v, static inline bool match_not_and(const maskedvalue_list& mvals, uint32 v,
FuncT comp) 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 false;
} }
return true; return true;
@ -577,9 +580,8 @@ RuleFileMagicState* RuleMatcher::InitFileMagic() const
{ {
RuleFileMagicState* state = new RuleFileMagicState(); 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); assert(set->re);
RuleFileMagicState::Matcher* m = new RuleFileMagicState::Matcher; RuleFileMagicState::Matcher* m = new RuleFileMagicState::Matcher;
m->state = new RE_Match_State(set->re); 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); DBG_LOG(DBG_RULES, "Checking rule: %s", r->id);
// Check whether all patterns of the rule have matched. // 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; return false;
// See if depth is satisfied. // See if depth is satisfied.
if ( matchpos > r->patterns[j]->offset + r->patterns[j]->depth ) if ( matchpos > pattern->offset + pattern->depth )
return false; return false;
// FIXME: How to check for offset ??? ### // FIXME: How to check for offset ??? ###
@ -640,10 +642,8 @@ RuleMatcher::MIME_Matches* RuleMatcher::Match(RuleFileMagicState* state,
bool newmatch = false; 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) ) if ( m->state->Match(data, len, true, false, true) )
newmatch = true; newmatch = true;
} }
@ -655,9 +655,8 @@ RuleMatcher::MIME_Matches* RuleMatcher::Match(RuleFileMagicState* state,
AcceptingMatchSet accepted_matches; 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(); const AcceptingMatchSet& ams = m->state->AcceptedMatches();
accepted_matches.insert(ams.begin(), ams.end()); accepted_matches.insert(ams.begin(), ams.end());
} }
@ -682,10 +681,10 @@ RuleMatcher::MIME_Matches* RuleMatcher::Match(RuleFileMagicState* state,
{ {
Rule* r = *it; Rule* r = *it;
loop_over_list(r->actions, rai) for ( const auto& action : r->actions )
{ {
const RuleActionMIME* ram = const RuleActionMIME* ram =
dynamic_cast<const RuleActionMIME*>(r->actions[rai]); dynamic_cast<const RuleActionMIME*>(action);
if ( ! ram ) if ( ! ram )
continue; continue;
@ -734,11 +733,8 @@ RuleEndpointState* RuleMatcher::InitEndpoint(analyzer::Analyzer* analyzer,
{ {
for ( int i = 0; i < Rule::TYPES; ++i ) 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); assert(set->re);
RuleEndpointState::Matcher* m = RuleEndpointState::Matcher* m =
@ -855,9 +851,8 @@ void RuleMatcher::Match(RuleEndpointState* state, Rule::PatternType type,
} }
// Feed data into all relevant matchers. // 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 && if ( m->type == type &&
m->state->Match((const u_char*) data, data_len, m->state->Match((const u_char*) data, data_len,
bol, eol, clear) ) bol, eol, clear) )
@ -872,9 +867,8 @@ void RuleMatcher::Match(RuleEndpointState* state, Rule::PatternType type,
AcceptingMatchSet accepted_matches; 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(); const AcceptingMatchSet& ams = m->state->AcceptedMatches();
accepted_matches.insert(ams.begin(), ams.end()); 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); 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); DBG_LOG(DBG_RULES, "Checking for accepted rule on HdrTest %d", h->id);
// Skip if rule does not belong to this node. // 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"); DBG_LOG(DBG_RULES, "On current node");
// Skip if rule already fired for this connection. // 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; continue;
// Remember that all patterns have matched. // Remember that all patterns have matched.
@ -960,9 +952,8 @@ void RuleMatcher::FinishEndpoint(RuleEndpointState* state)
void RuleMatcher::ExecPureRules(RuleEndpointState* state, bool eos) 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 ) for ( Rule* r = hdr_test->pure_rules; r; r = r->next )
ExecRulePurely(r, 0, state, eos); ExecRulePurely(r, 0, state, eos);
} }
@ -971,7 +962,7 @@ void RuleMatcher::ExecPureRules(RuleEndpointState* state, bool eos)
bool RuleMatcher::ExecRulePurely(Rule* r, BroString* s, bool RuleMatcher::ExecRulePurely(Rule* r, BroString* s,
RuleEndpointState* state, bool eos) RuleEndpointState* state, bool eos)
{ {
if ( state->matched_rules.is_member(r->Index()) ) if ( is_member_of(state->matched_rules, r->Index()) )
return false; return false;
DBG_LOG(DBG_RULES, "Checking rule %s purely", r->ID()); 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()); DBG_LOG(DBG_RULES, "Evaluating conditions for rule %s", r->ID());
// Check for other rules which have to match first. // 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; RuleEndpointState* pc_state = state;
if ( pc->opposite_dir ) if ( pc->opposite_dir )
@ -1014,7 +1003,7 @@ bool RuleMatcher::EvalRuleConditions(Rule* r, RuleEndpointState* state,
if ( ! pc->negate ) 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. // Precond rule has not matched yet.
return false; return false;
} }
@ -1024,13 +1013,13 @@ bool RuleMatcher::EvalRuleConditions(Rule* r, RuleEndpointState* state,
if ( ! eos ) if ( ! eos )
return false; 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; return false;
} }
} }
loop_over_list(r->conditions, l) for ( const auto& cond : r->conditions )
if ( ! r->conditions[l]->DoMatch(r, state, data, len) ) if ( ! cond->DoMatch(r, state, data, len) )
return false; return false;
DBG_LOG(DBG_RULES, "Conditions met: MATCH! %s", r->ID()); 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) const u_char* data, int len, bool eos)
{ {
if ( state->opposite && 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. // We have already executed the actions.
return; return;
state->matched_rules.append(r->Index()); state->matched_rules.push_back(r->Index());
loop_over_list(r->actions, i) for ( const auto& action : r->actions )
r->actions[i]->DoAction(r, state, data, len); action->DoAction(r, state, data, len);
// This rule may trigger some other rules; check them. // 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); ExecRule(dep, state, eos);
if ( state->opposite ) if ( state->opposite )
ExecRule(dep, state->opposite, eos); 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) void RuleMatcher::ExecRule(Rule* rule, RuleEndpointState* state, bool eos)
{ {
// Nothing to do if it has already matched. // 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; 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? // Is it on this HdrTest at all?
if ( ! h->ruleset->Contains(rule->Index()) ) if ( ! h->ruleset->Contains(rule->Index()) )
continue; continue;
@ -1099,20 +1085,20 @@ void RuleMatcher::ClearEndpointState(RuleEndpointState* state)
state->payload_size = -1; state->payload_size = -1;
loop_over_list(state->matchers, j) for ( const auto& matcher : state->matchers )
state->matchers[j]->state->Clear(); matcher->state->Clear();
} }
void RuleMatcher::ClearFileMagicState(RuleFileMagicState* state) const void RuleMatcher::ClearFileMagicState(RuleFileMagicState* state) const
{ {
loop_over_list(state->matchers, j) for ( const auto& matcher : state->matchers )
state->matchers[j]->state->Clear(); matcher->state->Clear();
} }
void RuleMatcher::PrintDebug() void RuleMatcher::PrintDebug()
{ {
loop_over_list(rules, i) for ( const auto& rule : rules )
rules[i]->PrintDebug(); rule->PrintDebug();
fprintf(stderr, "\n---------------\n"); fprintf(stderr, "\n---------------\n");
@ -1135,10 +1121,10 @@ void RuleMatcher::PrintTreeDebug(RuleHdrTest* node)
RuleHdrTest::PatternSet* set = node->psets[i][j]; RuleHdrTest::PatternSet* set = node->psets[i][j];
fprintf(stderr, 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(), set->patterns.length(),
Rule::TypeToString((Rule::PatternType) i), j, 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 ) 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); assert(set->re);
++stats->matchers; ++stats->matchers;
@ -1234,9 +1219,9 @@ void RuleMatcher::DumpStateStats(BroFile* f, RuleHdrTest* hdr_test)
set->re->DFA()->NumStates(), set->re->DFA()->NumStates(),
Rule::TypeToString((Rule::PatternType)i), j)); 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())); 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(); ListVal* lv = v->AsTableVal()->ConvertToPureList();
val_list* vals = lv->Vals(); val_list* vals = lv->Vals();
loop_over_list(*vals, i ) for ( const auto& val : *vals )
if ( ! val_to_maskedval((*vals)[i], append_to, prefix_vector) ) if ( ! val_to_maskedval(val, append_to, prefix_vector) )
{ {
Unref(lv); Unref(lv);
return; return;

View file

@ -56,13 +56,9 @@ struct MaskedValue {
uint32 mask; uint32 mask;
}; };
declare(PList, MaskedValue); typedef PList<MaskedValue> maskedvalue_list;
typedef PList(MaskedValue) maskedvalue_list; typedef PList<char> string_list;
typedef PList<BroString> bstr_list;
typedef PList(char) string_list;
declare(PList, BroString);
typedef PList(BroString) bstr_list;
// Get values from Bro's script-level variables. // Get values from Bro's script-level variables.
extern void id_to_maskedvallist(const char* id, maskedvalue_list* append_to, extern void id_to_maskedvallist(const char* id, maskedvalue_list* append_to,
@ -119,8 +115,7 @@ private:
int_list ids; // (only needed for debugging) int_list ids; // (only needed for debugging)
}; };
declare(PList, PatternSet); typedef PList<PatternSet> pattern_set_list;
typedef PList(PatternSet) pattern_set_list;
pattern_set_list psets[Rule::TYPES]; pattern_set_list psets[Rule::TYPES];
// List of rules belonging to this node. // List of rules belonging to this node.
@ -136,8 +131,7 @@ private:
int level; // level within the tree int level; // level within the tree
}; };
declare(PList, RuleHdrTest); typedef PList<RuleHdrTest> rule_hdr_test_list;
typedef PList(RuleHdrTest) rule_hdr_test_list;
// RuleEndpointState keeps the per-stream matching state of one // RuleEndpointState keeps the per-stream matching state of one
// connection endpoint. // connection endpoint.
@ -172,8 +166,7 @@ private:
Rule::PatternType type; Rule::PatternType type;
}; };
declare(PList, Matcher); typedef PList<Matcher> matcher_list;
typedef PList(Matcher) matcher_list;
bool is_orig; bool is_orig;
analyzer::Analyzer* analyzer; analyzer::Analyzer* analyzer;
@ -212,8 +205,7 @@ private:
RE_Match_State* state; RE_Match_State* state;
}; };
declare(PList, Matcher); typedef PList<Matcher> matcher_list;
typedef PList(Matcher) matcher_list;
matcher_list matchers; matcher_list matchers;
}; };

View file

@ -7,8 +7,7 @@
#include "Scope.h" #include "Scope.h"
#include "Reporter.h" #include "Reporter.h"
declare(PList,Scope); typedef PList<Scope> scope_list;
typedef PList(Scope) scope_list;
static scope_list scopes; static scope_list scopes;
static Scope* top_scope; static Scope* top_scope;
@ -20,7 +19,7 @@ Scope::Scope(ID* id, attr_list* al)
attrs = al; attrs = al;
return_type = 0; return_type = 0;
local = new PDict(ID)(ORDERED); local = new PDict<ID>(ORDERED);
inits = new id_list; inits = new id_list;
if ( id ) if ( id )
@ -48,8 +47,8 @@ Scope::~Scope()
if ( attrs ) if ( attrs )
{ {
loop_over_list(*attrs, i) for ( const auto& attr : *attrs )
Unref((*attrs)[i]); Unref(attr);
delete attrs; delete attrs;
} }
@ -109,7 +108,7 @@ void Scope::Describe(ODesc* d) const
TraversalCode Scope::Traverse(TraversalCallback* cb) const TraversalCode Scope::Traverse(TraversalCallback* cb) const
{ {
PDict(ID)* ids = GetIDs(); PDict<ID>* ids = GetIDs();
IterCookie* iter = ids->InitForIteration(); IterCookie* iter = ids->InitForIteration();
HashKey* key; HashKey* key;

View file

@ -15,8 +15,6 @@ class ID;
class BroType; class BroType;
class ListVal; class ListVal;
declare(PDict,ID);
class Scope : public BroObj { class Scope : public BroObj {
public: public:
explicit Scope(ID* id, attr_list* al); explicit Scope(ID* id, attr_list* al);
@ -35,11 +33,11 @@ public:
BroType* ReturnType() const { return return_type; } BroType* ReturnType() const { return return_type; }
int Length() const { return local->Length(); } int Length() const { return local->Length(); }
PDict(ID)* Vars() const { return local; } PDict<ID>* Vars() const { return local; }
ID* GenerateTemporary(const char* name); ID* GenerateTemporary(const char* name);
PDict(ID)* GetIDs() const { return local; } PDict<ID>* GetIDs() const { return local; }
// Returns the list of variables needing initialization, and // Returns the list of variables needing initialization, and
// removes it from this Scope. // removes it from this Scope.
@ -56,7 +54,7 @@ protected:
ID* scope_id; ID* scope_id;
attr_list* attrs; attr_list* attrs;
BroType* return_type; BroType* return_type;
PDict(ID)* local; PDict<ID>* local;
id_list* inits; id_list* inits;
}; };

View file

@ -20,9 +20,6 @@ class Connection;
class ConnCompressor; class ConnCompressor;
struct ConnID; struct ConnID;
declare(PDict,Connection);
declare(PDict,FragReassembler);
class Discarder; class Discarder;
class PacketFilter; class PacketFilter;
@ -216,10 +213,10 @@ protected:
const Packet *pkt, const EncapsulationStack* encap); const Packet *pkt, const EncapsulationStack* encap);
CompositeHash* ch; CompositeHash* ch;
PDict(Connection) tcp_conns; PDict<Connection> tcp_conns;
PDict(Connection) udp_conns; PDict<Connection> udp_conns;
PDict(Connection) icmp_conns; PDict<Connection> icmp_conns;
PDict(FragReassembler) fragments; PDict<FragReassembler> fragments;
typedef pair<IPAddr, IPAddr> IPPair; typedef pair<IPAddr, IPAddr> IPPair;
typedef pair<EncapsulatingConn, double> TunnelActivity; typedef pair<EncapsulatingConn, double> TunnelActivity;

View file

@ -239,7 +239,7 @@ void ProfileLogger::Log()
// Script-level state. // Script-level state.
unsigned int size, mem = 0; unsigned int size, mem = 0;
PDict(ID)* globals = global_scope()->Vars(); PDict<ID>* globals = global_scope()->Vars();
if ( expensive ) if ( expensive )
{ {

View file

@ -123,9 +123,9 @@ ExprListStmt::ExprListStmt(BroStmtTag t, ListExpr* arg_l)
l = arg_l; l = arg_l;
const expr_list& e = l->Exprs(); 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 ) if ( ! t || t->Tag() == TYPE_VOID )
Error("value of type void illegal"); Error("value of type void illegal");
} }
@ -172,9 +172,9 @@ TraversalCode ExprListStmt::Traverse(TraversalCallback* cb) const
HANDLE_TC_STMT_PRE(tc); HANDLE_TC_STMT_PRE(tc);
const expr_list& e = l->Exprs(); 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); HANDLE_TC_STMT_PRE(tc);
} }
@ -430,8 +430,8 @@ Case::~Case()
Unref(expr_cases); Unref(expr_cases);
Unref(s); Unref(s);
loop_over_list((*type_cases), i) for ( const auto& id : *type_cases )
Unref((*type_cases)[i]); Unref(id);
delete type_cases; delete type_cases;
} }
@ -631,9 +631,9 @@ SwitchStmt::SwitchStmt(Expr* index, case_list* arg_cases) :
{ {
have_types = true; 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) ) if ( ! can_cast_value_to_type(e->Type(), ct) )
{ {
@ -641,7 +641,7 @@ SwitchStmt::SwitchStmt(Expr* index, case_list* arg_cases) :
continue; continue;
} }
if ( ! AddCaseLabelTypeMapping((*tl)[j], i) ) if ( ! AddCaseLabelTypeMapping(t, i) )
{ {
c->Error("duplicate case label"); c->Error("duplicate case label");
continue; continue;
@ -665,8 +665,8 @@ SwitchStmt::SwitchStmt(Expr* index, case_list* arg_cases) :
SwitchStmt::~SwitchStmt() SwitchStmt::~SwitchStmt()
{ {
loop_over_list(*cases, i) for ( const auto& c : *cases )
Unref((*cases)[i]); Unref(c);
delete cases; delete cases;
delete comp_hash; delete comp_hash;
@ -793,9 +793,8 @@ int SwitchStmt::IsPure() const
if ( ! e->IsPure() ) if ( ! e->IsPure() )
return 0; return 0;
loop_over_list(*cases, i) for ( const auto& c : *cases )
{ {
Case* c = (*cases)[i];
if ( ! c->ExprCases()->IsPure() || ! c->Body()->IsPure() ) if ( ! c->ExprCases()->IsPure() || ! c->Body()->IsPure() )
return 0; return 0;
} }
@ -812,8 +811,8 @@ void SwitchStmt::Describe(ODesc* d) const
d->PushIndent(); d->PushIndent();
d->AddCount(cases->length()); d->AddCount(cases->length());
loop_over_list(*cases, i) for ( const auto& c : *cases )
(*cases)[i]->Describe(d); c->Describe(d);
d->PopIndent(); d->PopIndent();
if ( ! d->IsBinary() ) if ( ! d->IsBinary() )
@ -830,9 +829,9 @@ TraversalCode SwitchStmt::Traverse(TraversalCallback* cb) const
tc = e->Traverse(cb); tc = e->Traverse(cb);
HANDLE_TC_STMT_PRE(tc); 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); HANDLE_TC_STMT_PRE(tc);
} }
@ -1132,8 +1131,8 @@ ForStmt::ForStmt(id_list* arg_loop_vars, Expr* loop_expr, ID* val_var)
ForStmt::~ForStmt() ForStmt::~ForStmt()
{ {
loop_over_list(*loop_vars, i) for ( const auto& var : *loop_vars )
Unref((*loop_vars)[i]); Unref(var);
delete loop_vars; delete loop_vars;
Unref(value_var); 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 ) if ( v->Type()->Tag() == TYPE_TABLE )
{ {
TableVal* tv = v->AsTableVal(); TableVal* tv = v->AsTableVal();
const PDict(TableEntryVal)* loop_vals = tv->AsTable(); const PDict<TableEntryVal>* loop_vals = tv->AsTable();
if ( ! loop_vals->Length() ) if ( ! loop_vals->Length() )
return 0; return 0;
@ -1275,9 +1274,9 @@ TraversalCode ForStmt::Traverse(TraversalCallback* cb) const
TraversalCode tc = cb->PreStmt(this); TraversalCode tc = cb->PreStmt(this);
HANDLE_TC_STMT_PRE(tc); 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); HANDLE_TC_STMT_PRE(tc);
} }
@ -1445,8 +1444,8 @@ StmtList::StmtList() : Stmt(STMT_LIST)
StmtList::~StmtList() StmtList::~StmtList()
{ {
loop_over_list(stmts, i) for ( const auto& stmt : stmts )
Unref(stmts[i]); Unref(stmt);
} }
Val* StmtList::Exec(Frame* f, stmt_flow_type& flow) const 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(); RegisterAccess();
flow = FLOW_NEXT; 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 { // ### 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 { // ### Abort or something
} }
@ -1477,8 +1476,8 @@ Val* StmtList::Exec(Frame* f, stmt_flow_type& flow) const
int StmtList::IsPure() const int StmtList::IsPure() const
{ {
loop_over_list(stmts, i) for ( const auto& stmt : stmts )
if ( ! stmts[i]->IsPure() ) if ( ! stmt->IsPure() )
return 0; return 0;
return 1; return 1;
} }
@ -1502,9 +1501,9 @@ void StmtList::Describe(ODesc* d) const
d->NL(); d->NL();
} }
loop_over_list(stmts, i) for ( const auto& stmt : stmts )
{ {
stmts[i]->Describe(d); stmt->Describe(d);
d->NL(); d->NL();
} }
@ -1518,9 +1517,9 @@ TraversalCode StmtList::Traverse(TraversalCallback* cb) const
TraversalCode tc = cb->PreStmt(this); TraversalCode tc = cb->PreStmt(this);
HANDLE_TC_STMT_PRE(tc); 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); HANDLE_TC_STMT_PRE(tc);
} }
@ -1533,21 +1532,21 @@ Val* EventBodyList::Exec(Frame* f, stmt_flow_type& flow) const
RegisterAccess(); RegisterAccess();
flow = FLOW_NEXT; 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 // Ignore the return value, since there shouldn't be
// any; and ignore the flow, since we still execute // any; and ignore the flow, since we still execute
// all of the event bodies even if one of them does // all of the event bodies even if one of them does
// a FLOW_RETURN. // a FLOW_RETURN.
if ( ! pre_execute_stmt(stmts[i], f) ) if ( ! pre_execute_stmt(stmt, f) )
{ // ### Abort or something { // ### 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 { // ### Abort or something
} }
} }
@ -1563,16 +1562,16 @@ void EventBodyList::Describe(ODesc* d) const
{ {
if ( d->IsReadable() && stmts.length() > 0 ) if ( d->IsReadable() && stmts.length() > 0 )
{ {
loop_over_list(stmts, i) for ( const auto& stmt : stmts )
{ {
if ( ! d->IsBinary() ) if ( ! d->IsBinary() )
{ {
d->Add("{"); d->Add("{");
d->PushIndent(); d->PushIndent();
stmts[i]->AccessStats(d); stmt->AccessStats(d);
} }
stmts[i]->Describe(d); stmt->Describe(d);
if ( ! d->IsBinary() ) if ( ! d->IsBinary() )
{ {
@ -1588,8 +1587,8 @@ void EventBodyList::Describe(ODesc* d) const
InitStmt::~InitStmt() InitStmt::~InitStmt()
{ {
loop_over_list(*inits, i) for ( const auto& init : *inits )
Unref((*inits)[i]); Unref(init);
delete inits; delete inits;
} }
@ -1599,9 +1598,8 @@ Val* InitStmt::Exec(Frame* f, stmt_flow_type& flow) const
RegisterAccess(); RegisterAccess();
flow = FLOW_NEXT; flow = FLOW_NEXT;
loop_over_list(*inits, i) for ( const auto& aggr : *inits )
{ {
ID* aggr = (*inits)[i];
BroType* t = aggr->Type(); BroType* t = aggr->Type();
Val* v = 0; Val* v = 0;
@ -1649,9 +1647,9 @@ TraversalCode InitStmt::Traverse(TraversalCallback* cb) const
TraversalCode tc = cb->PreStmt(this); TraversalCode tc = cb->PreStmt(this);
HANDLE_TC_STMT_PRE(tc); 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); HANDLE_TC_STMT_PRE(tc);
} }

View file

@ -17,8 +17,6 @@
class StmtList; class StmtList;
class ForStmt; class ForStmt;
declare(PDict, int);
class Stmt : public BroObj { class Stmt : public BroObj {
public: public:
BroStmtTag Tag() const { return tag; } BroStmtTag Tag() const { return tag; }
@ -195,8 +193,7 @@ protected:
Stmt* s; Stmt* s;
}; };
declare(PList,Case); typedef PList<Case> case_list;
typedef PList(Case) case_list;
class SwitchStmt : public ExprStmt { class SwitchStmt : public ExprStmt {
public: public:
@ -238,7 +235,7 @@ protected:
case_list* cases; case_list* cases;
int default_case_idx; int default_case_idx;
CompositeHash* comp_hash; CompositeHash* comp_hash;
PDict(int) case_label_value_map; PDict<int> case_label_value_map;
std::vector<std::pair<ID*, int>> case_label_type_list; std::vector<std::pair<ID*, int>> case_label_type_list;
}; };

View file

@ -207,16 +207,16 @@ unsigned int BroType::MemoryAllocation() const
TypeList::~TypeList() TypeList::~TypeList()
{ {
loop_over_list(types, i) for ( const auto& type : types )
Unref(types[i]); Unref(type);
Unref(pure_type); Unref(pure_type);
} }
int TypeList::AllMatch(const BroType* t, int is_init) const int TypeList::AllMatch(const BroType* t, int is_init) const
{ {
loop_over_list(types, i) for ( const auto& type : types )
if ( ! same_type(types[i], t, is_init) ) if ( ! same_type(type, t, is_init) )
return 0; return 0;
return 1; return 1;
} }
@ -381,9 +381,8 @@ TableType::TableType(TypeList* ind, BroType* yield)
type_list* tl = indices->Types(); type_list* tl = indices->Types();
loop_over_list(*tl, i) for ( const auto& tli : *tl )
{ {
BroType* tli = (*tl)[i];
InternalTypeTag t = tli->InternalType(); InternalTypeTag t = tli->InternalType();
if ( t == TYPE_INTERNAL_ERROR ) if ( t == TYPE_INTERNAL_ERROR )
@ -704,8 +703,8 @@ RecordType::RecordType(type_decl_list* arg_types) : BroType(TYPE_RECORD)
RecordType* RecordType::ShallowClone() RecordType* RecordType::ShallowClone()
{ {
auto pass = new type_decl_list(); auto pass = new type_decl_list();
loop_over_list(*types, i) for ( const auto& type : *types )
pass->append(new TypeDecl(*(*types)[i])); pass->append(new TypeDecl(*type));
return new RecordType(pass); return new RecordType(pass);
} }
@ -713,8 +712,8 @@ RecordType::~RecordType()
{ {
if ( types ) if ( types )
{ {
loop_over_list(*types, i) for ( auto type : *types )
delete (*types)[i]; delete type;
delete types; delete types;
} }
@ -823,17 +822,15 @@ const char* RecordType::AddFields(type_decl_list* others, attr_list* attr)
if ( 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; log = true;
} }
} }
loop_over_list(*others, i) for ( const auto& td : *others )
{ {
TypeDecl* td = (*others)[i];
if ( ! td->FindAttr(ATTR_DEFAULT) && if ( ! td->FindAttr(ATTR_DEFAULT) &&
! td->FindAttr(ATTR_OPTIONAL) ) ! td->FindAttr(ATTR_OPTIONAL) )
return "extension field must be &optional or have &default"; return "extension field must be &optional or have &default";
@ -883,11 +880,11 @@ void RecordType::DescribeFields(ODesc* d) const
{ {
d->AddCount(0); d->AddCount(0);
d->AddCount(types->length()); 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->SP();
d->Add((*types)[i]->id); d->Add(type->id);
d->SP(); d->SP();
} }
} }

View file

@ -455,8 +455,7 @@ public:
const char* id; const char* id;
}; };
declare(PList,TypeDecl); typedef PList<TypeDecl> type_decl_list;
typedef PList(TypeDecl) type_decl_list;
class RecordType : public BroType { class RecordType : public BroType {
public: public:

View file

@ -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 // cut_points is a set of pairs of indices in str that should
// be removed/replaced. A pair <x,y> means "delete starting // be removed/replaced. A pair <x,y> means "delete starting
// at offset x, up to but not including offset y". // at offset x, up to but not including offset y".
List(ptr_compat_int) cut_points; // where RE matches pieces of str vector<std::pair<int, int>> cut_points;
int size = 0; // size of result int size = 0; // size of result
@ -1058,8 +1058,7 @@ Val* StringVal::Substitute(RE_Matcher* re, StringVal* repl, bool do_all)
break; break;
// s[offset .. offset+end_of_match-1] matches re. // s[offset .. offset+end_of_match-1] matches re.
cut_points.append(offset); cut_points.push_back({offset, offset + end_of_match});
cut_points.append(offset + end_of_match);
offset += end_of_match; offset += end_of_match;
n -= 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 // size now reflects amount of space copied. Factor in amount
// of space for replacement text. // of space for replacement text.
int num_cut_points = cut_points.length() / 2; size += cut_points.size() * repl->Len();
size += num_cut_points * repl->Len();
// And a final NUL for good health. // And a final NUL for good health.
++size; ++size;
@ -1086,13 +1084,13 @@ Val* StringVal::Substitute(RE_Matcher* re, StringVal* repl, bool do_all)
// Copy it all over. // Copy it all over.
int start_offset = 0; 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); memcpy(r, s + start_offset, num_to_copy);
r += num_to_copy; r += num_to_copy;
start_offset = cut_points[i+1]; start_offset = point.second;
// Now add in replacement text. // Now add in replacement text.
memcpy(r, repl->Bytes(), repl->Len()); memcpy(r, repl->Bytes(), repl->Len());
@ -1189,8 +1187,8 @@ ListVal::ListVal(TypeTag t)
ListVal::~ListVal() ListVal::~ListVal()
{ {
loop_over_list(vals, i) for ( const auto& val : vals )
Unref(vals[i]); Unref(val);
Unref(type); Unref(type);
} }
@ -1200,9 +1198,9 @@ RE_Matcher* ListVal::BuildRE() const
Internal("non-string list in ListVal::IncludedInString"); Internal("non-string list in ListVal::IncludedInString");
RE_Matcher* re = new RE_Matcher(); 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); re->AddPat(vs);
} }
@ -1231,8 +1229,8 @@ TableVal* ListVal::ConvertToSet() const
SetType* s = new SetType(set_index, 0); SetType* s = new SetType(set_index, 0);
TableVal* t = new TableVal(s); TableVal* t = new TableVal(s);
loop_over_list(vals, i) for ( const auto& val : vals )
t->Assign(vals[i], 0); t->Assign(val, 0);
Unref(s); Unref(s);
return t; return t;
@ -1269,8 +1267,8 @@ Val* ListVal::DoClone(CloneState* state)
lv->vals.resize(vals.length()); lv->vals.resize(vals.length());
state->NewClone(this, lv); state->NewClone(this, lv);
loop_over_list(vals, i) for ( const auto& val : vals )
lv->Append(vals[i]->Clone(state)); lv->Append(val->Clone(state));
return lv; return lv;
} }
@ -1278,8 +1276,8 @@ Val* ListVal::DoClone(CloneState* state)
unsigned int ListVal::MemoryAllocation() const unsigned int ListVal::MemoryAllocation() const
{ {
unsigned int size = 0; unsigned int size = 0;
loop_over_list(vals, i) for ( const auto& val : vals )
size += vals[i]->MemoryAllocation(); size += val->MemoryAllocation();
return size + padded_sizeof(*this) + vals.MemoryAllocation() - padded_sizeof(vals) return size + padded_sizeof(*this) + vals.MemoryAllocation() - padded_sizeof(vals)
+ type->MemoryAllocation(); + type->MemoryAllocation();
@ -1334,7 +1332,7 @@ void TableVal::Init(TableType* t)
subnets = 0; subnets = 0;
table_hash = new CompositeHash(table_type->Indices()); table_hash = new CompositeHash(table_type->Indices());
val.table_val = new PDict(TableEntryVal); val.table_val = new PDict<TableEntryVal>;
val.table_val->SetDeleteFunc(table_entry_val_delete_func); val.table_val->SetDeleteFunc(table_entry_val_delete_func);
} }
@ -1357,7 +1355,7 @@ void TableVal::RemoveAll()
{ {
// Here we take the brute force approach. // Here we take the brute force approach.
delete AsTable(); delete AsTable();
val.table_val = new PDict(TableEntryVal); val.table_val = new PDict<TableEntryVal>;
val.table_val->SetDeleteFunc(table_entry_val_delete_func); val.table_val->SetDeleteFunc(table_entry_val_delete_func);
} }
@ -1370,7 +1368,7 @@ int TableVal::RecursiveSize() const
!= TYPE_TABLE ) != TYPE_TABLE )
return n; return n;
PDict(TableEntryVal)* v = val.table_val; PDict<TableEntryVal>* v = val.table_val;
IterCookie* c = v->InitForIteration(); IterCookie* c = v->InitForIteration();
TableEntryVal* tv; TableEntryVal* tv;
@ -1508,7 +1506,7 @@ int TableVal::AddTo(Val* val, int is_first_init, bool propagate_ops) const
return 0; return 0;
} }
const PDict(TableEntryVal)* tbl = AsTable(); const PDict<TableEntryVal>* tbl = AsTable();
IterCookie* c = tbl->InitForIteration(); IterCookie* c = tbl->InitForIteration();
HashKey* k; HashKey* k;
@ -1556,7 +1554,7 @@ int TableVal::RemoveFrom(Val* val) const
return 0; return 0;
} }
const PDict(TableEntryVal)* tbl = AsTable(); const PDict<TableEntryVal>* tbl = AsTable();
IterCookie* c = tbl->InitForIteration(); IterCookie* c = tbl->InitForIteration();
HashKey* k; HashKey* k;
@ -1578,14 +1576,14 @@ TableVal* TableVal::Intersect(const TableVal* tv) const
{ {
TableVal* result = new TableVal(table_type); TableVal* result = new TableVal(table_type);
const PDict(TableEntryVal)* t0 = AsTable(); const PDict<TableEntryVal>* t0 = AsTable();
const PDict(TableEntryVal)* t1 = tv->AsTable(); const PDict<TableEntryVal>* t1 = tv->AsTable();
PDict(TableEntryVal)* t2 = result->AsNonConstTable(); PDict<TableEntryVal>* t2 = result->AsNonConstTable();
// Figure out which is smaller; assign it to t1. // Figure out which is smaller; assign it to t1.
if ( t1->Length() > t0->Length() ) if ( t1->Length() > t0->Length() )
{ // Swap. { // Swap.
const PDict(TableEntryVal)* tmp = t1; const PDict<TableEntryVal>* tmp = t1;
t1 = t0; t1 = t0;
t0 = tmp; t0 = tmp;
} }
@ -1607,8 +1605,8 @@ TableVal* TableVal::Intersect(const TableVal* tv) const
bool TableVal::EqualTo(const TableVal* tv) const bool TableVal::EqualTo(const TableVal* tv) const
{ {
const PDict(TableEntryVal)* t0 = AsTable(); const PDict<TableEntryVal>* t0 = AsTable();
const PDict(TableEntryVal)* t1 = tv->AsTable(); const PDict<TableEntryVal>* t1 = tv->AsTable();
if ( t0->Length() != t1->Length() ) if ( t0->Length() != t1->Length() )
return false; return false;
@ -1634,8 +1632,8 @@ bool TableVal::EqualTo(const TableVal* tv) const
bool TableVal::IsSubsetOf(const TableVal* tv) const bool TableVal::IsSubsetOf(const TableVal* tv) const
{ {
const PDict(TableEntryVal)* t0 = AsTable(); const PDict<TableEntryVal>* t0 = AsTable();
const PDict(TableEntryVal)* t1 = tv->AsTable(); const PDict<TableEntryVal>* t1 = tv->AsTable();
if ( t0->Length() > t1->Length() ) if ( t0->Length() > t1->Length() )
return false; return false;
@ -1771,8 +1769,8 @@ Val* TableVal::Default(Val* index)
{ {
const val_list* vl0 = index->AsListVal()->Vals(); const val_list* vl0 = index->AsListVal()->Vals();
vl = val_list(vl0->length()); vl = val_list(vl0->length());
loop_over_list(*vl0, i) for ( const auto& v : *vl0 )
vl.append((*vl0)[i]->Ref()); vl.append(v->Ref());
} }
else else
{ {
@ -1828,7 +1826,7 @@ Val* TableVal::Lookup(Val* index, bool use_default_val)
return def; return def;
} }
const PDict(TableEntryVal)* tbl = AsTable(); const PDict<TableEntryVal>* tbl = AsTable();
if ( tbl->Length() > 0 ) if ( tbl->Length() > 0 )
{ {
@ -1973,7 +1971,7 @@ ListVal* TableVal::ConvertToList(TypeTag t) const
{ {
ListVal* l = new ListVal(t); ListVal* l = new ListVal(t);
const PDict(TableEntryVal)* tbl = AsTable(); const PDict<TableEntryVal>* tbl = AsTable();
IterCookie* c = tbl->InitForIteration(); IterCookie* c = tbl->InitForIteration();
HashKey* k; HashKey* k;
@ -2014,7 +2012,7 @@ ListVal* TableVal::ConvertToPureList() const
void TableVal::Describe(ODesc* d) const void TableVal::Describe(ODesc* d) const
{ {
const PDict(TableEntryVal)* tbl = AsTable(); const PDict<TableEntryVal>* tbl = AsTable();
int n = tbl->Length(); int n = tbl->Length();
if ( d->IsBinary() || d->IsPortable() ) if ( d->IsBinary() || d->IsPortable() )
@ -2158,7 +2156,7 @@ void TableVal::DoExpire(double t)
if ( ! type ) if ( ! type )
return; // FIX ME ### return; // FIX ME ###
PDict(TableEntryVal)* tbl = AsNonConstTable(); PDict<TableEntryVal>* tbl = AsNonConstTable();
double timeout = GetExpireTime(); double timeout = GetExpireTime();
@ -2343,7 +2341,7 @@ Val* TableVal::DoClone(CloneState* state)
auto tv = new TableVal(table_type); auto tv = new TableVal(table_type);
state->NewClone(this, tv); state->NewClone(this, tv);
const PDict(TableEntryVal)* tbl = AsTable(); const PDict<TableEntryVal>* tbl = AsTable();
IterCookie* cookie = tbl->InitForIteration(); IterCookie* cookie = tbl->InitForIteration();
HashKey* key; HashKey* key;
@ -2392,7 +2390,7 @@ unsigned int TableVal::MemoryAllocation() const
{ {
unsigned int size = 0; unsigned int size = 0;
PDict(TableEntryVal)* v = val.table_val; PDict<TableEntryVal>* v = val.table_val;
IterCookie* c = v->InitForIteration(); IterCookie* c = v->InitForIteration();
TableEntryVal* tv; TableEntryVal* tv;
@ -2675,9 +2673,9 @@ Val* RecordVal::DoClone(CloneState* state)
rv->origin = nullptr; rv->origin = nullptr;
state->NewClone(this, rv); 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); rv->val.val_list_val->append(v);
} }
@ -2687,12 +2685,10 @@ Val* RecordVal::DoClone(CloneState* state)
unsigned int RecordVal::MemoryAllocation() const unsigned int RecordVal::MemoryAllocation() const
{ {
unsigned int size = 0; unsigned int size = 0;
const val_list* vl = AsRecord(); const val_list* vl = AsRecord();
loop_over_list(*vl, i) for ( const auto& v : *vl )
{ {
Val* v = (*vl)[i];
if ( v ) if ( v )
size += v->MemoryAllocation(); size += v->MemoryAllocation();
} }
@ -3070,8 +3066,8 @@ void delete_vals(val_list* vals)
{ {
if ( vals ) if ( vals )
{ {
loop_over_list(*vals, i) for ( const auto& val : *vals )
Unref((*vals)[i]); Unref(val);
delete vals; delete vals;
} }
} }

View file

@ -55,7 +55,6 @@ class StateAccess;
class VectorVal; class VectorVal;
class TableEntryVal; class TableEntryVal;
declare(PDict,TableEntryVal);
typedef union { typedef union {
// Used for bool, int, enum. // Used for bool, int, enum.
@ -77,7 +76,7 @@ typedef union {
Func* func_val; Func* func_val;
BroFile* file_val; BroFile* file_val;
RE_Matcher* re_val; RE_Matcher* re_val;
PDict(TableEntryVal)* table_val; PDict<TableEntryVal>* table_val;
val_list* val_list_val; val_list* val_list_val;
vector<Val*>* vector_val; vector<Val*>* vector_val;
@ -228,7 +227,7 @@ public:
CONST_ACCESSOR2(TYPE_ENUM, int, int_val, AsEnum) CONST_ACCESSOR2(TYPE_ENUM, int, int_val, AsEnum)
CONST_ACCESSOR(TYPE_STRING, BroString*, string_val, AsString) CONST_ACCESSOR(TYPE_STRING, BroString*, string_val, AsString)
CONST_ACCESSOR(TYPE_FUNC, Func*, func_val, AsFunc) CONST_ACCESSOR(TYPE_FUNC, Func*, func_val, AsFunc)
CONST_ACCESSOR(TYPE_TABLE, PDict(TableEntryVal)*, table_val, AsTable) CONST_ACCESSOR(TYPE_TABLE, PDict<TableEntryVal>*, table_val, AsTable)
CONST_ACCESSOR(TYPE_RECORD, val_list*, val_list_val, AsRecord) CONST_ACCESSOR(TYPE_RECORD, val_list*, val_list_val, AsRecord)
CONST_ACCESSOR(TYPE_FILE, BroFile*, file_val, AsFile) CONST_ACCESSOR(TYPE_FILE, BroFile*, file_val, AsFile)
CONST_ACCESSOR(TYPE_PATTERN, RE_Matcher*, re_val, AsPattern) CONST_ACCESSOR(TYPE_PATTERN, RE_Matcher*, re_val, AsPattern)
@ -400,7 +399,7 @@ protected:
#endif #endif
} }
ACCESSOR(TYPE_TABLE, PDict(TableEntryVal)*, table_val, AsNonConstTable) ACCESSOR(TYPE_TABLE, PDict<TableEntryVal>*, table_val, AsNonConstTable)
ACCESSOR(TYPE_RECORD, val_list*, val_list_val, AsNonConstRecord) ACCESSOR(TYPE_RECORD, val_list*, val_list_val, AsNonConstRecord)
// For internal use by the Val::Clone() methods. // For internal use by the Val::Clone() methods.

View file

@ -448,10 +448,8 @@ void end_func(Stmt* body)
if ( attrs ) if ( attrs )
{ {
loop_over_list(*attrs, i) for ( const auto& a : *attrs )
{ {
Attr* a = (*attrs)[i];
if ( a->Tag() == ATTR_DEPRECATED ) if ( a->Tag() == ATTR_DEPRECATED )
continue; continue;

View file

@ -728,11 +728,11 @@ void Analyzer::CancelTimers()
// traversing. Thus, we first make a copy of the list which we then // traversing. Thus, we first make a copy of the list which we then
// iterate through. // iterate through.
timer_list tmp(timers.length()); timer_list tmp(timers.length());
loop_over_list(timers, j) std::copy(timers.begin(), timers.end(), back_inserter(tmp));
tmp.append(timers[j]);
loop_over_list(tmp, i) // TODO: could be a for_each
Conn()->GetTimerMgr()->Cancel(tmp[i]); for ( auto timer : tmp )
Conn()->GetTimerMgr()->Cancel(timer);
timers_canceled = 1; timers_canceled = 1;
timers.clear(); timers.clear();

View file

@ -84,7 +84,6 @@ protected:
}; };
// declare(PList, MIME_Header);
typedef vector<MIME_Header*> MIME_HeaderList; typedef vector<MIME_Header*> MIME_HeaderList;
class MIME_Entity { class MIME_Entity {

View file

@ -99,8 +99,6 @@ protected:
Val* v; // single (perhaps compound) value corresponding to call Val* v; // single (perhaps compound) value corresponding to call
}; };
declare(PDict,RPC_CallInfo);
class RPC_Interpreter { class RPC_Interpreter {
public: public:
explicit RPC_Interpreter(analyzer::Analyzer* analyzer); explicit RPC_Interpreter(analyzer::Analyzer* analyzer);
@ -125,7 +123,7 @@ protected:
void Weird(const char* name, const char* addl = ""); void Weird(const char* name, const char* addl = "");
PDict(RPC_CallInfo) calls; PDict<RPC_CallInfo> calls;
analyzer::Analyzer* analyzer; analyzer::Analyzer* analyzer;
}; };

View file

@ -77,11 +77,10 @@ int SteppingStoneEndpoint::DataSent(double t, uint64 seq, int len, int caplen,
while ( stp_manager->OrderedEndpoints().length() > 0 ) 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(); e->Done();
Unref(e); Unref(e);
@ -109,9 +108,8 @@ int SteppingStoneEndpoint::DataSent(double t, uint64 seq, int len, int caplen,
stp_last_time = stp_resume_time = t; stp_last_time = stp_resume_time = t;
Event(stp_resume_endp, stp_id); 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() ) if ( ep->endp->TCP() != endp->TCP() )
{ {
Ref(ep); Ref(ep);

View file

@ -13,9 +13,6 @@ namespace analyzer { namespace stepping_stone {
class SteppingStoneEndpoint; class SteppingStoneEndpoint;
class SteppingStoneManager; class SteppingStoneManager;
declare(PQueue,SteppingStoneEndpoint);
declare(PDict,SteppingStoneEndpoint);
class SteppingStoneEndpoint : public BroObj { class SteppingStoneEndpoint : public BroObj {
public: public:
SteppingStoneEndpoint(tcp::TCP_Endpoint* e, SteppingStoneManager* m); SteppingStoneEndpoint(tcp::TCP_Endpoint* e, SteppingStoneManager* m);
@ -41,8 +38,8 @@ protected:
// no LOOP in Bro language. // no LOOP in Bro language.
int stp_id; int stp_id;
HashKey* stp_key; HashKey* stp_key;
PDict(SteppingStoneEndpoint) stp_inbound_endps; PDict<SteppingStoneEndpoint> stp_inbound_endps;
PDict(SteppingStoneEndpoint) stp_outbound_endps; PDict<SteppingStoneEndpoint> stp_outbound_endps;
}; };
class SteppingStone_Analyzer : public tcp::TCP_ApplicationAnalyzer { class SteppingStone_Analyzer : public tcp::TCP_ApplicationAnalyzer {
@ -76,14 +73,14 @@ class SteppingStoneManager {
public: public:
SteppingStoneManager() { endp_cnt = 0; } SteppingStoneManager() { endp_cnt = 0; }
PQueue(SteppingStoneEndpoint)& OrderedEndpoints() PQueue<SteppingStoneEndpoint>& OrderedEndpoints()
{ return ordered_endps; } { return ordered_endps; }
// Use postfix ++, since the first ID needs to be even. // Use postfix ++, since the first ID needs to be even.
int NextID() { return endp_cnt++; } int NextID() { return endp_cnt++; }
protected: protected:
PQueue(SteppingStoneEndpoint) ordered_endps; PQueue<SteppingStoneEndpoint> ordered_endps;
int endp_cnt; int endp_cnt;
}; };

View file

@ -1054,8 +1054,8 @@ void Manager::ProcessEvent(const broker::topic& topic, broker::zeek::Event ev)
mgr.QueueEventFast(handler, std::move(vl), SOURCE_BROKER); mgr.QueueEventFast(handler, std::move(vl), SOURCE_BROKER);
else else
{ {
loop_over_list(vl, i) for ( const auto& v : vl )
Unref(vl[i]); Unref(v);
} }
} }

View file

@ -28,7 +28,7 @@ std::set<std::string> val_to_topic_set(Val* val)
rval.emplace(val->AsString()->CheckString()); rval.emplace(val->AsString()->CheckString());
else else
{ {
const PDict(TableEntryVal)* tbl = val->AsTable(); const PDict<TableEntryVal>* tbl = val->AsTable();
if ( tbl->Length() == 0 ) if ( tbl->Length() == 0 )
return rval; return rval;

View file

@ -14,7 +14,6 @@
namespace file_analysis { namespace file_analysis {
class File; class File;
declare(PDict,Analyzer);
/** /**
* A set of file analysis analyzers indexed by an \c AnalyzerArgs (script-layer * 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 */ File* file; /**< File which owns the set */
CompositeHash* analyzer_hash; /**< AnalyzerArgs hashes. */ CompositeHash* analyzer_hash; /**< AnalyzerArgs hashes. */
PDict(file_analysis::Analyzer) analyzer_map; /**< Indexed by AnalyzerArgs. */ PDict<file_analysis::Analyzer> analyzer_map; /**< Indexed by AnalyzerArgs. */
/** /**
* Abstract base class for analyzer set modifications. * Abstract base class for analyzer set modifications.

View file

@ -26,9 +26,6 @@
namespace file_analysis { namespace file_analysis {
declare(PDict,bool);
declare(PDict,File);
/** /**
* Main entry point for interacting with file analysis. * Main entry point for interacting with file analysis.
*/ */
@ -339,8 +336,8 @@ public:
protected: protected:
friend class FileTimer; friend class FileTimer;
typedef PDict(bool) IDSet; typedef PDict<bool> IDSet;
typedef PDict(File) IDMap; typedef PDict<File> IDMap;
/** /**
* Create a new file to be analyzed or retrieve an existing one. * 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); TagSet* LookupMIMEType(const string& mtype, bool add_if_not_found);
PDict(File) id_map; /**< Map file ID to file_analysis::File records. */ PDict<File> id_map; /**< Map file ID to file_analysis::File records. */
PDict(bool) ignored; /**< Ignored files. Will be finally removed on EOF. */ PDict<bool> ignored; /**< Ignored files. Will be finally removed on EOF. */
string current_file_id; /**< Hash of what get_file_handle event sets. */ string current_file_id; /**< Hash of what get_file_handle event sets. */
RuleFileMagicState* magic_state; /**< File magic signature match state. */ RuleFileMagicState* magic_state; /**< File magic signature match state. */
MIMEMap mime_types;/**< Mapping of MIME types to analyzers. */ MIMEMap mime_types;/**< Mapping of MIME types to analyzers. */

View file

@ -52,8 +52,6 @@ static void input_hash_delete_func(void* val)
delete h; delete h;
} }
declare(PDict, InputHash);
/** /**
* Base stuff that every stream can do. * Base stuff that every stream can do.
*/ */
@ -109,8 +107,8 @@ public:
RecordType* rtype; RecordType* rtype;
RecordType* itype; RecordType* itype;
PDict(InputHash)* currDict; PDict<InputHash>* currDict;
PDict(InputHash)* lastDict; PDict<InputHash>* lastDict;
Func* pred; Func* pred;
@ -703,9 +701,9 @@ bool Manager::CreateTableStream(RecordVal* fval)
stream->itype = idx->AsRecordType(); stream->itype = idx->AsRecordType();
stream->event = event ? event_registry->Lookup(event->Name()) : 0; stream->event = event ? event_registry->Lookup(event->Name()) : 0;
stream->error_event = error_event ? event_registry->Lookup(error_event->Name()) : nullptr; stream->error_event = error_event ? event_registry->Lookup(error_event->Name()) : nullptr;
stream->currDict = new PDict(InputHash); stream->currDict = new PDict<InputHash>;
stream->currDict->SetDeleteFunc(input_hash_delete_func); stream->currDict->SetDeleteFunc(input_hash_delete_func);
stream->lastDict = new PDict(InputHash); stream->lastDict = new PDict<InputHash>;
stream->lastDict->SetDeleteFunc(input_hash_delete_func); stream->lastDict->SetDeleteFunc(input_hash_delete_func);
stream->want_record = ( want_record->InternalInt() == 1 ); stream->want_record = ( want_record->InternalInt() == 1 );
@ -1425,7 +1423,7 @@ void Manager::EndCurrentSend(ReaderFrontend* reader)
delete(stream->lastDict); delete(stream->lastDict);
stream->lastDict = stream->currDict; stream->lastDict = stream->currDict;
stream->currDict = new PDict(InputHash); stream->currDict = new PDict<InputHash>;
stream->currDict->SetDeleteFunc(input_hash_delete_func); stream->currDict->SetDeleteFunc(input_hash_delete_func);
#ifdef DEBUG #ifdef DEBUG
@ -1895,8 +1893,8 @@ bool Manager::SendEvent(ReaderFrontend* reader, const string& name, const int nu
if ( convert_error ) if ( convert_error )
{ {
loop_over_list(vl, i) for ( const auto& v : vl )
Unref(vl[i]); Unref(v);
return false; return false;
} }

View file

@ -10,8 +10,6 @@
#include "Dict.h" #include "Dict.h"
#include "Packet.h" #include "Packet.h"
declare(PDict,BPF_Program);
namespace iosource { namespace iosource {
/** /**

View file

@ -164,11 +164,8 @@ static type_decl_list* copy_type_decl_list(type_decl_list* tdl)
type_decl_list* rval = new type_decl_list(); type_decl_list* rval = new type_decl_list();
loop_over_list(*tdl, i) for ( const auto& td : *tdl )
{
TypeDecl* td = (*tdl)[i];
rval->append(new TypeDecl(*td)); rval->append(new TypeDecl(*td));
}
return rval; return rval;
} }
@ -180,9 +177,8 @@ static attr_list* copy_attr_list(attr_list* al)
attr_list* rval = new attr_list(); attr_list* rval = new attr_list();
loop_over_list(*al, i) for ( const auto& a : *al )
{ {
Attr* a = (*al)[i];
::Ref(a); ::Ref(a);
rval->append(a); rval->append(a);
} }

View file

@ -40,7 +40,7 @@ HashKey* TopkVal::GetHash(Val* v) const
TopkVal::TopkVal(uint64 arg_size) : OpaqueVal(topk_type) TopkVal::TopkVal(uint64 arg_size) : OpaqueVal(topk_type)
{ {
elementDict = new PDict(Element); elementDict = new PDict<Element>;
elementDict->SetDeleteFunc(topk_element_hash_delete_func); elementDict->SetDeleteFunc(topk_element_hash_delete_func);
size = arg_size; size = arg_size;
type = 0; type = 0;
@ -51,7 +51,7 @@ TopkVal::TopkVal(uint64 arg_size) : OpaqueVal(topk_type)
TopkVal::TopkVal() : OpaqueVal(topk_type) TopkVal::TopkVal() : OpaqueVal(topk_type)
{ {
elementDict = new PDict(Element); elementDict = new PDict<Element>;
elementDict->SetDeleteFunc(topk_element_hash_delete_func); elementDict->SetDeleteFunc(topk_element_hash_delete_func);
size = 0; size = 0;
type = 0; type = 0;

View file

@ -33,8 +33,6 @@ struct Element {
~Element(); ~Element();
}; };
declare(PDict, Element);
class TopkVal : public OpaqueVal { class TopkVal : public OpaqueVal {
public: public:
@ -168,7 +166,7 @@ private:
BroType* type; BroType* type;
CompositeHash* hash; CompositeHash* hash;
std::list<Bucket*> buckets; std::list<Bucket*> buckets;
PDict(Element)* elementDict; PDict<Element>* elementDict;
uint64 size; // how many elements are we tracking? uint64 size; // how many elements are we tracking?
uint64 numElements; // how many elements do we have at the moment uint64 numElements; // how many elements do we have at the moment
bool pruned; // was this data structure pruned? bool pruned; // was this data structure pruned?

View file

@ -126,8 +126,7 @@ public:
// A stack of input buffers we're scanning. file_stack[len-1] is the // A stack of input buffers we're scanning. file_stack[len-1] is the
// top of the stack. // top of the stack.
declare(PList,FileInfo); static PList<FileInfo> file_stack;
static PList(FileInfo) file_stack;
#define RET_CONST(v) \ #define RET_CONST(v) \
{ \ { \
@ -757,7 +756,7 @@ void do_atif(Expr* expr)
if ( ! val->AsBool() ) if ( ! val->AsBool() )
{ {
if_stack.append(current_depth); if_stack.push_back(current_depth);
BEGIN(IGNORE); BEGIN(IGNORE);
} }
} }
@ -770,7 +769,7 @@ void do_atifdef(const char* id)
if ( ! (i = lookup_ID(id, current_module.c_str())) ) if ( ! (i = lookup_ID(id, current_module.c_str())) )
{ {
if_stack.append(current_depth); if_stack.push_back(current_depth);
BEGIN(IGNORE); BEGIN(IGNORE);
} }
@ -785,7 +784,7 @@ void do_atifndef(const char *id)
if ( (i = lookup_ID(id, current_module.c_str())) ) if ( (i = lookup_ID(id, current_module.c_str())) )
{ {
if_stack.append(current_depth); if_stack.push_back(current_depth);
BEGIN(IGNORE); BEGIN(IGNORE);
} }
@ -797,17 +796,17 @@ void do_atelse()
if ( current_depth == 0 ) if ( current_depth == 0 )
reporter->Error("@else without @if..."); reporter->Error("@else without @if...");
if ( if_stack.length() && current_depth > if_stack.last() ) if ( ! if_stack.empty() && current_depth > if_stack.back() )
return; return;
if ( YY_START == INITIAL ) if ( YY_START == INITIAL )
{ {
if_stack.append(current_depth); if_stack.push_back(current_depth);
BEGIN(IGNORE); BEGIN(IGNORE);
} }
else else
{ {
if_stack.get(); if_stack.pop_back();
BEGIN(INITIAL); BEGIN(INITIAL);
} }
} }
@ -817,10 +816,10 @@ void do_atendif()
if ( current_depth == 0 ) if ( current_depth == 0 )
reporter->Error("unbalanced @if... @endif"); reporter->Error("unbalanced @if... @endif");
if ( current_depth == if_stack.last() ) if ( current_depth == if_stack.back() )
{ {
BEGIN(INITIAL); BEGIN(INITIAL);
if_stack.get(); if_stack.pop_back();
} }
--current_depth; --current_depth;
@ -901,6 +900,7 @@ int yywrap()
yy_delete_buffer(YY_CURRENT_BUFFER); yy_delete_buffer(YY_CURRENT_BUFFER);
if ( file_stack.length() > 0 )
delete file_stack.remove_nth(file_stack.length() - 1); delete file_stack.remove_nth(file_stack.length() - 1);
if ( YY_CURRENT_BUFFER ) if ( YY_CURRENT_BUFFER )

View file

@ -61,15 +61,15 @@ function levenshtein_distance%(s1: string, s2: string%): count
function string_cat%(...%): string function string_cat%(...%): string
%{ %{
int n = 0; int n = 0;
loop_over_list(@ARG@, i) for ( const auto& a : @ARG@ )
n += @ARG@[i]->AsString()->Len(); n += a->AsString()->Len();
u_char* b = new u_char[n+1]; u_char* b = new u_char[n+1];
BroString* s = new BroString(1, b, n); 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()); memcpy(b, s->Bytes(), s->Len());
b += 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; return a;
} }
%%} %%}
## Splits a string into an array of strings according to a pattern. ## Splits a string into an array of strings according to a pattern.

View file

@ -1001,11 +1001,12 @@ string bro_prefixes()
{ {
string rval; string rval;
loop_over_list(prefixes, j) for ( const auto& prefix : prefixes )
if ( j == 0 ) {
rval.append(prefixes[j]); if ( ! rval.empty() )
else rval.append(":");
rval.append(":").append(prefixes[j]); rval.append(prefix);
}
return rval; return rval;
} }

View file

@ -1566,8 +1566,8 @@ function cat%(...%): string
ODesc d; ODesc d;
d.SetStyle(RAW_STYLE); d.SetStyle(RAW_STYLE);
loop_over_list(@ARG@, i) for ( const auto& a : @ARG@ )
@ARG@[i]->Describe(&d); a->Describe(&d);
BroString* s = new BroString(1, d.TakeBytes(), d.Len()); BroString* s = new BroString(1, d.TakeBytes(), d.Len());
s->SetUseFreeToDelete(true); s->SetUseFreeToDelete(true);
@ -1898,7 +1898,7 @@ function reading_traces%(%): bool
function global_sizes%(%): var_sizes function global_sizes%(%): var_sizes
%{ %{
TableVal* sizes = new TableVal(var_sizes); TableVal* sizes = new TableVal(var_sizes);
PDict(ID)* globals = global_scope()->Vars(); PDict<ID>* globals = global_scope()->Vars();
IterCookie* c = globals->InitForIteration(); IterCookie* c = globals->InitForIteration();
ID* id; ID* id;
@ -1925,7 +1925,7 @@ function global_sizes%(%): var_sizes
function global_ids%(%): id_table function global_ids%(%): id_table
%{ %{
TableVal* ids = new TableVal(id_table); TableVal* ids = new TableVal(id_table);
PDict(ID)* globals = global_scope()->Vars(); PDict<ID>* globals = global_scope()->Vars();
IterCookie* c = globals->InitForIteration(); IterCookie* c = globals->InitForIteration();
ID* id; ID* id;