Exemplo n.º 1
0
static struct userlist_user *
allocate_login_on_pool(
        struct uldb_mysql_state *state,
        int user_id)
{
  struct users_cache *uc = &state->users;
  struct userlist_user *u;
  struct xml_tree *u_xml, *l, *r;

  if (user_id <= 0) return 0;
  if (user_id > EJ_MAX_USER_ID) return 0;

  if (user_id >= uc->size) {
    int new_size = uc->size;
    struct userlist_user **new_map;

    if (!new_size) new_size = 1024;
    while (user_id >= new_size) new_size *= 2;
    XCALLOC(new_map, new_size);
    if (uc->size > 0)
      memcpy(new_map, uc->user_map, uc->size * sizeof(uc->user_map[0]));
    xfree(uc->user_map);
    uc->size = new_size;
    uc->user_map = new_map;
  }

  if ((u = uc->user_map[user_id])) {
    // don't even try to free nested cntsregs and cookies
    if (u->contests) {
      u->contests->first_down = u->contests->last_down = 0;
    }
    if (u->cookies) {
      u->cookies->first_down = u->cookies->last_down = 0;
    }
    u_xml = (struct xml_tree*) u;
    l = u_xml->left;
    r = u_xml->right;
    userlist_elem_free_data(u_xml);
    u->id = user_id;
    u_xml->left = l;
    u_xml->right = r;

    MOVE_TO_FRONT(u_xml, uc->first, uc->last, left, right);
    return u;
  }

  if (uc->count == USERS_POOL_SIZE) {
    do_remove_login_from_pool(uc, (struct userlist_user*) uc->last);
  }

  u = (struct userlist_user*) userlist_node_alloc(USERLIST_T_USER);
  u->id = user_id;
  u_xml = (struct xml_tree*) u;
  LINK_FIRST(u_xml, uc->first, uc->last, left, right);
  uc->user_map[user_id] = u;
  uc->count++;
  return u;
}
Exemplo n.º 2
0
static void
group_cache_add(struct uldb_mysql_state *state, struct userlist_group *grp)
{
  int new_size = 0;
  struct userlist_group **new_map = 0;

  if (!grp) return;
  ASSERT(grp->group_id > 0);
  if (grp->group_id < state->groups.size
      && state->groups.group_map[grp->group_id] == grp)
    return;
  if (grp->group_id < state->groups.size
      && state->groups.group_map[grp->group_id]) {
    // cache descync?
    group_cache_drop(state);
  }

  if (grp->group_id >= state->groups.size) {
    if (!(new_size = state->groups.size)) new_size = 32;
    while (new_size <= grp->group_id) {
      new_size *= 2;
    }
    XCALLOC(new_map, new_size);
    if (state->groups.size > 0) {
      memcpy(new_map, state->groups.group_map,
             state->groups.size * sizeof(new_map[0]));
    }
    xfree(state->groups.group_map);
    state->groups.size = new_size;
    state->groups.group_map = new_map;
  }

  state->groups.group_map[grp->group_id] = grp;
  LINK_FIRST(&grp->b, state->groups.first, state->groups.last, left, right);
  state->groups.count++;
}