Beispiel #1
0
static inline int Server_copy_active_handlers(Server *srv, Server *copy_from)
{
    int i = 0;
    for(i = 0; i < darray_end(copy_from->handlers); i++) {
        Handler *from = darray_get(copy_from->handlers, i);

        int j = 0;
        for(j = 0; j < darray_end(srv->handlers); j++) {
            Handler *to = darray_get(srv->handlers, j);

            if(same_handler(from, to))
            {
                debug("Swapping %p original for %p replacement", to, from);
                RouteUpdater update = {.original = to, .replacement = from};
                tst_traverse(srv->hosts->routes, update_host_routes, &update);

                darray_set(srv->handlers, j, from);
                // swap them around so that the darrays stay full
                darray_set(copy_from->handlers, i, to);
                to->running = 0;
                break;
            }
        }
    }

    return 0;
}
Beispiel #2
0
char *test_set()
{
	darray_set(array, 0, val1);
	darray_set(array, 1, val2);

	return NULL;
}
Beispiel #3
0
void Server_queue_cleanup()
{
    if(darray_end(SERVER_QUEUE) < SERVER_ACTIVE) {
        // skip it, not enough to care about
        return;
    }

    // pop the last one off to make sure it's never deleted
    Server *cur_srv = darray_pop(SERVER_QUEUE);
    uint32_t too_old = time(NULL) - SERVER_TTL;
    int i = 0;

    // TODO: kind of a dumb way to do this since it reorders the list
    // go through all but the max we want to keep
    for(i = 0; i < darray_end(SERVER_QUEUE) - SERVER_ACTIVE; i++) {
        Server *srv = darray_get(SERVER_QUEUE, i);

        if(srv->created_on < too_old) {
            Server *replace = darray_pop(SERVER_QUEUE);
            darray_set(SERVER_QUEUE, i, replace);

            srv->listen_fd = -1; // don't close it
            Server_destroy(srv);
        }
    }

    // put the sacred server back on the end
    darray_push(SERVER_QUEUE, cur_srv);

    return;
}
void * darray_pop(DArray *array) {

  void *value;
  value = darray_get(array, array->last);
  darray_set(array, array->last, NULL);
  array->last--;
  return value;
}
DArray * darray_radix_sort(DArray *array) {

  DArray *buckets, *bucket;
  int *val, i, j, cur, mask, sortval, sorted;

  buckets = darray_init();

  mask = 1;

  do {
    sorted = 1;

    // reset the buckets
    for (i = 0; i < 10; i++) {
      darray_set(buckets, i, darray_init());
    }

    // sort the values into buckets
    for (i = 0; i <= (array->last); i++) {
      val = (int*)darray_get(array, i);
      sortval = (*val / mask) % 10;
      if (sortval > 0) { sorted = 0; }
      darray_push((PDArray)darray_get(buckets, sortval), val);
    }

    // rebuild array
    cur = 0;

    for (i = 0; i < 10; i++) {
      bucket = (PDArray)darray_get(buckets, i);

      for (j = 0; j <= (bucket->last); j++) {
        darray_set(array,
          cur++,
          darray_get(bucket, j));
      }
    }

    mask *= 10;

  } while (!sorted);

  return array;
}
Beispiel #6
0
static inline darray_t *Filter_lookup_create(StateEvent next)
{
    darray_t *filts = Filter_lookup(next);

    if(filts == NULL) {
        // lazy load them
        filts = darray_create(sizeof(Filter), 10);
        check_mem(filts);
        darray_set(REGISTERED_FILTERS, next - EVENT_MIN, filts);
    }

    return filts;
error:
    return NULL;
}
Beispiel #7
0
static inline DArray *hashmap_find_bucket(Hashmap *map, void *key, int create, uint32_t *hash_out) {
    uint32_t hash = map->hash(key);
    int bucket_n = hash % DEFAULT_NUMBER_OF_BUCKETS;
    check(bucket_n >= 0, "Invalid bucket found: %d", bucket_n);
    *hash_out = hash; // store it for the return so the caller can use it

    DArray *bucket = darray_get(map->buckets, bucket_n);

    if(!bucket && create) {
        //new bucket, set it up
        bucket = darray_create(sizeof(void *), DEFAULT_NUMBER_OF_BUCKETS);
        check_mem(bucket);
        darray_set(map->buckets, bucket_n, bucket);
    }

    return bucket;

error:
    return NULL;
}
Beispiel #8
0
void *hashmap_delete(Hashmap *map, void *key) {
    uint32_t hash = 0;
    DArray *bucket = hashmap_find_bucket(map, key, 0, &hash);
    if (!bucket) return NULL;

    int i = hashmap_get_node(map, hash, bucket, key);
    if (i == -1) return NULL;

    HashmapNode *node = darray_get(bucket, i);
    void *data = node->data;
    free(node);

    HashmapNode *ending = darray_pop(bucket);

    if (ending != node) {
        // looks like it's not the last one, swap it
        darray_set(bucket, i, ending);
    }

    return data;
}
Beispiel #9
0
int Register_connect(int fd, Connection* data)
{
    check(fd < MAX_REGISTERED_FDS, "FD given to register is greater than max.");
    check(data != NULL, "data can't be NULL");

    Registration *reg = darray_get(REGISTRATIONS, fd);

    if(reg == NULL) {
        reg = darray_new(REGISTRATIONS);
        check(reg != NULL, "Failed to allocate a new registration.");

        // we only set this here since they stay in list forever rather than recycle
        darray_set(REGISTRATIONS, fd, reg);
        darray_attach(REGISTRATIONS, reg);
    }

    if(Register_valid(reg)) {
        // force them to exit
        int rc = Register_disconnect(fd);
        check(rc != -1, "Weird error trying to disconnect. Tell Zed.");
        tasksignal(reg->task, SIGINT);
    }

    reg->data = data;
    reg->last_ping = THE_CURRENT_TIME_IS;
    reg->fd = fd;
    reg->task = taskself();

    reg->id = UINT32_MAX; // start off with an invalid conn_id
    
    // keep track of the number of registered things we're tracking
    NUM_REG_FD++;

    return 0;
error:
    return -1;
}
Beispiel #10
0
int main(int argc, char *argv[])
{

  dyn_array_t arr = darray_make(4);

  // Store 1,2,3,4 in array
  for (int i = 0; i < darray_size(arr); ++i) darray_set(arr, i, i);

  // Print arr[i] = i for i=[1,4]
  for (int i = 0; i < darray_size(arr); ++i) printf("arr[%d] = %d\n", i, *darray_get(arr, i));

  darray_append(&arr, 5);
  darray_append(&arr, 6);
  darray_prepend(&arr, 7);
  darray_prepend(&arr, 8);

  // Print arr[i] = j for i=[1,8]
  for (int i = 0; i < darray_size(arr); ++i) printf("arr[%d] = %d\n", i, *darray_get(arr, i));

  darray_free(&arr);
  arr = NULL;
  
  return 0;
}
void                MemoryAllocation_Set(DArray *array, int index, MemoryAllocation *s) { darray_set(array, index, s); }
Beispiel #12
0
char *test_darray_operations()
{
    darray_t *array = darray_create(sizeof(int), 100);
    mu_assert(array != NULL, "darray_create failed.");
    mu_assert(array->contents != NULL, "contents are wrong in darray");
    mu_assert(array->end == 0, "end isn't at the right spot");
    mu_assert(array->element_size == sizeof(int), "element size is wrong.");
    mu_assert(array->max == 100, "wrong max length on initial size");

    int *val1 = darray_new(array);
    mu_assert(val1 != NULL, "failed to make a new element");

    int *val2 = darray_new(array);
    mu_assert(val2 != NULL, "failed to make a new element");

    darray_set(array, 0, val1);
    darray_set(array, 1, val2);

    mu_assert(darray_get(array, 0) == val1, "Wrong first value.");
    mu_assert(darray_get(array, 1) == val2, "Wrong second value.");

    int *val_check = darray_remove(array, 0);
    mu_assert(val_check != NULL, "Should not get NULL.");
    mu_assert(*val_check == *val1, "Should get the first value.");
    mu_assert(darray_get(array, 0) == NULL, "Should be gone.");
    darray_free(val_check);

    val_check = darray_remove(array, 1);
    mu_assert(val_check != NULL, "Should not get NULL.");
    mu_assert(*val_check == *val2, "Should get the first value.");
    mu_assert(darray_get(array, 1) == NULL, "Should be gone.");
    darray_free(val_check);

    int old_max = array->max;
    darray_expand(array);
    mu_assert(array->max == old_max + array->expand_rate, "Wrong size after expand.");

    darray_contract(array);
    mu_assert(array->max == array->expand_rate + 1, "Should stay at the expand_rate at least.");

    darray_contract(array);
    mu_assert(array->max == array->expand_rate + 1, "Should stay at the expand_rate at least.");

    int i = 0;
    for(i = 0; i < 1000; i++) {
        int *val = darray_new(array);
        darray_attach(array, val); 
        *val = i * 333;
        darray_push(array, val);
    }

    mu_assert(array->max == 1201, "Wrong max size.");

    for(i = 999; i > 0; i--) {
        int *val = darray_pop(array);
        mu_assert(val != NULL, "Shouldn't get a NULL.");
        mu_assert(*val == i * 333, "Wrong value.");
        darray_free(val);
    }

    darray_destroy(array);
 
    return NULL;
}