示例#1
0
        void save_binary_chunk(void const* address, std::size_t count) // override
        {
            if (filter_ || chunks_ == 0 || count < HPX_ZERO_COPY_SERIALIZATION_THRESHOLD) {
                // fall back to serialization_chunk-less archive
                this->output_container::save_binary(address, count);
            }
            else {
                HPX_ASSERT(get_num_chunks() > current_chunk_);
                HPX_ASSERT(
                    get_chunk_type(current_chunk_) == chunk_type_index ||
                    get_chunk_size(current_chunk_) != 0);

                // complement current serialization_chunk by setting its length
                if (get_chunk_type(current_chunk_) == chunk_type_index)
                {
                    HPX_ASSERT(get_chunk_size(current_chunk_) == 0);

                    set_chunk_size(current_chunk_,
                        current_ - get_chunk_data(current_chunk_).index_);
                }

                // add a new serialization_chunk referring to the external buffer
                chunks_->push_back(create_pointer_chunk(address, count));
                ++current_chunk_;
            }
        }
示例#2
0
void open_wav(char *wav)
  {
  TCHANINFO *inf;
  int i;
  struct t_wave *wavinf;
  FILE *snd;

  i=find_free_channel();
  if (i==-1) return;
  last_channel=i;
  inf=chaninfo+i;
  snd=fopen(wav,"rb");
  if (snd==NULL)
     {
     printf("Soubor %s neexistuje.\n",wav);
     return;
     }
  if (find_chunk(snd,WAV_FMT)==-1)
     {
     printf("Soubor %s ma poskozenou hlavicku\n",wav);
     fclose(snd);
     return;
     }
  wavinf=(struct t_wave *)malloc(get_chunk_size(snd));
  if (wavinf==NULL)
     {
     puts("Nedostatek pamˆti.");
     return;
     }
  read_chunk(snd,wavinf);
  if (wavinf->wav_mode!=1)
     {
     printf("Tento program podporuje pouze WAVy typu 1 (%s)\n",wav);
     free(wavinf);
     fclose(snd);
     return;
     }
  inf->chans=wavinf->chans;
  inf->bit8=wavinf->freq*wavinf->chans==wavinf->bps;
  inf->repstart=-1;
  inf->id=-1;
  inf->left.volume=32768;
  inf->right.volume=32768;
  inf->left.vls=0;
  inf->right.vls=0;
  free(wavinf);
  if (find_chunk(snd,WAV_DATA)==0)
     {
     printf("Soubor %s je poskozen\n",wav);
     fclose(snd);
     return;
     }
  inf->size=get_chunk_size(snd);
  fseek(snd,4,SEEK_CUR);
  inf->snd=snd;
  }
// sizes
int* count_recvcounts() {
	int* counts = (int*) calloc(size, sizeof(int));
	for (int i = 0; i < size; i++) {
		counts[i] = get_chunk_size(i);
	}
	return counts;
}
示例#4
0
        void load_binary_chunk(void* address, std::size_t count) // override
        {
            HPX_ASSERT((std::int64_t)count >= 0);

            if (chunks_ == nullptr ||
                count < HPX_ZERO_COPY_SERIALIZATION_THRESHOLD ||
                filter_)
            {
                // fall back to serialization_chunk-less archive
                this->input_container::load_binary(address, count);
            }
            else {
                HPX_ASSERT(current_chunk_ != std::size_t(-1));
                HPX_ASSERT(get_chunk_type(current_chunk_) == chunk_type_pointer);

                if (get_chunk_size(current_chunk_) != count)
                {
                    HPX_THROW_EXCEPTION(serialization_error
                      , "input_container::load_binary_chunk"
                      , "archive data bstream data chunk size mismatch");
                    return;
                }

                // unfortunately we can't implement a zero copy policy on
                // the receiving end
                // as the memory was already allocated by the serialization code
                std::memcpy(address, get_chunk_data(current_chunk_).pos_, count);
                ++current_chunk_;
            }
        }
int calculate_shift(int given_rank) {
	int result = 0;
	for (int i = 0; i < given_rank; i++) {
		result += get_chunk_size(i);
	}
	return result;
}
示例#6
0
        void save_binary(void const* address, std::size_t count) // override
        {
            HPX_ASSERT(count != 0);
            {
                if (filter_) {
                    filter_->save(address, count);
                }
                else {
                    // make sure there is a current serialization_chunk descriptor
                    // available
                    if (chunks_)
                    {
                        HPX_ASSERT(get_num_chunks() > current_chunk_);
                        if (get_chunk_type(current_chunk_) == chunk_type_pointer ||
                            get_chunk_size(current_chunk_) != 0)
                        {
                            // add a new serialization_chunk
                            chunks_->push_back(create_index_chunk(current_, 0));
                            ++current_chunk_;
                        }
                    }

                    if (cont_.size() < current_ + count)
                        cont_.resize(cont_.size() + count);

                    if (count == 1)
                        cont_[current_] = *static_cast<unsigned char const*>(address);
                    else
                        std::memcpy(&cont_[current_], address, count);
                }
                current_ += count;
            }
        }
示例#7
0
static struct vsf_transfer_ret
do_file_send_sendfile(struct vsf_session* p_sess, int net_fd, int file_fd,
                      filesize_t curr_file_offset, filesize_t bytes_to_send)
{
    int retval;
    unsigned int chunk_size = 0;
    struct vsf_transfer_ret ret_struct = { 0, 0 };
    filesize_t init_file_offset = curr_file_offset;
    filesize_t bytes_sent;
    if (p_sess->bw_rate_max)
    {
        chunk_size = get_chunk_size();
    }
    /* Just because I can ;-) */
    retval = vsf_sysutil_sendfile(net_fd, file_fd, &curr_file_offset,
                                  bytes_to_send, chunk_size);
    bytes_sent = curr_file_offset - init_file_offset;
    ret_struct.transferred = bytes_sent;
    if (vsf_sysutil_retval_is_error(retval))
    {
        vsf_cmdio_write(p_sess, FTP_BADSENDNET, "Failure writing network stream.");
        ret_struct.retval = -1;
        return ret_struct;
    }
    else if (bytes_sent != bytes_to_send)
    {
        vsf_cmdio_write(p_sess, FTP_BADSENDFILE,
                        "Failure writing network stream.");
        ret_struct.retval = -1;
        return ret_struct;
    }
    vsf_cmdio_write(p_sess, FTP_TRANSFEROK, "File send OK.");
    return ret_struct;
}
示例#8
0
static struct vsf_transfer_ret
do_file_send_sendfile(struct vsf_session* p_sess, int net_fd, int file_fd,
                      filesize_t curr_file_offset, filesize_t bytes_to_send)
{
  int retval;
  unsigned int chunk_size = 0;
  struct vsf_transfer_ret ret_struct = { 0, 0 };
  filesize_t init_file_offset = curr_file_offset;
  filesize_t bytes_sent;
  if (p_sess->bw_rate_max)
  {
    chunk_size = get_chunk_size();
  }
  /* Just because I can ;-) */
  retval = vsf_sysutil_sendfile(net_fd, file_fd, &curr_file_offset,
                                bytes_to_send, chunk_size);
  bytes_sent = curr_file_offset - init_file_offset;
  ret_struct.transferred = bytes_sent;
  if (vsf_sysutil_retval_is_error(retval))
  {
    ret_struct.retval = -2;
    return ret_struct;
  }
  else if (bytes_sent != bytes_to_send)
  {
    ret_struct.retval = -2;
    return ret_struct;
  }
  return ret_struct; 
}
示例#9
0
mf_handle_t mf_open(const char *pathname) {

    created_logger = logger_init("log.txt");

    if(pathname == NULL) {
        errno = EINVAL;
        write_log_to_file(Error,"mf_open: wrong pathname, its value is NULL!\n");
        return NULL;
    }

    int fd = open(pathname, O_RDWR, 0666);
    if(fd == -1) {
        write_log_to_file(Error,"mf_open: problems with file, fd = -1!\n");
        return NULL;
    }

    ch_pool_t *ch_pool;
    ch_pool = ch_pool_init(fd, PROT_READ | PROT_WRITE);
    ch_pool -> flag = 0;

    if(ch_pool == NULL) {
        write_log_to_file(Error,"mf_open: chunk pool initialization failed!\n");
        return NULL;
    }

    ch_pool -> file_size = mf_file_size((mf_handle_t)ch_pool);
    ch_pool -> chunk_size_min = get_chunk_size(1);

    if(strcmp(pathname, "test0") == 0) {
        ch_pool -> flag = 1;
    }

    return (mf_handle_t)ch_pool;
}
示例#10
0
chunk_t *ch_init(off_t index, off_t len, chpool_t *chpool)
{
    if(!chpool)
    {
        log_write(ERROR, "ch_init(index=%d, len=%d): invaid input",
                  index, len);
        return NULL;
    }
    log_write(DEBUG, "ch_init(index=%d, len=%d): started", index, len);

    chunk_t *new_chunk;
    int error = chp_get_free_chptr(chpool, &new_chunk);
    if(error)
    {
        log_write(ERROR, "ch_init(index=%d, len=%d: can't get free chunk, error=%d",
                  index, len, error);
        return NULL;
    }

    new_chunk->data = mmap(NULL, get_chunk_size(len), chpool->prot,
                           MAP_SHARED, chpool->fd, get_chunk_size(index));
    if(new_chunk->data == MAP_FAILED)
    {
        log_write(ERROR, "ch_init(index=%d, len=%d): mmap failed, errno=%d",
                  index, len, errno);
        printf("errno=%d\n", errno);
        return NULL;
    }

    new_chunk->rc = 1;
    new_chunk->len = len;
    new_chunk->index = index;
    new_chunk->chpool = chpool;

    error = ht_add_item(chpool->ht, (hkey_t)index, (hvalue_t)new_chunk);
    if(error)
    {
        log_write(ERROR, "ch_init(index=%d, len=%d): can't add chunk to hash table, error=%d",
                  index, len, error);
        return NULL;
    }

    log_write(DEBUG, "ch_init(index=%d, len=%d): finished", index, len);

    return new_chunk;
}
示例#11
0
chunk_t* find_in_range(hash_table_t* h_table, off_t offset, size_t size) {
  int i = 0;
  list_element* ptr = NULL;
  off_t offset_chunk =  0;
  off_t size_chunk = 0;
  for(i = 0; i < (h_table -> size); i++) {
    ptr = (h_table -> table)[i];
    while(ptr) {
      offset_chunk = (ptr -> data -> index) * get_chunk_size(1);
      size_chunk = (ptr -> data -> length) * get_chunk_size(1);
      if((offset_chunk <= offset) && ((offset_chunk + size_chunk) >= (offset + size))) {
        return ptr -> data;
      }
      ptr = ptr -> next;
    }
  }
  return NULL;
}
示例#12
0
static struct vsf_transfer_ret
do_file_send_rwloop(struct vsf_session* p_sess, int file_fd, int is_ascii)
{
  static char* p_readbuf;
  static char* p_asciibuf;
  struct vsf_transfer_ret ret_struct = { 0, 0 };
  unsigned int chunk_size = get_chunk_size();
  char* p_writefrom_buf;
  if (p_readbuf == 0)
  {
    /* NOTE!! * 2 factor because we can double the data by doing our ASCII
     * linefeed mangling
     */
    vsf_secbuf_alloc(&p_asciibuf, VSFTP_DATA_BUFSIZE * 2);
    vsf_secbuf_alloc(&p_readbuf, VSFTP_DATA_BUFSIZE);
  }
  if (is_ascii)
  {
    p_writefrom_buf = p_asciibuf;
  }
  else
  {
    p_writefrom_buf = p_readbuf;
  }
  while (1)
  {
    unsigned int num_to_write;
    int retval = vsf_sysutil_read(file_fd, p_readbuf, chunk_size);
    if (vsf_sysutil_retval_is_error(retval))
    {
      ret_struct.retval = -1;
      return ret_struct;
    }
    else if (retval == 0)
    {
      /* Success - cool */
      return ret_struct;
    }
    if (is_ascii)
    {
      num_to_write = vsf_ascii_bin_to_ascii(p_readbuf, p_asciibuf,
                                            (unsigned int) retval);
    }
    else
    {
      num_to_write = (unsigned int) retval;
    }
    retval = ftp_write_data(p_sess, p_writefrom_buf, num_to_write);
    if (vsf_sysutil_retval_is_error(retval) ||
        (unsigned int) retval != num_to_write)
    {
      ret_struct.retval = -2;
      return ret_struct;
    }
    ret_struct.transferred += (unsigned int) retval;
  }
}
示例#13
0
static struct vsf_transfer_ret
do_file_recv(struct vsf_session* p_sess, int file_fd, int is_ascii)
{
  static char* p_recvbuf;
  unsigned int num_to_write;
  struct vsf_transfer_ret ret_struct = { 0, 0 };
  unsigned int chunk_size = get_chunk_size();
  int prev_cr = 0;
  if (p_recvbuf == 0)
  {
    /* Now that we do ASCII conversion properly, the plus one is to cater for
     * the fact we may need to stick a '\r' at the front of the buffer if the
     * last buffer fragment eneded in a '\r' and the current buffer fragment
     * does not start with a '\n'.
     */
    vsf_secbuf_alloc(&p_recvbuf, VSFTP_DATA_BUFSIZE + 1);
  }
  while (1)
  {
    const char* p_writebuf = p_recvbuf + 1;
    int retval = ftp_read_data(p_sess, p_recvbuf + 1, chunk_size);
    if (vsf_sysutil_retval_is_error(retval))
    {
      ret_struct.retval = -2;
      return ret_struct;
    }
    else if (retval == 0 && !prev_cr)
    {
      /* Transfer done, nifty */
      return ret_struct;
    }
    num_to_write = (unsigned int) retval;
    ret_struct.transferred += num_to_write;
    if (is_ascii)
    {
      /* Handle ASCII conversion if we have to. Note that using the same
       * buffer for source and destination is safe, because the ASCII ->
       * binary transform only ever results in a smaller file.
       */
      struct ascii_to_bin_ret ret =
        vsf_ascii_ascii_to_bin(p_recvbuf, num_to_write, prev_cr);
      num_to_write = ret.stored;
      prev_cr = ret.last_was_cr;
      p_writebuf = ret.p_buf;
    }
    retval = vsf_sysutil_write_loop(file_fd, p_writebuf, num_to_write);
    if (vsf_sysutil_retval_is_error(retval) ||
        (unsigned int) retval != num_to_write)
    {
      ret_struct.retval = -1;
      return ret_struct;
    }
  }
}
示例#14
0
        void set_filter(binary_filter* filter) // override
        {
            HPX_ASSERT(0 == filter_);
            filter_ = filter;
            start_compressing_at_ = current_;

            if (chunks_) {
                HPX_ASSERT(get_num_chunks() == 1 && get_chunk_size(0) == 0);
                chunks_->clear();
            }
        }
示例#15
0
        ~output_container()
        {
            if (filter_) {
                std::size_t written = 0;

                if (cont_.size() < current_)
                    cont_.resize(current_);
                current_ = start_compressing_at_;

                do {
                    bool flushed = filter_->flush(&cont_[current_],
                        cont_.size()-current_, written);

                    current_ += written;
                    if (flushed)
                        break;

                    // resize container
                    cont_.resize(cont_.size()*2);

                } while (true);

                cont_.resize(current_);         // truncate container
            }
            else if (chunks_) {
                HPX_ASSERT(get_num_chunks() > current_chunk_);
                HPX_ASSERT(
                    get_chunk_type(current_chunk_) == chunk_type_index ||
                    get_chunk_size(current_chunk_) != 0);

                // complement current serialization_chunk by setting its length
                if (get_chunk_type(current_chunk_) == chunk_type_index)
                {
                    HPX_ASSERT(get_chunk_size(current_chunk_) == 0);

                    set_chunk_size(current_chunk_,
                        current_ - get_chunk_data(current_chunk_).index_);
                }
            }
        }
示例#16
0
文件: main.c 项目: shurik111333/spbu
//Before running run: "ulimit -s unlimited"
int main(int argc, char* argv[]) {
    parse_opts(argc, argv);

    srand(time(NULL)); 
    int tick = 0;
    struct list **chunks_lifetime = (struct list **)malloc(sizeof(struct list*) * max_ticks);// The stack memory has somee restrictions
    for (int i = 0; i < max_ticks; i++) {
        chunks_lifetime[i] = create_list();
    }

    struct memory *mem = create_memory(memory_size);
    printf("Starting\n"); 
    while (1) {
        if (need_trace) {
            printf("Tick %d:\n", tick);
        }
        while (!is_list_empty(chunks_lifetime[tick])) {
            int8_t *ptr = get_and_remove(chunks_lifetime[tick]);
            if (need_trace) {
                printf("   Removing chunk with size %d bytes\n", get_chunk_size(ptr));
            }
            free_chunk(mem, ptr);
        }
        SIZE_TYPE chunk_size = rand_range(min_chunk_size, max_chunk_size) / 4 * 4;
        int8_t *ptr = allocate_chunk(mem, chunk_size);

        if (ptr == NULL) {
            printf("Oops. We have no free memory to allocate %d bytes on tick %d. Exiting\n", chunk_size, tick);
            break;
        }
        int chunk_lifetime = rand_range(min_lifetime, max_lifetime);
        if (tick + chunk_lifetime >= max_ticks) {
            printf("The maximum number of ticks(%d) reached. Exiting\n", max_ticks);
            break;
        }
        
        add_to_list(chunks_lifetime[tick + chunk_lifetime], ptr);
        if (need_trace){
            printf("   Allocating chunk with size %d bytes. Its lifetime is %d ticks\n", chunk_size, chunk_lifetime);
        }
        tick++;
    }

    for (int i = 0; i < max_ticks; i++) {
        free_list(chunks_lifetime[i]);
    }
    free_memory(mem);
    free(chunks_lifetime);
    return 0;
}
void print_matrix(double* matrix) {
	int chunk_size = get_chunk_size(rank);
	for (int process = 0; process < size; process++) {
		MPI_Barrier(MPI_COMM_WORLD);
		if (rank == process) {
			for (int i = 0; i < chunk_size; i++) {
				for (int j = 0; j < N; j++) {
					printf("%0.0f ", matrix[i * N + j]);
				}
				printf("\n");
			}
		}
	}
}
示例#18
0
int chp_chunk_release(chunk_t *chunk)
{
    if(!chunk)
        return EINVAL;

    log_write(DEBUG, "chp_chunk_release: started");

    if(chunk->rc == 0)
    {
        log_write(WARNING, "chp_chunk_release: trying to release chunk with zero rc");
        return EAGAIN;
    }

    if(--chunk->rc == 0)
    {
        int error = munmap(chunk->data, get_chunk_size(chunk->len));
        if(error == -1)
        {
            log_write(WARNING, "chp_chunk_release: can't unmap memory");
            return errno;
        }

        chunk->rc = 0;
        chunk->len = 0;
        chunk->index = 0;
        chunk->data = NULL;

        error = ht_del_item_by_kav(chunk->chpool->ht,
                                   (hkey_t)chunk->index, (hvalue_t)chunk);
        if(error)
        {
            log_write(WARNING, "chp_chunk_release: can't delete chunk from hash table, error=%d",
                      error);
            return error;
        }

        error = dcl_add_last(chunk->chpool->free_list, (lvalue_t)chunk);
        if(error)
        {
            log_write(WARNING, "chp_chunk_release: can't add chunk to free list, error=%d",
                      error);
            return error;
        }
    }

    log_write(DEBUG, "chp_chunk_release: finished");

    return 0;
}
示例#19
0
static struct vsf_transfer_ret
do_file_recv(struct vsf_session* p_sess, int file_fd, int is_ascii)
{
    static char* p_recvbuf;
    unsigned int num_to_write;
    struct vsf_transfer_ret ret_struct = { 0, 0 };
    unsigned int chunk_size = get_chunk_size();
    if (p_recvbuf == 0)
    {
        vsf_secbuf_alloc(&p_recvbuf, VSFTP_DATA_BUFSIZE);
    }
    while (1)
    {
        int retval = ftp_read_data(p_sess, p_recvbuf, chunk_size);
        if (vsf_sysutil_retval_is_error(retval))
        {
            vsf_cmdio_write(p_sess, FTP_BADSENDNET,
                            "Failure reading network stream.");
            ret_struct.retval = -1;
            return ret_struct;
        }
        else if (retval == 0)
        {
            /* Transfer done, nifty */
            vsf_cmdio_write(p_sess, FTP_TRANSFEROK, "File receive OK.");
            return ret_struct;
        }
        num_to_write = (unsigned int) retval;
        ret_struct.transferred += num_to_write;
        if (is_ascii)
        {
            /* Handle ASCII conversion if we have to. Note that using the same
             * buffer for source and destination is safe, because the ASCII ->
             * binary transform only ever results in a smaller file.
             */
            num_to_write = vsf_ascii_ascii_to_bin(p_recvbuf, p_recvbuf,
                                                  num_to_write);
        }
        retval = vsf_sysutil_write_loop(file_fd, p_recvbuf, num_to_write);
        if (vsf_sysutil_retval_is_error(retval) ||
                (unsigned int) retval != num_to_write)
        {
            vsf_cmdio_write(p_sess, FTP_BADSENDFILE,
                            "Failure writing to local file.");
            ret_struct.retval = -1;
            return ret_struct;
        }
    }
}
示例#20
0
        void save_binary(void const* address, std::size_t count) // override
        {
            HPX_ASSERT(count != 0);
            {
                if (filter_) {
                    filter_->save(address, count);
                }
                else {
                    // make sure there is a current serialization_chunk descriptor
                    // available
                    if (chunks_)
                    {
                        HPX_ASSERT(get_num_chunks() > current_chunk_);
                        if (get_chunk_type(current_chunk_) == chunk_type_pointer ||
                            get_chunk_size(current_chunk_) != 0)
                        {
                            // add a new serialization_chunk, reuse chunks,
                            // if possible
                            // the chunk size will be set at the end
                            if (chunks_->size() <= current_chunk_ + 1)
                            {
                                chunks_->push_back(
                                    create_index_chunk(current_, 0));
                                ++current_chunk_;
                            }
                            else
                            {
                                (*chunks_)[++current_chunk_] =
                                    create_index_chunk(current_, 0);
                            }
                        }
                    }

                    if (cont_.size() < current_ + count)
                        cont_.resize(cont_.size() + count);

                    detail::access_data<Container>::write(
                        cont_, count, current_, address);
                }
                current_ += count;
            }
        }
示例#21
0
        void load_binary(void* address, std::size_t count)
        {
            if (filter_) {
                filter_->load(address, count);
            }
            else {
                std::size_t new_current = current_ + count;
                if (new_current > access_traits::size(cont_))
                {
                    HPX_THROW_EXCEPTION(serialization_error
                      , "input_container::load_binary"
                      , "archive data bstream is too short");
                    return;
                }

                access_traits::read(cont_, count, current_, address);

                current_ = new_current;

                if (chunks_) {
                    current_chunk_size_ += count;

                    // make sure we switch to the next serialization_chunk if
                    // necessary
                    std::size_t current_chunk_size = get_chunk_size(current_chunk_);
                    if (current_chunk_size != 0 && current_chunk_size_ >=
                        current_chunk_size)
                    {
                        // raise an error if we read past the serialization_chunk
                        if (current_chunk_size_ > current_chunk_size)
                        {
                            HPX_THROW_EXCEPTION(serialization_error
                              , "input_container::load_binary"
                              , "archive data bstream structure mismatch");
                            return;
                        }
                        ++current_chunk_;
                        current_chunk_size_ = 0;
                    }
                }
            }
        }
示例#22
0
static int chp_get_free_chptr(chpool_t *chpool, chunk_t **new_chunk)
{
    if(!chpool)
        return EINVAL;

    log_write(INFO, "chp_get_free_chptr: started");

    if(!chpool->free_list->size)
    {
        log_write(INFO, "chp_get_free_chptr: free_list is empty");

        if(!chpool->zero_list->size)
        {
            log_write(INFO, "chp_get_free_chptr: zero_list also is empty");

            chp_add_loaf(chpool);

            *new_chunk = chpool->free_list->head->value;
            dcl_del_first(chpool->free_list);
        }
        else
        {
            *new_chunk = chpool->zero_list->head->value;
            if(munmap((*new_chunk)->data, get_chunk_size((*new_chunk)->len)) == -1)
            {
                log_write(ERROR,"chp_get_free_chptr: can't unmap memory");
                return NULL;
            }

            dcl_del_first(chpool->zero_list);
        }
    }
    else
    {
        *new_chunk = chpool->free_list->head->value;
        dcl_del_first(chpool->free_list);
    }

    log_write(INFO, "chp_get_free_chptr: finished");
    return 0;
}
示例#23
0
void process_file(void *ptr)
{
	char *filename = (char *) ptr;
	fprintf(stderr,"%s: processing log file %s\n", program_name, filename);
	
	FILE *file_to_process = fopen(filename,"r");
	if (file_to_process == NULL)
	{
		err_sys("Cannot open file %s\n", filename);
		exit(1);
	}
	
	long file_size = get_size_of_file(file_to_process);
//	if (is_trivial_file(file_size))
//	{
//		process((void*)file_to_process);
//	}
//	else
	{
		long num_threads = get_number_of_threads();
		long chunk_size = get_chunk_size(file_size, num_threads);
		pthread_t threads[num_threads];
		long byte_endings[num_threads];
		long indices[2];
		
		int i;
		for (i = 0; i < num_threads; ++i)
		{
			get_indices(file_to_process, i, chunk_size, byte_endings, indices);
//			validate_chunk(file_to_process, "validation.txt", indices[0], indices[1], i);
			process_chunk(file_to_process, i, threads, indices[0], indices[1]);
		}
		
		for (i = 0; i < num_threads; ++i)
			pthread_join(threads[i], NULL);
	}
}
示例#24
0
extern "C" void open_file (const char *pathname, int flags, struct client client) {
  uint32_t file_id;
  struct decafs_file_stat stat;
  int cursor;

  // If the file does not exist
  if ((decafs_file_sstat ((char *)pathname, &stat, client)) == FILE_NOT_FOUND) {
    // If we are going to write to the file, create it
    if (flags & O_RDWR) {
      printf ("\tfile not found... creating now\n");
      // Create the file
      struct timeval time;
      gettimeofday(&time, NULL);
                          // change 4th param to get_replica_size()
                          // implement in vmeta
      file_id = add_file ((char *)pathname, get_stripe_size(), get_chunk_size(),
                          get_chunk_size(), time, client);
    }
    // We can't read from nothing!
    else {
      if (send_open_result (client, FILE_NOT_FOUND_FOR_READING) < 0) {
        printf ("\tOpen result could not reach client.\n");
      }
      return;
   }
  }
  else {
    file_id = stat.file_id;
  }

  printf ("\tfile %s has id %d.\n", pathname, file_id);

  // If we're opening with read only, obtain a read lock
  if (flags & O_RDWR) {
    // if we can't get a write lock, return that the file is in use so we can't
    // open it
    if (get_exclusive_lock (client, file_id) < 0) {
      if (send_open_result (client, FILE_IN_USE) < 0) {
        printf ("\tOpen result could not reach client.\n");
      }
      return;
    }
    printf ("\tobtained a write lock.\n");
  }
  // obtain a write lock
  else {
    // if we can't get a read lock, return that the file is in use so we can't
    // open it
    if (get_shared_lock (client, file_id) < 0) {
      if (send_open_result (client, FILE_IN_USE) < 0) {
        printf ("\tOpen result could not reach client.\n");
      }
      return;
    }
    printf ("\tobtained a read lock.\n");
  }

  cursor = new_file_cursor (file_id, client);
  if (flags & O_APPEND) {
    printf ("\tfile opened with O_APPEND, moving cursor to EOF.\n");
    set_file_cursor (cursor, stat.size, client);
  }

  if (send_open_result (client, cursor) < 0) {
    printf ("\tOpen result could not reach client.\n");
  }
}
示例#25
0
文件: malloc.c 项目: dgryski/trifles
static void *
small_malloc(int size)
{

   __malloc_chunk_t *chunk;
   int i, validbits;
   unsigned int mask;

   size = get_chunk_size(size);
   i = get_chunk_list(size);
   chunk = malloc_chunks[i];

   /* how many bits are valid for this chunks bitmap? */
   validbits = (sizeof(__malloc_chunk_t) - 8) / size;
   mask = (1 << (validbits)) - 1;

   /* search through chunk list */
   for (;;) {

      if (chunk == 0) {

         /* out of chunks -- allocate a new one */

         if (free_chunks) {
            /* try and grab a recycled chunk */
            chunk = free_chunks;
            free_chunks = free_chunks->next;
            chunk->next = 0;
         } else {
            /* nothing on the free list */
            /* ask the system for some memory */
            chunk = sbrk(128);
            /* couldn't grab more memory -- must be out */
            if (chunk == 0)
               return 0;
         }

         /* no blocks currently allocated on this page */
         chunk->size = get_chunk_size(size);
         chunk->bitmap = 0;

         /* add to head of list */
         chunk->next = malloc_chunks[i];
         malloc_chunks[i] = chunk;
      }

      /* scan chunk's bitmap for a free block */
      if ((chunk->bitmap & mask) == mask) {
         /* all blocks are used -- move on to next one */
         chunk = chunk->next;
         continue;
      }

      /* at least one is free */
      for (i = 0; i < validbits; i++) {
         if (!ISSET(chunk->bitmap, i)) {
            /* found free block */
            SETBIT(chunk->bitmap, i);
            return (void *) &chunk->data[chunk->size * i];
         }
      }
   }
}
示例#26
0
void mix_buffer(int size)
  {
  int chan,i;
  memset(source,0,size*2);
  for(chan=0;chan<CHANNELS;chan++)
     {
     if (chaninfo[chan].snd!=NULL)
        {
        TCHANINFO *inf=chaninfo+chan;

        for(i=0;i<size;i+=2)
           {
           int left,right;
           if (inf->bit8)
              {
              fread(&left,1,1,inf->snd);left=(short)(left*256);
              left^=0xffff8000;
              inf->size--;
              }
           else
              {
              fread(&left,1,2,inf->snd);left=(short)left;
              inf->size-=2;
              }
           if (inf->chans==1) right=left;
           else
              if (inf->bit8)
                 {
                 fread(&right,1,1,inf->snd);right=(short)(right*256);
                 right^=0xffff8000;
                 inf->size--;
                 }
              else
                 {
                 fread(&right,1,2,inf->snd);right=(short)right;
                 inf->size-=2;
                 }
           left=(int)left*inf->left.volume/(32768);
           right=(int)right*inf->right.volume/(32768);
           left=left*glob_volume/256;
           right=right*glob_volume/256;
           calc_vls(&inf->left);
           calc_vls(&inf->right);
           left+=source[i];
           right+=source[i+1];
           if (left>32767) left=32767;
           if (left<-32767) left=-32767;
           if (right>32767) right=32767;
           if (right<-32767) right=-32767;
           source[i]=left;
           source[i+1]=right;
           if (inf->size<=0)
              {
              if (inf->repstart!=-1)
                 {
                 fseek(inf->snd,0,SEEK_SET);
                 find_chunk(inf->snd,WAV_DATA);
                 inf->size=get_chunk_size(inf->snd);
                 fseek(inf->snd,4,SEEK_CUR);
                 fseek(inf->snd,inf->repstart,SEEK_CUR);
                 inf->size-=inf->repstart;
                 }
              else
                 {
                 fclose(inf->snd);
                 inf->snd=NULL;
                 break;
                 }
              }
           }
        }
     }
  }
示例#27
0
bool poc_file::read_chunk_data(int idx, void *data) const
{
    return read_chunk_data(idx, data, get_chunk_size(idx));
}