// construct an array of given length and type_size
IArrayPNTR Construct_Array(unsigned length, unsigned type_size, void *init,
			   bool contains_pointers ) {
  IArrayPNTR this = (IArrayPNTR) DAL_alloc(sizeof(IArrayStruct), true);
  if( this == NULL ) {
    DAL_error(OUT_OF_MEMORY_ERROR);
    return NULL;
  }
  this->type_size=type_size; this->decRef=IArray_decRef; 
  this->length=length; // set this->length (will be set to 0 if length is 0)
  if(length == 0) // if length is 0 ensure creation of element 0 to avoid deref
	  length = 1; // problems, but maintain this->length = 0  
  DAL_assign(&this->data, DAL_alloc(type_size * length, contains_pointers));
  if(this->data == NULL){
	  DAL_error(OUT_OF_MEMORY_ERROR);
	  return NULL;
  }
  if(init != NULL){// initialise every element with init
	  int i; void *pntr = this->data;
	  for(i=0; i<length; i++){
		  if(contains_pointers) memncpy(pntr, &init, type_size);
		  else memncpy(pntr, init, type_size);
		  pntr = (((char *) pntr) + type_size);
	  }
	  // if init was pointer, add length n.o. refs to init's memory header
	  if(contains_pointers) DAL_modRef_by_n(init, length);
  }
  return this;
}
// function to construct an array
IArrayPNTR Construct_Array(unsigned num_dims, unsigned type_size, void *init, 
			   bool contains_pointers, int *lengths){
  //if(*lengths <= 0) return NULL; // have reached end of lengths array
  IArrayPNTR this = (IArrayPNTR) DAL_alloc(sizeof(IArrayStruct), true);
  if( this == NULL ) {
    DAL_error(OUT_OF_MEMORY_ERROR);
    return NULL;
  }
  this->decRef = Array_decRef; this->num_dims = num_dims;
  this->copyObject = copy_Array; this->length = lengths[0]; 
  this->type_size = (num_dims > 1 ? sizeof(IArrayPNTR) : type_size);
  bool elements_are_pointers = (num_dims>1 ? true : contains_pointers);
  DAL_assign(&this->data, (IArrayPNTR*) 
	     (DAL_alloc(this->length * this->type_size, elements_are_pointers)));
  if(num_dims > 1){ // need array of arrays
    int i;
    for(i=0; i<this->length; i++){
      DAL_assign( &((IArrayPNTR *) this->data)[i], 
		  Construct_Array(num_dims-1, type_size, init, contains_pointers, 
				  lengths+1));
    }
    return this;
  }
  // if we get to here num_dims == 1 (we have got to last dim)
  // should fill array with init if it is not set to NULL
  if(init != NULL){
    int i; void *pntr = this->data;
    for(i=0; i<this->length; i++){
      memncpy(pntr, init, type_size);
      pntr = (((char *) pntr) + type_size);
    }
  }
  return this;
}
// Generated from: uk.ac.stand.cs.insense.compiler.unixCCgen.TypeMarshaller::printSerializer
void* serialize_u( unsigned *p,int* size )  { 
// Generated from: uk.ac.stand.cs.insense.compiler.unixCCgen.Code::genMallocAssign
	void*  pntr=( void* ) DAL_alloc( 128,false ) ;
	if( pntr== NULL ) { 
		DAL_error(OUT_OF_MEMORY_ERROR);
		return NULL;
	} 
	void* result=pntr;
	int used= 0;
	used += 2;
	if(used > 128){ 
		DAL_error( SERIALISATION_ERROR ) ;
		return NULL;
	} 
	memncpy( pntr,"u",2 ) ;
	pntr =((char *)pntr)+2;	
	used += sizeof( int ) ;
	if(used > 128){ 
		DAL_error( SERIALISATION_ERROR ) ;
		return NULL;
	} 
	memncpy( (char *)pntr,(char *)p,sizeof( int )  ) ;
	*size=used;
	return result;
} 
// Generated from: uk.ac.stand.cs.insense.compiler.unixCCgen.TypeMarshaller::printSerializer
void* serialize_s( StringPNTR p,int* size )  { 
// Generated from: uk.ac.stand.cs.insense.compiler.unixCCgen.Code::genMallocAssign
	void*  pntr=( void* ) DAL_alloc( 128,false ) ;
	if( pntr== NULL ) { 
		DAL_error(OUT_OF_MEMORY_ERROR);
		return NULL;
	} 
	void* result=pntr;
	int used= 0;
	used += 2;
	if(used > 128){ 
		DAL_error( SERIALISATION_ERROR ) ;
		return NULL;
	} 
	memncpy( pntr,"s",2 ) ;
	pntr =((char *)pntr)+2;	
	used += p->length + 1;
	if(used > 128){ 
		DAL_error( SERIALISATION_ERROR ) ;
		return NULL;
	} 
	memncpy( (char *)pntr,p->data,p->length + 1 ) ;
	*size=used;
	return result;
} 
Beispiel #5
0
char *stringcat(char *s1, char *s2){
	int i=0, c=0, len1 = strlen(s1), len2 = strlen(s2);
	char *result = DAL_alloc((len1 + len2 + 1), false);
	for(i=0; i<len1; i++){
		result[c++] = s1[i];
	}
	for(i=0; i<len2; i++){
		result[c++] = s2[i];
	}
	result[c] = '\0';
	return result;
}
struct_Suaddr_apayload___PNTR construct_struct_Suaddr_apayload__( unsigned  addr,AnyTypePNTR  payload ) { 
// Generated from: uk.ac.stand.cs.insense.compiler.incesosCCgen.Code::genMallocAssign
	struct_Suaddr_apayload___PNTR  pntr=( struct_Suaddr_apayload___PNTR ) DAL_alloc( sizeof( struct_Suaddr_apayload___struct  ) ,true ) ;
	if( pntr== NULL ) { 
		DAL_error(OUT_OF_MEMORY_ERROR);
		return NULL;
	} 
	pntr->_decRef=decRef_struct_Suaddr_apayload__;
	pntr->addr=addr;
	DAL_assign( &pntr->payload,payload ) ;
	return pntr;
} 
// (public) functions and constructors 
List_PNTR Construct_List(void){
  List_PNTR this = (List_PNTR) DAL_alloc(sizeof(ListStruct), true);
  if(this == 0){
    DAL_error(OUT_OF_MEMORY_ERROR);
    return 0;
  }
  this->decRef = List_decRef;
  // alloc sets to 0
  //this->first = NULL; // initially empty list
  //this->next = NULL;  // iterator pointer has nothing to point to
  return(this);
}
Beispiel #8
0
NetPacket_PNTR construct_NetPacket(StringPNTR addr, AnyTypePNTR payload)
{
	NetPacket_PNTR pntr = (NetPacket_PNTR) DAL_alloc(sizeof(NetPacket_struct), true);
	if(pntr == NULL)
	{
		DAL_error(OUT_OF_MEMORY_ERROR);
		return NULL;
	}
	pntr->_decRef = decRef_NetPacket;
	DAL_assign(&pntr->addr, addr);
	DAL_assign(&pntr->payload,payload);
	return pntr;
}
Beispiel #9
0
FunctionPairPNTR Construct_FunctionPair(serialf_t ser, deserialf_t des){
//    printf("Construct_FunctionPair\n");
  FunctionPairPNTR this;
  if(ser == NULL && des == NULL){
    DAL_error(NULL_ELEMENT_ERROR);
  } 
  this = DAL_alloc(sizeof(FunctionPairStruct), false);
  if(this == NULL){
    DAL_error(OUT_OF_MEMORY_ERROR);
    return NULL;
  }
  this->ser = ser;
  this->des = des;
  return this;
} 
Beispiel #10
0
/*
 * Initialise an Insense component and start a POSIX thread running the behaviour function from the component.
 */
void *component_create(behaviour_ft behaviour, int struct_size, int stack_size, int argc, void *argv[], int core) {
	// Define thread
	struct IComponent_data *this_ptr;
	// Allocate space for the struct
#if HEAPS == HEAP_PRIVATE // Private heaps
	struct shMapType *heapElement = new_PrivateHeap(); 		// Create a new private heap (to be put to the heap map)
	this_ptr = DAL_alloc_in_specific_heap(struct_size, true, heapElement); 			// Allocate space for this_ptr in the newly created private heap
	if (this_ptr == NULL ) {
		return NULL ;
	} else {
		memset(this_ptr, 0, struct_size);
	}
#else // Shared heap
	if ((this_ptr = ((struct IComponent_data *) DAL_alloc(struct_size, true))) == NULL ) {
		return NULL;
	} else {
		memset(this_ptr, 0, struct_size);
	}
#endif
	// Initialize this->comp_create_sem
	my_sem_init(&(this_ptr->component_create_sem), 0);
	// Setup the stopped condition
	if (struct_size) {
		struct IComponent_data *t = (struct IComponent_data*) this_ptr;
		t->stopped = 0;
	}

	// Define new structure for arguments for the wrapper function
	// Whenever dealing with garbage collection, first define as NULL
	struct argStructType * argStruct = malloc(sizeof(struct argStructType));
	argStruct->behaviour = behaviour;
	argStruct->argc = argc;
	argStruct->argv = argv;
	argStruct->this_ptr = this_ptr;

	// Create thread
#if HEAPS == HEAP_PRIVATE // Private heaps
	pthread_mutex_lock(&thread_lock); // Lock mutex to make component thread wait until its heap has been put inserted into the heap map
#endif

	pthread_create(&this_ptr->behav_thread, NULL, startRoutine, argStruct); // Create a POSIX thread, use wrapper function startRoutine to pass three arguments to the function running inside of the thread

	//Set affinity
#if AFFINITY_ALGO != AFFINITY_DYNAMIC
	if (core != -1) { // Manually passed Core ID
		setAffinityToCore(this_ptr->behav_thread, core);// Use passed ID of a core
	} else { // Core ID was not passed to the component_create function
		setAffinity(this_ptr->behav_thread);// Use an algorithm defined in GlobalVars.h
	}
	getAffinityThread(this_ptr->behav_thread); // Check if setting affinity worked. Data outputted only in PRINTMC is defined.
#endif

	// If small heaps are used, add a new entry to the map with a pointer to a newly created pthread.
#if HEAPS == HEAP_PRIVATE // Private heaps
	heapElement->thread_id = this_ptr->behav_thread; // Put a thread id to the element to be to the map
	PRINTFMC("Component created. Thread ID: %x\n", heapElement->thread_id);
	listAdd(SHList, heapElement);
	pthread_mutex_unlock(&thread_lock); // Unlock mutex to permit component to continue now that its heap has been put inserted into the heap map
#endif
	// Insert thread into the list of threads
	listAdd(threadList, this_ptr->behav_thread);
	my_sem_wait(&this_ptr->component_create_sem); // Wait for creation of the component
	return this_ptr;
}