/** \brief Initializes memory module Initializes root of memory tree and marks the lowest gigabyte as locked. */ int mem_init(void){ root = _create_node(ROOT_SIZE,0,0); root->state = SPLIT; root->left = _create_node(ROOT_SIZE-1,0,root); root->left->state = SPLIT; root->left->left = _create_node(ROOT_SIZE-2,0,root->left); root->left->left->state = LOCKED; return 0; }
node* module::_add_node(const string &node_name, const string &node_type, map<string, module*> &map_module, map<string, cell*> &map_cell) { node* new_node; map<string, module*>::iterator ite_m; map<string, cell*>::iterator ite_c; if ((ite_c = map_cell.find(node_type)) != map_cell.end()) { new_node = _create_node(node_name, TYPE_CELL, ite_c->second, NULL); } else if ((ite_m = map_module.find(node_type)) != map_module.end()) { new_node = _create_node(node_name, TYPE_MODULE, NULL, ite_m->second); } else { new_node = NULL; } return new_node; }
struct gtree_node *gtree_child(struct gtree_node *node, go_move move, bool expand) { assert(node); const int index = gtree_move_index(node, move); if (index < 0) { return NULL; } const struct gtree_schema *schema = node->schema; struct gtree_node **children = (void*) &node->_vdata[schema->movedata_base_offset + node->num_moves * sizeof(go_move)]; if (children[index]) { return children[index]; } else if (!expand) { return NULL; } else { // expand child node struct gtree_node *child = _create_node(node, move); if (!child) { // error creating child return NULL; } children[index] = child; return child; } }
struct config_node *clone_config_node_with_mem(struct dm_pool *mem, const struct config_node *cn, int siblings) { struct config_node *new_cn; if (!cn) return NULL; if (!(new_cn = _create_node(mem))) { log_error("Failed to clone config node."); return NULL; } if ((cn->key && !(new_cn->key = dm_pool_strdup(mem, cn->key)))) { log_error("Failed to clone config node key."); return NULL; } if ((cn->v && !(new_cn->v = _clone_config_value(mem, cn->v))) || (cn->child && !(new_cn->child = clone_config_node_with_mem(mem, cn->child, 1))) || (siblings && cn->sib && !(new_cn->sib = clone_config_node_with_mem(mem, cn->sib, siblings)))) return_NULL; /* 'new_cn' released with mem pool */ return new_cn; }
int dm_hash_insert_allow_multiple(struct dm_hash_table *t, const char *key, const void *val, uint32_t val_len) { struct dm_hash_node *n; struct dm_hash_node *first; int len = strlen(key) + 1; unsigned h; n = _create_node(key, len); if (!n) return 0; n->data = (void *)val; n->data_len = val_len; h = _hash(key, len) & (t->num_slots - 1); first = t->slots[h]; if (first) n->next = first; else n->next = 0; t->slots[h] = n; t->num_nodes++; return 1; }
static struct config_node *_section(struct parser *p) { /* IDENTIFIER SECTION_B_CHAR VALUE* SECTION_E_CHAR */ struct config_node *root, *n, *l = NULL; if (!(root = _create_node(p->mem))) return_0; if (!(root->key = _dup_tok(p))) return_0; match(TOK_IDENTIFIER); if (p->t == TOK_SECTION_B) { match(TOK_SECTION_B); while (p->t != TOK_SECTION_E) { if (!(n = _section(p))) return_0; if (!root->child) root->child = n; else l->sib = n; n->parent = root; l = n; } match(TOK_SECTION_E); } else { match(TOK_EQ); if (!(root->v = _value(p))) return_0; } return root; }
NodeType *create_param(bool is_option, char *param) { /*printf("create_param\n");*/ NodeType *pn = _create_node(TypeParam); NodeParam param_node; param_node.param = format_param(is_option, param); pn->param = param_node; return pn; }
NodeType *create_pipe(NodeType *cmd, NodeType *pipe) { /*printf("create_pipe\n");*/ NodeType *pn = _create_node(TypePipe); NodePipe pipe_node; pipe_node.cmd = cmd; pipe_node.pipe = pipe; pn->pipe = pipe_node; return pn; }
int node_create_light(NID nid, uint32_t height, uint32_t children, int version, struct env *e, struct node **n) { return _create_node(nid, height, children, version, n, e, 1); }
NodeType *create_pair(NodeType *car, NodeType *cdr) { /*printf("create_pair\n");*/ NodeType *pn = _create_node(TypePair); NodePair pair_node; pair_node.car = car; pair_node.cdr = cdr; pn->pair = pair_node; return pn; }
NodeType *create_redir(NodeType *cmd, NodeType *file) { /*printf("create_redir\n");*/ NodeType *pn = _create_node(TypeRedir); NodeRedir redir_node; redir_node.cmd = cmd; redir_node.file = file; pn->redir = redir_node; return pn; }
NodeType *create_cmd(char* cmd, NodeType *params) { /*printf("create_cmd\n");*/ NodeType *pn = _create_node(TypeCmd); NodeCmd cmd_node; cmd_node.cmd = cmd; cmd_node.params = params; pn->cmd = cmd_node; return pn; }
uint32_t _dyn_alloc(struct mem_node* node, uint32_t size, mem_alloc_flags flags){ uint32_t res; switch(node->state){ case FREE: if(node->size == size || node->size == MINIMUM_SIZE){ node->state = FULL; if(!(flags & MEM_FAKE_ALLOC)){ node->mem = malloc(1 << size); } return node->loc; }else{ node->state = SPLIT; node->left = _create_node(node->size-1,node->loc,node); return _dyn_alloc(node->left,size,flags); } case LOCKED: if(!(flags & MEM_USE_LOCKED)){ return INSUFFICIENT_SPACE; } case SPLIT: if(node->left == 0){ node->left = _create_node(node->size-1,node->loc,node); return _dyn_alloc(node->left,size,flags); }else{ res = _dyn_alloc(node->left,size,flags); if(res == INSUFFICIENT_SPACE){ if(node->right == 0){ node->right = _create_node(node->size-1,node->loc+(1<<(node->size-1)),node); //TESTME return _dyn_alloc(node->right,size,flags); }else{ return _dyn_alloc(node->right,size,flags); } }else{ return res; } } case FULL: return INSUFFICIENT_SPACE; } return 0; }
static struct dm_config_node *_make_node(struct dm_pool *mem, const char *key_b, const char *key_e, struct dm_config_node *parent) { struct dm_config_node *n; if (!(n = _create_node(mem))) return_NULL; n->key = _dup_token(mem, key_b, key_e); if (parent) { n->parent = parent; n->sib = parent->child; parent->child = n; } return n; }
struct dm_config_node *dm_config_create_node(struct dm_config_tree *cft, const char *key) { struct dm_config_node *cn; if (!(cn = _create_node(cft->mem))) { log_error("Failed to create config node."); return NULL; } if (!(cn->key = dm_pool_strdup(cft->mem, key))) { log_error("Failed to create config node's key."); return NULL; } cn->parent = NULL; cn->v = NULL; return cn; }
int dm_hash_insert_binary(struct dm_hash_table *t, const char *key, uint32_t len, void *data) { struct dm_hash_node **c = _find(t, key, len); if (*c) (*c)->data = data; else { struct dm_hash_node *n = _create_node(key, len); if (!n) return 0; n->data = data; n->next = 0; *c = n; t->num_nodes++; } return 1; }
struct config_node *create_config_node(struct config_tree *cft, const char *key) { struct cs *c = (struct cs *) cft; struct config_node *cn; if (!(cn = _create_node(c->mem))) { log_error("Failed to create config node."); return NULL; } if (!(cn->key = dm_pool_strdup(c->mem, key))) { log_error("Failed to create config node's key."); return NULL; } if (!(cn->v = _create_value(c->mem))) { log_error("Failed to create config node's value."); return NULL; } cn->parent = NULL; cn->v->type = CFG_INT; cn->v->v.i = 0; cn->v->next = NULL; return cn; }