// 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;
} 
// 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;
} 
int dsk1interrupt(void) {
	request_desc_p target, prev;
	disk_desc *ptr;
	int ticks;
	double seek_time, rotate_time, transfer_time;
	STATWORD ps;

	disable(ps);
	ptr = (disk_desc *)devtab[DISK1].dvioblk;
	if(!ptr) {
		restore(ps);
		return SYSERR;
	}

	if(!ptr -> request_head) {
		disk1_preempt = MAXINT;
		restore(ps);
		return OK;
	}

	for(target = ptr -> request_head, prev = NULL;target -> next;prev = target, target = target -> next); 
	if(prev != NULL)
		prev -> next = NULL;
	else
		ptr -> request_head = NULL;

	if(target -> type == READ_OPERATION) {
		ptr -> no_of_reads++;
		memncpy(target -> buffer, ptr -> disk + target -> block_no * ptr -> block_size, ptr -> block_size * target -> count);
	}
	else {
		ptr -> no_of_writes++;
		memncpy(ptr -> disk + target -> block_no * ptr -> block_size, target -> buffer, ptr -> block_size * target -> count);
	}
	ptr -> head_sector = target -> block_no + target -> count;
	ready(target -> process_id, RESCHNO);
	freemem(target, sizeof(request_desc));

	// Disk Schedule
	dskschedule(ptr, PA4_DISK_SCHEDULE);
	
	if(ptr -> request_head) {
		for(target = ptr -> request_head;target -> next;target = target -> next);
		calculate_time(ptr, target -> block_no, &seek_time, &rotate_time);
		calculate_transfer_time(ptr, target -> block_no, target -> count, &transfer_time);
		ticks = ms_to_ticks(seek_time + rotate_time + transfer_time);
		target -> ticks = ticks;
		disk1_preempt = ticks;
	}
	else
		disk1_preempt = MAXINT;

	restore(ps);
	return OK;
}
// 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::printDeSerializer
void* deserialize_8( void* p )  { 
	void *result;
	uint8_t value;
	memncpy( (char *)&value,(char *)p,sizeof( char )  ) ;
	result = Construct_ByteAnyType0( value,"8" ) ;
	return result;
} 
// Generated from: uk.ac.stand.cs.insense.compiler.unixCCgen.TypeMarshaller::printDeSerializer
void* deserialize_r( void* p )  { 
	void *result;
	float value;
	memncpy( (char *)&value,(char *)p,sizeof( float )  ) ;
	result = Construct_RealAnyType0( value,"r" ) ;
	return result;
} 
// Generated from: uk.ac.stand.cs.insense.compiler.unixCCgen.TypeMarshaller::printDeSerializer
void* deserialize_u( void* p )  { 
	void *result;
	unsigned value;
	memncpy( (char *)&value,(char *)p,sizeof( int )  ) ;
	result = Construct_UnsignedIntAnyType0( value,"u" ) ;
	return result;
} 
// Generated from: uk.ac.stand.cs.insense.compiler.unixCCgen.TypeMarshaller::printDeSerializer
void* deserialize_i( void* p )  { 
	void *result;
	int value;
	memncpy( (char *)&value,(char *)p,sizeof( int )  ) ;
	result = Construct_IntAnyType0( value,"i" ) ;
	return result;
} 
// Generated from: uk.ac.stand.cs.insense.compiler.unixCCgen.TypeMarshaller::printDeSerializer
void* deserialize_b( void* p )  { 
	void *result;
	bool value;
	memncpy( (char *)&value,(char *)p,sizeof( int )  ) ;
	result = Construct_BoolAnyType0( value,"b" ) ;
	return result;
} 
Exemple #11
0
/*
 * get information from interface
 */
static int dev_ifconf(char *arg)
{
		struct ifconf ifc;
		struct ifreq ifr;
		struct net_device *dev;
		char *pos;
		int len;
//		int error;

		memncpy((char *)&ifc,arg,sizeof(struct ifconf));
		len = ifc.ifc_len;
		pos = ifc.ifc_buf;

		for(dev = dev_base; dev != NULL;dev = dev->next)
		{
				if(!(dev->flags & IFF_UP)) 
						continue;
				memset((char *)&ifr,0,sizeof(struct ifreq));
				strcpy(ifr.ifr_name,dev->name);
				(*((struct sockaddr_in *)&ifr.ifr_addr)).sin_addr.s_addr = dev->pa_addr;
				(*((struct sockaddr_in *)&ifr.ifr_addr)).sin_family = dev->family;
				memncpy(pos,(char *)&ifr,sizeof(struct ifreq));

				pos += sizeof(struct ifreq);
				len -= sizeof(struct ifreq);

				if(len < sizeof(struct ifreq))
						break;
		}

		ifc.ifc_len = (pos - ifc.ifc_buf);
		ifc.ifc_req = (struct ifreq*)ifc.ifc_buf;
		memncpy(arg,(char *)&ifc,sizeof(struct ifconf));

		return (pos - arg);
}
Exemple #12
0
bool split(char *result_str, char * origin_str, char split_chr, int split_index)
{
    char *origin_str_buf = NULL;
    char *result_str_buf = NULL;
    char *origin_str_buf_head = NULL;
    int current_split_index = -1;
    int old_str_index = 0;
    int new_str_index = 0;

    if (*origin_str == '\0' || origin_str == NULL || strlen(origin_str) == 0)
    {
        perror("Empty String");
        return false;
    }
    origin_str_buf = (char *)calloc(strlen(origin_str) + 1, sizeof(char));
    memcpy(origin_str_buf, origin_str, strlen(origin_str));
    origin_str_buf_head = origin_str_buf;
    while('\0' != *origin_str_buf)
    {
	if (split_chr == *origin_str_buf )
        {
	    if (new_str_index - old_str_index == 0)
	    {
		    old_str_index++;
		    new_str_index++;
		    origin_str_buf++;
		    continue;
	    }
	    free(result_str_buf);
	    result_str_buf  = NULL;
	    result_str_buf = (char *)calloc(new_str_index - old_str_index + 1, sizeof(char));
	    memncpy(result_str_buf, origin_str, old_str_index, new_str_index - old_str_index );
	    if(split_strip(result_str_buf, &split_chr) == false)
            {
                printf("String strip error.\n");
                free(result_str_buf);
                result_str_buf = NULL;
                free(origin_str_buf_head);
                origin_str_buf_head = NULL;
                return false;
            }
	    new_str_index++;
	    old_str_index = new_str_index;
            current_split_index++;
            if(current_split_index == split_index)
            {
                memcpy(result_str, result_str_buf, strlen(result_str_buf));
                free(result_str_buf);
                result_str_buf = NULL;
                free(origin_str_buf_head);
                origin_str_buf_head = NULL;
                return true;
            }
            else
            {
                free(result_str_buf);
                result_str_buf = NULL;
            }
        }
        else
        {
            new_str_index++;
        }
        origin_str_buf++;
    }
    if(current_split_index == -1 && old_str_index == new_str_index)
    {
        printf("Can't find split char.\n");
        free(origin_str_buf_head);
        origin_str_buf_head = NULL;
        return false;
    }
    else
    {
        result_str_buf = (char *)calloc(new_str_index - old_str_index + 1, sizeof(char));
        memncpy(result_str_buf, origin_str, old_str_index, new_str_index - old_str_index);
	if(split_strip(result_str_buf, &split_chr) == false)
        {
            printf("String strip error.\n");
            free(origin_str_buf_head);
            origin_str_buf_head = NULL;
            free(result_str_buf);
            result_str_buf = NULL;
            return false;
        }
        current_split_index++;
        if(current_split_index == split_index)
        {
            memcpy(result_str, result_str_buf, strlen(result_str_buf));
            free(origin_str_buf_head);
            origin_str_buf_head = NULL;
            free(result_str_buf);
            result_str_buf = NULL;
            return true;
        }
        else
        {
            printf("Split index error.\n");
            free(origin_str_buf_head);
            origin_str_buf_head = NULL;
            free(result_str_buf);
            result_str_buf = NULL;
            return false;
        }
    }
}