Esempio n. 1
0
void datastore_put(struct datastore *ds, void *data, unsigned int length, int in_unitsize, int *probelist)
{
	int capacity, stored, size, num_chunks, chunk_bytes_free, chunk_offset;
	gpointer chunk;

	if(ds->chunklist == NULL)
		chunk = new_chunk(&ds);
	else
		chunk = g_slist_last(ds->chunklist)->data;
	num_chunks = g_slist_length(ds->chunklist);
	capacity = (num_chunks * DATASTORE_CHUNKSIZE);
	chunk_bytes_free = capacity - (ds->ds_unitsize * ds->num_units);
	chunk_offset = capacity - (DATASTORE_CHUNKSIZE * (num_chunks - 1)) - chunk_bytes_free;
	stored = 0;
	while(stored < length) {
		if(chunk_bytes_free == 0) {
			chunk = new_chunk(&ds);
			chunk_bytes_free = DATASTORE_CHUNKSIZE;
			chunk_offset = 0;
		}

		if(length - stored > chunk_bytes_free)
			size = chunk_bytes_free;
		else
			/* last part, won't fill up this chunk */
			size = length - stored;
		memcpy(chunk + chunk_offset, data + stored, size);
		chunk_bytes_free -= size;
		stored += size;
	}
	ds->num_units += stored / ds->ds_unitsize;

}
Esempio n. 2
0
static int make_chunk_chain (SLindex_Type length, Chunk_Type **firstp, Chunk_Type **lastp, int chunk_size)
{
   Chunk_Type *last, *first;

   if (NULL == (first = new_chunk (chunk_size)))
     return -1;
   length -= chunk_size;

   last = first;
   while (length > 0)
     {
	Chunk_Type *next;
	if (NULL == (next = new_chunk (chunk_size)))
	  {
	     delete_chunk_chain (first);
	     return -1;
	  }
	last->next = next;
	next->prev = last;
	last = next;
	length -= chunk_size;
     }
   *firstp = first;
   *lastp = last;
   return 0;
}
Esempio n. 3
0
static void* read_recipe_thread(void *arg) {

	int i, j, k;
	for (i = 0; i < jcr.bv->number_of_files; i++) {
		TIMER_DECLARE(1);
		TIMER_BEGIN(1);

		struct recipeMeta *r = read_next_recipe_meta(jcr.bv);

		struct chunk *c = new_chunk(sdslen(r->filename) + 1);
		strcpy(c->data, r->filename);
		SET_CHUNK(c, CHUNK_FILE_START);

		TIMER_END(1, jcr.read_recipe_time);

		sync_queue_push(restore_recipe_queue, c);

		jcr.file_num++;

		for (j = 0; j < r->chunknum; j++) {
			TIMER_DECLARE(1);
			TIMER_BEGIN(1);

			struct chunkPointer* cp = read_next_n_chunk_pointers(jcr.bv, 1, &k);

			struct chunk* c = new_chunk(0);
			memcpy(&c->fp, &cp->fp, sizeof(fingerprint));
			c->size = cp->size;
			c->id = cp->id;

			TIMER_END(1, jcr.read_recipe_time);
			jcr.data_size += c->size;
			jcr.chunk_num++;

			sync_queue_push(restore_recipe_queue, c);
			free(cp);
		}

		c = new_chunk(0);
		SET_CHUNK(c, CHUNK_FILE_END);
		sync_queue_push(restore_recipe_queue, c);

		free_recipe_meta(r);
	}

	sync_queue_term(restore_recipe_queue);
	return NULL;
}
Esempio n. 4
0
    void* BlockDataAllocator::Allocate()
    {
        boost::mutex::scoped_lock lock(mutex_);
        void* result = 0;
        for(size_t i = 0; i < chunks_.size(); ++i)
        {
            result = chunks_[i]->TryAllocate();
            if (result)
            {
                break;
            }
        }

        if (!result)
        {
            boost::shared_ptr<MemoryChunk> new_chunk(new MemoryChunk());
            result = new_chunk->TryAllocate();
            assert(result);

            if (result)
            {
                chunks_.push_back(new_chunk);
            }
        }

        return result;
    }
Esempio n. 5
0
 // This alloc_top will insure that the object is aligned based on
 // the alignment given.
 void * alloc_top(size_t size, size_t align) 
 {loop:
   top -= size;
   align_top(align);
   if (top < bottom) {new_chunk(); goto loop;}
   return top;
 }
Esempio n. 6
0
/**
 * @brief
 *		dup_chunk - copy constructor for chunk
 *
 * @param[in]	ochunk	-	old chunk structure
 *
 * @return	duplicate chunk structure.
 */
chunk *
dup_chunk(chunk *ochunk)
{
	chunk *nchunk;

	if (ochunk == NULL)
		return NULL;

	nchunk = new_chunk();

	if (nchunk == NULL)
		return NULL;

	nchunk->num_chunks = ochunk->num_chunks;
	nchunk->seq_num = ochunk->seq_num;
	nchunk->str_chunk = string_dup(ochunk->str_chunk);
	nchunk->req = dup_resource_req_list(ochunk->req);

	if (nchunk->req == NULL) {
		free_chunk(nchunk);
		return NULL;
	}

	return nchunk;
}
Esempio n. 7
0
 // This alloc_bottom will insure that the object is aligned based on the
 // alignment given.
 void * alloc_bottom(size_t size, size_t align) 
 {loop:
   align_bottom(align);
   byte * tmp = bottom;
   bottom += size;
   if (bottom > top) {new_chunk(); goto loop;}
   return tmp;
 }
Esempio n. 8
0
 void * alloc_temp(size_t size) {
   temp_end = bottom + size;
   if (temp_end > top) {
     new_chunk();
     temp_end = bottom + size;
   }
   return bottom;
 }
Esempio n. 9
0
void HText_appendCharacter (HText *text, char ch)
#endif
{
  if (text->srcalloc < text->srclen + 1)
    new_chunk (text);

  text->htmlSrc[text->srclen++] = ch;

  return;
}
Esempio n. 10
0
/* product of permutations. Put the result in perm1. */
static void
perm_mul_i(GEN perm1, GEN perm2)
{
  long i, N = lg(perm1);
  pari_sp av = avma;
  GEN perm = new_chunk(N);
  for (i=1; i<N; i++) perm[i] = perm1[perm2[i]];
  for (i=1; i<N; i++) perm1[i]= perm[i];
  avma = av;
}
Esempio n. 11
0
/** Stores a particle ID and position, allocating a new memory chunk if necessary.
 * \param[in] n the numerical ID of the inserted particle.
 * \param[in] (x,y,z) the position vector of the inserted particle.
 * \param[in] r the radius of the particle. */
void pre_container_poly::put(int n,double x,double y,double z,double r) {
	if((xperiodic||(x>=ax&&x<=bx))&&(yperiodic||(y>=ay&&y<=by))&&(zperiodic||(z>=az&&z<=bz))) {
		if(ch_id==e_id) new_chunk();
		*(ch_id++)=n;
		*(ch_p++)=x;*(ch_p++)=y;*(ch_p++)=z;*(ch_p++)=r;
	}
#if VOROPP_REPORT_OUT_OF_BOUNDS ==1
	else fprintf(stderr,"Out of bounds: (x,y,z)=(%g,%g,%g)\n",x,y,z);
#endif
}
Esempio n. 12
0
File: gcd.c Progetto: jkeuffer/pari
GEN
gcdii(GEN a, GEN b)
{
  long v, w;
  pari_sp av;
  GEN t;

  switch (absi_cmp(a,b))
  {
    case 0: return absi(a);
    case -1: swap(a,b);
  }
  if (!signe(b)) return absi(a);
  /* here |a|>|b|>0. Try single precision first */
  if (lgefint(a)==3)
    return igcduu((ulong)a[2], (ulong)b[2]);
  if (lgefint(b)==3)
  {
    ulong u = resiu(a,(ulong)b[2]);
    if (!u) return absi(b);
    return igcduu((ulong)b[2], u);
  }
  /* larger than gcd: "avma=av" gerepile (erasing t) is valid */
  av = avma; (void)new_chunk(lgefint(b)+1); /* HACK */
  t = remii(a,b);
  if (!signe(t)) { avma=av; return absi(b); }

  a = b; b = t;
  v = vali(a); a = shifti(a,-v); setabssign(a);
  w = vali(b); b = shifti(b,-w); setabssign(b);
  if (w < v) v = w;
  switch(absi_cmp(a,b))
  {
    case  0: avma=av; a=shifti(a,v); return a;
    case -1: swap(a,b);
  }
  if (is_pm1(b)) { avma=av; return int2n(v); }
 {
  /* general case */
  /*This serve two purposes: 1) mpn_gcd destroy its input and need an extra
   * limb 2) this allows us to use icopy instead of gerepile later.  NOTE: we
   * must put u before d else the final icopy could fail.
   */
  GEN res= cgeti(lgefint(a)+1);
  GEN ca = icopy_ef(a,lgefint(a)+1);
  GEN cb = icopy_ef(b,lgefint(b)+1);
  long l = mpn_gcd(LIMBS(res), LIMBS(ca), NLIMBS(ca), LIMBS(cb), NLIMBS(cb));
  res[1] = evalsigne(1)|evallgefint(l+2);
  avma=av;
  return shifti(res,v);
  }
}
Esempio n. 13
0
void send_segment(struct segment* s) {
    /*
     * CHUNK_SEGMENT_START and _END are used for
     * reconstructing the segment in filter phase.
     */
    struct chunk* ss = new_chunk(0);
    SET_CHUNK(ss, CHUNK_SEGMENT_START);
    sync_queue_push(dedup_queue, ss);

    GSequenceIter *end = g_sequence_get_end_iter(s->chunks);
    GSequenceIter *begin = g_sequence_get_begin_iter(s->chunks);
    while(begin != end) {
        struct chunk* c = g_sequence_get(begin);
        if (!CHECK_CHUNK(c, CHUNK_FILE_START) && !CHECK_CHUNK(c, CHUNK_FILE_END)) {
            if (CHECK_CHUNK(c, CHUNK_DUPLICATE)) {
                if (c->id == TEMPORARY_ID) {
                    DEBUG("Dedup phase: %ldth chunk is identical to a unique chunk",
                          chunk_num++);
                } else {
                    DEBUG("Dedup phase: %ldth chunk is duplicate in container %lld",
                          chunk_num++, c->id);
                }
            } else {
                DEBUG("Dedup phase: %ldth chunk is unique", chunk_num++);
            }

        }
        sync_queue_push(dedup_queue, c);
        g_sequence_remove(begin);
        begin = g_sequence_get_begin_iter(s->chunks);
    }

    struct chunk* se = new_chunk(0);
    SET_CHUNK(se, CHUNK_SEGMENT_END);
    sync_queue_push(dedup_queue, se);

    s->chunk_num = 0;

}
Esempio n. 14
0
/* all entries in y have the same type t = t_VEC, COL, MAT or VECSMALL
 * concatenate y[k1..k2], with yi = y + ki, k1 <= k2 */
static GEN
catmany(GEN y1, GEN y2, long t)
{
  long i, L;
  GEN z, y;
  if (y1 == y2) return gel(y1,0);
  if (t == t_MAT) return catmanyMAT(y1, y2);
  if (t == t_STR) return catmanySTR(y1, y2);
  L = 1;
  for (y = y2; y >= y1; y--)
  {
    GEN c = gel(y,0);
    long nc = lg(c)-1;
    if (nc == 0) continue;
    L += nc;
    z = new_chunk(nc) - 1;
    for (i=1; i<=nc; i++) gel(z,i) = gel(c,i);
  }
  z = new_chunk(1);
  *z = evaltyp(t) | evallg(L);
  return z;
}
Esempio n. 15
0
void add_chunk (struct atom *atm_ptr, int component_number, double contact_area, double reentrant_area, double accessible_area)
{
	struct chunk *head_chunk, *this_chunk;
	struct chunk *previous_chunk;
	struct chunk *added_chunk;

	head_chunk = atm_ptr -> head_chunk;

	if (head_chunk == NULL) {
		/* start new linked list */
		added_chunk = new_chunk (atm_ptr, component_number,
			contact_area, reentrant_area, accessible_area);
		if (error()) return;
		atm_ptr -> head_chunk = added_chunk;
		return;
	}
	/* hunt down linked list */
	previous_chunk = NULL;
	for (this_chunk = head_chunk; this_chunk != NULL;
		this_chunk = this_chunk -> next[0]) {
		if (this_chunk -> component_number == component_number) {
			this_chunk -> contact_area += contact_area;
			this_chunk -> reentrant_area += reentrant_area;
			this_chunk -> accessible_area += accessible_area;
			return;
		}
		if (this_chunk -> component_number > component_number) break;
		previous_chunk = this_chunk;
	}
	/* link into list */
	added_chunk = new_chunk (atm_ptr, component_number,
		contact_area, reentrant_area, accessible_area);
	if (previous_chunk == NULL)
		atm_ptr -> head_chunk = added_chunk;
	else previous_chunk -> next[0] = added_chunk;
	added_chunk -> next[0] = this_chunk;
	return;
}
Esempio n. 16
0
 // returns a pointer the the new beginning of the temp memory
 void * resize_temp(size_t size) {
   if (temp_end == 0)
     return alloc_temp(size);
   if (bottom + size <= top) {
     temp_end = bottom + size;
   } else {
     size_t s = temp_end - bottom;
     byte * p = bottom;
     new_chunk();
     memcpy(bottom, p, s);
     temp_end = bottom + size;
   }
   return bottom;
 }
Esempio n. 17
0
/* see catmany() */
static GEN
catmanyMAT(GEN y1, GEN y2)
{
  long i, h = 0, L = 1;
  GEN z, y;
  for (y = y2; y >= y1; y--)
  {
    GEN c = gel(y,0);
    long nc = lg(c)-1;
    if (nc == 0) continue;
    if (h != lgcols(c))
    {
      if (h) err_cat(gel(y2,0), c);
      h = lgcols(c);
    }
    L += nc;
    z = new_chunk(nc) - 1;
    for (i=1; i<=nc; i++) gel(z,i) = gel(c,i);
  }
  z = new_chunk(1);
  *z = evaltyp(t_MAT) | evallg(L);
  return z;
}
Esempio n. 18
0
struct chunk* get_chunk_in_container(struct container* c, fingerprint *fp) {
	struct metaEntry* me = get_metaentry_in_container_meta(&c->meta, fp);

	assert(me);

	struct chunk* ck = new_chunk(me->len);

	if (destor.simulation_level < SIMULATION_RESTORE)
		memcpy(ck->data, c->data + me->off, me->len);

	ck->size = me->len;
	ck->id = c->meta.id;
	memcpy(&ck->fp, &fp, sizeof(fingerprint));

	return ck;
}
Esempio n. 19
0
void HText_appendBlock (HText *text, char *data, int len)
{
  if (!data)
    return;

  while (text->srcalloc < text->srclen + len + 1)
    new_chunk (text);

/*  bcopy (data, (text->htmlSrc + text->srclen), len);*/
  memcpy((text->htmlSrc + text->srclen), data, len);

  text->srclen += len;
  text->htmlSrc[text->srclen] = '\0';

  return;
}
Esempio n. 20
0
 // returns a pointer to the beginning of the new memory (in
 // otherwords the END of the temp memory BEFORE the call to grow
 // temp) NOT the beginning if the temp memory
 void * grow_temp(size_t s) {
   if (temp_end == 0)
     return alloc_temp(s);
   unsigned old_size = temp_end - bottom;
   unsigned size = old_size + s;
   if (bottom + size <= top) {
     temp_end = bottom + size;
   } else {
     size_t s = temp_end - bottom;
     byte * p = bottom;
     new_chunk();
     memcpy(bottom, p, s);
     temp_end = bottom + size;
   }
   return bottom + old_size;
 }
Esempio n. 21
0
static Channel *find_channel(Song *song, Chunk **chunk)
{
    /* last chunk is finished */
    if(*chunk && (*chunk)->pos == (*chunk)->nb_channels)
    {
        (*chunk)->pos = 0;
        *chunk = NULL;
    }
    /* no current chunk ; auto-allocate one */
    if(!*chunk)
    {
        if(g_debug)
            fprintf(stderr, "Autocreating chunk (1 channel)\n");
        *chunk = new_chunk(song, 1);
    }
    return (*chunk)->channels[(*chunk)->pos];
}
bool VDAgent::handle_display_config(VDAgentDisplayConfig* display_config, uint32_t port)
{
    DisplaySettingOptions disp_setting_opts;
    VDIChunk* reply_chunk;
    VDAgentMessage* reply_msg;
    VDAgentReply* reply;
    DWORD msg_size;

    if (display_config->flags & VD_AGENT_DISPLAY_CONFIG_FLAG_DISABLE_WALLPAPER) {
        disp_setting_opts._disable_wallpaper = TRUE;
    }

    if (display_config->flags & VD_AGENT_DISPLAY_CONFIG_FLAG_DISABLE_FONT_SMOOTH) {
       disp_setting_opts._disable_font_smoothing = TRUE;
    }

    if (display_config->flags & VD_AGENT_DISPLAY_CONFIG_FLAG_DISABLE_ANIMATION) {
        disp_setting_opts._disable_animation = TRUE;
    }

    _display_setting.set(disp_setting_opts);

    if (display_config->flags & VD_AGENT_DISPLAY_CONFIG_FLAG_SET_COLOR_DEPTH) {
        set_display_depth(display_config->depth);
    }

    msg_size = VD_MESSAGE_HEADER_SIZE + sizeof(VDAgentReply);
    reply_chunk = new_chunk(msg_size);
    if (!reply_chunk) {
        return false;
    }
    reply_chunk->hdr.port = port;
    reply_chunk->hdr.size = sizeof(VDAgentMessage) + sizeof(VDAgentReply);
    reply_msg = (VDAgentMessage*)reply_chunk->data;
    reply_msg->protocol = VD_AGENT_PROTOCOL;
    reply_msg->type = VD_AGENT_REPLY;
    reply_msg->opaque = 0;
    reply_msg->size = sizeof(VDAgentReply);
    reply = (VDAgentReply*)reply_msg->data;
    reply->type = VD_AGENT_DISPLAY_CONFIG;
    reply->error = VD_AGENT_SUCCESS;
    enqueue_chunk(reply_chunk);
    return true;
}
Esempio n. 23
0
void HText_appendText (HText *text, char *str)
{
  int len;

  if (!str)
    return;

  len = strlen (str);

  while (text->srcalloc < text->srclen + len + 1)
    new_chunk (text);

/*  bcopy (str, (text->htmlSrc + text->srclen), len);*/
  memcpy((text->htmlSrc + text->srclen), str, len);

  text->srclen += len;
  text->htmlSrc[text->srclen] = '\0';

  return;
}
Esempio n. 24
0
/* cy is a cycle; compute cy^l as a permutation */
static GEN
cycle_power_to_perm(GEN perm,GEN cy,long l)
{
  long lp,i,j,b, N = lg(perm), lcy = lg(cy)-1;

  lp = l % lcy;
  for (i=1; i<N; i++) perm[i] = i;
  if (lp)
  {
    pari_sp av = avma;
    GEN p1 = new_chunk(N);
    b = cy[1];
    for (i=1; i<lcy; i++) b = (perm[b] = cy[i+1]);
    perm[b] = cy[1];
    for (i=1; i<N; i++) p1[i] = perm[i];

    for (j=2; j<=lp; j++) perm_mul_i(perm,p1);
    avma = av;
  }
  return perm;
}
Esempio n. 25
0
static int rpc_add(rpc_ctx_t* ctx, char* fmt, ...)
{
	void** void_ptr;
	va_list ap;
	str s = {"", 0};
	struct text_chunk* l;

	va_start(ap, fmt);
	while(*fmt) {
		if (*fmt == '{' || *fmt == '[') {
			void_ptr = va_arg(ap, void**);
			l = new_chunk(&s);
			if (!l) {
				rpc_fault(ctx, 500, "Internal Server Error");
				goto err;
			}
			l->ctx=ctx;
			append_chunk(ctx, l);
			*void_ptr = l;
		} else {
			if (print_value(ctx, *fmt, &ap) < 0) goto err;
bool VDAgent::send_announce_capabilities(bool request)
{
    DWORD msg_size;
    VDIChunk* caps_chunk;
    VDAgentMessage* caps_msg;
    VDAgentAnnounceCapabilities* caps;
    uint32_t caps_size;
    uint32_t internal_msg_size = sizeof(VDAgentAnnounceCapabilities) + VD_AGENT_CAPS_BYTES;

    msg_size = VD_MESSAGE_HEADER_SIZE + internal_msg_size;
    caps_chunk = new_chunk(msg_size);
    if (!caps_chunk) {
        return false;
    }
    caps_size = VD_AGENT_CAPS_SIZE;
    caps_chunk->hdr.port = VDP_CLIENT_PORT;
    caps_chunk->hdr.size = sizeof(VDAgentMessage) + internal_msg_size;
    caps_msg = (VDAgentMessage*)caps_chunk->data;
    caps_msg->protocol = VD_AGENT_PROTOCOL;
    caps_msg->type = VD_AGENT_ANNOUNCE_CAPABILITIES;
    caps_msg->opaque = 0;
    caps_msg->size = internal_msg_size;
    caps = (VDAgentAnnounceCapabilities*)caps_msg->data;
    caps->request = request;
    memset(caps->caps, 0, VD_AGENT_CAPS_BYTES);
    VD_AGENT_SET_CAPABILITY(caps->caps, VD_AGENT_CAP_MOUSE_STATE);
    VD_AGENT_SET_CAPABILITY(caps->caps, VD_AGENT_CAP_MONITORS_CONFIG);
    VD_AGENT_SET_CAPABILITY(caps->caps, VD_AGENT_CAP_REPLY);
    VD_AGENT_SET_CAPABILITY(caps->caps, VD_AGENT_CAP_DISPLAY_CONFIG);
    VD_AGENT_SET_CAPABILITY(caps->caps, VD_AGENT_CAP_CLIPBOARD_BY_DEMAND);
    VD_AGENT_SET_CAPABILITY(caps->caps, VD_AGENT_CAP_SPARSE_MONITORS_CONFIG);
    VD_AGENT_SET_CAPABILITY(caps->caps, VD_AGENT_CAP_GUEST_LINEEND_CRLF);
    VD_AGENT_SET_CAPABILITY(caps->caps, VD_AGENT_CAP_MAX_CLIPBOARD);
    vd_printf("Sending capabilities:");
    for (uint32_t i = 0 ; i < caps_size; ++i) {
        vd_printf("%X", caps->caps[i]);
    }
    enqueue_chunk(caps_chunk);
    return true;
}
bool VDAgent::write_message(uint32_t type, uint32_t size = 0, void* data = NULL)
{
    VDIChunk* chunk;
    VDAgentMessage* msg;

    chunk = new_chunk(VD_MESSAGE_HEADER_SIZE + size);
    if (!chunk) {
        return false;
    }
    chunk->hdr.port = VDP_CLIENT_PORT;
    chunk->hdr.size = sizeof(VDAgentMessage) + size;
    msg = (VDAgentMessage*)chunk->data;
    msg->protocol = VD_AGENT_PROTOCOL;
    msg->type = type;
    msg->opaque = 0;
    msg->size = size;
    if (size && data) {
        memcpy(msg->data, data, size);
    }
    enqueue_chunk(chunk);
    return true;
}
Esempio n. 28
0
GEN
shallowextract(GEN x, GEN L)
{
  long i,j, tl = typ(L), tx = typ(x), lx = lg(x);
  GEN y;

  switch(tx)
  {
    case t_VEC:
    case t_COL:
    case t_MAT:
    case t_VECSMALL: break;
    default: pari_err_TYPE("extract",x);

  }
  if (tl==t_INT)
  { /* extract components of x as per the bits of mask L */
    long k, l, ix, iy, maxj;
    GEN Ld;
    if (!signe(L)) return cgetg(1,tx);
    y = new_chunk(lx);
    l = lgefint(L)-1; ix = iy = 1;
    maxj = BITS_IN_LONG - bfffo(*int_MSW(L));
    if ((l-2) * BITS_IN_LONG + maxj >= lx)
      pari_err_TYPE("vecextract [mask too large]", L);
    for (k = 2, Ld = int_LSW(L); k < l; k++, Ld = int_nextW(Ld))
    {
      ulong B = *Ld;
      for (j = 0; j < BITS_IN_LONG; j++, B >>= 1, ix++)
        if (B & 1) y[iy++] = x[ix];
    }
    { /* k = l */
      ulong B = *Ld;
      for (j = 0; j < maxj; j++, B >>= 1, ix++)
        if (B & 1) y[iy++] = x[ix];
    }
    y[0] = evaltyp(tx) | evallg(iy);
    return y;
  }
bool VDAgent::write_clipboard(VDAgentMessage* msg, uint32_t size)
{
    uint32_t pos = 0;
    bool ret = true;

    ASSERT(msg && size);
    //FIXME: do it smarter - no loop, no memcopy
    MUTEX_LOCK(_message_mutex);
    while (pos < size) {
        DWORD n = MIN(sizeof(VDIChunk) + size - pos, VD_AGENT_MAX_DATA_SIZE);
        VDIChunk* chunk = new_chunk(n);
        if (!chunk) {
            ret = false;
            break;
        }
        chunk->hdr.port = VDP_CLIENT_PORT;
        chunk->hdr.size = n - sizeof(VDIChunk);
        memcpy(chunk->data, (char*)msg + pos, n - sizeof(VDIChunk));
        enqueue_chunk(chunk);
        pos += (n - sizeof(VDIChunk));
    }
    MUTEX_UNLOCK(_message_mutex);
    return ret;
}
Esempio n. 30
0
static void* read_trace_thread(void *argv) {

	FILE *trace_file = fopen(jcr.path, "r");
	char line[128];

	while (1) {
		TIMER_DECLARE(1);
		TIMER_BEGIN(1);
		fgets(line, 128, trace_file);
		TIMER_END(1, jcr.read_time);

		if (strcmp(line, "stream end") == 0) {
			sync_queue_term(trace_queue);
			break;
		}

		struct chunk* c;

		TIMER_BEGIN(1),

		assert(strncmp(line, "file start ", 11) == 0);
		int filenamelen;
		sscanf(line, "file start %d", &filenamelen);

		/* An additional '\n' is read */
		c = new_chunk(filenamelen + 2);
		fgets(c->data, filenamelen + 2, trace_file);
		c->data[filenamelen] = 0;
		VERBOSE("Reading: %s", c->data);

		SET_CHUNK(c, CHUNK_FILE_START);

		TIMER_END(1, jcr.read_time);

		sync_queue_push(trace_queue, c);

		TIMER_BEGIN(1);
		fgets(line, 128, trace_file);
		while (strncmp(line, "file end", 8) != 0) {
			c = new_chunk(0);

			char code[41];
			strncpy(code, line, 40);
			code2hash(code, c->fp);

			c->size = atoi(line + 41);
			jcr.chunk_num++;
			jcr.data_size += c->size;

			TIMER_END(1, jcr.read_time);
			sync_queue_push(trace_queue, c);
			TIMER_BEGIN(1),

			fgets(line, 128, trace_file);
		}

		c = new_chunk(0);
		SET_CHUNK(c, CHUNK_FILE_END);
		sync_queue_push(trace_queue, c);

		jcr.file_num++;
	}

	fclose(trace_file);
	return NULL;
}