void CacheList::empty()
{
  for (int i=0; i<total; i++)
  {
    if (list[i].file_number>=0 && list[i].last_access!=-1)
      unmalloc(&list[i]);
  }
  free(list);
  if (fp) delete fp;
  if (last_dir) delete last_dir;

  if (prof_data)
  {
    delete prof_data;
    prof_data=NULL;
  }

  total=0;                    // reinitalize
  list=NULL;
  last_registered=-1;
  fp=NULL;

  last_access=1;
  used=ful=0;
  last_dir=NULL;
  last_file=-1;
  prof_data=NULL;
}
Beispiel #2
0
static unmap_t *get_board_res(unstr_t *filename)
{
	unmap_t *resmap = 0;
	unstr_t *data = 0;
	unstr_t *p1 = 0;
	unstr_t *p2 = 0;
	unstr_t *line = 0;
	size_t index = 0;
	char *p = 0;
	int *nres = 0;

	data = unstr_file_get_contents(filename);
	if(unstr_empty(data)){
		return NULL;
	}
	resmap = unmap_init();
	p1 = unstr_init_memory(32);
	p2 = unstr_init_memory(128);
	
	while((line = unstr_strtok(data, "\n", &index)) != NULL){
		if(unstr_sscanf(line, "$.dat<>$", p1, p2) == 2){
			p = strrchr(p2->data, ' ');
			if(p != NULL){
				nres = unmalloc(sizeof(int));
				*nres = (int)strtol(p + 2, NULL, 10); /* +2は、空白と開きカッコをスキップ */
				free(unmap_find(resmap, p1->data, p1->length));
				unmap_set(resmap, p1->data, p1->length, nres);
			}
		}
		unstr_free(line);
	}
	unstr_delete(4, data, p1, p2, line);
	return resmap;
}
void CacheList::free_oldest()
{
  int32_t old_time = last_access;
  CacheItem *ci=list,*oldest=NULL;
  ful=1;

  for (int i = 0; i < total; i++, ci++)
  {
    if (ci->data && ci->last_access < old_time)
    {
      oldest = ci;
      old_time = ci->last_access;
    }
  }
  if (oldest)
  {
    dprintf("mem_maker : freeing %s\n",spec_types[oldest->type]);
    unmalloc(oldest);
  }
  else
  {
    close_graphics();
    printf("Out of memory, please remove any TSR's device drivers you can\n");
    exit(0);
  }
}
Beispiel #4
0
void CacheList::unreg(int id) {
	if (list[id].file_number >= 0) {
		unmalloc(&list[id]);
		list[id].file_number = -1;
	} else
		printf("Error : trying to unregister free object\n");
}
Beispiel #5
0
int main(int argc, char *argv[])
{
	pthread_t tid;
	unmap_t *sl = 0;
	databox_t *databox = 0;
	nich_t *nich = 0;
	unarray_t *ar = 0;
	size_t sl_len = 0;
	size_t i = 0;

	get_read2ch_config(argc, argv, &g_conf);
	sl = get_server(&g_conf);
	sl_len = unmap_size(sl);

	for(i = 0; i < sl_len; i++){
		ar = unmap_at(sl, i);
		if(ar != NULL){
			nich = unarray_at(ar, 0);
			if(nich != NULL){
				/* スレッド作成 */
				databox = unmalloc(sizeof(databox_t));
				databox->bl = ar;
				databox->key = unstr_copy(nich->server);
				databox->conf = &g_conf;
				if(pthread_create(&tid, NULL, mainThread, databox)){
					unstr_free(databox->key);
					free(databox);
					printf("%s スレッド生成エラー\n", nich->server->data);
				}
				databox = NULL;
				/* 1秒止める */
				sleep(1);
			}
		}
	}
	/* 仮 */
	while(1){
		pthread_mutex_lock(&g_mutex);
		if(g_stop_flag){
			printf("Exit threads\n");
			break;
		}
		pthread_mutex_unlock(&g_mutex);
		sleep(600);
	}
	read2ch_config_free(&g_conf);
	unmap_free(sl, sl_free);
	return 0;
}
Beispiel #6
0
static void push_board(unmap_t *hash, const unstr_t *server, const unstr_t *board)
{
	unarray_t *list = 0;
	nich_t *nich = unmalloc(sizeof(nich_t));
	nich->server = unstr_copy(server);
	nich->board = unstr_copy(board);
	nich->thread = NULL;
	list = unmap_find(hash, server->data, server->length);
	if(list == NULL){
		list = unarray_init(32);
		unarray_push(list, nich);
		unmap_set(hash, server->data, server->length, list);
	} else {
		unarray_push(list, nich);
	}
	/* listは開放しない */
}
Beispiel #7
0
static unarray_t *get_board(un2ch_t *get, nich_t *nich)
{
	unstr_t *data = 0;
	unarray_t *tl = 0;
	nich_t *n = 0;
	unstr_t *p1 = 0;
	unstr_t *p2 = 0;
	unstr_t *line = 0;
	unstr_t *filename = 0;
	unmap_t *resmap = 0;
	int *res = 0;
	size_t index = 0;
	char *p = 0;
	int nres = 0;
	if(get == NULL || nich == NULL){
		return NULL;
	}
	tl = unarray_init(512);
	p1 = unstr_init_memory(16);
	p2 = unstr_init_memory(256);

	un2ch_set_info(get, nich->server, nich->board, NULL);
	filename = unstr_copy(get->logfile);
	/* 必要箇所のみ更新するための準備 */
	resmap = get_board_res(filename);
	data = un2ch_get_data(get);
	if(unstr_empty(data)){
		unstr_delete(4, data, p1, p2, filename);
		unarray_free(tl, NULL);
		unmap_free(resmap, free);
		printf("ita error\n");
		return NULL;
	}
	while((line = unstr_strtok(data, "\n", &index)) != NULL){
		if(unstr_sscanf(line, "$.dat<>$", p1, p2) == 2){
			n = unmalloc(sizeof(nich_t));
			n->server = unstr_copy(nich->server);
			n->board = unstr_copy(nich->board);
			n->thread = unstr_copy(p1);
			p = strrchr(p2->data, ' ');
			if((resmap != NULL) && (p != NULL)){
				/* +2は、空白と開きカッコをスキップ */
				nres = (int)strtol(p + 2, NULL, 10);
				res = unmap_find(resmap, p1->data, p1->length);
				if((res != NULL) && (*res != nres)){
					/* レス数が違う */
					unarray_push(tl, n);
				} else {
					/* 解放 */
					nich_free(n);
				}
			} else {
				/* 板未取得 */
				unarray_push(tl, n);
			}
		}
		unstr_free(line);
	}
	unstr_delete(5, data, p1, p2, line, filename);
	unmap_free(resmap, free);
	data = un2ch_get_board_name(get);
	unstr_free(data);
	return tl;
}
void CacheList::load_cache_prof_info(char *filename, level *lev)
{
  int j;
  for (j=0; j<this->total; j++)
    if (list[j].last_access>=0)      // reset all loaded cache items to 0, all non-load to -1
      list[j].last_access=0;

  preload_cache(lev);                // preliminary guesses at stuff to load

  int load_fail=1;
  bFILE *fp=open_file(filename,"rb");
  if (!fp->open_failure())
  {
    spec_directory sd(fp);
    spec_entry *se=sd.find("cache profile info");   // see if the cache profile info is in the file
    if (se)
    {
      fp->seek(se->offset,0);

      char name[255];
      int tnames=0;
      int *fnum_remap;    // remaps old filenumbers into current ones

      tnames=fp->read_uint16();
      if (tnames)                     /// make sure there isn't bad info in the file
      {
    fnum_remap=(int *)malloc(sizeof(int)*tnames);

    int i;
    for (i=0; i<tnames; i++)
    {
      fp->read(name,fp->read_uint8());
      fnum_remap[i]=-1;                    // initialize the map to no-map

      int j;
      for (j=0; j<crc_manager.total_filenames(); j++)
        if (!strcmp(crc_manager.get_filename(j),name))
          fnum_remap[i]=j;
    }

    int tsaved = fp->read_uint32();


    int *priority=(int *)malloc(tsaved*sizeof(int));
    memset(priority,0xff,tsaved*sizeof(int));   // initialize to -1
    int tmatches=0;

    sorted_id_list=(int *)malloc(sizeof(int)*total);
    for (j=0; j<total; j++) sorted_id_list[j]=j;
    qsort(sorted_id_list,total,sizeof(int),s_offset_compare);

    for (i=0; i<tsaved; i++)
    {
      fp->read_uint8(); // read type
      short file_num=fp->read_uint16();
      if (file_num>=tnames)  // bad data?
        file_num=-1;
      else file_num=fnum_remap[file_num];

      uint32_t offset=fp->read_uint32();

      // search for a match
      j=search(sorted_id_list,file_num,offset);
      if (j!=-1)
      {
        if (list[j].last_access<0)  // if not loaded
          list[j].last_access=-2;      // mark as needing loading
        else list[j].last_access=2;   // mark as loaded and needing to stay that way
        priority[i]=j;
        tmatches++;
      }
    }

    free(sorted_id_list);            // was used for searching, no longer needed

    for (j=0; j<total; j++)
      if (list[j].last_access==0)
        unmalloc(list+j);             // free any cache entries that are not accessed at all in the level


    ful=0;
    int tcached=0;
    for (j=0; j<total; j++)    // now load all of the objects until full
    {
//      stat_man->update(j*70/total+25);
      if (list[j].file_number>=0 && list[j].last_access==-2)
      {
        list[j].last_access=-1;
        if (!ful)
        {
          switch (list[j].type)
          {
        case SPEC_BACKTILE : backt(j); break;
        case SPEC_FORETILE : foret(j); break;
        case SPEC_CHARACTER :
        case SPEC_CHARACTER2 : fig(j); break;
        case SPEC_IMAGE : img(j); break;
        case SPEC_PARTICLE : part(j); break;
        case SPEC_EXTERN_SFX : sfx(j); break;
        case SPEC_EXTERNAL_LCACHE : lblock(j); break;
        case SPEC_PALETTE : ctint(j); break;
          }
          tcached++;
        }
      }
    }
    load_fail=0;
//    if (full())
//      dprintf("Cache filled while loading\n");

    if (tsaved>tmatches)
      tmatches=tsaved+1;

    last_access=tmatches+1;
    for (i=0; i<tsaved; i++)      // reorder the last access of each cache to reflect prioirties
    {
      if (priority[i]!=-1)
      {
        if (list[priority[i]].last_access!=-1)            // make sure this wasn't the last item
          list[priority[i]].last_access=tmatches--;
      }
    }

    free(priority);
    free(fnum_remap);


      }
    }
  }

  if (load_fail) // no cache file, go solely on above gueses
  {
    int j;
    for (j=0; j<total; j++)    // now load all of the objects until full, don't free old stuff
    {
//      stat_man->update(j*70/total+25);

      if (list[j].file_number>=0 && list[j].last_access==-2)
      {
    list[j].last_access=-1;
    if (!ful)
    {
      switch (list[j].type)
      {
        case SPEC_BACKTILE : backt(j); break;
        case SPEC_FORETILE : foret(j); break;
        case SPEC_CHARACTER :
        case SPEC_CHARACTER2 : fig(j); break;
        case SPEC_IMAGE : img(j); break;
        case SPEC_PARTICLE : part(j); break;
        case SPEC_EXTERN_SFX : sfx(j); break;
        case SPEC_EXTERNAL_LCACHE : lblock(j); break;
        case SPEC_PALETTE : ctint(j); break;
      }
    }
      }
    }
    if (full())
      dprintf("Cache filled while loading\n");
  }
  delete fp;
}