
#include<stdio.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<pthread.h>

#include"sitar.h"
#include"sitar_scan.h"
#include<almemsys/almemsys.h>


extern global_data_t gd;
extern swap_array_t sa;



int swap_malloc_set(void)
{
int count;
int count_t;
count = 0;
count_t = 0;

#ifdef MALLOC_BUG
printf("MALLOC: swap_malloc_set():\n");
#endif

/* gd */

if((sa.count=(int*)malloc(sizeof(int)*(gd.td.tcount + 2)))==NULL){exit(1);}
if((sa.buf_err=(int*)malloc(sizeof(int)*(gd.td.tcount + 2)))==NULL){exit(1);}
if((sa.swap_data=(char***)malloc(sizeof(char**)*(gd.td.tcount + 2)))==NULL){exit(1);}

for(count = 0; count < gd.td.tcount; count++){
   sa.count[count] = 0;
   sa.buf_err[count] = 0;
   if((sa.swap_data[count]=(char**)malloc(sizeof(char*)*(gd.td.tbufmax + 2)))==NULL){exit(1);}
   sacalloc(count);
   }

// if((sa.pt=(pthread_t *)malloc(sizeof(pthread_t)*(gd.td.tcount + 2)))==NULL){exit(1);}
if((sa.pt=(pthread_t *)malloc(sizeof(pthread_t)*(gd.td.tcount + 2)))==NULL){exit(1);}
if((sa.tmp_data=(char**)malloc(sizeof(char*)*(gd.td.tbufmax + 2)))==NULL){exit(1);}

for(count = 0; count < gd.td.tcount; count++){
   sa.pt[count] = 0;
   }

// for(count = 0; count < B_MAX; count++){     // pthread 識別子
//   sa.pt[count] = (pthread_t *)malloc(sizeof(pthread_t));
//   }


sa.main_buf_err = 0;

satmpcalloc();

return 0;
}




int swap_malloc_free(void)
{
int count;
int count_t;
count = 0;
count_t = 0;

#ifdef FREE_BUG
printf("FREE: swap_malloc_free():\n");
#endif

for(count = 0; count < gd.td.tcount; count ++){
   safree(count);
   free(sa.swap_data[count]);
   }

// for(count = 0; count < B_MAX; count++){   // pthread 識別子
//    free(sa.pt[count]); 
//   }
 
#ifdef FREE_BUG
printf("FREE: swap_malloc_free(): satmpfree()\n");
#endif
satmpfree();

free(sa.tmp_data);
free(sa.swap_data);
free(sa.count);
free(sa.buf_err);
free(sa.pt);

return 0;
}



