Ejemplo n.º 1
0
int main(int argc, char *argv[]) {
	// expect one argument atleast.
	if( argc < 2 ) {
		fprintf(stderr,"usage: %s <input-file>", argv[0]);
		exit(-1);
	}

	char *input_file = argv[1];
	FILE *fp = fopen(input_file, "rb");
	int numchunks = get_numchunks(input_file), i = 0;

	// check for sanity.
	if( numchunks < 0 || fp == NULL) {
		fprintf(stderr, "Can't stat the file %s: %s\n", input_file, strerror(errno));
		exit(-1);
	}

	// create the array. 
	uint8_t **hashes = (uint8_t **) malloc(numchunks * sizeof(uint8_t *));
	if( hashes == NULL ) {
		fprintf(stderr, "Out of memory!!!");
		exit(-1);
	}

	// allocate the individual elements.
	for(i=0;i<numchunks;i++) {
		hashes[i] = malloc((SHA1_HASH_SIZE)*sizeof(uint8_t));
		if( hashes[i] == NULL ) {
			fprintf(stderr, "Out of memory!!!");
			exit(-1);
		}
	}

	int chunks = make_chunks(fp, hashes);
	char ascii[SHA1_HASH_SIZE*2+1]; // the ascii string.
	for(i=0;i<chunks;i++) {
		hex2ascii(hashes[i], SHA1_HASH_SIZE, ascii);
		printf("%d %s\n",i,ascii);
		free(hashes[i]);
	}

	// free the memory.
	free(hashes);
	// close file.
	fclose(fp);

	// return success.
	return 0;
}
void myth_malloc_wrapper_init_worker(int rank)
{
#ifdef MYTH_WRAP_MALLOC_RUNTIME
  /* is it possible to come here before myth_malloc_wrapper_init is called? */
  if (!g_wrap_malloc) return;
#endif
  int i;
  //allocate freelist
  assert(real_malloc);
  g_myth_malloc_wrapper_fl[rank]=real_malloc(sizeof(myth_freelist_t)*FREE_LIST_NUM);
  //initialize
#if FIX_FALSE_SHARING3
  for (i=0;i<FREE_LIST_NUM;i++){
    size_t realsize=MYTH_MALLOC_INDEX_TO_RSIZE(i);
    size_t reqsize=realsize+sizeof(malloc_wrapper_header);
    if (reqsize <= MYTH_WRAP_MALLOC_MIN_MALLOC_SZ) {
      g_myth_malloc_wrapper_fl[rank][i] =
	make_chunks(reqsize,
		    MYTH_WRAP_MALLOC_MIN_MALLOC_SZ);
    } else {
      g_myth_malloc_wrapper_fl[rank][i] = 0;
    }
  }
#else
  for (i=0;i<FREE_LIST_NUM;i++){myth_freelist_init(g_myth_malloc_wrapper_fl[rank][i]);}
#endif
  __sync_fetch_and_add(&g_alloc_hook_ok,1);

#if 0
  printf("g_myth_malloc_wrapper_fl = %ld\n", 
	 (long)g_myth_malloc_wrapper_fl);

  printf("env[%d] = %ld\n", rank, (long)myth_get_current_env());
  printf("g_myth_malloc_wrapper_fl[%d] = %ld\n", 
	 rank, (long)g_myth_malloc_wrapper_fl[rank]);
#elif 0
  printf("0 0 %d %ld A\n", rank, (long)g_myth_malloc_wrapper_fl);
  printf("0 0 %d %ld F\n", rank, (long)g_myth_malloc_wrapper_fl[rank]);
  printf("0 0 %d %ld E\n", rank, (long)&g_envs[rank]);
#endif
}
static void * myth_malloc_wrapper_malloc(size_t size)
{
#ifdef MYTH_WRAP_MALLOC_RUNTIME
  /* fall back to the bump allocator before wrapping completed */
  if (!g_wrap_malloc_completed) {
    void *ptr = sys_alloc_align(16, size);
    return ptr;
  }
  /* no wrap. call the real one */
  if (!g_wrap_malloc) {
    return real_malloc(size);
  }
#endif

  //fprintf(stderr,"malloc %d\n",size);
  malloc_wrapper_header_t ptr;
  size_t realsize;
  int idx;
  if (size<16)size=16;
  if (!real_malloc){
    static int load_malloc_protect=0;
    if (load_malloc_protect==0){
      load_malloc_protect=1;
      real_malloc=dlsym(RTLD_NEXT,"malloc");
    }
    else return NULL;
    assert(real_malloc);
  }
  if ((!g_worker_thread_num) || (g_alloc_hook_ok!=g_worker_thread_num) || (size>MYTH_MALLOC_FLSIZE_MAX)){
    ptr=real_malloc(size+sizeof(malloc_wrapper_header));
    if (!ptr){
      fprintf(stderr,"size=%llu\n",(unsigned long long)size);
    }
    assert(ptr);
    ptr->s.fl_index=FREE_LIST_NUM;
    ptr->s.org_ptr=ptr;
    //fprintf(stderr,"malloc A,%p,%d\n",ptr,FREE_LIST_NUM);
    return (void*)(ptr+1);
  }
  idx=MYTH_MALLOC_SIZE_TO_INDEX(size);
  realsize=MYTH_MALLOC_INDEX_TO_RSIZE(idx);
  void **fl_ptr;
  myth_running_env_t env;
  env=myth_get_current_env();
  int rank=env->rank;
  myth_freelist_pop(g_myth_malloc_wrapper_fl[rank][idx],fl_ptr);

#if FIX_FALSE_SHARING2
  if (!fl_ptr) {
    g_myth_malloc_wrapper_fl[rank][idx] =
      make_chunks(realsize+sizeof(malloc_wrapper_header),
		  MYTH_WRAP_MALLOC_MIN_MALLOC_SZ);
    myth_freelist_pop(g_myth_malloc_wrapper_fl[rank][idx],fl_ptr);
    assert(fl_ptr);
  } 
  ptr=(malloc_wrapper_header_t)fl_ptr;
#else
  if (!fl_ptr){
    //Freelist is empty, allocate
    ptr=real_malloc(realsize+sizeof(malloc_wrapper_header));
    //fprintf(stderr,"malloc B,%p,%d\n",ptr,idx);
    assert(ptr);
  }
  else{
    ptr=(malloc_wrapper_header_t)fl_ptr;
  }
#endif
  ptr->s.fl_index=idx;
  ptr->s.org_ptr=ptr;
  return (void*)(ptr+1);
}