| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | #include "master.hpp"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:41:14 -04:00
										 |  |  | namespace factor { | 
					
						
							| 
									
										
										
										
											2009-05-04 02:46:13 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-16 01:00:08 -04:00
										 |  |  | /* Allocates memory */ | 
					
						
							| 
									
										
										
										
											2013-05-11 21:41:14 -04:00
										 |  |  | array* factor_vm::allot_array(cell capacity, cell fill_) { | 
					
						
							|  |  |  |   data_root<object> fill(fill_, this); | 
					
						
							|  |  |  |   array* new_array = allot_uninitialized_array<array>(capacity); | 
					
						
							|  |  |  |   memset_cell(new_array->data(), fill.value(), capacity * sizeof(cell)); | 
					
						
							|  |  |  |   return new_array; | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-16 01:00:08 -04:00
										 |  |  | /* Allocates memory */ | 
					
						
							| 
									
										
										
										
											2013-05-11 21:41:14 -04:00
										 |  |  | void factor_vm::primitive_array() { | 
					
						
							| 
									
										
										
										
											2014-05-29 13:22:43 -04:00
										 |  |  |   cell fill = ctx->pop(); | 
					
						
							| 
									
										
										
										
											2013-05-11 21:41:14 -04:00
										 |  |  |   cell capacity = unbox_array_size(); | 
					
						
							| 
									
										
										
										
											2014-05-29 13:22:43 -04:00
										 |  |  |   array* new_array = allot_array(capacity, fill); | 
					
						
							| 
									
										
										
										
											2013-05-11 21:41:14 -04:00
										 |  |  |   ctx->push(tag<array>(new_array)); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-16 01:00:08 -04:00
										 |  |  | /* Allocates memory */ | 
					
						
							| 
									
										
										
										
											2013-05-11 21:41:14 -04:00
										 |  |  | cell factor_vm::allot_array_4(cell v1_, cell v2_, cell v3_, cell v4_) { | 
					
						
							|  |  |  |   data_root<object> v1(v1_, this); | 
					
						
							|  |  |  |   data_root<object> v2(v2_, this); | 
					
						
							|  |  |  |   data_root<object> v3(v3_, this); | 
					
						
							|  |  |  |   data_root<object> v4(v4_, this); | 
					
						
							|  |  |  |   data_root<array> a(allot_uninitialized_array<array>(4), this); | 
					
						
							|  |  |  |   set_array_nth(a.untagged(), 0, v1.value()); | 
					
						
							|  |  |  |   set_array_nth(a.untagged(), 1, v2.value()); | 
					
						
							|  |  |  |   set_array_nth(a.untagged(), 2, v3.value()); | 
					
						
							|  |  |  |   set_array_nth(a.untagged(), 3, v4.value()); | 
					
						
							|  |  |  |   return a.value(); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-16 01:00:08 -04:00
										 |  |  | /* Allocates memory */ | 
					
						
							| 
									
										
										
										
											2013-05-11 21:41:14 -04:00
										 |  |  | void factor_vm::primitive_resize_array() { | 
					
						
							|  |  |  |   data_root<array> a(ctx->pop(), this); | 
					
						
							| 
									
										
										
										
											2015-11-24 10:00:50 -05:00
										 |  |  |   check_tagged(a); | 
					
						
							| 
									
										
										
										
											2013-05-11 21:41:14 -04:00
										 |  |  |   cell capacity = unbox_array_size(); | 
					
						
							|  |  |  |   ctx->push(tag<array>(reallot_array(a.untagged(), capacity))); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-16 01:00:08 -04:00
										 |  |  | /* Allocates memory */ | 
					
						
							| 
									
										
										
										
											2013-05-11 21:41:14 -04:00
										 |  |  | cell factor_vm::std_vector_to_array(std::vector<cell>& elements) { | 
					
						
							| 
									
										
										
										
											2014-06-11 14:33:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:41:14 -04:00
										 |  |  |   cell element_count = elements.size(); | 
					
						
							| 
									
										
										
										
											2014-06-11 14:33:33 -04:00
										 |  |  |   cell orig_size = data_roots.size(); | 
					
						
							|  |  |  |   data_roots.reserve(orig_size + element_count); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   for (cell n = 0; n < element_count; n++) { | 
					
						
							|  |  |  |     data_roots.push_back(&elements[n]); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2010-02-02 07:46:17 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:41:14 -04:00
										 |  |  |   tagged<array> objects(allot_uninitialized_array<array>(element_count)); | 
					
						
							|  |  |  |   memcpy(objects->data(), &elements[0], element_count * sizeof(cell)); | 
					
						
							| 
									
										
										
										
											2014-06-11 14:33:33 -04:00
										 |  |  |   data_roots.resize(orig_size); | 
					
						
							| 
									
										
										
										
											2013-05-11 21:41:14 -04:00
										 |  |  |   return objects.value(); | 
					
						
							| 
									
										
										
										
											2010-02-02 07:46:17 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-16 01:00:08 -04:00
										 |  |  | /* Allocates memory */ | 
					
						
							| 
									
										
										
										
											2013-05-11 21:41:14 -04:00
										 |  |  | void growable_array::add(cell elt_) { | 
					
						
							|  |  |  |   factor_vm* parent = elements.parent; | 
					
						
							|  |  |  |   data_root<object> elt(elt_, parent); | 
					
						
							|  |  |  |   if (count == array_capacity(elements.untagged())) | 
					
						
							|  |  |  |     elements = parent->reallot_array(elements.untagged(), count * 2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   parent->set_array_nth(elements.untagged(), count++, elt.value()); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-16 01:00:08 -04:00
										 |  |  | /* Allocates memory */ | 
					
						
							| 
									
										
										
										
											2013-05-11 21:41:14 -04:00
										 |  |  | void growable_array::append(array* elts_) { | 
					
						
							|  |  |  |   factor_vm* parent = elements.parent; | 
					
						
							|  |  |  |   data_root<array> elts(elts_, parent); | 
					
						
							|  |  |  |   cell capacity = array_capacity(elts.untagged()); | 
					
						
							|  |  |  |   if (count + capacity > array_capacity(elements.untagged())) { | 
					
						
							|  |  |  |     elements = | 
					
						
							|  |  |  |         parent->reallot_array(elements.untagged(), (count + capacity) * 2); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   for (cell index = 0; index < capacity; index++) | 
					
						
							|  |  |  |     parent->set_array_nth(elements.untagged(), count++, | 
					
						
							|  |  |  |                           array_nth(elts.untagged(), index)); | 
					
						
							| 
									
										
										
										
											2009-10-14 20:24:23 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-16 01:00:08 -04:00
										 |  |  | /* Allocates memory */ | 
					
						
							| 
									
										
										
										
											2013-05-11 21:41:14 -04:00
										 |  |  | void growable_array::trim() { | 
					
						
							|  |  |  |   factor_vm* parent = elements.parent; | 
					
						
							|  |  |  |   elements = parent->reallot_array(elements.untagged(), count); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-05-04 02:46:13 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | } |