Browse Source

new header

master
despair 5 years ago
parent
commit
d277e7730f
  1. 3
      src/CFGFileParser.cpp
  2. 34
      src/CFGFileParser.h
  3. 97
      src/TLSFAlloc.h
  4. 18
      src/main.cpp
  5. 124
      src/netrunner.h

3
src/CFGFileParser.cpp

@ -1,6 +1,5 @@ @@ -1,6 +1,5 @@
#include "CFGFileParser.h"
#include <climits>
#include <string.h>
CFGFileParser::CFGFileParser(const char* filename){
// Open the config file, get its size,
@ -51,8 +50,8 @@ void CFGFileParser::ParseText() { @@ -51,8 +50,8 @@ void CFGFileParser::ParseText() {
// how about an array of keys, and of values? where key[i] and value[i] are correspondent
while (tmp != nullptr){
// separate key and value here, will write up in the morning
}
}
}
// Writes the fully parsed configuration to a BrowserConfiguration

34
src/CFGFileParser.h

@ -1,40 +1,8 @@ @@ -1,40 +1,8 @@
#ifndef __CFGFILEPARSER_H__
#define __CFGFILEPARSER_H__
#include <string>
#include <unordered_map>
#include <utility>
#include <algorithm>
#include "TLSFAlloc.h"
#include "netrunner.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;
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
// being processed by CFGFileParser. No longer a "trivial class"

97
src/TLSFAlloc.h

@ -1,97 +0,0 @@ @@ -1,97 +0,0 @@
// This is a custom C++ allocator that wraps the 2LSF C heap allocator
// -despair
// in case of emergency, set DEBUG_TLSF_CPP somewhere before including this header
#include <limits>
#include <iostream>
#include "tlsf.h"
namespace ntr {
template <class T>
class TLSFAlloc {
public:
typedef T value_type;
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 difference_type;
template <class U>
struct rebind {
typedef TLSFAlloc<U> other;
};
pointer address (reference value) const {
return &value;
}
const_pointer address (const_reference value) const {
return &value;
}
// these are the constructors for same: had I not ported sbrk(2) to winnt,
// the constructor would have generated a memory pool from above.
TLSFAlloc() throw() {}
TLSFAlloc(const TLSFAlloc&) throw() {}
template <class U> TLSFAlloc(const TLSFAlloc<U>&) throw() {}
~TLSFAlloc() throw() {}
// placement new
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
// print message and allocate memory with 2LSF
std::cerr << "allocate " << size << " element(s)" << " of size " << sizeof(T) << std::endl;
ptr ret = (ptr)(tlsf_malloc(size * sizeof(T)));
std::cerr << " allocated at: " << (void*)ret << std::endl;
#else
pointer ret = static_cast<pointer>(tlsf_malloc(size * sizeof(T)));
#endif
return ret;
}
// destroy elements of initialized storage p
void destroy (pointer p) {
p->~T();
}
// return maximum number of elements that can be allocated
size_type max_size() const throw() {
return std::numeric_limits<std::size_t>::max() / sizeof(T);
}
// allocate but don't initialize num elements of type T
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;
pointer ret = (pointer)(tlsf_malloc(num*sizeof(T)));
std::cerr << " allocated at: " << (void*)ret << std::endl; return ret;
#else
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 (pointer p, const T& value) {
new (reinterpret_cast<void*>(p))T(value);
}
// deallocate storage p of deleted elements
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;
#endif
tlsf_free(reinterpret_cast<void*>(p));
}
};
// return that all specializations of this allocator are interchangeable
template <class T1, class T2> bool operator== (const TLSFAlloc<T1>&,const TLSFAlloc<T2>&) throw() { return true; }
template <class T1, class T2> bool operator!= (const TLSFAlloc<T1>&, const TLSFAlloc<T2>&) throw() { return false; }
}

18
src/main.cpp

@ -108,10 +108,24 @@ int main(int argc, char *argv[]) { @@ -108,10 +108,24 @@ int main(int argc, char *argv[]) {
#endif
#if defined(VERSION) && defined(PLATFORM)
std::cout << "# # ######" << std::endl << "## # ###### ##### # # # # # # # # ###### #####" << std::endl << "# # # # # # # # # ## # ## # # # #" << std::endl << "# # # ##### # ###### # # # # # # # # ##### # #" << std::endl << "# # # # # # # # # # # # # # # # #####" << std::endl << "# ## # # # # # # # ## # ## # # #" << std::endl << "# # ###### # # # #### # # # # ###### # #" << std::endl;
std::cout \
<< "# # ######" << std::endl \
<< "## # ###### ##### # # # # # # # # ###### #####" << std::endl \
<< "# # # # # # # # # ## # ## # # # #" << std::endl \
<< "# # # ##### # ###### # # # # # # # # ##### # #" << std::endl \
<< "# # # # # # # # # # # # # # # # #####" << std::endl \
<< "# ## # # # # # # # ## # ## # # #" << std::endl \
<< "# # ###### # # # #### # # # # ###### # #" << std::endl;
std::cout << "/g/ntr - NetRunner build " << __DATE__ << ": rev-" << VERSION << " for " << PLATFORM << std::endl;
#else
std::cout << "# # ######" << std::endl << "## # ###### ##### # # # # # # # # ###### #####" << std::endl << "# # # # # # # # # ## # ## # # # #" << std::endl << "# # # ##### # ###### # # # # # # # # ##### # #" << std::endl << "# # # # # # # # # # # # # # # # #####" << std::endl << "# ## # # # # # # # ## # ## # # #" << std::endl << "# # ###### # # # #### # # # # ###### # #" << std::endl;
std::cout \
<< "# # ######" << std::endl \
<< "## # ###### ##### # # # # # # # # ###### #####" << std::endl \
<< "# # # # # # # # # ## # ## # # # #" << std::endl \
<< "# # # ##### # ###### # # # # # # # # ##### # #" << std::endl \
<< "# # # # # # # # # # # # # # # # #####" << std::endl \
<< "# ## # # # # # # # ## # ## # # #" << std::endl \
<< "# # ###### # # # #### # # # # ###### # #" << std::endl;
std::cout << "/g/ntr - NetRunner build " << __DATE__ << ": internal-dev non-Makefile build" << std::endl;
#endif
Environment::init();

124
src/netrunner.h

@ -0,0 +1,124 @@ @@ -0,0 +1,124 @@
#include "Murmur3.h"
#include <string>
#include <unordered_map>
#include <utility>
#include <algorithm>
#include <limits>
#include <iostream>
#include <cstring>
#include "tlsf.h"
// let's try fast strings
namespace ntr{
template <class T>
class TLSFAlloc {
public:
typedef T value_type;
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 difference_type;
template <class U>
struct rebind {
typedef TLSFAlloc<U> other;
};
pointer address (reference value) const {
return &value;
}
const_pointer address (const_reference value) const {
return &value;
}
// these are the constructors for same: had I not ported sbrk(2) to winnt,
// the constructor would have generated a memory pool from above.
TLSFAlloc() throw() {}
TLSFAlloc(const TLSFAlloc&) throw() {}
template <class U> TLSFAlloc(const TLSFAlloc<U>&) throw() {}
~TLSFAlloc() throw() {}
// placement new
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
// print message and allocate memory with 2LSF
std::cerr << "allocate " << size << " element(s)" << " of size " << sizeof(T) << std::endl;
ptr ret = (ptr)(tlsf_malloc(size * sizeof(T)));
std::cerr << " allocated at: " << (void*)ret << std::endl;
#else
pointer ret = static_cast<pointer>(tlsf_malloc(size * sizeof(T)));
#endif
return ret;
}
// destroy elements of initialized storage p
void destroy (pointer p) {
p->~T();
}
// return maximum number of elements that can be allocated
size_type max_size() const throw() {
return std::numeric_limits<std::size_t>::max() / sizeof(T);
}
// allocate but don't initialize num elements of type T
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;
pointer ret = (pointer)(tlsf_malloc(num*sizeof(T)));
std::cerr << " allocated at: " << (void*)ret << std::endl; return ret;
#else
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 (pointer p, const T& value) {
new (reinterpret_cast<void*>(p))T(value);
}
// deallocate storage p of deleted elements
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;
#endif
tlsf_free(reinterpret_cast<void*>(p));
}
};
// return that all specializations of this allocator are interchangeable
template <class T1, class T2> bool operator== (const TLSFAlloc<T1>&,const TLSFAlloc<T2>&) throw() { return true; }
template <class T1, class T2> bool operator!= (const TLSFAlloc<T1>&, const TLSFAlloc<T2>&) throw() { return false; }
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;
}
Loading…
Cancel
Save