示例#1
0
文件: xMutex.c 项目: rockyx/dntcore
void x_mutex_free(xMutex *mutex)
{
	x_return_if_fail(mutex);
	x_mutex_unlock(mutex);
	CloseHandle(mutex->handle);
	free(mutex);
}
示例#2
0
static x_boolean release_block(t_mchecker mc, t_block b, x_boolean stop, x_boolean discard) {

  x_boolean result;

  result = check_block(b, stop);
  
  x_mutex_lock(test_mutex, x_eternal);

  x_list_remove(b);
  decrement_count(b, b->size);

  if (discard) {
    total_discarded += 1;
    if (total_discarded % 1000 == 0) {
      oempa("Discarding Wonka Chunk %d...\n", total_discarded);
    }
    discardMem(b);
  }
  else {
    releaseMem(b);
  }

  x_mutex_unlock(test_mutex);

  return result;
  
}
示例#3
0
文件: xMutex.c 项目: rockyx/dntcore
void x_mutex_free(xMutex *mutex)
{
	x_return_if_fail(mutex);
	
	x_mutex_unlock(mutex);

	pthread_mutex_destroy(&(mutex->handle));

	free(mutex);
}
示例#4
0
static void ts_mem_control(void * t) {

//  x_size avail = heap_end - heap_start;
  x_int i;
  x_status status;
  x_int loops = 0;
  Warg argument;
  x_int thread_releases = 0;
  x_int discards = 0;
  x_int bytes_discarded = 0;
  x_int blocks_discarded = 0;
  x_int from_tests;
  x_int total_check;

  test_mutex = x_mem_calloc(sizeof(x_Mutex));
  x_mutex_create(test_mutex);

  while (1) {

    if (state == 0) {
      Mcheckers = x_mem_alloc(sizeof(t_Mchecker) * num_checkers);

      for (i = 0; i < num_checkers; i++) {
        Mcheckers[i].thread = x_mem_alloc(sizeof(x_Thread));
        Mcheckers[i].stack = x_mem_alloc(MEMT_STACK_SIZE);
        status = x_thread_create(Mcheckers[i].thread, ts_mem_check, &Mcheckers[i], Mcheckers[i].stack, MEMT_STACK_SIZE, 5, TF_START);
        if (status != xs_success) {
          oempa("%s: status = '%s'\n", __FILE__, x_status2char(status));
          exit(0);
        }
      }
      
      state = 1;
    }

    if (global_force_release) {
      force_released += 1;
      x_thread_sleep((x_size)(num_checkers / 4));
      oempa("Going to re-enable allocation, %d forced releases, current %d bytes in use.\n", force_released);
      global_force_release = 0;
      x_thread_sleep(2);
    }

    oempa("%d bytes in use, A = %d, R = %d, Rs = %d Rg = %d\n", global_in_use, allocations, reallocations, shrinked, growed);
    oempa("%d checker threads, %d free bytes.\n", num_checkers, x_mem_avail());

    argument.number = 0;
    argument.testblocks = 0;
    argument.bytes = 0;
    status = x_mem_walk(x_eternal, callback, &argument);
    if (status != xs_success) {
      oempa("Status = '%s'\n", x_status2char(status));
      exit(0);
    }
    
    oempa("WALKER: chunks used = %d, %d (%d ref) blocks, %d Mb walked.\n", argument.number, argument.testblocks, total_blocks, argument.bytes / (1024 * 1024));

#if defined(DEBUG)
    heapCheck;
#endif

    if (loops > 1 && loops % 30 == 0) {
      thread_releases += 1;
      oempa("************* Forcing all threads to release their blocks ***************\n");
      global_force_release = 1;
      x_thread_sleep((x_sleep)(num_checkers / 4));
      oempa("Heap used after total release %d bytes, %d releases, %d forced\n", thread_releases, force_released);
      global_force_release = 0;
    }

    if (loops > 1) {
      x_mutex_lock(test_mutex, x_eternal);
      x_mem_lock(x_eternal);
      discards += 1;
      oempa("WONKA COLLECT: starting collection cycle %d, free = %d bytes.\n", discards, x_mem_avail());
      x_mem_collect(&bytes_discarded, &blocks_discarded);
      oempa("WONKA COLLECT: collection %d, collected %d Kb from %d chunks (%d reported), free = %d bytes.\n", discards, (bytes_discarded / 1024), blocks_discarded, total_discarded, x_mem_avail());
      total_discarded = 0;
#ifdef DEBUG
      if (loops % 10 == 0) {
        reportMemStat((loops % 2 == 0) ? 1 : 2);
      }
#endif
      total_check = 0;
      oempa("CHECK %d == %d ? %s\n", global_in_use, total_check, (global_in_use == total_check) ? "YES" : "NO");
      if (global_in_use != total_check) {
        exit(0);
      }
      x_mem_unlock();
      x_mutex_unlock(test_mutex);
    }
    
    x_thread_sleep(second * 3);
    loops += 1;
    
  }
  
}
示例#5
0
static t_block allocate_block(t_mchecker mc, t_block list, x_size size, x_boolean cleared) {

  x_ubyte * data;
  x_ubyte pattern;
  t_block b;
  x_size i;
  x_word tag;
  x_size which = x_random() % 5;

  tag = ID_anon;

  x_mutex_lock(test_mutex, x_eternal);

  /*
  ** We use the which parameter to bring some variation in the __LINE__ number
  ** for checking the memory dumper.
  */
  
  if (cleared) {
    if (which == 0) {
      b = allocClearedMem(sizeof(t_Block) + size);
    }
    else if (which == 1) {
      b = allocClearedMem(sizeof(t_Block) + size);
    }
    else if (which == 2) {
      b = allocClearedMem(sizeof(t_Block) + size);
    }
    else if (which == 3) {
      b = allocClearedMem(sizeof(t_Block) + size);
    }
    else {
      b = allocClearedMem(sizeof(t_Block) + size);
    }
  }
  else {
    if (which == 0) {
      b = allocMem(sizeof(t_Block) + size);
    }
    else if (which == 1) {
      b = allocMem(sizeof(t_Block) + size);
    }
    else if (which == 2) {
      b = allocMem(sizeof(t_Block) + size);
    }
    else if (which == 3) {
      b = allocMem(sizeof(t_Block) + size);
    }
    else {
      b = allocMem(sizeof(t_Block) + size);
    }
  }

  if (b == NULL) {
    x_mutex_unlock(test_mutex);
    return NULL;
  }
  
  /*
  ** Check block is cleared correctly.
  */
  
  if (cleared) {
    data = (x_ubyte *)b;
    for (i = 0; i < sizeof(t_Block) + size; i++) {
      if (*data++ != 0x00) {
        oempa("Block of size %d not cleared properly. data[%d] == 0x%02x\n", sizeof(t_Block) + size, i, data[i]);
        exit(0);
      }
    }
  }

  b->size = size;
  pattern = (x_ubyte) x_random();
  b->pattern = pattern;

  /*
  ** Write the pattern in the block.
  */
  
  data = b->data;
  set_data(data, size, pattern);

  x_list_insert(list, b);

  increment_count(b, size);

  setMemTag(b, 33);
  
  x_mutex_unlock(test_mutex);

  return b;
    
}
示例#6
0
static t_block reallocate_block(t_mchecker mc, t_block list, t_block block, x_size new_size) {

  t_block new_block;
  x_size old_size = block->size;
  x_ubyte pattern = block->pattern;
  x_ubyte * data;
  x_size which = x_random() % 5;
  x_size orsize;
  x_size nrsize;
  w_chunk chunk;

  x_mutex_lock(test_mutex, x_eternal);

  x_list_remove(block);

  chunk = block2chunk(block);
  orsize = x_mem_size(chunk);

  /*
  ** Use 'which' to bring some variation in the __LINE__ number.
  */
  
  if (which == 0) {
    new_block = reallocMem(block, sizeof(t_Block) + new_size);
  }
  else if (which == 1) {
    new_block = reallocMem(block, sizeof(t_Block) + new_size);
  }
  else if (which == 2) {
    new_block = reallocMem(block, sizeof(t_Block) + new_size);
  }
  else if (which == 3) {
    new_block = reallocMem(block, sizeof(t_Block) + new_size);
  }
  else {
    new_block = reallocMem(block, sizeof(t_Block) + new_size);
  }
  
  if (new_block == NULL) {
    x_list_insert(list, block);
    x_mutex_unlock(test_mutex);
    return NULL;
  }
  else {
    chunk = block2chunk(new_block);
    nrsize = x_mem_size(chunk);
    new_block->size = new_size;
    change_count(orsize, nrsize, new_block, old_size, new_size);
  }
  
  x_list_insert(list, new_block);

  /*
  ** If the block has grown, fill the new room with the pattern data.
  */

  if (old_size < new_size) {
    data = & new_block->data[old_size];
    set_data(data, new_size - old_size, pattern);
    growed += 1;
  }
  else {
    shrinked += 1;
  }

  x_mutex_unlock(test_mutex);

  return new_block;

}
示例#7
0
w_int keyboard_poll(w_int *VK, w_char *keychar, w_int *mod, w_int *pressed) {
  XEvent  e;
  KeySym  keysym;
  x_xk2vk iter;
  w_ubyte buffer[5];
  w_int   i;
  
  x_mutex_lock(xlock, x_eternal);
  
  if(XCheckWindowEvent(display, window, KeyPressMask | KeyReleaseMask, &e)) {

    /*
    ** Lookup the keysym and the string it represents.
    */
    
    i = XLookupString(&e.xkey, (char *)&buffer, 5, &keysym, &status_in_out);

    if(i == 1) {
      *keychar = buffer[0];
    }
    else {
      woempa(5, "Too many keychars ?!?\n");
    }
   
    /*
    ** Get the modifiers.
    */ 
    
    *mod = 0;
    if((e.xkey.state & X_SHIFT) == X_SHIFT) *mod |= MOD_SHIFT;
    if((e.xkey.state & X_CAPS)  == X_CAPS)  *mod ^= MOD_SHIFT;
    if((e.xkey.state & X_CTRL)  == X_CTRL)  *mod |= MOD_CTRL;
    if((e.xkey.state & X_META)  == X_META)  *mod |= MOD_META;
    if((e.xkey.state & X_ALT)   == X_ALT)   *mod |= MOD_ALT;

    /*
    ** Translate XK to VK.
    */

    *VK = VK_UNDEFINED;
    iter = (x_xk2vk)&x_keymap;  
    while(*VK == VK_UNDEFINED && iter->XK != 0xFFFFFFFF) {
      if(iter->XK == keysym) {
        *VK = iter->VK;
      }
      iter++;
    }
      
    if (e.type == KeyPress) {
      woempa(5, "Key Pressed : %d, %d, %s\n", keysym, *mod, &buffer);
      *pressed = 1;
    }
    else if (e.type == KeyRelease) {
      woempa(5, "Key Released : %d, %d, %s\n", keysym, *mod, &buffer);
      *pressed = 0;
    }

    x_mutex_unlock(xlock);
    
    return 1;
  }

  x_mutex_unlock(xlock);
  
  return 0;
}