Browse Source

preparing for nightly

master
despair 5 years ago
parent
commit
6809d5b40b
  1. 43
      src/CFGFileParser.cpp
  2. 34
      src/CFGFileParser.h
  3. 31
      src/TLSFAlloc.h

43
src/CFGFileParser.cpp

@ -28,8 +28,8 @@ void CFGFileParser::ParseText() { @@ -28,8 +28,8 @@ void CFGFileParser::ParseText() {
// reallocate as needed.
char *tmp, *token;
size_t pass1_length = 0;
// Second pass. All comments and .tags stripped out. Starts off at 4KB, increases if necessary.
char* directives = static_cast<char*>(tlsf_calloc(4096, sizeof(char)));
// Second pass. All comments and .tags stripped out. Starts off at 512 bytes, increases if necessary.
char* directives = static_cast<char*>(tlsf_calloc(512, sizeof(char)));
token = strtok_r(buffer, "\n", &tmp);
while (tmp != nullptr) {
if (token[0] == '#' || token[0] == '.' || token[0] == '\n'){ // Comment, Perl directive, or single <LF> found, skip over
@ -37,7 +37,7 @@ void CFGFileParser::ParseText() { @@ -37,7 +37,7 @@ void CFGFileParser::ParseText() {
}
else { // Config directive found, add to second-pass buffer
// once we reach the 4KB mark, realloc exactly enough to keep going
if (pass1_length >= 4096){
if (pass1_length >= 512){
tlsf_realloc(directives, pass1_length+strlen(token)+2);
}
strcat(directives, token);
@ -47,12 +47,12 @@ void CFGFileParser::ParseText() { @@ -47,12 +47,12 @@ void CFGFileParser::ParseText() {
}
}
// First pass complete, second pass: break up into single directives and <key, value> pairs
tmp = reinterpret_cast<char*>(45); // just some random junk to avoid breaking out
char *key, *value;
tmp = reinterpret_cast<char*>(45); // just some random junk to avoid breaking out in the first iteration
ntr::fast_string key, value;
while (tmp != nullptr){
key = strtok_r(directives, ":", &tmp);
value = strtok_r(nullptr, "\t", &tmp);
//cfg->Settings.insert({key, value});
key = strtok_r(directives, ":", &tmp);
value = strtok_r(nullptr, "\t", &tmp);
cfg->Settings.insert({key,value});
}
}
@ -61,29 +61,4 @@ void CFGFileParser::ParseText() { @@ -61,29 +61,4 @@ void CFGFileParser::ParseText() {
// object supplied by the user.
void CFGFileParser::WriteConfig(BrowserConfiguration &config){
config.Settings = cfg->Settings;
}
namespace ntr{
size_t string_hash(const fast_string &str){
size_t out = 0;
// instead of tying this to i386/amd64, because someone is eventually going to
// port this to ARM or some other RISC nonsense
#if UINTPTR_MAX == 0xffffffff
MurmurHash3_x86_128 ( &str, sizeof(str), 7904542L, &out );
#elif UINTPTR_MAX == 0xffffffffffffffff
MurmurHash3_x64_128 ( &str, sizeof(str), 5484754L, &out );
#else
#endif
return out;
}
bool fast_string_compare(fast_string t1, fast_string t2){
int i = strcmp(t1.c_str(), t2.c_str());
if (!i){
return true;
}
else{
return false;
}
}
}
}

34
src/CFGFileParser.h

@ -3,20 +3,37 @@ @@ -3,20 +3,37 @@
#include <string>
#include <unordered_map>
#include <utility>
#include <functional>
#include <algorithm>
#include "TLSFAlloc.h"
#include "Murmur3.h"
#include "tlsf.h"
#include <sys/stat.h>
#include <cstdint>
#include "Murmur3.h"
// let's try fast strings
namespace ntr{
typedef std::basic_string<char, std::char_traits<char>, TLSFAlloc<char>> fast_string;
size_t string_hash(const fast_string &str);
bool fast_string_compare(fast_string t1, fast_string t2);
typedef std::unordered_map<fast_string,fast_string,std::function<decltype(string_hash)>,std::function<decltype(fast_string_compare)>,TLSFAlloc<std::pair<const fast_string, fast_string>>> stringmap;
typedef std::basic_string<char, std::char_traits<char>, TLSFAlloc<char>> fast_string;
class StringHash{
public:
std::size_t operator() (const fast_string& s) const {
size_t out = 0;
// 32-bit hash only, all others overflow. RIP AMD64 users...
MurmurHash3_x86_32 ( &s, sizeof(s), 7904542L, &out );
return out;
}
};
class StringComparison{
public:
bool operator ()(const fast_string &a, const fast_string &b) const {
int i = strcmp(a.c_str(), b.c_str());
if (!i){
return true;
}
else{
return false;
}
}
};
typedef std::unordered_map<fast_string,fast_string,StringHash,StringComparison,TLSFAlloc<std::pair<const fast_string, fast_string>>> stringmap;
}
// Class which holds the parsed configuration options after
@ -54,7 +71,8 @@ private: @@ -54,7 +71,8 @@ private:
FILE *cfg_file;
char *buffer;
struct stat *cfg_fileinfo = reinterpret_cast<struct stat*>(tlsf_malloc(sizeof(struct stat)));
BrowserConfiguration *cfg;
BrowserConfiguration *cfg = new BrowserConfiguration();
//std::pair<ntr::fast_string,ntr::fast_string>
size_t bytesRead;
};
#endif // __CFGFILEPARSER_H__

31
src/TLSFAlloc.h

@ -11,22 +11,22 @@ namespace ntr { @@ -11,22 +11,22 @@ namespace ntr {
class TLSFAlloc {
public:
typedef T value_type;
typedef T* ptr;
typedef const T* const_ptr;
typedef T& ref;
typedef const T& const_ref;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t diff_type;
typedef std::ptrdiff_t difference_type;
template <class U>
struct rebind {
typedef TLSFAlloc<U> other;
};
ptr address (ref value) const {
pointer address (reference value) const {
return &value;
}
const_ptr address (const_ref value) const {
const_pointer address (const_reference value) const {
return &value;
}
@ -38,7 +38,7 @@ namespace ntr { @@ -38,7 +38,7 @@ namespace ntr {
~TLSFAlloc() throw() {}
// placement new
void* operator new (std::size_t size, void*) {
static void* operator new (std::size_t size, void*) {
// because this is a static, it can't access this->allocate
// so I've jsut copied allocate here
#ifdef DEBUG_TLSF_CPP
@ -47,13 +47,13 @@ namespace ntr { @@ -47,13 +47,13 @@ namespace ntr {
ptr ret = (ptr)(tlsf_malloc(size * sizeof(T)));
std::cerr << " allocated at: " << (void*)ret << std::endl;
#else
ptr ret = static_cast<ptr>(tlsf_malloc(size * sizeof(T)));
pointer ret = static_cast<pointer>(tlsf_malloc(size * sizeof(T)));
#endif
return ret;
}
// destroy elements of initialized storage p
void static destroy (ptr p) {
void destroy (pointer p) {
p->~T();
}
@ -63,25 +63,26 @@ namespace ntr { @@ -63,25 +63,26 @@ namespace ntr {
}
// allocate but don't initialize num elements of type T
ptr allocate(size_type num, const void* = 0) {
pointer allocate(size_type num, const void* = 0) {
#ifdef DEBUG_TLSF_CPP
// print message and allocate memory with 2LSF
std::cerr << "allocate " << num << " element(s)" << " of size " << sizeof(T) << std::endl;
ptr ret = (ptr)(tlsf_malloc(num*sizeof(T)));
pointer ret = (pointer)(tlsf_malloc(num*sizeof(T)));
std::cerr << " allocated at: " << (void*)ret << std::endl; return ret;
#else
ptr ret = static_cast<ptr>(tlsf_malloc(num*sizeof(T))); return ret;
pointer ret = static_cast<pointer>(tlsf_malloc(num*sizeof(T)));
return ret;
#endif
}
// initialize elements of allocated storage p with value value
// using placement new (defined here)
void construct (ptr p, const T& value) {
void construct (pointer p, const T& value) {
new (reinterpret_cast<void*>(p))T(value);
}
// deallocate storage p of deleted elements
void deallocate(ptr p, size_type num) {
void deallocate(pointer p, size_type num) {
#ifdef DEBUG_TLSF_CPP
// print message and deallocate memory with global delete
std::cerr << "deallocate " << num << " element(s)" << " of size " << sizeof(T) << " at: " << (void*)p << std::endl;

Loading…
Cancel
Save