32 #include "os/memory.h" 40 extern Mutex* dvector_lock;
48 void copy_on_write() {
59 if ( *(
int*)lock.data() == 1 ) {
66 MID new_mem= dynalloc( mem.get_size() );
68 if (!new_mem.is_valid()) {
72 ERR_FAIL_COND( new_mem.is_valid() );
77 int *rc = (
int*)dst_lock.data();
81 T * dst = (T*)(rc + 1 );
83 T * src =(T*) ((
int*)lock.data() + 1 );
85 int count = (mem.get_size() -
sizeof(int)) /
sizeof(T);
87 for (
int i=0;i<count;i++) {
89 memnew_placement( &dst[i], T(src[i]) );
92 (*(
int*)lock.data())--;
105 void reference(
const DVector& p_dvector ) {
110 dvector_lock->
lock();
112 if (!p_dvector.mem.is_valid()) {
121 int * rc = (
int*)lock.data();
136 dvector_lock->
lock();
138 if (!mem.is_valid()) {
147 int * rc = (
int*)lock.data();
154 int count = (mem.get_size() -
sizeof(int)) /
sizeof(T);
156 for (
int i=0;i<count;i++) {
181 _FORCE_INLINE_
const T& operator[](
int p_index)
const {
return mem[p_index]; }
182 _FORCE_INLINE_
const T *ptr()
const {
return mem; }
193 _FORCE_INLINE_ T& operator[](
int p_index) {
return mem[p_index]; }
194 _FORCE_INLINE_ T *ptr() {
return mem; }
196 Write() { mem=NULL; }
203 if (mem.is_valid()) {
205 r.mem = (
const T*)((
int*)r.lock.data()+1);
212 if (mem.is_valid()) {
215 w.mem = (T*)((
int*)w.lock.data()+1);
221 void fill_with(
const MC& p_mc) {
228 for(
const typename MC::Element *E=p_mc.front();E;E=E->next()) {
235 void remove(
int p_index) {
238 ERR_FAIL_INDEX(p_index, s);
240 for (
int i=p_index; i<s-1; i++) {
248 inline int size()
const;
249 T
get(
int p_index)
const;
250 void set(
int p_index,
const T& p_val);
251 void push_back(
const T& p_val);
252 void append(
const T& p_val) { push_back(p_val); }
254 int ds = p_arr.size();
260 Read r = p_arr.read();
261 for(
int i=0;i<ds;i++)
266 Error insert(
int p_pos,
const T& p_val) {
269 ERR_FAIL_INDEX_V(p_pos,s+1,ERR_INVALID_PARAMETER);
273 for (
int i=s;i>p_pos;i--)
282 bool is_locked()
const {
return mem.is_locked(); }
284 inline const T operator[](
int p_index)
const;
286 Error resize(
int p_size);
289 void operator=(
const DVector& p_dvector) { reference(p_dvector); }
299 return mem.is_valid() ? ((mem.get_size() -
sizeof(int)) /
sizeof(T) ) : 0;
305 return operator[](p_index);
311 if (p_index<0 || p_index>=size()) {
312 ERR_FAIL_COND(p_index<0 || p_index>=size());
322 resize( size() + 1 );
323 set( size() -1, p_val );
329 if (p_index<0 || p_index>=size()) {
331 ERR_FAIL_COND_V(p_index<0 || p_index>=size(),aux);
344 dvector_lock->
lock();
346 bool same = p_size==size();
364 ERR_FAIL_COND_V( mem.is_locked(), ERR_LOCKED );
366 if (p_size > size() ) {
374 mem = dynalloc( p_size *
sizeof(T) +
sizeof(
int) );
376 int *rc = ((
int*)lock.data());
381 if (dynrealloc( mem, p_size *
sizeof(T) +
sizeof(
int) )!=OK ) {
383 ERR_FAIL_V(ERR_OUT_OF_MEMORY);
392 T *t = (T*)((
int*)lock.data() + 1);
394 for (
int i=oldsize;i<p_size;i++) {
396 memnew_placement(&t[i], T );
407 T *t = (T*)((
int*)lock.data() + 1);
409 for (
int i=p_size;i<oldsize;i++) {
416 if (dynrealloc( mem, p_size *
sizeof(T) +
sizeof(
int) )!=OK ) {
418 ERR_FAIL_V(ERR_OUT_OF_MEMORY);
Definition: dvector.h:187
Definition: dvector.h:175
virtual void unlock()=0
Unlock the mutex, let other threads continue.
virtual void lock()=0
Lock the mutex, block if locked by someone else.