int is_effectual_move(Board b, Move m) { Board tmp = rotate_for_move(b, m); int this, i; int data[BOARDSIZE]; for (int r=0; r<BOARDSIZE; r++) { i = 0; memset(data, 0, sizeof(int)*BOARDSIZE); for (int c=BOARDSIZE-1; c >= 0; c--) { this = bget(tmp, r, c); if (this != 0) { if (data[i] == 0) { data[i] = this; } else if (this == data[i]) { data[i] += this; i++; } else { i++; data[i] = this; } } } for (int c=0; c<BOARDSIZE; c++) { if (bget(tmp, r, c) != data[BOARDSIZE-1-c]) { free_board(tmp); return 1; } } } free_board(tmp); return 0; }
packet_data* pd_alloc(uint16_t size, uint16_t block_size) { packet_data* pd = NULL; packet_data* next = NULL; while(size) { uint16_t alloc_size = size > block_size ? block_size : size; if (next == NULL) { pd = bget(align_size(sizeof(packet_data) + alloc_size)); next = pd; } else { next->next = bget(align_size(sizeof(packet_data) + alloc_size)); next = next->next; } if (next == NULL) { pd_free(pd); return NULL; } pd->data = (uint8_t*)pd + sizeof(packet_data); pd->next = NULL; pd->size = alloc_size; pd->total_size = size; pd->refs_count = 1; size -= alloc_size; } return pd; }
void *Malloc(ulong_t size) { bool iflag; void *result; result = bget(size); if (result) memset(result, '\0', size); if (!result) { // Print("Kernel Malloc pool exhaused, shutting down\n"); sbrk(size); result = bget(size); if (result) memset(result, '\0', size); if (!result) { // Hardware_Shutdown(); } } return result; }
int max_tile(Board b) { int max = 0; for (int r=0; r<BOARDSIZE; r++) for (int c=0; c<BOARDSIZE; c++) if (bget(b, r, c) > max) max = bget(b, r, c); return max; }
void *bgetr(void *buf, bufsize size) { void *nbuf; bufsize osize; /* Old size of buffer */ struct bhead *b; nbuf = bget(size); if ((nbuf != NULL) && (buf != NULL)) { /* Acquire new buffer */ b = BH(getPointerOffset(buf, -(int32_t)sizeof(struct bhead))); osize = -b->bsize; #ifdef BECtl if (osize == 0) { /* Buffer acquired directly through acqfcn. */ struct bdhead *bd; bd = BDH(getPointerOffset(buf, -(int32_t)sizeof(struct bdhead))); osize = bd->tsize - (int32_t)sizeof(struct bdhead); } else{ #endif osize -= (int32_t)sizeof(struct bhead); #ifdef BECtl } #endif assert(osize > 0); V memcpy((int8_t *) nbuf, (int8_t *) buf, /* Copy the data */ (MemSize) ((size < osize) ? size : osize)); brel(buf); } return nbuf; }
void* BGet::bgetr(void *buf, bufsize size) { void *nbuf; bufsize osize; /* Old size of buffer */ struct bhead *b; if ((nbuf = bget(size)) == NULL) { /* Acquire new buffer */ return NULL; } if (buf == NULL) { return nbuf; } b = BH(((char *) buf) - sizeof(struct bhead)); osize = -b->bsize; #ifdef BECtl if (osize == 0) { /* Buffer acquired directly through acqfcn. */ struct bdhead *bd; bd = BDH(((char *) buf) - sizeof(struct bdhead)); osize = bd->tsize - sizeof(struct bdhead); } else #endif osize -= sizeof(struct bhead); assert(osize > 0); V memcpy((char *) nbuf, (char *) buf, /* Copy the data */ (MemSize) ((size < osize) ? size : osize)); brel(buf); return nbuf; }
/* ******************************************************************** */ PFBYTE _ks_malloc(int num_elements, int size) { PFBYTE ptr; # if (defined (RTKBCPP)) ptr = ks_dpmi_alloc((word)(num_elements * size)); # elif (INCLUDE_BGET) ptr = ((PFBYTE)bget(num_elements * size)); # elif (INCLUDE_WINSOCK || INCLUDE_BSDSOCK) ptr = malloc(num_elements * size); # elif (defined(SEG_IAR)) /* protected mode */ ptr = malloc(num_elements * size); # elif ( defined(__BORLANDC__) ) /* real mode */ ptr = _fmalloc(num_elements * size); # else #error: ks_malloc needs to be implemented # endif #if (DISPLAY_MALLOC) DEBUG_ERROR("ks_malloc allocs: ", DINT1, ptr, 0); #endif return(ptr); }
void dec_prepare_input_file(FILE *fp) { printf("Preparing input file...\n"); input_file_header = malloc(sizeof(binheader)); printf("Getting header...\n"); binh_get_header(input_file_header, fp, &frequencies, &frequency_length, &firsts, &max_bits, &nbits_run, &nbits_code); first_enc = input_file_header->firstEncoding; sec_enc = input_file_header->secondEncoding; third_enc = input_file_header->thirdEncoding; output_buffer = calloc(input_file_header->numChannels, sizeof(bitbuffer)); output_vector = calloc(input_file_header->numChannels, sizeof(uint32_t *)); /* TODO: is this right??? memset(output_vector, 0, input_file_header->numChannels*sizeof(uint32_t *)); */ data_buffer = calloc(input_file_header->numChannels, sizeof(bitbuffer)); data_vector = calloc(input_file_header->numChannels, sizeof(uint32_t *)); for(curr_channel=0; curr_channel<input_file_header->numChannels; curr_channel++){ printf("Reading data buffer"); if( bget(&(output_buffer[curr_channel]), fp)){ ERROR("Couldn't read bit buffer from input file"); } } }
/* BufferPoolAllocate * Allocates the requested size and outputs two addresses. The * virtual pointer to the accessible data, and the address of its * corresponding physical address for hardware. */ OsStatus_t BufferPoolAllocate( _In_ BufferPool_t* Pool, _In_ size_t Size, _Out_ uintptr_t** VirtualPointer, _Out_ uintptr_t* PhysicalAddress) { // Variables void *Allocation = NULL; // Debug TRACE("BufferPoolAllocate(Size %u)", Size); // Perform an allocation Allocation = bget(Pool->Pool, Size); if (Allocation == NULL) { ERROR("Failed to allocate bufferpool memory (size %u)", Size); return OsError; } // Calculate the addresses and update out's *VirtualPointer = (uintptr_t*)Allocation; *PhysicalAddress = GetBufferDma(Pool->Buffer) + ((uintptr_t)Allocation - (uintptr_t)GetBufferDataPointer(Pool->Buffer)); TRACE(" > Virtual address 0x%x => Physical address 0x%x", (uintptr_t*)Allocation, *PhysicalAddress); return OsSuccess; }
static int join_leaf ( tree_s *tree, branch_s *parent, leaf_s *child, int k) { leaf_s *sibling; sibling = bget(tree->t_dev, parent->br_key[k+1].k_block); if (!sibling) return qERR_NOT_FOUND; if (child->l_total + sibling->l_total > MAX_FREE) { FN; compact(child); compact(sibling); copy_recs(child, sibling, 0); memmove( &parent->br_key[k+1], &parent->br_key[k+2], sizeof(parent->br_key[0]) * (parent->br_num - (k+2))); --parent->br_num; bdirty(parent); } //verify_leaf(child, WHERE); bput(sibling); // Should free sibling return 0; }
int is2048(Board b) { for (int r=0; r<BOARDSIZE; r++) for (int c=0; c<BOARDSIZE; c++) if (bget(b, r, c) == 2048) return 1; return 0; }
static int join_branch ( tree_s *tree, branch_s *parent, branch_s *child, int k) { branch_s *sibling; sibling = bget(tree->t_dev, parent->br_key[k+1].k_block); if (!sibling) { return qERR_NOT_FOUND; } if (child->br_num+sibling->br_num < KEYS_PER_BRANCH-1) { FN; child->br_key[child->br_num].k_key = parent->br_key[k+1].k_key; child->br_key[child->br_num].k_block = sibling->br_first; ++child->br_num; memmove( &child->br_key[child->br_num], sibling->br_key, sibling->br_num * sizeof(sibling->br_key[0])); child->br_num += sibling->br_num; bdirty(child); memmove( &parent->br_key[k+1], &parent->br_key[k+2], sizeof(parent->br_key[0]) * (parent->br_num - (k+2))); --parent->br_num; bdirty(parent); } bput(sibling); // Sibling should be freed. return 0; }
/* bgetr * Reallocate a buffer. This is a minimal implementation, * simply in terms of brel() and bget(). It could be * enhanced to allow the buffer to grow into adjacent free * blocks and to avoid moving data unnecessarily. */ void* bgetr( _In_ BytePool_t *pool, _In_ void *buf, _In_ long size) { // Variables void *nbuf; long osize; /* Old size of buffer */ struct bhead *b; if ((nbuf = bget(pool, size)) == NULL) { /* Acquire new buffer */ return NULL; } if (buf == NULL) { return nbuf; } b = BH(((char *) buf) - sizeof(struct bhead)); osize = -b->bsize; #ifdef BECtl if (osize == 0) { /* Buffer acquired directly through acqfcn. */ struct bdhead *bd; bd = BDH(((char *) buf) - sizeof(struct bdhead)); osize = bd->tsize - sizeof(struct bdhead); } else #endif osize -= sizeof(struct bhead); assert(osize > 0); V memcpy((char *) nbuf, (char *) buf, /* Copy the data */ (MemSize) ((size < osize) ? size : osize)); brel(pool, buf); return nbuf; }
/* bgetz * Allocate a buffer and clear its contents to zero. We clear * the entire contents of the buffer to zero, not just the * region requested by the caller. */ void* bgetz( _In_ BytePool_t *pool, _In_ long size) { char *buf = (char *) bget(pool, size); if (buf != NULL) { struct bhead *b; long rsize; b = BH(buf - sizeof(struct bhead)); rsize = -(b->bsize); if (rsize == 0) { struct bdhead *bd; bd = BDH(buf - sizeof(struct bdhead)); rsize = bd->tsize - sizeof(struct bdhead); } else { rsize -= sizeof(struct bhead); } assert(rsize >= size); V memset(buf, 0, (MemSize) rsize); } return ((void *) buf); }
static int insert_branch ( tree_s *tree, branch_s *parent, u64 key, void *rec, unint len) { void *child; int k; /* Critical that this be signed */ FN; for (;;) { do { k = binary_search_branch(key, parent->br_key, parent->br_num); child = bget(bdev(parent), parent->br_key[k].k_block); if (!child) { bput(parent); return qERR_NOT_FOUND; } } while (split(child, parent, k, len)); bput(parent); if (type(child) != BRANCH) { return insert_head(tree, child, key, rec, len); } parent = child; } }
static void *raw_malloc(size_t hdr_size, size_t ftr_size, size_t pl_size) { void *ptr = NULL; size_t s = hdr_size + ftr_size + pl_size; /* * Make sure that malloc has correct alignment of returned buffers. * The assumption is that uintptr_t will be as wide as the largest * required alignment of any type. */ COMPILE_TIME_ASSERT(SizeQuant >= sizeof(uintptr_t)); raw_malloc_validate_pools(); /* Check wrapping */ if (s < pl_size) goto out; /* BGET doesn't like 0 sized allocations */ if (!s) s++; ptr = bget(s); out: raw_malloc_return_hook(ptr, pl_size); return ptr; }
/* * ino_dirty() * Purge a directory entry back to the fs * * Whenever we have completed a series of modifications to some inode * data we need to write them back to the disk in the directory. */ void ino_dirty(struct inode *i) { struct dirent *d; void *handle; /* * First off grab the directory info off the disk */ handle = bget(i->i_dirblk); if (handle == NULL ) { perror("bfs ino_dirty"); exit(1); } d = (struct dirent *)((char *)bdata(handle) + i->i_diroff); /* * Then write the new information into the buffer */ memcpy(d->d_name, i->i_name, BFSNAMELEN); d->d_inum = i->i_num; d->d_start = i->i_start; d->d_len = i->i_fsize; /* * Then mark the buffer dirty and free the space */ bdirty(handle); bfree(handle); }
static lgfs2_rgrps_t mockup_rgrp(void) { struct gfs2_sbd *sdp; lgfs2_rgrps_t rgs; unsigned i; uint64_t addr; struct gfs2_rindex ri = {0}; lgfs2_rgrp_t rg; uint32_t rgsize = (1024 << 20) / 4096; sdp = calloc(1, sizeof(*sdp)); fail_unless(sdp != NULL); sdp->device.length = rgsize + 20; sdp->device_fd = -1; sdp->bsize = sdp->sd_sb.sb_bsize = 4096; compute_constants(sdp); rgs = lgfs2_rgrps_init(sdp, 0, 0); fail_unless(rgs != NULL); lgfs2_rgrps_plan(rgs, sdp->device.length - 16, rgsize); addr = lgfs2_rindex_entry_new(rgs, &ri, 16, rgsize); ck_assert(addr != 0); rg = lgfs2_rgrps_append(rgs, &ri); fail_unless(rg != NULL); for (i = 0; i < rg->ri.ri_length; i++) { rg->bits[i].bi_bh = bget(sdp, rg->ri.ri_addr + i); fail_unless(rg->bits[i].bi_bh != NULL); } return rgs; }
/** * Read a field from a file to the IO library. * * @param ncid identifies the netCDF file * @param varid the variable ID to be read * @param ioid: the I/O description ID as passed back by * PIOc_InitDecomp(). * @param arraylen: the length of the array to be read. This * is the length of the distrubited array. That is, the length of * the portion of the data that is on the processor. * @param array: pointer to the data to be read. This is a * pointer to the distributed portion of the array that is on this * processor. * @return 0 for success, error code otherwise. * @ingroup PIO_read_darray * @author Jim Edwards, Ed Hartnett */ int PIOc_read_darray(int ncid, int varid, int ioid, PIO_Offset arraylen, void *array) { iosystem_desc_t *ios; /* Pointer to io system information. */ file_desc_t *file; /* Pointer to file information. */ io_desc_t *iodesc; /* Pointer to IO description information. */ void *iobuf = NULL; /* holds the data as read on the io node. */ size_t rlen = 0; /* the length of data in iobuf. */ int ierr; /* Return code. */ /* Get the file info. */ if ((ierr = pio_get_file(ncid, &file))) return pio_err(NULL, NULL, PIO_EBADID, __FILE__, __LINE__); ios = file->iosystem; /* Get the iodesc. */ if (!(iodesc = pio_get_iodesc_from_id(ioid))) return pio_err(ios, file, PIO_EBADID, __FILE__, __LINE__); pioassert(iodesc->rearranger == PIO_REARR_BOX || iodesc->rearranger == PIO_REARR_SUBSET, "unknown rearranger", __FILE__, __LINE__); /* ??? */ if (ios->iomaster == MPI_ROOT) rlen = iodesc->maxiobuflen; else rlen = iodesc->llen; /* Allocate a buffer for one record. */ if (ios->ioproc && rlen > 0) if (!(iobuf = bget(iodesc->mpitype_size * rlen))) return pio_err(ios, file, PIO_ENOMEM, __FILE__, __LINE__); /* Call the correct darray read function based on iotype. */ switch (file->iotype) { case PIO_IOTYPE_NETCDF: case PIO_IOTYPE_NETCDF4C: if ((ierr = pio_read_darray_nc_serial(file, iodesc, varid, iobuf))) return pio_err(ios, file, ierr, __FILE__, __LINE__); break; case PIO_IOTYPE_PNETCDF: case PIO_IOTYPE_NETCDF4P: if ((ierr = pio_read_darray_nc(file, iodesc, varid, iobuf))) return pio_err(ios, file, ierr, __FILE__, __LINE__); break; default: return pio_err(NULL, NULL, PIO_EBADIOTYPE, __FILE__, __LINE__); } /* Rearrange the data. */ if ((ierr = rearrange_io2comp(ios, iodesc, iobuf, array))) return pio_err(ios, file, ierr, __FILE__, __LINE__); /* Free the buffer. */ if (rlen > 0) brel(iobuf); return PIO_NOERR; }
/* Returns 1 if equal, 0 if not */ int explicit_equal(Board b, int exp[BOARDSIZE][BOARDSIZE]) { for (int r=0; r<BOARDSIZE; r++) { for (int c=0; c<BOARDSIZE; c++) { if (exp[r][c] != bget(b,r,c)) return 0; } } return 1; }
PointList open_spaces(Board b) { PointList pl = pl_create(); for (int r=0; r<BOARDSIZE; r++) { for (int c=0; c<BOARDSIZE; c++) { if (bget(b, r, c) == 0) pl_insert(pl, r, c); } } return pl; }
// Return a B_BUSY buf with the contents of the indicated disk sector. struct buf* bread(uint dev, uint sector) { struct buf *b; b = bget(dev, sector); if(!(b->flags & B_VALID)) memiderw(b); return b; }
void *malloc(int size) { void *p; mutex_lock(&malloc_lock); p = bget(size); mutex_unlock(&malloc_lock); return p; }
//------------------------------------------------------------------------------------------------------------------- // breadpolling() -- Return a B_BUSY buf with the contents of the indicated disk sector. Use polling instead of // interrupts. //------------------------------------------------------------------------------------------------------------------- struct buf *breadpolling(uint dev, uint sector) { extern void iderwpoll(struct buf *b); struct buf *b; b = bget(dev, sector); if (!(b->flags & B_VALID)) iderwpoll(b); return b; }
// Return a B_BUSY buf with the contents of the indicated block. struct buf* bread(uint dev, uint blockno) { struct buf *b; b = bget(dev, blockno); if(!(b->flags & B_VALID)) { iderw(b); } return b; }
// Return a B_BUSY buf with the contents of the indicated block. struct buf* bread(uint dev, uint blockno) { struct buf *b; //回收回来的要么是一个已经存在的,但可能是dirty的数据。也有可能是个什么都没有的。 //0和dirty是不一样的。dirty是写回去,0是直接从硬盘里面读数据的。 b = bget(dev, blockno); if(!(b->flags & B_VALID)) { iderw(b); } return b; }
//***************************************************************************** // //! Allocates a block of memory from the SDRAM heap. //! //! \param ulSize is the size in bytes of the block of SDRAM to allocate. //! //! This function allocates a block of SDRAM and returns its pointer. If //! insufficient space exists to fulfill the request, a NULL pointer is returned. //! //! \return Returns a non-zero pointer on success or NULL if it is not possible //! to allocate the required memory. // //***************************************************************************** void * ExtRAMAlloc(unsigned long ulSize) { if(g_bSDRAMPresent) { return(bget(ulSize)); } else { return((void *)0); } }
void print_board(Board b) { print_horizontal(); for (int r=0; r < BOARDSIZE; r++) { printf("|"); for (int c=0; c < BOARDSIZE; c++) { int val = bget(b, r,c); for (int i=0; i < NUMCHARS - num_digits(val); i++) printf(" "); // Right-justify numbers printf("%d", val); } printf("|\n"); } print_horizontal(); }
TASKTYPE TASKMOD consumer(void * n){ int i,j; char c; printf(" [[Consumer started]] "); i=0; j=0; c = 'A'; while (c != ' ' && j<500) { ++i; ++j; c = bget(); if (i==50){ printf("\n"); i=1; } } HExitThread(j); return 0; }
Board invert_rotate_for_move(Board og, Move m) { Board b = empty_board(); for (int r=0; r<BOARDSIZE; r++) { for (int c=0; c<BOARDSIZE; c++) { switch (m) { case Up: place(b, r, c, bget(og, c, BOARDSIZE-1-r)); break; case Down: place(b, r, c, bget(og, BOARDSIZE-1-c, r)); break; case Left: place(b, r, c, bget(og, r, BOARDSIZE-1-c)); break; case Right: place(b, r, c, bget(og, r, c)); break; default: break; } } } return b; }