Example #1
0
static gboolean
concat_frags (GList *list)
{
	GList *l;
	guchar *ptr;
	gulong length = 0;
	gulong initial_length;
	gulong initial_offset;
	GIOPRecvBuffer *head;

	head = list->data;

	length = head->msg.header.message_size;
	initial_offset = (head->cur - head->message_body);
	initial_length = (head->end - head->cur);

	length += initial_offset - 12; /* include what we read of the header */

	g_assert (head->free_body);

	if (alloc_buffer (head, head->message_body, length)) {
		dprintf (ERRORS, "failed to allocate fragment collation buffer");
		return TRUE;
	}

	head->left_to_read = 0;
	head->cur = head->message_body + initial_offset;

	ptr = head->cur + initial_length;

	for (l = list->next; l; l = l->next) {
		gulong len;
		GIOPRecvBuffer *buf = l->data;
			
		len = buf->end - buf->cur;
		memcpy (ptr, buf->cur, len);
		ptr+= len;
	}

	head->end = ptr;

	return FALSE;
}
Example #2
0
File: file.c Project: HarryR/sanos
int dfs_ftruncate(struct file *filp, off64_t size) {
  struct inode *inode;
  int rc;
  unsigned int blocks;
  blkno_t blk;
  struct buf *buf;

  if (size > DFS_MAXFILESIZE) return -EFBIG;

  inode = (struct inode *) filp->data;
  if (S_ISDIR(inode->desc->mode)) return -EISDIR;

  if (size < 0) return -EINVAL;
  if (size == inode->desc->size) return 0;

  blocks = ((size_t) size + inode->fs->blocksize - 1) / inode->fs->blocksize;

  if (size > inode->desc->size) {
    while (inode->desc->blocks < blocks) {
      blk = expand_inode(inode);
      if (blk == NOBLOCK) return -ENOSPC;

      buf = alloc_buffer(inode->fs->cache, blk);
      if (!buf) return -EIO;

      memset(buf->data, 0, inode->fs->blocksize);

      mark_buffer_updated(inode->fs->cache, buf);
      release_buffer(inode->fs->cache, buf);
    }
  } else {
    rc = truncate_inode(inode, blocks);
    if (rc < 0) return rc;
  }

  inode->desc->size = size;
  mark_inode_dirty(inode);

  filp->flags |= F_MODIFIED;

  return 0;
}
Example #3
0
static void report( neko_vm *vm, value exc ) {
#if OSX
	CFStringRef title = CFSTR("Uncaught exception");
	CFStringRef message;
#endif
	int i = 0;
	buffer b = alloc_buffer(NULL);
	value st = neko_exc_stack(vm);
	if( val_array_size(st) > 20 ) {
		i = val_array_size(st) - 20;
		buffer_append(b,"...\n");
	}
	for(i;i<val_array_size(st);i++) {
		value s = val_array_ptr(st)[i];
		if( val_is_null(s) )
			buffer_append(b,"Called from a C function\n");
		else if( val_is_string(s) ) {
			buffer_append(b,"Called from ");
			buffer_append(b,val_string(s));
			buffer_append(b," (no debug available)\n");
		} else if( val_is_array(s) && val_array_size(s) == 2 && val_is_string(val_array_ptr(s)[0]) && val_is_int(val_array_ptr(s)[1]) ) {
			buffer_append(b,"Called from ");
			buffer_append(b,val_string(val_array_ptr(s)[0]));
			buffer_append(b," line ");
			val_buffer(b,val_array_ptr(s)[1]);
			buffer_append(b,"\n");
		} else {
			buffer_append(b,"Called from ");
			val_buffer(b,s);
			buffer_append(b,"\n");
		}
	}
	val_buffer(b,exc);
#if _WIN32
	MessageBox(NULL,val_string(buffer_to_string(b)),"Uncaught exception",MB_OK | MB_ICONERROR);
#elif OSX
	message = CFStringCreateWithCString(NULL,val_string(buffer_to_string(b)), kCFStringEncodingUTF8);
	CFUserNotificationDisplayNotice(0,0,NULL,NULL,NULL,title,message,NULL);
#elif LINUX
	fprintf(stderr,"Uncaught Exception: %s\n",val_string(buffer_to_string(b)));
#endif
}
Example #4
0
static void
test_enqueue_and_dequeue_message_queue() {
  message_queue *queue = create_message_queue();
  assert_true( queue != NULL );

  buffer *enq_buf = alloc_buffer();
  assert_true( enqueue_message( queue, enq_buf ) );
  assert_int_equal( queue->length, 1 );

  buffer *deq_buf = dequeue_message( queue );
  assert_true( deq_buf == enq_buf );
  free_buffer( deq_buf );
  assert_int_equal( queue->length, 0 );

  deq_buf = dequeue_message( queue );
  assert_true( deq_buf == NULL );
  assert_int_equal( queue->length, 0 );

  assert_true( delete_message_queue( queue ) );
}
Example #5
0
File: ne.c Project: hagenkaye/ne
buffer *new_buffer(void)
{
    buffer *b = alloc_buffer(cur_buffer);

    if (b)
    {
        clear_buffer(b);
        if (cur_buffer)
        {
            add(&b->b_node, &cur_buffer->b_node);
        }
        else
        {
            add_head(&buffers, &b->b_node);
        }
        cur_buffer = b;
    }

    return b;
}
Example #6
0
char *get_write_ptr (netbuffer_t *H, int len) {
  netbuffer_t *X = H->prev, *Y;
  assert ((unsigned long) len < NET_BUFFER_SIZE);
  assert (H->state == NB_MAGIC_HEAD || H->state == NB_MAGIC_BUSYHEAD);
  if (X->wptr + len <= X->end) {
    return X->wptr;
  }
  Y = alloc_buffer();
  if (!Y) { return 0; }
  H->extra++;
  X->next = Y;
  Y->prev = X;
  Y->next = H;
  H->prev = Y;
  if (X->pptr) {
    Y->pptr = Y->rptr;
  }
  assert (Y->wptr + len <= Y->end);
  return Y->wptr;
}
Example #7
0
static void assert_backpatches_branches(unsigned char expected_prefix,
					unsigned char expected_opc,
					unsigned char expected_target,
					struct basic_block *branch_bb,
					struct basic_block *target_bb)
{
	struct buffer *buf;

	branch_bb->is_emitted = false;
	target_bb->is_emitted = false;

	buf = alloc_buffer();

	emit_body(branch_bb, buf);
	assert_prefixed_mem_insn_5(expected_prefix, expected_opc, 0x00, 0x00, 0x00, 0x00, buffer_ptr(buf));

	emit_body(target_bb, buf);
	assert_prefixed_mem_insn_5(expected_prefix, expected_opc, expected_target, 0x00, 0x00, 0x00, buffer_ptr(buf));

	free_buffer(buf);
}
Example #8
0
static void
echo_request_interval( void *user_data ) {
  struct switch_info *sw_info = user_data;

  buffer *buf = alloc_buffer();
  echo_body *body = append_back_buffer( buf, sizeof( echo_body ) );
  body->datapath_id = htonll( switch_info.datapath_id );
  struct timespec now;
  clock_gettime( CLOCK_MONOTONIC, &now );
  body->sec = htonl( ( uint32_t ) now.tv_sec );
  body->nsec = htonl( ( uint32_t ) now.tv_nsec );
  sw_info->echo_request_xid = generate_xid();

  int err = ofpmsg_send_echorequest( sw_info, sw_info->echo_request_xid, buf );
  if ( err < 0 ) {
    switch_event_disconnected( &switch_info );
    return;
  }

  switch_set_timeout( ECHO_REPLY_TIMEOUT, echo_reply_timeout, NULL );
}
Example #9
0
void					wolf(t_trd *t)
{
	t->time = 0;
	t->tp = 0;
	t->dtp = 0;
	t->posx = 10;
	t->posy = 9.5;
	t->dirx = -1;
	t->diry = 0;
	t->plx = 0;
	t->ply = 0.66;
	t->hit[0] = 0;
	t->hit[1] = 0;
	t->hit[2] = 0;
	t->hit[3] = 0;
	map_generator(t);
	show_me_map(t);
	alloc_buffer(t);
	calc(t);
	free_buffer(t);
}
Example #10
0
/**
	put_env : var:string -> val:string -> void
	<doc>Set some environment variable value</doc>
**/
static value put_env( value e, value v ) {
#ifdef HX_WINRT
   // Do nothing
	return alloc_null();
#elif defined(NEKO_WINDOWS)
	val_check(e,string);
	val_check(v,string);
	buffer b = alloc_buffer(0);
	val_buffer(b,e);
	buffer_append_sub(b,"=",1);
	val_buffer(b,v);
	if( putenv(buffer_data(b)) != 0 )
		return alloc_null();
#else
	val_check(e,string);
	val_check(v,string);
	if( setenv(val_string(e),val_string(v),1) != 0 )
		return alloc_null();
#endif
	return alloc_bool(true);
}
Example #11
0
/**
	regexp_replace_fun : 'regexp -> from:string -> f:('regexp -> any) -> string
	<doc>Perform a replacement of all matched substrings by calling [f] for every match</doc>
**/
static value regexp_replace_fun( value o, value s, value f ) {
	val_check_kind(o,k_regexp);
	val_check(s,string);
	val_check_function(f,1);
	{
		pcredata *d = PCRE(o);
		buffer b = alloc_buffer(NULL);
		int pos = 0;
		int len = val_strlen(s);
		const char *str = val_string(s);
		d->str = s;
		while( pcre_exec(d->r,NULL,str,len,pos,0,d->matchs,d->nmatchs * 3) >= 0 ) {
			buffer_append_sub(b,str+pos,d->matchs[0] - pos);
			val_buffer(b,val_call1(f,o));
			pos = d->matchs[1];
		}
		d->str = alloc_null();
		buffer_append_sub(b,str+pos,len-pos);
		return buffer_to_string(b);
	}
}
Example #12
0
static  value ssl_read( value ssl ) {
	int len, bufsize = 256;
	buffer b;
	unsigned char buf[256];
	mbedtls_ssl_context *ctx;
	val_check_kind(ssl,k_ssl);
	ctx = val_ssl(ssl);
	b = alloc_buffer(NULL);
	while( true ) {
		POSIX_LABEL(read_again);
		len = mbedtls_ssl_read( ctx, buf, bufsize );
		if( len == SOCKET_ERROR ) {
			HANDLE_EINTR(read_again);
			return block_error();
		}
		if( len == 0 )
			break;
		buffer_append_sub(b,(const char *)buf,len);
	}
	return buffer_to_string(b);
}
Example #13
0
static value alloc_result( connection *c, MYSQL_RES *r ) {
	result *res = (result*)alloc(sizeof(result));
	value o = alloc_abstract(k_result,res);
	int num_fields = mysql_num_fields(r);
	int i,j;
	MYSQL_FIELD *fields = mysql_fetch_fields(r);
	res->r = r;
	res->conv_date = c->conv_date;
	res->conv_bytes = c->conv_bytes;
	res->conv_string = c->conv_string;
	res->current = NULL;
	res->nfields = num_fields;
	res->fields_ids = (field*)alloc_private(sizeof(field)*num_fields);
	res->fields_convs = (CONV*)alloc_private(sizeof(CONV)*num_fields);	
	for(i=0;i<num_fields;i++) {
		field id;
		if( strchr(fields[i].name,'(') )
			id = val_id("???"); // looks like an inner request : prevent hashing + cashing it
		else {
			id = val_id(fields[i].name);
			for(j=0;j<i;j++)
				if( res->fields_ids[j] == id ) {
					buffer b = alloc_buffer("Error, same field ids for : ");
					buffer_append(b,fields[i].name);
					buffer_append(b,":");
					val_buffer(b,alloc_int(i));
					buffer_append(b," and ");
					buffer_append(b,fields[j].name);
					buffer_append(b,":");
					val_buffer(b,alloc_int(j));
					buffer_append(b,".");
					bfailure(b);
				}
		}
		res->fields_ids[i] = id;
		res->fields_convs[i] = convert_type(fields[i].type,fields[i].flags,fields[i].length);
	}
	val_gc(o,free_result);
	return o;
}
Example #14
0
static buffer *
store_packet_to_buffer( const char *filename ) {
  assert( filename != NULL );

  FILE *fp = fopen( filename, "r" );
  if ( fp == NULL ) {
    // "Can't open a file of test data."
    return NULL;
  }

  // Skip
  if ( fseek( fp, sizeof( struct pcap_file_header ) + sizeof( uint32_t ) * 2,
              SEEK_CUR ) != 0 ) {
    fclose( fp );
    return NULL;
  }

  uint32_t length[ 2 ];
  size_t size = fread( &length, 1, sizeof( length ), fp );
  if ( size < sizeof( length ) ) {
    fclose( fp );
    return NULL;
  }

  buffer *buffer = alloc_buffer();
  if ( buffer == NULL ) {
    fclose( fp );
    return NULL;
  }
  size = fread( append_back_buffer( buffer, length[ 0 ] ), 1, length[ 0 ], fp );
  if ( size < buffer->length ) {
    free_buffer( buffer );
    fclose( fp );
    return NULL;
  }

  fclose( fp );
  return buffer;
}
Example #15
0
int hash_init(int power) 
{
	size_t bytes;
	int ret = 0;

	if (power)
		hashpower = power;
	bytes = hashsize(hashpower) * sizeof(void *);
	ret = alloc_buffer(&primary_hts, bytes, __GFP_ZERO);
	if (ret) {
		PRINTK("alloc primary_hashtable error\n");
		goto out;
	} else {
		BUFFER_PTR(&primary_hts, primary_hashtable);
	}

	ATOMIC32_SET(stats.hash_power_level, hashpower);
	ATOMIC64_SET(stats.hash_bytes, bytes);

out:
	return ret;
}
Example #16
0
/***********************************************************************//**
 * @brief Save table column into FITS file
 *
 * The table column is only saved if it is linked to a FITS file and if the
 * data are indeed present in the class instance. This avoids saving of data
 * that have not been modified.
 *
 * Refer to GFitsTableCol::save_column() for more information.
 ***************************************************************************/
void GFitsTableBoolCol::save(void)
{
    // Free buffer
    free_buffer();

    // Allocate buffer
    alloc_buffer();

    // Transfer string into buffer
    for (int i = 0; i < m_size; ++i) {
        m_buffer[i] = (char)m_data[i];
    }

    // Save column
    save_column();

    // Free buffer
    free_buffer();

    // Return
    return;
}
Example #17
0
void *nb_alloc (netbuffer_t *H, int len) {
  netbuffer_t *X, *Y;
  int s, t;
  void *res;
  if (!H || len < 0 || len > NETBUFF_DATA_SIZE) { return 0; }
  assert (H->state == NB_MAGIC_HEAD || H->state == NB_MAGIC_BUSYHEAD);
  assert (!H->pptr);

  X = H->prev;
  t = -((long) X->wptr) & 3;
  s = X->end - X->wptr; 

  if (len + t <= s) {
    res = X->wptr += t;
    X->wptr += len;
    H->total_bytes += len;
    return res;
  }

  Y = alloc_buffer();
  if (!Y) { return 0; }

  X->next = Y;
  Y->prev = X;
  Y->next = H;
  H->prev = Y;
  H->extra++;

  s = Y->end - Y->wptr; 

  if (len <= s) {
    res = Y->wptr;
    Y->wptr += len;
    H->total_bytes += len;
    return res;
  }

  return 0;
}
Example #18
0
static value loader_loadmodule( value mname, value vthis ) {
	value o = val_this();
	value cache;
	val_check(o,object);
	val_check(mname,string);
	val_check(vthis,object);
	cache = val_field(o,id_cache);
	val_check(cache,object);
	{
		reader r;
		readp p;
		neko_module *m;
		neko_vm *vm = NEKO_VM();
		field mid = val_id(val_string(mname));
		value mv = val_field(cache,mid);
		if( val_is_kind(mv,neko_kind_module) ) {
			m = (neko_module*)val_data(mv);
			return m->exports;
		}
		open_module(val_field(o,id_path),val_string(mname),&r,&p);
		if( vm->fstats ) vm->fstats(vm,"neko_read_module",1);
		m = neko_read_module(r,p,vthis);
		if( vm->fstats ) vm->fstats(vm,"neko_read_module",0);
		close_module(p);
		if( m == NULL ) {
			buffer b = alloc_buffer("Invalid module : ");
			val_buffer(b,mname);
			bfailure(b);
		}
		m->name = alloc_string(val_string(mname));
		mv = alloc_abstract(neko_kind_module,m);
		alloc_field(cache,mid,mv);
		if( vm->fstats ) vm->fstats(vm,val_string(mname),1);
		neko_vm_execute(neko_vm_current(),m);
		if( vm->fstats ) vm->fstats(vm,val_string(mname),0);
		return m->exports;
	}
}
Example #19
0
/**
	socket_read : 'socket -> string
	<doc>Read the whole content of a the data available from a socket until the connection close.
	If the socket hasn't been close by the other side, the function might block.
	</doc>
**/
static value socket_read( value o ) {
	buffer b;
	char buf[256];
	int len;
	SOCKET sock = val_sock(o);
	b = alloc_buffer(NULL);
	gc_enter_blocking();
	while( true ) {
		POSIX_LABEL(read_again);
		len = recv(sock,buf,256,MSG_NOSIGNAL);
		if( len == SOCKET_ERROR ) {
			HANDLE_EINTR(read_again);
			return block_error();
		}
		if( len == 0 )
			break;
	        gc_exit_blocking();
		buffer_append_sub(b,buf,len);
	        gc_enter_blocking();
	}
	gc_exit_blocking();
	return buffer_val(b);
}
Example #20
0
File: thread.c Project: zhyg/mapred
int thread_add(int wfd, int rfd) 
{
    static int i = 0;
    if (i >= wet->evno) {
        log("thread add error, out of bouder\n");
        return -1;
    }
    event_set(&(wet->ev[i].e), wfd, EV_WRITE | EV_PERSIST, event_handler, (void*)&(wet->ev[i]));
    event_base_set(wet->base, &(wet->ev[i].e));
    event_add(&(wet->ev[i].e), 0);
    alloc_buffer(&wet->ev[i].buffer, 4096);
    wet->ev[i].fd = wfd;
    
    struct fdev_t* evs = ret->evs;
    event_set(&evs[i].ev, rfd, EV_READ | EV_PERSIST, revent_handler, (void*)&evs[i]);
    event_base_set(ret->base, &evs[i].ev);
    event_add(&evs[i].ev, 0);
    create_stream(&evs[i].stream, 4096);
    evs[i].stream.fd = rfd;

    ++i;
    return 0;
}
HeapWord* G1ParGCAllocator::allocate_slow(GCAllocPurpose purpose, size_t word_sz, AllocationContext_t context) {
  HeapWord* obj = NULL;
  size_t gclab_word_size = _g1h->desired_plab_sz(purpose);
  if (word_sz * 100 < gclab_word_size * ParallelGCBufferWastePct) {
    G1ParGCAllocBuffer* alloc_buf = alloc_buffer(purpose, context);
    add_to_alloc_buffer_waste(alloc_buf->words_remaining());
    alloc_buf->retire(false /* end_of_gc */, false /* retain */);

    HeapWord* buf = _g1h->par_allocate_during_gc(purpose, gclab_word_size, context);
    if (buf == NULL) {
      return NULL; // Let caller handle allocation failure.
    }
    // Otherwise.
    alloc_buf->set_word_size(gclab_word_size);
    alloc_buf->set_buf(buf);

    obj = alloc_buf->allocate(word_sz);
    assert(obj != NULL, "buffer was definitely big enough...");
  } else {
    obj = _g1h->par_allocate_during_gc(purpose, word_sz, context);
  }
  return obj;
}
Example #22
0
static int
decode_atom(ei_x_buff *eip, int *index, const char *cmp)
{
    int err;
    int size;
    int ret;
    char *buf;

    buf = alloc_buffer(eip, index, &size);
    err = ei_decode_atom(eip->buff, index, buf);
    if (err != 0) {
        msg_warn("cannot decode atom");
        return err;
    }
    buf[size-1] = '\0';

    ret = strcmp(buf, cmp);
    if (ret != 0)
        msg_warn("bad atom: %s", buf);

    myfree(buf);
    return ret;
}
Example #23
0
int main ( int argc, char** argv )
{
    FILE* fh;
    byte_t* buf;
    size_t flen;
    class_t cls;
    flags_t flags;
    if ( get_options ( argc, argv, &flags ) )
        return 1;
    if ( ! ( buf = alloc_buffer ( ) ) )
        return 1;
    if ( ! ( fh = open_input_file ( argv [ argc - 1 ] ) ) )
        return 1;
    if ( ! ( flen = load_file ( buf, fh ) ) )
        return 1;
    if ( classify_image ( &cls, buf, flen ) )
        return 1;

    if ( flags.action == ACTION_CONVERT )
        return action_convert ( &cls, buf, flen, argv [ argc - 1 ] );
    else
        return action_print ( &cls );
}
HeapWord* G1PLABAllocator::allocate_direct_or_new_plab(InCSetState dest,
                                                       size_t word_sz,
                                                       AllocationContext_t context) {
  size_t gclab_word_size = _g1h->desired_plab_sz(dest);
  if (word_sz * 100 < gclab_word_size * ParallelGCBufferWastePct) {
    G1PLAB* alloc_buf = alloc_buffer(dest, context);
    alloc_buf->retire();

    HeapWord* buf = _allocator->par_allocate_during_gc(dest, gclab_word_size, context);
    if (buf == NULL) {
      return NULL; // Let caller handle allocation failure.
    }
    // Otherwise.
    alloc_buf->set_word_size(gclab_word_size);
    alloc_buf->set_buf(buf);

    HeapWord* const obj = alloc_buf->allocate(word_sz);
    assert(obj != NULL, "buffer was definitely big enough...");
    return obj;
  } else {
    return _allocator->par_allocate_during_gc(dest, word_sz, context);
  }
}
Example #25
0
static value do_replace( value o, value s, value s2, bool all ) {	
	val_check_kind(o,k_regexp);	
	val_check(s,string);
	val_check(s2,string);	
	{
		pcredata *d = PCRE(o);
		buffer b = alloc_buffer(NULL);
		int pos = 0;
		int len = val_strlen(s);
		const char *str = val_string(s);
		const char *str2 = val_string(s2);
		int len2 = val_strlen(s2);
		while( pcre_exec(d->r,NULL,str,len,pos,0,d->matchs,d->nmatchs * 3) >= 0 ) {
			buffer_append_sub(b,str+pos,d->matchs[0] - pos);
			buffer_append_sub(b,str2,len2);
			pos = d->matchs[1];
			if( !all )
				break;
		}
		d->str = alloc_null();
		buffer_append_sub(b,str+pos,len-pos);
		return buffer_to_string(b);
	}
}
Example #26
0
File: dir.c Project: HarryR/sanos
int add_dir_entry(struct inode *dir, char *name, int len, vfs_ino_t ino)
{
  unsigned int block;
  vfs_blkno_t blk;
  struct buf *buf;
  char *p;
  struct dentry *de;
  struct dentry *newde;
  int minlen;

  if (len <= 0 || len >= MAXPATH) return -1;
  if (!VFS_S_ISDIR(dir->desc->mode)) return -1;

  for (block = 0; block < dir->desc->blocks; block++)
  {
    blk = get_inode_block(dir, block);
    buf = get_buffer(dir->fs->cache, blk);
    if (!buf) return -1;

    p = buf->data;
    while (p < buf->data + dir->fs->blocksize)
    {
      de = (struct dentry *) p;
      minlen = sizeof(struct dentry) + NAME_ALIGN_LEN(de->namelen);

      if (de->reclen >= minlen + sizeof(struct dentry) + NAME_ALIGN_LEN(de->namelen))
      {
        newde = (struct dentry *) (p + minlen);

        newde->ino = ino;
        newde->reclen = de->reclen - minlen;
        newde->namelen = len;
        memcpy(newde->name, name, len);

        de->reclen = minlen;

        mark_buffer_updated(buf);
        release_buffer(dir->fs->cache, buf);

        dir->desc->mtime = time(NULL);
        mark_inode_dirty(dir);

        return 0;
      }

      p += de->reclen;
    }

    release_buffer(dir->fs->cache, buf);
  }

  blk = expand_inode(dir);
  if (blk == -1) return -1;

  buf = alloc_buffer(dir->fs->cache, blk);
  if (!buf) return -1;

  dir->desc->size += dir->fs->blocksize;
  dir->desc->mtime = time(NULL);
  mark_inode_dirty(dir);

  newde = (struct dentry *) (buf->data);
  newde->ino = ino;
  newde->reclen = dir->fs->blocksize;
  newde->namelen = len;
  memcpy(newde->name, name, len);

  mark_buffer_updated(buf);
  release_buffer(dir->fs->cache, buf);

  return 0;
}
Example #27
0
/**
 * 加载配置文件
 */
void init_conf(){
	buffer_t *rbuf=alloc_buffer(CONF_READ_BUF);
	int fd=open_conf_file();
	enum PARSE_STATE status=RAW;
	config_module_t *current_module;
	command_t *current_command;
	char_t *module_name=init_char();
	char_t *command_key=init_char();
	char_t *command_value=init_char();
	while(1){
		if(has_space(rbuf)){
			intptr_t count=read(fd,rbuf->data+rbuf->limit,rbuf->size-rbuf->limit);
			if(count<=0){
				goto CONFIG_END;
			}
			rbuf->limit+=count;
		}
		while(has_remaining(rbuf)){
    		char c=*(rbuf->data+rbuf->current);
    		rbuf->current+=1;
    		if(status==RAW&&c==brace_start){
    			status=MODULE_START;
    		}else if(status==MODULE_START&&!char_is_special(c)){
    			if(c==brace_start){
    				add_terminal(module_name);
    				current_module=find_config_module(module_name);
    				if(current_module==NULL){
    					my_log(ERROR,"config error,please check\n");
    					goto CONFIG_END;
    				}else{
    					status=FIND_COMMAND_KEY;
    				}
    			}else if(c==brace_end){
    				goto CONFIG_END;
    			}else{
    				add_char(module_name,c);
    			}
    		}else if(status==FIND_COMMAND_KEY&&!char_is_special(c)){
    			if(c==brace_end){
    				current_module=NULL;
    				reset_char(module_name);
    				status=MODULE_START;
    			}else{
    				add_char(command_key,c);
    				status=COMMAND_KEY_START;
    			}
    		}else if(status==COMMAND_KEY_START){
    			if(!char_is_special(c)){
    				add_char(command_key,c);
    			}else{
    				add_terminal(command_key);
    				current_command=find_config_command(command_key,current_module);
    				if(current_command==NULL){
    					my_log(ERROR,"config error,please check\n");
    					goto CONFIG_END;
    				}
    				status=COMMAND_VALUE_START;
    			}
				
    		}else if(status==COMMAND_VALUE_START&&!char_is_special(c)){
    			if(c!=semicolon){
    				add_char(command_value,c);
    			}else{
    				add_terminal(command_value);
    				current_command->set_value(command_value);
    				current_command=NULL;
    				reset_char(command_value);
    				reset_char(command_key);
    				status=FIND_COMMAND_KEY;
    			}
    		}
    	}
	}
CONFIG_END:
	destroy_buffer(rbuf);
}
Example #28
0
File: super.c Project: HarryR/sanos
struct filsys *create_filesystem(vfs_devno_t devno, int blocksize, int inode_ratio, int quick)
{
  struct filsys *fs;
  unsigned int blocks;
  unsigned int first_block;
  struct groupdesc *gd;
  struct buf *buf;
  unsigned int i, j;
  vfs_ino_t ino;
  struct inode *root;
  char *buffer;

  // Allocate file system
  fs = (struct filsys *) malloc(sizeof(struct filsys));
  memset(fs, 0, sizeof(struct filsys));

  // Allocate super block
  fs->super = (struct superblock *) malloc(SECTORSIZE);
  memset(fs->super, 0, SECTORSIZE);
  fs->super_dirty = 1;

  // Set device number and block size
  fs->devno = devno;
  fs->blocksize = blocksize;

  // Initialize buffer cache
  fs->cache = init_buffer_pool(devno, CACHEBUFFERS, fs->blocksize);

  // Set signature, version and block size in super block
  fs->super->signature = DFS_SIGNATURE;
  fs->super->version = DFS_VERSION;
  fs->super->log_block_size = bits(blocksize);

  // Each group has as many blocks as can be represented by the block bitmap block
  fs->super->blocks_per_group = fs->blocksize * 8;

  // Get the device size in sectors from the device and convert it to blocks
  fs->super->block_count = dev_getsize(fs->devno) / (fs->blocksize / SECTORSIZE);

  // Set cache size
  fs->super->cache_buffers = CACHEBUFFERS;
  if (fs->super->cache_buffers > fs->super->block_count) fs->super->cache_buffers = 64;

  // The number of inodes in a group is computed as a ratio of the size of group
  fs->inodes_per_block = fs->blocksize / sizeof(struct inodedesc);
  if (fs->super->blocks_per_group < fs->super->block_count)
    fs->super->inodes_per_group = fs->blocksize * fs->super->blocks_per_group / inode_ratio;
  else
    fs->super->inodes_per_group = fs->blocksize * fs->super->block_count / inode_ratio;
  if (fs->super->inodes_per_group > fs->blocksize * 8) fs->super->inodes_per_group = fs->blocksize * 8;
  fs->inode_blocks_per_group = (fs->super->inodes_per_group * sizeof(struct inodedesc) + fs->blocksize - 1) / fs->blocksize;

  // Calculate the number of block pointers per block directory page
  fs->log_blkptrs_per_block = fs->super->log_block_size - 2;

  // Calculate the number of group descriptors and the number of blocks to store them
  fs->super->group_count = (fs->super->block_count + fs->super->blocks_per_group - 1) / fs->super->blocks_per_group;
  fs->groupdescs_per_block = fs->blocksize / sizeof(struct groupdesc);
  fs->groupdesc_blocks = (fs->super->group_count * sizeof(struct groupdesc) + fs->blocksize - 1) / fs->blocksize;

  // The reserved blocks are allocated right after the super block
  fs->super->first_reserved_block = 1;
  if (fs->blocksize <= SECTORSIZE) fs->super->first_reserved_block++;
  fs->super->reserved_blocks = RESERVED_BLOCKS;

  // The group descriptor table starts after the superblock and reserved blocks
  fs->super->groupdesc_table_block = fs->super->first_reserved_block + fs->super->reserved_blocks;

  // If the last group is too small to hold the bitmaps and inode table skip it
  blocks =  fs->super->block_count % fs->super->blocks_per_group;
  if (blocks > 0 && blocks < fs->inode_blocks_per_group + 2) fs->super->group_count--;
  if (fs->super->group_count == 0) panic("filesystem too small");

  // Zero all blocks on disk
  if (!quick)
  {
    buffer = (char *) malloc(fs->blocksize);
    memset(buffer, 0, fs->blocksize);

    for (i = fs->super->groupdesc_table_block + fs->groupdesc_blocks; i < fs->super->block_count; i++)
    {
      dev_write(fs->devno, buffer, fs->blocksize, i);
    }

    free(buffer);
  }

  // Allocate group descriptors
  fs->groupdesc_buffers = (struct buf **) malloc(sizeof(struct buf *) * fs->groupdesc_blocks);
  fs->groups = (struct group *) malloc(sizeof(struct group) * fs->super->group_count);

  for (i = 0; i < fs->groupdesc_blocks; i++)
  {
    fs->groupdesc_buffers[i] = alloc_buffer(fs->cache, fs->super->groupdesc_table_block + i);
  }

  for (i = 0; i < fs->super->group_count; i++)
  {
    gd = (struct groupdesc *) fs->groupdesc_buffers[i / fs->groupdescs_per_block]->data;
    gd += (i % fs->groupdescs_per_block);

    fs->groups[i].desc = gd;
    fs->groups[i].first_free_block = 0;
    fs->groups[i].first_free_inode = 0;
  }

  // Reserve inode for root directory
  fs->super->reserved_inodes = RESERVED_INODES;

  // Set inode count based on group count
  fs->super->inode_count = fs->super->inodes_per_group * fs->super->group_count;

  // All blocks and inodes initially free
  fs->super->free_inode_count = fs->super->inode_count;
  fs->super->free_block_count = fs->super->block_count;

  // Initialize block bitmaps
  for (i = 0; i < fs->super->group_count; i++)
  {
    gd = fs->groups[i].desc;
    blocks = 0;
    first_block = fs->super->blocks_per_group * i;

    // The first group needs blocks for the super block and the group descriptors
    if (i == 0) blocks = fs->super->groupdesc_table_block + fs->groupdesc_blocks;

    // Next blocks in group are the block bitmap, inode bitmap and the inode table
    gd->block_bitmap_block = first_block + blocks++;
    gd->inode_bitmap_block = first_block + blocks++;
    gd->inode_table_block = first_block + blocks;
    blocks += fs->inode_blocks_per_group;

    // Update block bitmap
    buf = alloc_buffer(fs->cache, gd->block_bitmap_block);
    set_bits(buf->data, 0, blocks);
    mark_buffer_updated(buf);
    release_buffer(fs->cache, buf);

    // Determine the block count for the group. The last group may be truncated
    if (fs->super->blocks_per_group * (i + 1) > fs->super->block_count)
      gd->block_count = fs->super->block_count - fs->super->blocks_per_group * i;
    else
      gd->block_count = fs->super->blocks_per_group;

    // Set the count of free blocks and inodes for group
    gd->free_inode_count = fs->super->inodes_per_group;
    gd->free_block_count = gd->block_count - blocks;

    // Update super block
    fs->super->free_block_count -= blocks;

    mark_group_desc_dirty(fs, i);
  }

  // Zero out block and inode bitmaps and inode tables
  if (quick)
  {
    buffer = (char *) malloc(fs->blocksize);
    memset(buffer, 0, fs->blocksize);

    for (i = 0; i < fs->super->group_count; i++)
    {
      gd = fs->groups[i].desc;

      dev_write(fs->devno, buffer, fs->blocksize, gd->block_bitmap_block);
      dev_write(fs->devno, buffer, fs->blocksize, gd->inode_bitmap_block);
      for (j = 0; j < fs->inode_blocks_per_group; j++)
      {
        dev_write(fs->devno, buffer, fs->blocksize, gd->inode_table_block + j);
      }
    }

    free(buffer);
  }

  // Reserve inodes
  for (i = 0; i < RESERVED_INODES; i++)
  {
    ino = new_inode(fs, 0, 0);
    if (ino != i) panic("unexpected inode");
  }

  // Create root directory
  root = get_inode(fs, DFS_INODE_ROOT);
  root->desc->mode = VFS_S_IFDIR | VFS_S_IRWXU | VFS_S_IRWXG | VFS_S_IRWXO;
  root->desc->ctime = root->desc->mtime = time(NULL);
  root->desc->linkcount = 1;
  mark_buffer_updated(root->buf);
  release_inode(root);

  return fs;
}
void G1PLABAllocator::undo_allocation(InCSetState dest, HeapWord* obj, size_t word_sz, AllocationContext_t context) {
  alloc_buffer(dest, context)->undo_allocation(obj, word_sz);
}
Example #30
0
/**
	process_run : cmd:string -> args:string array -> 'process
	<doc>
	Start a process using a command and the specified arguments.
	When args is not null, cmd and args will be auto-quoted/escaped.
	If no auto-quoting/escaping is desired, you should append necessary 
	arguments to cmd as if it is inputted to the shell directly, and pass
	null as args.
	</doc>
**/
static value process_run( value cmd, value vargs ) {
	int i, isRaw;
	vprocess *p;
	val_check(cmd,string);
	isRaw = val_is_null(vargs);
	if (!isRaw) {
		val_check(vargs,array);
	}
#	ifdef NEKO_WINDOWS
	{		 
		SECURITY_ATTRIBUTES sattr;		
		STARTUPINFO sinf;
		HANDLE proc = GetCurrentProcess();
		HANDLE oread,eread,iwrite;
		// creates commandline
		buffer b = alloc_buffer(NULL);
		value sargs;
		if (isRaw) {
			char* cmdexe = getenv("COMSPEC");
			if (!cmdexe) cmdexe = "cmd.exe";
			buffer_append(b,"\"");
			buffer_append(b,cmdexe);
			buffer_append(b,"\" /C \"");
			buffer_append(b,val_string(cmd));
			buffer_append_char(b,'"');
		} else {
			buffer_append_char(b,'"');
			val_buffer(b,cmd);
			buffer_append_char(b,'"');
			for(i=0;i<val_array_size(vargs);i++) {
				value v = val_array_ptr(vargs)[i];
				int j,len;
				unsigned int bs_count = 0;
				unsigned int k;
				val_check(v,string);
				len = val_strlen(v);
				buffer_append(b," \"");
				for(j=0;j<len;j++) {
					char c = val_string(v)[j];
					switch( c ) {
					case '"':
						// Double backslashes.
						for (k=0;k<bs_count*2;k++) {
							buffer_append_char(b,'\\');
						}
						bs_count = 0;
						buffer_append(b, "\\\"");
						break;
					case '\\':
						// Don't know if we need to double yet.
						bs_count++;
						break;
					default:
						// Normal char
						for (k=0;k<bs_count;k++) {
							buffer_append_char(b,'\\');
						}
						bs_count = 0;
						buffer_append_char(b,c);
						break;
					}
				}
				// Add remaining backslashes, if any.
				for (k=0;k<bs_count*2;k++) {
					buffer_append_char(b,'\\');
				}
				buffer_append_char(b,'"');
			}
		}
		sargs = buffer_to_string(b);
		p = (vprocess*)alloc_private(sizeof(vprocess));
		// startup process
		sattr.nLength = sizeof(sattr);
		sattr.bInheritHandle = TRUE;
		sattr.lpSecurityDescriptor = NULL;
		memset(&sinf,0,sizeof(sinf));
		sinf.cb = sizeof(sinf);
		sinf.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
		sinf.wShowWindow = SW_HIDE;
		CreatePipe(&oread,&sinf.hStdOutput,&sattr,0);
		CreatePipe(&eread,&sinf.hStdError,&sattr,0);
		CreatePipe(&sinf.hStdInput,&iwrite,&sattr,0);
		DuplicateHandle(proc,oread,proc,&p->oread,0,FALSE,DUPLICATE_SAME_ACCESS);
		DuplicateHandle(proc,eread,proc,&p->eread,0,FALSE,DUPLICATE_SAME_ACCESS);
		DuplicateHandle(proc,iwrite,proc,&p->iwrite,0,FALSE,DUPLICATE_SAME_ACCESS);
		CloseHandle(oread);
		CloseHandle(eread);
		CloseHandle(iwrite);
		if( !CreateProcess(NULL,val_string(sargs),NULL,NULL,TRUE,0,NULL,NULL,&sinf,&p->pinf) )			
			neko_error();
		// close unused pipes
		CloseHandle(sinf.hStdOutput);
		CloseHandle(sinf.hStdError);
		CloseHandle(sinf.hStdInput);
	}
#	else
	char **argv;
	if (isRaw) {
		argv = (char**)alloc_private(sizeof(char*)*4);
		argv[0] = "/bin/sh";
		argv[1] = "-c";
		argv[2] = val_string(cmd);
		argv[3] = NULL;
	} else {
		argv = (char**)alloc_private(sizeof(char*)*(val_array_size(vargs)+2));
		argv[0] = val_string(cmd);
		for(i=0;i<val_array_size(vargs);i++) {
			value v = val_array_ptr(vargs)[i];
			val_check(v,string);
			argv[i+1] = val_string(v);
		}
		argv[i+1] = NULL;
	}
	int input[2], output[2], error[2];
	if( pipe(input) || pipe(output) || pipe(error) )
		neko_error();
	p = (vprocess*)alloc_private(sizeof(vprocess));
	p->pid = fork();
	if( p->pid == -1 ) {
		do_close(input[0]);
		do_close(input[1]);
		do_close(output[0]);
		do_close(output[1]);
		do_close(error[0]);
		do_close(error[1]);
		neko_error();
	}
	// child
	if( p->pid == 0 ) {
		close(input[1]);
		close(output[0]);
		close(error[0]);
		dup2(input[0],0);
		dup2(output[1],1);
		dup2(error[1],2);
		execvp(argv[0],argv);
		fprintf(stderr,"Command not found : %s\n",val_string(cmd));
		exit(1);
	}
	// parent
	do_close(input[0]);
	do_close(output[1]);
	do_close(error[1]);
	p->iwrite = input[1];
	p->oread = output[0];
	p->eread = error[0];
#	endif
	{
		value vp = alloc_abstract(k_process,p);
		val_gc(vp,free_process);
		return vp;
	}
}