Example #1
0
boolean hashtable_resize(hashtable_t* hashtable, int capacity)
{
	if (hashtable->storage == 0)
		return false;

	size_t size = capacity*(sizeof(void*) + hashtable->key_size + 1);
	arena_t* storage = arena_create(size);
	void** itemStore = arena_alloc(&storage, capacity*sizeof(void*));
	byte* keyStore = arena_alloc(&storage, capacity*(hashtable->key_size + 1));

	for (int b = 0; b < hashtable->capacity; b++)
	{
		byte* key = HASHKEY(hashtable->keys, b, hashtable->key_size);
		if (*key)
		{
			uint hash = hashtable->hash(key + 1, hashtable->key_size);
			if (!_inner_hashtable_add(itemStore, keyStore, capacity, hash, hashtable->key_size, key + 1, hashtable->items[b]))
			{
				arena_destroy(storage);
				return false;
			}
		}
	}

	arena_destroy(hashtable->storage);

	hashtable->storage = storage;
	hashtable->keys = keyStore;
	hashtable->items = itemStore;
	hashtable->capacity = capacity;
	return true;
}
Example #2
0
void *arena_calloc(arena_t arena, long elem_count, long elem_size, const char *file, int line){
	void *ptr;
	assert(elem_count>0);
	ptr = arena_alloc(arena, elem_count * elem_size, file, line);
	memset(ptr, 0, elem_count * elem_size);
	return ptr;
}
Example #3
0
hashtable_t* hashtable_from_arena(arena_t* arena, int capacity, int keySize)
{
	hashtable_t* hashtable = arena_alloc(&hashtables, sizeof(hashtable_t));

	size_t size = capacity*(sizeof(void*) + keySize + 1);
	hashtable->hash = _key_hash_binary;
	hashtable->match = _key_match_binary;
	hashtable->key_size = keySize;
	hashtable->items = arena_alloc(&arena, capacity * sizeof(void*));
	hashtable->keys = arena_alloc(&arena, capacity *(sizeof(keySize) + 1));
	hashtable->capacity = capacity;
	hashtable->storage = 0;
	hashtable->count = 0;

	return hashtable;
}
Example #4
0
static couchfile_modify_result *make_modres(arena* a, couchfile_modify_request *rq)
{
    couchfile_modify_result *res = arena_alloc(a, sizeof(couchfile_modify_result));
    if (!res) {
        return NULL;
    }
    res->arena = a;
    res->arena_transient = NULL;
    res->values = make_nodelist(a, 0);
    if (!res->values) {
        return NULL;
    }
    res->values_end = res->values;
    res->pointers = make_nodelist(a, 0);
    if (!res->pointers) {
        return NULL;
    }
    res->pointers_end = res->pointers;
    res->node_len = 0;
    res->count = 0;
    res->modified = 0;
    res->error_state = 0;
    res->rq = rq;
    return res;
}
Example #5
0
// 有序链表(从小到大),为简化起见,假设key都是C字符串
int list_add(char* base, char* key, int key_len, char* val, int val_len)
{
    btree_node* bp = (btree_node*) base;
    // alloc
    int mem = arena_alloc(base, sizeof(list_node)+key_len+val_len);
    if(mem == 0)
        return 0;
    list_node* np = (list_node*)(base+mem);
    np->key_len = key_len;
    np->val_len = val_len;
    memcpy(get_key(np), key, key_len);
    memcpy(get_val(np), val, val_len);

    int* i = &bp->cell_list;
    while(*i != 0) {
        list_node* tmp = (list_node*)(base+*i);
        if(memcmp(get_key(tmp), key, key_len) > 0)
            break;
        i = &tmp->next;
    }

    np->next = *i;
    *i = mem;

    return 1;
}
Example #6
0
nsapi_error_t LWIP::socket_open(nsapi_socket_t *handle, nsapi_protocol_t proto)
{
    // check if network is connected
//    if (lwip_connected == NSAPI_STATUS_DISCONNECTED) {
//        return NSAPI_ERROR_NO_CONNECTION;
//    }

    // allocate a socket
    struct mbed_lwip_socket *s = arena_alloc();
    if (!s) {
        return NSAPI_ERROR_NO_SOCKET;
    }

    enum netconn_type lwip_proto = proto == NSAPI_TCP ? NETCONN_TCP : NETCONN_UDP;

#if LWIP_IPV6
    // Enable IPv6 (or dual-stack)
    lwip_proto = (enum netconn_type)(lwip_proto | NETCONN_TYPE_IPV6);
#endif

    s->conn = netconn_new_with_callback(lwip_proto, &LWIP::socket_callback);

    if (!s->conn) {
        arena_dealloc(s);
        return NSAPI_ERROR_NO_SOCKET;
    }

    netconn_set_recvtimeout(s->conn, 1);
    *(struct mbed_lwip_socket **)handle = s;
    return 0;
}
Example #7
0
File: stmt.c Project: unixaaa/LuxCC
int install_switch_label(long long val, int is_default)
{
    SwitchLabel *np;
    unsigned long h;

    h = is_default?0:HASH_VAL2((unsigned long)val); /* 'default' labels are always at bucket zero */
    np = switch_labels[switch_nesting_level][h];
    while (np != NULL) {
        if (is_default) {
            if (np->is_default)
                break;
        } else if (!np->is_default && np->val==val) {
            break;
        }
        np = np->next;
    }

    if (np == NULL) {
        /* not found in this switch nesting level */
        np = arena_alloc(switch_arena[switch_nesting_level], sizeof(SwitchLabel));
        np->val = val;
        np->is_default = is_default;
        np->next = switch_labels[switch_nesting_level][h];
        switch_labels[switch_nesting_level][h] = np;
        return TRUE; /* success */
    } else {
        return FALSE; /* failure */
    }
}
Example #8
0
static inline avl_node *
new_node (arena **owner)
{
  if (owner != NULL)
    return arena_alloc (owner, sizeof (avl_node));
  else
    return xmalloc (sizeof (avl_node));
}
Example #9
0
void* collection_new_item(collection_t* collection, size_t sizeofItem)
{
	assert(collection->item_storage);

	void* item = arena_alloc(&collection->item_storage, sizeofItem);
	collection_push(collection, item);
	return item;
}
Example #10
0
// The jailname can't contain periods, unlike hostname, which often should
char *hostname_to_jailname(const char *str) {
	size_t len = strlen(str) + 1;
	char *result = arena_alloc(config_parser_arena, len);
	for (size_t i = 0; i < len; i++)
		result[i] = (str[i] == '.') ? '_' : str[i];
	result[len - 1] = '\0';
	return result;
}
Example #11
0
File: matrix.c Project: 8l/CompCert
struct matrix * mtranslate(flt sx, flt sy, flt sz)
{
  struct matrix * m = arena_alloc(sizeof(struct matrix));
  m->xx = 1.0;  m->xy = 0.0;  m->xz = 0.0;  m->xt = sx;
  m->yx = 0.0;  m->yy = 1.0;  m->yz = 0.0;  m->yt = sy;
  m->zx = 0.0;  m->zy = 0.0;  m->zz = 1.0;  m->zt = sz;
  return m;
}
Example #12
0
File: matrix.c Project: 8l/CompCert
struct matrix * mscale(flt sx, flt sy, flt sz)
{
  struct matrix * m = arena_alloc(sizeof(struct matrix));
  m->xx = sx;   m->xy = 0.0;  m->xz = 0.0;  m->xt = 0.0;
  m->yx = 0.0;  m->yy = sy ;  m->yz = 0.0;  m->yt = 0.0;
  m->zx = 0.0;  m->zy = 0.0;  m->zz = sz ;  m->zt = 0.0;
  return m;
}
Example #13
0
static PLHashEntry *graphnode_allocentry(void *pool, const void *key)
{
    tmgraphnode* node = (tmgraphnode*)arena_alloc(pool, sizeof(tmgraphnode));
    if (!node)
        return NULL;
    init_graphnode(node);
    return &node->entry;
}
Example #14
0
File: ld.c Project: unixaaa/LuxCC
void *new_local_symbol(void)
{
    void *p;

    if ((p=arena_alloc(local_arena, sizeof(Symbol))) == NULL)
        TERMINATE("%s: out of memory", prog_name);

    return p;
}
Example #15
0
void *kpages_zalloc(size_t size, int flags)
{
	void *ret = arena_alloc(kpages_arena, size, flags);

	if (!ret)
		return NULL;
	memset(ret, 0, size);
	return ret;
}
Example #16
0
static PLHashEntry *method_allocentry(void *pool, const void *key)
{
    tmmethodnode *node =
        (tmmethodnode*) arena_alloc(pool, sizeof(tmmethodnode));
    if (!node)
        return NULL;
    init_method(node);
    return &node->graphnode.entry;
}
Example #17
0
File: matrix.c Project: 8l/CompCert
struct matrix * mrotatez(flt a)
{
  struct matrix * m = arena_alloc(sizeof(struct matrix));
  flt c = cos(a);
  flt s = sin(a);
  m->xx = c;    m->xy = - s;  m->xz = 0.0;  m->xt = 0.0;
  m->yx = s;    m->yy = c;    m->yz = 0.0;  m->yt = 0.0;
  m->zx = 0.0;  m->zy = 0.0;  m->zz = 1.0;  m->zt = 0.0;
  return m;
}
Example #18
0
collection_t* collection_from_arena(arena_t** arena)
{
	collection_t* collection = (collection_t*)arena_alloc(arena, sizeof(collection_t));
	collection->head = 0;
	collection->count = 0;
	collection->node_storage = *arena;
	collection->item_storage = *arena;

	return collection;
}
Example #19
0
static nodelist *make_nodelist(arena* a, size_t bufsize)
{
    nodelist *r = arena_alloc(a, sizeof(nodelist) + bufsize);
    if (!r) {
        return NULL;
    }
    memset(r, 0, sizeof(nodelist));
    r->data.size = bufsize;
    r->data.buf = ((char *) r) + (sizeof(nodelist));
    return r;
}
Example #20
0
/*
 * Similar to util.c:read_view_record(), but it uses arena allocator, which is
 * required for the existing semantics/api of btree bottom-up build in
 * src/btree_modify.cc.
 */
static int read_record(FILE *f, arena *a, sized_buf *k, sized_buf *v)
{
    uint16_t klen;
    uint32_t vlen, len;

    if (fread(&len, sizeof(len), 1, f) != 1) {
        if (feof(f)) {
            return 0;
        } else {
            return COUCHSTORE_ERROR_READ;
        }
    }

    if (fread(&klen, sizeof(klen), 1, f) != 1) {
        return COUCHSTORE_ERROR_READ;
    }

    klen = ntohs(klen);
    vlen = len - sizeof(klen) - klen;

    k->size = klen;
    k->buf = (char *) arena_alloc(a, k->size);
    if (k->buf == NULL) {
        return COUCHSTORE_ERROR_ALLOC_FAIL;
    }

    v->size = vlen;
    v->buf = (char *) arena_alloc(a, v->size);
    if (v->buf == NULL) {
        return COUCHSTORE_ERROR_ALLOC_FAIL;
    }

    if (fread(k->buf, k->size, 1, f) != 1) {
        return FILE_MERGER_ERROR_FILE_READ;
    }
    if (fread(v->buf, v->size, 1, f) != 1) {
        return FILE_MERGER_ERROR_FILE_READ;
    }

    return len;
}
Example #21
0
internal
collection_node_t* _alloc_node(collection_t* collection)
{
	if (collection->free_list == 0)
	{
		return arena_alloc(&collection->node_storage, sizeof(collection_node_t));
	}

	collection_node_t* node = collection->free_list;
	collection->free_list = node->next;
	return node;
}
Example #22
0
collection_t* create_collection(size_t initialSize, size_t itemSize)
{
	collection_t* collection = (collection_t*)arena_alloc(&collections, sizeof(collection_t));
	collection->head = 0;
	collection->count = 0;
	collection->capacity = initialSize > 10 ? initialSize : 10;
	collection->node_storage = arena_create(collection->capacity * sizeof(collection_node_t));

	collection->item_storage = itemSize 
		? arena_create(initialSize * itemSize)
		: 0;

	return collection;
}
Example #23
0
static node_pointer *read_pointer(arena* a, sized_buf *key, char *buf)
{
    //Parse KP pair into a node_pointer {K, {ptr, reduce_value, subtreesize}}
    node_pointer *p = (node_pointer *) arena_alloc(a, sizeof(node_pointer));
    if (!p) {
        return NULL;
    }
    const raw_node_pointer *raw = (const raw_node_pointer*)buf;
    p->pointer = decode_raw48(raw->pointer);
    p->subtreesize = decode_raw48(raw->subtreesize);
    p->reduce_value.size = decode_raw16(raw->reduce_value_size);
    p->reduce_value.buf = buf + sizeof(*raw);
    p->key = *key;
    return p;
}
Example #24
0
File: vec.c Project: seanpringle/lt
vec_t*
vec_alloc ()
{
  vec_count++;
  vec_created++;
  vec_t *vec = arena_alloc(vecs, sizeof(vec_t));

  ensure(vec)
  {
    stacktrace();
    errorf("arena_alloc vecs");
  }
  vec->items = heap_alloc(sizeof(void*) * VEC_STEP);
  vec->count = 0;
  return vec;
}
Example #25
0
File: stmt.c Project: unixaaa/LuxCC
int install_label_name(char *name)
{
    unsigned h;
    LabelName *np;

    h = HASH_VAL(name);
    for (np = label_names[h]; np != NULL; np = np->next)
        if (equal(name, np->name))
            break;

    if (np == NULL) {
        np = arena_alloc(label_names_arena, sizeof(LabelName));
        np->name = name;
        np->next = label_names[h];
        label_names[h] = np;
        return TRUE; /* success */
    } else {
        return FALSE; /* failure */
    }
}
Example #26
0
couchfile_modify_result *new_btree_modres(arena *a, arena *transient_arena, Db* db, compare_info* cmp, reduce_fn reduce,
        reduce_fn rereduce)
{
    couchfile_modify_request* rq = arena_alloc(a, sizeof(couchfile_modify_request));
    rq->cmp = *cmp;
    rq->db = db;
    rq->num_actions = 0;
    rq->fetch_callback = NULL;
    rq->reduce = reduce;
    rq->rereduce = rereduce;
    rq->compacting = 1;

    couchfile_modify_result* mr = make_modres(a, rq);
    if (!mr)
        return NULL;
    mr->arena_transient = transient_arena;
    mr->modified = 1;
    mr->node_type = KV_NODE;

    return mr;
}
Example #27
0
File: matrix.c Project: 8l/CompCert
struct matrix * mcompose(struct matrix * m, struct matrix * n)
{
  struct matrix * r = arena_alloc(sizeof(struct matrix));

  r->xx = m->xx * n->xx + m->xy * n->yx + m->xz * n->zx;
  r->xy = m->xx * n->xy + m->xy * n->yy + m->xz * n->zy;
  r->xz = m->xx * n->xz + m->xy * n->yz + m->xz * n->zz;
  r->xt = m->xx * n->xt + m->xy * n->yt + m->xz * n->zt + m->xt;

  r->yx = m->yx * n->xx + m->yy * n->yx + m->yz * n->zx;
  r->yy = m->yx * n->xy + m->yy * n->yy + m->yz * n->zy;
  r->yz = m->yx * n->xz + m->yy * n->yz + m->yz * n->zz;
  r->yt = m->yx * n->xt + m->yy * n->yt + m->yz * n->zt + m->yt;

  r->zx = m->zx * n->xx + m->zy * n->yx + m->zz * n->zx;
  r->zy = m->zx * n->xy + m->zy * n->yy + m->zz * n->zy;
  r->zz = m->zx * n->xz + m->zy * n->yz + m->zz * n->zz;
  r->zt = m->zx * n->xt + m->zy * n->yt + m->zz * n->zt + m->zt;

  return r;
}
Example #28
0
nsapi_error_t LWIP::socket_accept(nsapi_socket_t server, nsapi_socket_t *handle, SocketAddress *address)
{
#if LWIP_TCP
    struct mbed_lwip_socket *s = (struct mbed_lwip_socket *)server;
    struct mbed_lwip_socket *ns = arena_alloc();
    if (!ns) {
        return NSAPI_ERROR_NO_SOCKET;
    }

    if (s->conn->pcb.tcp->state != LISTEN) {
        return NSAPI_ERROR_PARAMETER;
    }

    err_t err = netconn_accept(s->conn, &ns->conn);
    if (err != ERR_OK) {
        arena_dealloc(ns);
        return err_remap(err);
    }

    netconn_set_recvtimeout(ns->conn, 1);
    *(struct mbed_lwip_socket **)handle = ns;

    ip_addr_t peer_addr;
    nsapi_addr_t addr;
    u16_t port;
    (void) netconn_peer(ns->conn, &peer_addr, &port);
    convert_lwip_addr_to_mbed(&addr, &peer_addr);

    if (address) {
        address->set_addr(addr);
        address->set_port(port);
    }

    netconn_set_nonblocking(ns->conn, true);

    return 0;
#else
    return NSAPI_ERROR_UNSUPPORTED;
#endif
}
Example #29
0
File: avl.c Project: ampimis/RtkGps
avl_tree *avl_create_nl(MAYBE_ARENA avl_comparison_func cmp, void *param)
{
	avl_tree *tree;

	assert(cmp != NULL);
#if PSPP
  if (owner)
	tree = arena_alloc (owner, sizeof (avl_tree));
  else
#endif
	tree = xmalloc (sizeof (avl_tree));

#if PSPP
	tree->owner = owner;
#endif
  tree->root.link[0] = NULL;
  tree->root.link[1] = NULL; 
  tree->cmp = cmp;
  tree->count = 0;
  tree->param = param;
  thread_create_mutex_nl(&tree->mutex);
  return tree;
}
Example #30
0
couchstore_error_t TreeWriterWrite(TreeWriter* writer,
                                   tree_file* treefile,
                                   node_pointer** out_root)
{
    couchstore_error_t errcode = COUCHSTORE_SUCCESS;
    arena* transient_arena = new_arena(0);
    arena* persistent_arena = new_arena(0);
    error_unless(transient_arena && persistent_arena, COUCHSTORE_ERROR_ALLOC_FAIL);

    rewind(writer->file);

    // Create the structure to write the tree to the db:
    compare_info idcmp;
    sized_buf tmp;
    idcmp.compare = writer->key_compare;
    idcmp.arg = &tmp;

    couchfile_modify_result* target_mr = new_btree_modres(persistent_arena,
                                                          transient_arena,
                                                          treefile, &idcmp,
                                                          writer->reduce, 
                                                          writer->rereduce,
                                                          DB_CHUNK_THRESHOLD,
                                                          DB_CHUNK_THRESHOLD);
    if(target_mr == NULL) {
        error_pass(COUCHSTORE_ERROR_ALLOC_FAIL);
    }

    // Read all the key/value pairs from the file and add them to the tree:
    uint16_t klen;
    uint32_t vlen;
    sized_buf k, v;
    while(1) {
        if(fread(&klen, sizeof(klen), 1, writer->file) != 1) {
            break;
        }
        if(fread(&vlen, sizeof(vlen), 1, writer->file) != 1) {
            break;
        }
        k.size = ntohs(klen);
        k.buf = arena_alloc(transient_arena, k.size);
        v.size = ntohl(vlen);
        v.buf = arena_alloc(transient_arena, v.size);
        if(fread(k.buf, k.size, 1, writer->file) != 1) {
            error_pass(COUCHSTORE_ERROR_READ);
        }
        if(fread(v.buf, v.size, 1, writer->file) != 1) {
            error_pass(COUCHSTORE_ERROR_READ);
        }
        //printf("K: '%.*s'\n", k.size, k.buf);
        mr_push_item(&k, &v, target_mr);
        if(target_mr->count == 0) {
            /* No items queued, we must have just flushed. We can safely rewind the transient arena. */
            arena_free_all(transient_arena);
        }
    }

    // Check for file error:
    int readerr = ferror(writer->file);
    if(readerr != 0 && readerr != EOF) {
        error_pass(COUCHSTORE_ERROR_READ);
    }

    // Finish up the tree:
    *out_root = complete_new_btree(target_mr, &errcode);

cleanup:
    delete_arena(transient_arena);
    delete_arena(persistent_arena);
    return errcode;
}