Beispiel #1
0
/*
 * ShmemDynAlloc
 */
void *
ShmemDynAlloc(Size size)
{
	void *block = NULL;
	Size padded_size;

	size = Max(ALIGN(size), MIN_ALLOC_SIZE);
	for (padded_size = 1; padded_size < size && padded_size <= 1024; padded_size *= 2);
	size = Max(size, padded_size);

	block = get_block(size);

	if (block == NULL)
	{
		/*
		 * Don't request fewer than 1k from ShmemAlloc.
		 * The more contiguous memory we have, the better we
		 * can combat fragmentation.
		 */
		Size alloc_size = Max(size, MIN_SHMEM_ALLOC_SIZE);
		block = ShmemAlloc(BLOCK_SIZE(alloc_size));

		memset(block, 0, BLOCK_SIZE(alloc_size));
		block = (void *) ((intptr_t) block + sizeof(Header));
		init_block(block, alloc_size, true);
		mark_allocated(block);
	}

	if (get_size(block) - size >= MIN_BLOCK_SIZE)
		split_block(block, size);

	Assert(is_allocated(block));

	return block;
}
Beispiel #2
0
static ULONG readwriteDisk
	(
		struct AFSBase *afsbase,
		struct Volume *volume,
		ULONG start,
		ULONG count,
		APTR mem,
		ULONG cmd
	)
{
LONG retval;
struct IOHandle *ioh = &volume->ioh;
UQUAD offset;

	if (start + count <= volume->countblocks)
	{
		ioh->ioreq->iotd_Req.io_Command = cmd;
		ioh->ioreq->iotd_Req.io_Length = count*BLOCK_SIZE(volume);
		ioh->ioreq->iotd_Req.io_Data = mem;

		offset  = (UQUAD)volume->startblock * volume->sectorsize
			+ (UQUAD)start * BLOCK_SIZE(volume);

		ioh->ioreq->iotd_Req.io_Offset = 0xFFFFFFFF & offset;
		ioh->ioreq->iotd_Req.io_Actual = offset>>32;
		retval = DoIO((struct IORequest *)&ioh->ioreq->iotd_Req);
		ioh->flags |= IOHF_MOTOR_OFF;
	}
Beispiel #3
0
/*
 * find a dir entry, return it if found, or return NULL.
 */
static const struct ext2_dir_entry *
ext2_find_entry(struct fs_info *fs, struct inode *inode, const char *dname)
{
    block_t index = 0;
    uint32_t i = 0, offset, maxoffset;
    const struct ext2_dir_entry *de;
    const char *data;
    size_t dname_len = strlen(dname);

    while (i < inode->size) {
	data = ext2_get_cache(inode, index++);
	offset = 0;
	maxoffset =  min(BLOCK_SIZE(fs), i-inode->size);

	/* The smallest possible size is 9 bytes */
	while (offset < maxoffset-8) {
	    de = (const struct ext2_dir_entry *)(data + offset);
	    if (de->d_rec_len > maxoffset - offset)
		break;

	    if (ext2_match_entry(dname, dname_len, de))
		return de;

	    offset += de->d_rec_len;
	}
	i += BLOCK_SIZE(fs);
    }

    return NULL;
}
Beispiel #4
0
static int iso_readdir(struct file *file, struct dirent *dirent)
{
    struct fs_info *fs = file->fs;
    struct inode *inode = file->inode;
    const struct iso_dir_entry *de;
    const char *data = NULL;
    char *rr_name = NULL;
    int name_len, ret;
    
    while (1) {
	size_t offset = file->offset & (BLOCK_SIZE(fs) - 1);

	if (!data) {
	    uint32_t i = file->offset >> BLOCK_SHIFT(fs);
	    if (i >= inode->blocks)
		return -1;
	    data = get_cache(fs->fs_dev, PVT(inode)->lba + i);
	}
	de = (const struct iso_dir_entry *)(data + offset);
	
	if (de->length < 33 || offset + de->length > BLOCK_SIZE(fs)) {
	    file->offset = (file->offset + BLOCK_SIZE(fs))
		& ~(BLOCK_SIZE(fs) - 1); /* Start of the next block */
	    data = NULL;
	    continue;
	}
	break;
    }
Beispiel #5
0
//------------------------------------------------------------------------------
OrbitalEquation::OrbitalEquation(Config *cfg,
                                 vector<bitset<BITS> > slaterDeterminants,
                                 Interaction *V,
                                 SingleParticleOperator *h):
    cfg(cfg)
{
    this->slaterDeterminants = slaterDeterminants;
    this->V = V;
    this->h = h;

    try {
        nParticles = cfg->lookup("system.nParticles");
        nGrid = cfg->lookup("spatialDiscretization.nGrid");
        nOrbitals = cfg->lookup("spatialDiscretization.nSpatialOrbitals");
    } catch (const SettingNotFoundException &nfex) {
        cerr << "OrbitalEquation::Error reading from config object." << endl;
        exit(EXIT_FAILURE);
    }
    nSlaterDeterminants = slaterDeterminants.size();
    invRho = cx_mat(nOrbitals, nOrbitals);

    U = zeros<cx_mat>(nGrid, nOrbitals);
    rightHandSide = zeros<cx_mat>(nGrid, nOrbitals);

//    Q = cx_mat(nGrid, nGrid);
    rho1 = zeros<cx_mat>(2*nOrbitals, 2*nOrbitals); // TMP

    myRank = 0;
    nNodes = 1;
#ifdef USE_MPI
    // MPI-------------------------------------------
    MPI_Comm_rank(MPI_COMM_WORLD, &myRank);
    MPI_Comm_size(MPI_COMM_WORLD, &nNodes);
#endif
    sizeRij = ivec(nNodes);
    int tot = nGrid*nOrbitals;

    allRH = imat(nGrid, nOrbitals);
    int node = 0;
    int s = 0;
    for (int j = 0; j < nOrbitals; j++) {
        for (int i = 0; i < nGrid; i++) {
            if (myRank == node){
                myRij.push_back(pair<int, int>(i,j));
            }
            allRH(i,j) = node;
            s++;
            if(s >= BLOCK_SIZE(node, nNodes, tot)){
                sizeRij(node) = BLOCK_SIZE(node, nNodes, tot);
                s = 0;
                node++;
            }
        }
    }
}
Beispiel #6
0
/*
 * init. the fs meta data, return the block size bits.
 */
static int ext2_fs_init(struct fs_info *fs)
{
    struct disk *disk = fs->fs_dev->disk;
    struct ext2_sb_info *sbi;
    struct ext2_super_block sb;
    struct cache *cs;

    /* read the super block */
    disk->rdwr_sectors(disk, &sb, 2, 2, 0);

    /* check if it is ext2, since we also support btrfs now */
    if (sb.s_magic != EXT2_SUPER_MAGIC)
	return -1;

    sbi = malloc(sizeof(*sbi));
    if (!sbi) {
	malloc_error("ext2_sb_info structure");
	return -1;
    }
    fs->fs_info = sbi;

    if (sb.s_magic != EXT2_SUPER_MAGIC) {
	printf("ext2 mount error: it's not a EXT2/3/4 file system!\n");
	return 0;
    }

    fs->sector_shift = disk->sector_shift;
    fs->block_shift  = sb.s_log_block_size + 10;
    fs->sector_size  = 1 << fs->sector_shift;
    fs->block_size   = 1 << fs->block_shift;

    sbi->s_inodes_per_group = sb.s_inodes_per_group;
    sbi->s_blocks_per_group = sb.s_blocks_per_group;
    sbi->s_inodes_per_block = BLOCK_SIZE(fs) / sb.s_inode_size;
    if (sb.s_desc_size < sizeof(struct ext2_group_desc))
	sb.s_desc_size = sizeof(struct ext2_group_desc);
    sbi->s_desc_per_block   = BLOCK_SIZE(fs) / sb.s_desc_size;
    sbi->s_groups_count     = (sb.s_blocks_count - sb.s_first_data_block
			       + EXT2_BLOCKS_PER_GROUP(fs) - 1)
	                      / EXT2_BLOCKS_PER_GROUP(fs);
    sbi->s_first_data_block = sb.s_first_data_block;
    sbi->s_inode_size = sb.s_inode_size;

    /* Volume UUID */
    memcpy(sbi->s_uuid, sb.s_uuid, sizeof(sbi->s_uuid));

    /* Initialize the cache, and force block zero to all zero */
    cache_init(fs->fs_dev, fs->block_shift);
    cs = _get_cache_block(fs->fs_dev, 0);
    memset(cs->data, 0, fs->block_size);
    cache_lock_block(cs);

    return fs->block_shift;
}
Beispiel #7
0
int vHeapSelfTest( int trace )
{
	{ // find out total size of heap
		xBlockLink *pxBlock = ( xBlockLink * )xHeap.ucHeap;
		int totalSize = 0;
		if( trace ) {
#ifdef DEBUG_HEAP_EXTRA
			DTRACE( "%s%11s%11s%11s%11s%11s%11s\n\r",
					"TAG", "Next Free", "Prev Block", "Block Size", 
					"Act. Size", "Caller", "Block Type");
#else /* ! DEBUG_HEAP_EXTRA */
			DTRACE( "%s%11s%11s%11s%11s%11s\n\r",
					"TAG", "My Addr", "Next Free", "Prev Block", "Block Size", "Block Type");
#endif /* DEBUG_HEAP_EXTRA */
			DTRACE( "HST%11x%11x%11x%11d%11c\n\r",
				&xStart, xStart.pxNextFreeBlock, xStart.pxPrev,  
					xStart.xBlockSize, 'X' );
		}

		/* A counter to prevent infinite loop */
		int max_sane_blocks = 10000;
	        while( max_sane_blocks-- ) {
			if( trace ) {
#ifdef DEBUG_HEAP_EXTRA
				DTRACE( "HST%11x%11x%11d%11d%11x ",
						pxBlock->pxNextFreeBlock, pxBlock->pxPrev, 
						BLOCK_SIZE( pxBlock ), GET_ACTUAL_SIZE( pxBlock ),
						GET_CALLER_ADDR( pxBlock ) );
#else /* ! DEBUG_HEAP_EXTRA */
				DTRACE( "HST%11x%11x%11x%11d ",
					pxBlock, pxBlock->pxNextFreeBlock, pxBlock->pxPrev, 
						BLOCK_SIZE( pxBlock ) );
#endif /* DEBUG_HEAP_EXTRA */
				if (BLOCK_SIZE( pxBlock ) == 0) {
					DTRACE("Unknown fault: Cannot find next block\n\r");
					break;
				}

				if( IS_ALLOCATED_BLOCK(pxBlock) ) {
					DTRACE( "A\n\r" );
				} else {
					DTRACE( "F\n\r" );
				}
			}
			totalSize += BLOCK_SIZE( pxBlock );
			if( IS_LAST_BLOCK( pxBlock ) )
				break;
			pxBlock = NEXT_BLOCK( pxBlock );
		}
		if( totalSize != configTOTAL_HEAP_SIZE )
			return 1;
	}
	return 0;
}
Beispiel #8
0
void _free(void * ptr){
	set_unused(ptr);
	void *next_ptr = NEXT_BLOCK(ptr);
	if( next_ptr!= NULL && !IS_USED(next_ptr)){
		set_block_size(ptr, BLOCK_SIZE(ptr) + BLOCK_SIZE(next_ptr));
	}

	void* pre_ptr = pre_block(ptr);
	if(pre_ptr != NULL && !IS_USED(pre_ptr)){
		set_block_size(pre_ptr, BLOCK_SIZE(ptr) + BLOCK_SIZE(pre_ptr));
	}

}
Beispiel #9
0
off_t stream_seek(stream_t* stream, off_t offset)
{
    off_t curr;

    offset *= BLOCK_SIZE(stream);
    curr = lseek(stream->fd, 0, SEEK_CUR);
    if(offset > curr)
        gstats.seek_forward++;
    else if(offset < curr)
        gstats.seek_backward++;
    curr = lseek(stream->fd, offset, SEEK_SET);
    stream->pos = curr / BLOCK_SIZE(stream);
    return stream->pos;
}
Beispiel #10
0
void		best_fit_split_block(t_mblk *block, size_t size)
{
	t_mblk	*new_block;

	new_block = (t_mblk *)((char *)block + BLOCK_SIZE(size));
	new_block->magic = FT_MALLOC_MAGIC;
	new_block->prev = block;
	new_block->next = block->next;
	new_block->size = block->size - BLOCK_SIZE(size);
	new_block->allocated = false;
	block->size = size;
	block->next = new_block;
	if (new_block->next)
		new_block->next->prev = new_block;
}
Beispiel #11
0
static bool
coalesce_blocks(void *ptr1, void *ptr2)
{
	void *tmpptr = Min(ptr1, ptr2);
	Size new_size;
	void *next;

	ptr2 = Max(ptr1, ptr2);
	ptr1 = tmpptr;

	if (get_end(ptr1) != get_header(ptr2))
		return false;

	Assert(get_next(ptr1) == ptr2);
	Assert(!is_allocated(ptr1));
	Assert(!is_allocated(ptr2));

	new_size = get_size(ptr1) + BLOCK_SIZE(get_size(ptr2));
	get_header(ptr1)->size = new_size;
	/* Mark ptr2 as no longer an ICE BOODA. */
	get_header(ptr2)->magic = 0;

	next = get_next(ptr2);
	set_next(ptr1, next);

	if (next)
		set_prev(next, ptr1);

	return true;
}
Beispiel #12
0
void* pre_block(void *ptr){
	void* start = HEAP_START;
	if(ptr == start)
		return NULL;

	return ptr - BLOCK_SIZE(ptr);
}
Beispiel #13
0
void mem_pool::pool_print( CLogger* log )
{
    int i, j;
    mempage_t* curr;

    for( i = 0; i < MEMORY_POOL_LISTS; ++i )
    {
        if( !(pool[i].first) )
            continue;

        if( pool_lock )
            pool_lock( i );

        log->DumpVar( "\npool[%d] chunk = %d", i, BLOCK_SIZE(i) );
        log->DumpVar( "\n\tpool[%d].page_size = %lu", i,
                (unsigned long)pool[i].page_size );
        log->DumpVar( "\n\tpool[%d].useable = %lu", i,
                (unsigned long)pool[i].useable );
        for( j = 0; j < MEMORY_POOL_BUFFER; ++j )
            log->DumpVar( "\n\tpool[%d].buffer[%d] = %p", i, j, pool[i].buffer[j] );
        log->DumpVar( "\n\tpool[%d].first = %p: ", i, pool[i].first );

        curr = pool[i].first;

        while( curr )
        {
            log->DumpVar( "\n\tnext = %p | count = %lu | free = %p",
                    curr->next, (unsigned long)curr->count, curr->free );
            curr = curr->next;
        }

        if( pool_unlock )
            pool_unlock( i );
    }
}
Beispiel #14
0
/*
 * Read one directory entry at a time
 */
static int ext2_readdir(struct file *file, struct dirent *dirent)
{
    struct fs_info *fs = file->fs;
    struct inode *inode = file->inode;
    const struct ext2_dir_entry *de;
    const char *data;
    block_t index = file->offset >> fs->block_shift;

    if (file->offset >= inode->size)
	return -1;		/* End of file */

    data = ext2_get_cache(inode, index);
    de = (const struct ext2_dir_entry *)
	(data + (file->offset & (BLOCK_SIZE(fs) - 1)));

    dirent->d_ino = de->d_inode;
    dirent->d_off = file->offset;
    dirent->d_reclen = offsetof(struct dirent, d_name) + de->d_name_len + 1;
    dirent->d_type = ext2_cvt_type(de->d_file_type);
    memcpy(dirent->d_name, de->d_name, de->d_name_len);
    dirent->d_name[de->d_name_len] = '\0';

    file->offset += de->d_rec_len;  /* Update for next reading */

    return 0;
}
Beispiel #15
0
/*-----------------------------------------------------------------------------
*   Create a new MemBlock, return NULL on out of memory
*----------------------------------------------------------------------------*/
static MemBlock *new_block( size_t client_size, char *file, int lineno )
{
    MemBlock *block;
    size_t    block_size;

    /* create memory to hold MemBlock + client area + fence */
    block_size = BLOCK_SIZE( client_size );

    block = malloc( block_size );
    check( block,
           "memory alloc (%u bytes) failed at %s:%d", client_size, file, lineno );

    /* init block */
    block->signature   = MEMBLOCK_SIGN;
    block->destructor  = NULL;
    block->flags.in_collection	 = FALSE;
    block->flags.destroy_atextit = FALSE;
    block->client_size = client_size;
    block->file        = file;
    block->lineno      = lineno;

    /* fill fences */
    memset( START_FENCE_PTR( block ), FENCE_SIGN, FENCE_SIZE );
    memset( END_FENCE_PTR( block ),   FENCE_SIGN, FENCE_SIZE );

    /* add to list of blocks in reverse order, so that cleanup is reversed */
    DL_PREPEND(g_mem_blocks, block);

    return block;

error:
    return NULL;
}
Beispiel #16
0
/* get the block size */
int fastlzlibGetBlockSize(zfast_stream *s) {
  if (s != NULL && s->state != NULL) {
    assert(strcmp(s->state->magic, MAGIC) == 0);
    return BLOCK_SIZE(s);
  }
  return 0;
}
Beispiel #17
0
static bool	check_extend_block(t_ctx *ctx, t_mblk *block, size_t size)
{
	(void)ctx;
	if (block->next && !block->next->allocated)
		return (block->size + BLOCK_SIZE(block->next->size) >= size);
	return (false);
}
Beispiel #18
0
int main (int argc, char *argv[]) {
   dtype **a;       /* First factor, a matrix */
   dtype *b;        /* Second factor, a vector */
   dtype *c_block;  /* Partial product vector */
   dtype *c;        /* Replicated product vector */
   double    max_seconds;
   double    seconds;    /* Elapsed time for matrix-vector multiply */
   dtype *storage;  /* Matrix elements stored here */
   int    i, j;     /* Loop indices */
   int    id;       /* Process ID number */
   int    m;        /* Rows in matrix */
   int    n;        /* Columns in matrix */
   int    nprime;   /* Elements in vector */
   int    p;        /* Number of processes */
   int    rows;     /* Number of rows on this process */
   int    its;

   MPI_Init (&argc, &argv);
   MPI_Comm_rank (MPI_COMM_WORLD, &id);
   MPI_Comm_size (MPI_COMM_WORLD, &p);

   read_row_striped_matrix (argv[1], (void *) &a,
      (void *) &storage, mpitype, &m, &n, MPI_COMM_WORLD);
   rows = BLOCK_SIZE(id,p,m);
   print_row_striped_matrix ((void **) a, mpitype, m, n,
      MPI_COMM_WORLD);

   read_replicated_vector (argv[2], (void *) &b, mpitype,
      &nprime, MPI_COMM_WORLD);
   print_replicated_vector (b, mpitype, nprime,
      MPI_COMM_WORLD);

   c_block = (dtype *) malloc (rows * sizeof(dtype));
   c = (dtype *) malloc (n * sizeof(dtype));
   MPI_Barrier (MPI_COMM_WORLD);
   seconds = - MPI_Wtime();
   for (i = 0; i < rows; i++) {
      c_block[i] = 0.0;
      for (j = 0; j < n; j++)
         c_block[i] += a[i][j] * b[j];
   }

   replicate_block_vector (c_block, n, (void *) c, mpitype,
      MPI_COMM_WORLD);
   MPI_Barrier (MPI_COMM_WORLD);
   seconds += MPI_Wtime();

   print_replicated_vector (c, mpitype, n, MPI_COMM_WORLD);

   MPI_Allreduce (&seconds, &max_seconds, 1, mpitype, MPI_MAX,
      MPI_COMM_WORLD);
   if (!id) {
      printf ("MV1) N = %d, Processes = %d, Time = %12.6f sec,",
         n, p, max_seconds);
      printf ("Mflop = %6.2f\n", 2*n*n/(1000000.0*max_seconds));
   }
   MPI_Finalize();
   return 0;
}
Beispiel #19
0
void* allocate_block(void * ptr, size_t size){
	size_t ex_size = BLOCK_SIZE(ptr);
	set_used(ptr);
	set_block_size(ptr, size);
	set_next_block(ptr, ex_size, size);

	return ptr + sizeof(header);
}
Beispiel #20
0
void *VectMul (void *th)
{
	long Tid = (long) th;

	// interleaving communication with computation

	if (Tid == NCORES-1) // this thread handles communication
	{
		if (id == 0)
		{
			MPI_Isend (v3[BLOCK_LOW(id,p,VLEN)], BLOCK_SIZE(id,p,VLEN), mpntype1, id^1, 0, MPI_COMM_WORLD, &Srqst);
			MPI_Irecv (v3[BLOCK_LOW(id^1,p,VLEN)], BLOCK_SIZE(id^1,p,VLEN), mpntype1, id^1, 1, MPI_COMM_WORLD, &Rrqst);
		}
		else if (id == 1)
		{
			MPI_Isend (v3[BLOCK_LOW(id,p,VLEN)], BLOCK_SIZE(id,p,VLEN), mpntype1, id^1, 1, MPI_COMM_WORLD, &Srqst);
			MPI_Irecv (v3[BLOCK_LOW(id^1,p,VLEN)], BLOCK_SIZE(id^1,p,VLEN), mpntype1, id^1, 0, MPI_COMM_WORLD, &Rrqst);
		}
	}
	else if (Tid < NCORES-1) // these threads handle computation
	{
		vectvectmul (v1, v2, Tid); // product of two vectors

		// reduction within a node using threads
		pthread_mutex_lock (&mutexvv);
		mpn_add_n (fin_sum, fin_sum, temp_sum[Tid], 2*LIMBS+1);
		pthread_mutex_unlock (&mutexvv);

		// each thread waits for every other thread to finish reduction
		wait (NCORES-1);

		// reduction across nodes using MPI
		if (Tid == 0)
		{
			MPI_Allreduce (fin_sum, result, 1, mpntype0, mpn_sum, MPI_COMM_WORLD);
			mpn_tdiv_qr (tquo, cnum, 0, result, 2*LIMBS+1, q, LIMBS);
		}
		wait (NCORES-1);
	}

	if (Tid == NCORES-1)  // waiting for the communications to terminate
	{
		MPI_Wait (&Srqst, &Sstatus);
		MPI_Wait (&Rrqst, &Rstatus);
	}
}
Beispiel #21
0
size_t vPortBiggestFreeBlockSize()
{
	xBlockLink *pxBlock = &xStart;
	while( pxBlock->pxNextFreeBlock != &xEnd )
		pxBlock = pxBlock->pxNextFreeBlock;

	return BLOCK_SIZE(pxBlock);
}
Beispiel #22
0
/*-----------------------------------------------------------*/
void vPortFree( void *pv )
{
pre_free_hook(pv);
unsigned char *puc = ( unsigned char * ) pv;
xBlockLink *pxLink;

 ASSERT( ( pv >= (void*)WMSDK_HEAP_START_ADDR ) ||
	 ( pv == NULL ) );
 ASSERT( ( pv <= (void*)lastHeapAddress ) ||
	 ( pv == NULL ) );

	if( pv )
	{
		/* The memory being freed will have an xBlockLink structure immediately
		before it. */
		puc -= heapSTRUCT_SIZE;

		/* This casting is to keep the compiler from issuing warnings. */
		pxLink = ( void * ) puc;

		ATRACE("MDC F %10x %6d %10d R: %x\r\n", 
		      puc + heapSTRUCT_SIZE, BLOCK_SIZE( pxLink ), 
		      xFreeBytesRemaining + BLOCK_SIZE(pxLink),
		      __builtin_return_address(0));

		post_free_hook(((unsigned)puc + heapSTRUCT_SIZE), 
				GET_ACTUAL_SIZE( pxLink ));

		randomizeAreaData((unsigned char*)((unsigned)puc + heapSTRUCT_SIZE),
				  BLOCK_SIZE( pxLink ) - heapSTRUCT_SIZE);

		vTaskSuspendAll();
		{
			/* Add this block to the list of free blocks. */
			SET_FREE( pxLink );
			xFreeBytesRemaining += BLOCK_SIZE(pxLink);
			prvInsertBlockIntoFreeList( ( ( xBlockLink * ) pxLink ) );
#ifdef FREERTOS_ENABLE_MALLOC_STATS
			hI.totalAllocations--;
#endif // FREERTOS_ENABLE_MALLOC_STATS
		}
		xTaskResumeAll();
	}
}
// p_ps->s_tpr should be initialized by caller with desired ring parameters.
// ifname should specify the name of the interface to bind to (e.g. "eth2").
// ring_type should be PACKET_RX_RING or PACKET_TX_RING.
//
// Returns 0 for success, non-zero for failure.  On failure, errno will be set.
//
// Upon successful compltetion, p_ps->fd will the the file descriptor of the
// socket, and p_ps->p_ring will be a poitner to the start of the ring buffer.
int hashpipe_pktsock_open(struct hashpipe_pktsock *p_ps, const char *ifname, int ring_type)
{
  struct ifreq s_ifr;
  struct sockaddr_ll my_addr;
  struct tpacket_req s_tpr;

  // Validate that nframes is multiple of nblocks
  // and that block size is a multiple of page size
  if(p_ps->nframes % p_ps->nblocks != 0
  || BLOCK_SIZE(p_ps) % sysconf(_SC_PAGESIZE) != 0) {
    errno = EINVAL;
    return -1;
  }

  // Create socket
  p_ps->fd = socket(PF_PACKET, SOCK_RAW, htons(PKTSOCK_PROTO));
  if(p_ps->fd == -1) {
    return -2;
  }

  /* get interface index of ifname */
  strncpy (s_ifr.ifr_name, ifname, sizeof(s_ifr.ifr_name));
  ioctl(p_ps->fd, SIOCGIFINDEX, &s_ifr);

  /* fill sockaddr_ll struct to prepare binding */
  my_addr.sll_family = AF_PACKET;
  my_addr.sll_protocol = htons(PKTSOCK_PROTO);
  my_addr.sll_ifindex =  s_ifr.ifr_ifindex;

  /* bind socket to interface */
  if(bind(p_ps->fd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr_ll))
      == -1) {
    return -3;
  }

  // Set socket options
  s_tpr.tp_block_size = p_ps->frame_size * p_ps->nframes / p_ps->nblocks;
  s_tpr.tp_block_nr = p_ps->nblocks;
  s_tpr.tp_frame_size = p_ps->frame_size;
  s_tpr.tp_frame_nr = p_ps->nframes;
  if(setsockopt(p_ps->fd, SOL_PACKET,
        ring_type, (void *) &s_tpr, sizeof(s_tpr)) == -1) {
    return -4;
  }

  // Map ring into memory space
  size_t size = p_ps->frame_size * p_ps->nframes;
  p_ps->p_ring = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, p_ps->fd, 0);
  if(p_ps->p_ring == (void *)-1) {
    return -5;
  }
  p_ps->next_idx = 0;

  return 0;
}
Beispiel #24
0
int block_read(stream_t* stream, void* b)
{
    ssize_t n;
    gstats.block_read++;
    n = read(stream->fd, b, BLOCK_SIZE(stream));
    if(n == -1)
        perror("Error reading block: ");
    gstats.bytes_read += n;
    stream->pos++;
    return 0;
}
Beispiel #25
0
static void	*extend_block(t_ctx *ctx, t_mblk *block, size_t size)
{
	(void)ctx;
	block->size += BLOCK_SIZE(block->next->size);
	if (block->next->next)
		block->next->next->prev = block;
	block->next = block->next->next;
	if ((block->size - size) >= sizeof(t_mblk))
		best_fit_split_block(block, size);
	return (DATA_PTR(block));
}
Beispiel #26
0
int block_write(stream_t* stream, void* b)
{
    ssize_t n;
    gstats.block_write++;
    printf("Block: 0x%X\n", (unsigned int)b);
    n = write(stream->fd, b, BLOCK_SIZE(stream));
    stream->pos++;
    if(n == -1)
        perror("Error writing block: ");
    gstats.bytes_written += n;
    return 0;
}
Beispiel #27
0
void response_handler(const CAEndpoint_t *object, const CAResponseInfo_t *responseInfo)
{
    printf("##########Received response from remote device #############\n");
    if (CA_ADAPTER_IP == object->adapter)
    {
        printf("Remote Address: %s Port: %d secured:%d\n", object->addr,
               object->port, object->flags & CA_SECURE);
    }
    else
    {
        printf("Remote Address: %s \n", object->addr);
    }
    printf("response result : %d\n", responseInfo->result);
    printf("Data: %s\n", responseInfo->info.payload);
    printf("Message type: %s\n", MESSAGE_TYPE[responseInfo->info.type]);
    printf("Token: %s\n", responseInfo->info.token);
    printf("Resource URI: %s \n", responseInfo->info.resourceUri);

    if (responseInfo->info.options)
    {
        uint32_t len = responseInfo->info.numOptions;
        uint32_t i;
        for (i = 0; i < len; i++)
        {
            printf("Option %d\n", i + 1);
            printf("ID : %d\n", responseInfo->info.options[i].optionID);
            printf("Data[%d]: %s\n", responseInfo->info.options[i].optionLength,
                   responseInfo->info.options[i].optionData);
        }
    }
    printf("############################################################\n");
    g_received = 1;

    //Check if this has secure communication information
    if (responseInfo->info.payload)
    {
        int securePort = get_secure_information(responseInfo->info.payload);
        if (0 < securePort) //Set the remote endpoint secure details and send response
        {
            printf("This is secure resource...\n");
        }
    }

#ifdef WITH_BWT
    // if received message is bulk data, create output file
    if ((responseInfo->info.payload) &&
            (responseInfo->info.payloadSize > BLOCK_SIZE(CA_DEFAULT_BLOCK_SIZE)))
    {
        create_file(responseInfo->info.payload, responseInfo->info.payloadSize);
    }
#endif
}
Beispiel #28
0
unsigned long buddy_estimate_bin(struct buddy_allocator *ba,
				 unsigned long size)
{
	unsigned long bin, ret;

	if (!ba) {
		return 0;
	}

	if (size < BLOCK_SIZE(ba->min_bin)) {
		return ba->min_bin;
	}

	ret = ba->max_bin;
	for (bin = ba->min_bin; bin < ba->max_bin; bin++) {
		if (size <= BLOCK_SIZE(bin)) {
			ret = bin;
			break;
		}
	}

	return ret;
}
Beispiel #29
0
t_mblk		*best_fit_extend_heap(t_mblk *prev, size_t size)
{
	t_mblk	*block;

	block = (t_mblk *)sbrk(BLOCK_SIZE(size));
	if (block == (void *)-1)
		return (NULL);
	block->magic = FT_MALLOC_MAGIC;
	block->prev = prev;
	block->next = NULL;
	block->size = size;
	block->allocated = true;
	if (prev)
		prev->next = block;
	return (block);
}
Beispiel #30
0
static int ext2_readlink(struct inode *inode, char *buf)
{
    struct fs_info *fs = inode->fs;
    int sec_per_block = 1 << (fs->block_shift - fs->sector_shift);
    bool fast_symlink;

    if (inode->size > BLOCK_SIZE(fs))
	return -1;		/* Error! */

    fast_symlink = (inode->file_acl ? sec_per_block : 0) == inode->blocks;
    if (fast_symlink)
	memcpy(buf, PVT(inode)->i_block, inode->size);
    else
	cache_get_file(inode, buf, inode->size);

    return inode->size;
}