Skip to content

rockeet/nark-bone

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nark-core

Now the newest version is in topling-ark

Core utilities of nark library

Prerequisite

Compile

$ cd /path/to/nark-bone
$ make

Components

valvec

`nark::valvec` is a alternative to `std::vector`, it use `malloc`/`realloc`/`free` to manage memory. `Type` of `nark::valvec` must be [memmove-able](#memmove-able).

valvec support nark-serialization.

Fast expansion

valvec is much faster than std::vector when expanding memory, when expanding, std::vector first allocate a larger memory, then copy-cons(move-cons in c++11) old objects, this is slow. But valvec just calling `realloc' to expand the memory, in virtual addressing system, for example:

   void* p = malloc(oldsize); // `p` is a virtual address
   /// do something
   void* q = realloc(p, newsize); // `q` is another virtual address

When available virtual address range pointed by p is not big enough, it is posible to just remap the physical address of p into a larger free virtual address range. Most popular malloc implementations have used remap to get this advantage.

low level functions

  • risk_set_data, risk_set_size, risk_set_capacity
  • risk_release_ownership

nark-fsa use these functions for supporting DFA memory map in an graceful way.

utility functions

   nark::valvec<int> vec;
   vec.push(1); // `push` is an alias of `push_back`
   vec.push(2); // expand when full
   //...
   vec.reserve(vec.size() + 100);
   vec.unchecked_push(123); // assert when full
   int val = vec.pop_val(); // throws when empty
   //...
   while (!vec.empty()) {
      int top = vec.unchecked_pop_val();
      //...
   }

memmove-able

When memmove an object `x` to another memory block `y` without destruct `x`, if `y` can be used as it is `x`, then `x` is memmove-able.

For example, gnu std::list,map,set... are not memmove-able, because they have self-references.

Lexical Cast

Both compiling and running are much fast than `boost::lexical_cast`. ```c++ #include

//... int i = nark::lcast("123"); long l = nark::lcase(std::string("1") + "23"); float f = nark::lcast("1.23"); double d = nark::lcast("1.23");


<h3 id="read-text-lines">Read text lines</h3>
```c++
#include <nark/util/linebuf.hpp>

//...
	nark::LineBuf line;
	nark::valvec<fstring> fields; // fstring didn't own memory
	while (line.getline(stdin) > 0) {
		line.chomp(); // like perl chomp
		line.split(&fields);
		if (fields.size()) {
			double dval = nark::lcast(fields[0]);
			//...
		}
	}