// 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;
}
// 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;
} 
void insertElement(List_PNTR l, void *element) {
  bool emptyList;
  emptyList = isEmpty(l);
  if(l==NULL){
    DAL_error(NULL_POINTER_ERROR);
    return;
  }
  // putting NULL in the list is an issue if the list is supposed
  // to contain valid references to objects, so prevented here 
  if(element == NULL){
    DAL_error(NULL_ELEMENT_ERROR);
    return;
  }
  // duplicates in list are also prevented here during debug
#if DEBUG
  if(!emptyList && containsElement(l, element)){
    DAL_error(DUPLICATE_ELEMENT_ERROR);
    return;
  }
#endif
  ListNode_PNTR newNode = Construct_ListNode();
  if(newNode == NULL){
    DAL_error(OUT_OF_MEMORY_ERROR);
    return;
  }
  DAL_assign(&newNode->payload, element); // increases ref count on element
  //newNode->payload = element; // or if payload not garbage collected
  newNode->tail = l->first;
  l->first = newNode;
  if(emptyList){ // we are about to add the first element to this list set the 
    l->next = l->first; // iterator index to point to the first node
  }
} 
// 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;
} 
Exemple #5
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;
} 
void *getRandomElement(List_PNTR l){
  unsigned short rand, max;
  if(l==NULL){
    DAL_error(NULL_POINTER_ERROR);
    return(NULL);
  }
  if(isEmpty(l)){
#if DEBUG
    DAL_error(EMPTY_CONTAINER_ERROR);
#endif
    return(NULL);
  }
  max = getListLength(l) - 1; // elements numbered from 0, so max=length-1
  rand = DAL_random(max);
  return(getElementN(l, rand));
}
// 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;
}
void* getNextElement(List_PNTR l){
  if(l==NULL){
    DAL_error(NULL_POINTER_ERROR);
    return(NULL);
  }
  if(isEmpty(l)){
#if DEBUG
    DAL_error(EMPTY_CONTAINER_ERROR);
#endif
    return(NULL);
  }
  void *element = l->next->payload;
  l->next = l->next->tail;
  if(l->next == NULL)   // if we iterated to the end of the list
    l->next = l->first; // wrap round to the beginning
  return(element);
}
/*
 * returns the reference count for a memory segment allocated by DAL_alloc
 */
unsigned DAL_getRef(void *pntr){
  if(pntr==NULL){
    DAL_error(NULL_POINTER_ERROR);
    return 0;
  }
  MemHeader header = (MemHeader) (pntr - sizeof(MemHeaderStruct));
  return(header->ref_count);
}
// Removes an element from the list l, decrements ref count on element
void removeElement(List_PNTR l, void *element) {
  // case: error, l is null, i.e. no list defined
  if(l==NULL){
    DAL_error(NULL_POINTER_ERROR);
    return;
  }
  if(isEmpty(l)){
#if DEBUG
    DAL_error(EMPTY_CONTAINER_ERROR);
#endif
    return;
  }
  if(element == NULL){
    DAL_error(ELEMENT_NOT_FOUND_ERROR);
    return;
  }
  // case: element we want to remove is right at the start of the list
  if(l->first->payload == element){
    ListNode_PNTR oldFirst = l->first;
    l->first = oldFirst->tail;
    // take action if we are removing the next iterator node
    if(l->next == oldFirst) l->next = l->first; 
    listFreeNode(oldFirst); // explicitly free memory used by node
    return;
  }
  // case: otherwise try to find element to remove
  ListNode_PNTR previous = l->first, current = previous->tail;
  while(current != NULL && current->payload!=element){
    previous = current;
    current = current->tail;
  }
  if(current!=NULL){ // we found the element to remove
    // take action if we are removing the next iterator node
    if(l->next == current) l->next = current->tail;
    if(l->next == NULL) l->next = l->first;
    // deal with removal
    previous->tail = current->tail;
    // explicitly free memory used by node
    listFreeNode(current);
  }
  else
    DAL_error(ELEMENT_NOT_FOUND_ERROR);
}
void removeAllElements(List_PNTR l){
  if(l==NULL){
    DAL_error(NULL_POINTER_ERROR);
    return;
  }
  if(isEmpty(l)){
#if DEBUG
    DAL_error(EMPTY_CONTAINER_ERROR);
#endif
    return;    
  }
  ListNode_PNTR previous, current = l->first;
  while( current != NULL){
    previous = current;
    current = current->tail;
    listFreeNode(previous); // free memory used by list node
  }
  l->first = l->next = NULL;
}
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);
}
bool containsElement(List_PNTR l, void *element){
  if(l==NULL){
#if DEBUG
    DAL_error(NULL_POINTER_ERROR);
#endif
    return(false);
  }
  if(isEmpty(l)){
#if DEBUG
    DAL_error(EMPTY_CONTAINER_ERROR);
#endif
    return(false);
  }
  ListNode_PNTR current = l->first;
  while (current != NULL){
    if(current->payload == element)
      return(true);
    current = current->tail;
  }
  return(false);
}
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;
}
unsigned getListLength(List_PNTR l){
  unsigned count = 0;
  if(l==NULL){
    DAL_error(NULL_POINTER_ERROR);
    return((unsigned)NULL);
  }
  ListNode_PNTR current = l->first;
  while (current != NULL){
    current = current->tail;
    count++;
  }
  return(count);
}
// function to de-serialise data received from the radio to a new AnyType
AnyTypePNTR deserialiseToAnyType(void *buffer){
	AnyTypePNTR newAny; void *data;
	FunctionPairPNTR funcs = mapGet(serialiserMap, (char *) buffer);
	if (funcs == NULL || funcs->des == NULL){
		DAL_error(SERIALISATION_ERROR);
		PRINTF("des2any:%s \n", (char *) buffer);
		return NULL;
	}
	//PRINTF(" des2any:%s ", (char *) buffer);
	data = ((char *) buffer) + strlen((char *)buffer) + 1; // 1 for \0
	newAny = (*(funcs->des))(data);
	//PRINTF("%p\n", newAny->value);
	return newAny;
}
static ListNode_PNTR Construct_ListNode(){
  // Not maintaining list nodes using garbage collector, waste of space and 
  // time as you always know when a node has been created and removed from the
  // list, so using BASE_mem_alloc here and will use explicit BASE_mem_free
  // in listFreeNode function (declared above & defined at bottom of file)
  //ListNode_PNTR this=(ListNode_PNTR) DAL_alloc(sizeof(ListNodeStruct),false);
  ListNode_PNTR this =(ListNode_PNTR) BASE_mem_alloc(sizeof(ListNodeStruct));
  if(this == 0){
    DAL_error(OUT_OF_MEMORY_ERROR);
    return 0;
  }
  // need set to NULL here as using BASE_mem_alloc which does not set mem to 0
  this->payload = NULL;
  this->tail = NULL;
  return this;
}
// function to serialise AnyType and return pointer to buffer 
// containing data in serial form
void *serialiseAnyType(AnyTypePNTR data, int *size){
	void *serial_buffer;
	FunctionPairPNTR funcs = mapGet(serialiserMap, data->type);
	if (funcs == NULL || funcs->ser == NULL){
		DAL_error(SERIALISATION_ERROR);
		PRINTF("serAny:%s \n", (char *) data->type);
		return NULL;
	}
	//PRINTF(" serany: %s \n", data->type);
	if(stringStartsWith(data->type, "i") || stringStartsWith(data->type, "e"))
		serial_buffer = (*(funcs->ser))(&(data->value.int_value), size);
	else if(stringStartsWith(data->type, "u"))
		serial_buffer = (*(funcs->ser))(&(data->value.unsigned_value), size);
	else if(stringStartsWith(data->type, "r"))
		serial_buffer = (*(funcs->ser))(&(data->value.real_value), size);
	else if(stringStartsWith(data->type, "b"))
		serial_buffer = (*(funcs->ser))(&(data->value.bool_value), size);
	else if(stringStartsWith(data->type, "8"))
		serial_buffer = (*(funcs->ser))(&(data->value.byte_value), size);
	else
		serial_buffer = (*(funcs->ser))(data->value.pointer_value, size);
	return serial_buffer;
}