The Squirrel interpreter. See http://www.squirrel-lang.org/

Dependents:   Squirrel

Committer:
jhnwkmn
Date:
Tue Dec 16 10:20:34 2014 +0000
Revision:
0:97a4f8cc534c
Initial import of Squirrel.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jhnwkmn 0:97a4f8cc534c 1 /* see copyright notice in squirrel.h */
jhnwkmn 0:97a4f8cc534c 2 #ifndef _SQUTILS_H_
jhnwkmn 0:97a4f8cc534c 3 #define _SQUTILS_H_
jhnwkmn 0:97a4f8cc534c 4
jhnwkmn 0:97a4f8cc534c 5 void *sq_vm_malloc(SQUnsignedInteger size);
jhnwkmn 0:97a4f8cc534c 6 void *sq_vm_realloc(void *p,SQUnsignedInteger oldsize,SQUnsignedInteger size);
jhnwkmn 0:97a4f8cc534c 7 void sq_vm_free(void *p,SQUnsignedInteger size);
jhnwkmn 0:97a4f8cc534c 8
jhnwkmn 0:97a4f8cc534c 9 #define sq_new(__ptr,__type) {__ptr=(__type *)sq_vm_malloc(sizeof(__type));new (__ptr) __type;}
jhnwkmn 0:97a4f8cc534c 10 #define sq_delete(__ptr,__type) {__ptr->~__type();sq_vm_free(__ptr,sizeof(__type));}
jhnwkmn 0:97a4f8cc534c 11 #define SQ_MALLOC(__size) sq_vm_malloc((__size));
jhnwkmn 0:97a4f8cc534c 12 #define SQ_FREE(__ptr,__size) sq_vm_free((__ptr),(__size));
jhnwkmn 0:97a4f8cc534c 13 #define SQ_REALLOC(__ptr,__oldsize,__size) sq_vm_realloc((__ptr),(__oldsize),(__size));
jhnwkmn 0:97a4f8cc534c 14
jhnwkmn 0:97a4f8cc534c 15 #define sq_aligning(v) (((size_t)(v) + (SQ_ALIGNMENT-1)) & (~(SQ_ALIGNMENT-1)))
jhnwkmn 0:97a4f8cc534c 16
jhnwkmn 0:97a4f8cc534c 17 //sqvector mini vector class, supports objects by value
jhnwkmn 0:97a4f8cc534c 18 template<typename T> class sqvector
jhnwkmn 0:97a4f8cc534c 19 {
jhnwkmn 0:97a4f8cc534c 20 public:
jhnwkmn 0:97a4f8cc534c 21 sqvector()
jhnwkmn 0:97a4f8cc534c 22 {
jhnwkmn 0:97a4f8cc534c 23 _vals = NULL;
jhnwkmn 0:97a4f8cc534c 24 _size = 0;
jhnwkmn 0:97a4f8cc534c 25 _allocated = 0;
jhnwkmn 0:97a4f8cc534c 26 }
jhnwkmn 0:97a4f8cc534c 27 sqvector(const sqvector<T>& v)
jhnwkmn 0:97a4f8cc534c 28 {
jhnwkmn 0:97a4f8cc534c 29 copy(v);
jhnwkmn 0:97a4f8cc534c 30 }
jhnwkmn 0:97a4f8cc534c 31 void copy(const sqvector<T>& v)
jhnwkmn 0:97a4f8cc534c 32 {
jhnwkmn 0:97a4f8cc534c 33 if(_size) {
jhnwkmn 0:97a4f8cc534c 34 resize(0); //destroys all previous stuff
jhnwkmn 0:97a4f8cc534c 35 }
jhnwkmn 0:97a4f8cc534c 36 //resize(v._size);
jhnwkmn 0:97a4f8cc534c 37 if(v._size > _allocated) {
jhnwkmn 0:97a4f8cc534c 38 _realloc(v._size);
jhnwkmn 0:97a4f8cc534c 39 }
jhnwkmn 0:97a4f8cc534c 40 for(SQUnsignedInteger i = 0; i < v._size; i++) {
jhnwkmn 0:97a4f8cc534c 41 new ((void *)&_vals[i]) T(v._vals[i]);
jhnwkmn 0:97a4f8cc534c 42 }
jhnwkmn 0:97a4f8cc534c 43 _size = v._size;
jhnwkmn 0:97a4f8cc534c 44 }
jhnwkmn 0:97a4f8cc534c 45 ~sqvector()
jhnwkmn 0:97a4f8cc534c 46 {
jhnwkmn 0:97a4f8cc534c 47 if(_allocated) {
jhnwkmn 0:97a4f8cc534c 48 for(SQUnsignedInteger i = 0; i < _size; i++)
jhnwkmn 0:97a4f8cc534c 49 _vals[i].~T();
jhnwkmn 0:97a4f8cc534c 50 SQ_FREE(_vals, (_allocated * sizeof(T)));
jhnwkmn 0:97a4f8cc534c 51 }
jhnwkmn 0:97a4f8cc534c 52 }
jhnwkmn 0:97a4f8cc534c 53 void reserve(SQUnsignedInteger newsize) { _realloc(newsize); }
jhnwkmn 0:97a4f8cc534c 54 void resize(SQUnsignedInteger newsize, const T& fill = T())
jhnwkmn 0:97a4f8cc534c 55 {
jhnwkmn 0:97a4f8cc534c 56 if(newsize > _allocated)
jhnwkmn 0:97a4f8cc534c 57 _realloc(newsize);
jhnwkmn 0:97a4f8cc534c 58 if(newsize > _size) {
jhnwkmn 0:97a4f8cc534c 59 while(_size < newsize) {
jhnwkmn 0:97a4f8cc534c 60 new ((void *)&_vals[_size]) T(fill);
jhnwkmn 0:97a4f8cc534c 61 _size++;
jhnwkmn 0:97a4f8cc534c 62 }
jhnwkmn 0:97a4f8cc534c 63 }
jhnwkmn 0:97a4f8cc534c 64 else{
jhnwkmn 0:97a4f8cc534c 65 for(SQUnsignedInteger i = newsize; i < _size; i++) {
jhnwkmn 0:97a4f8cc534c 66 _vals[i].~T();
jhnwkmn 0:97a4f8cc534c 67 }
jhnwkmn 0:97a4f8cc534c 68 _size = newsize;
jhnwkmn 0:97a4f8cc534c 69 }
jhnwkmn 0:97a4f8cc534c 70 }
jhnwkmn 0:97a4f8cc534c 71 void shrinktofit() { if(_size > 4) { _realloc(_size); } }
jhnwkmn 0:97a4f8cc534c 72 T& top() const { return _vals[_size - 1]; }
jhnwkmn 0:97a4f8cc534c 73 inline SQUnsignedInteger size() const { return _size; }
jhnwkmn 0:97a4f8cc534c 74 bool empty() const { return (_size <= 0); }
jhnwkmn 0:97a4f8cc534c 75 inline T &push_back(const T& val = T())
jhnwkmn 0:97a4f8cc534c 76 {
jhnwkmn 0:97a4f8cc534c 77 if(_allocated <= _size)
jhnwkmn 0:97a4f8cc534c 78 _realloc(_size * 2);
jhnwkmn 0:97a4f8cc534c 79 return *(new ((void *)&_vals[_size++]) T(val));
jhnwkmn 0:97a4f8cc534c 80 }
jhnwkmn 0:97a4f8cc534c 81 inline void pop_back()
jhnwkmn 0:97a4f8cc534c 82 {
jhnwkmn 0:97a4f8cc534c 83 _size--; _vals[_size].~T();
jhnwkmn 0:97a4f8cc534c 84 }
jhnwkmn 0:97a4f8cc534c 85 void insert(SQUnsignedInteger idx, const T& val)
jhnwkmn 0:97a4f8cc534c 86 {
jhnwkmn 0:97a4f8cc534c 87 resize(_size + 1);
jhnwkmn 0:97a4f8cc534c 88 for(SQUnsignedInteger i = _size - 1; i > idx; i--) {
jhnwkmn 0:97a4f8cc534c 89 _vals[i] = _vals[i - 1];
jhnwkmn 0:97a4f8cc534c 90 }
jhnwkmn 0:97a4f8cc534c 91 _vals[idx] = val;
jhnwkmn 0:97a4f8cc534c 92 }
jhnwkmn 0:97a4f8cc534c 93 void remove(SQUnsignedInteger idx)
jhnwkmn 0:97a4f8cc534c 94 {
jhnwkmn 0:97a4f8cc534c 95 _vals[idx].~T();
jhnwkmn 0:97a4f8cc534c 96 if(idx < (_size - 1)) {
jhnwkmn 0:97a4f8cc534c 97 memmove(&_vals[idx], &_vals[idx+1], sizeof(T) * (_size - idx - 1));
jhnwkmn 0:97a4f8cc534c 98 }
jhnwkmn 0:97a4f8cc534c 99 _size--;
jhnwkmn 0:97a4f8cc534c 100 }
jhnwkmn 0:97a4f8cc534c 101 SQUnsignedInteger capacity() { return _allocated; }
jhnwkmn 0:97a4f8cc534c 102 inline T &back() const { return _vals[_size - 1]; }
jhnwkmn 0:97a4f8cc534c 103 inline T& operator[](SQUnsignedInteger pos) const{ return _vals[pos]; }
jhnwkmn 0:97a4f8cc534c 104 T* _vals;
jhnwkmn 0:97a4f8cc534c 105 private:
jhnwkmn 0:97a4f8cc534c 106 void _realloc(SQUnsignedInteger newsize)
jhnwkmn 0:97a4f8cc534c 107 {
jhnwkmn 0:97a4f8cc534c 108 newsize = (newsize > 0)?newsize:4;
jhnwkmn 0:97a4f8cc534c 109 _vals = (T*)SQ_REALLOC(_vals, _allocated * sizeof(T), newsize * sizeof(T));
jhnwkmn 0:97a4f8cc534c 110 _allocated = newsize;
jhnwkmn 0:97a4f8cc534c 111 }
jhnwkmn 0:97a4f8cc534c 112 SQUnsignedInteger _size;
jhnwkmn 0:97a4f8cc534c 113 SQUnsignedInteger _allocated;
jhnwkmn 0:97a4f8cc534c 114 };
jhnwkmn 0:97a4f8cc534c 115
jhnwkmn 0:97a4f8cc534c 116 #endif //_SQUTILS_H_