Commit 6a4b9102 authored by Christoph Mallon's avatar Christoph Mallon
Browse files

Add OALLOC*() to make allocating from obstacks a bit nicer.

[r26468]
parent 645a0168
......@@ -71,13 +71,13 @@ char *xstrdup(const char *str);
/**
* Allocate an object with n elements of a flexible array member
*/
#define XMALLOCF(type, member, n) ((type*)xmalloc(offsetof(type, member) + sizeof(((type*)0)->member) * (n)))
#define XMALLOCF(type, member, n) ((type*)xmalloc(offsetof(type, member) + sizeof(*((type*)0)->member) * (n)))
/**
* Allocate an object with n elements of a flexible array member and zero the
* whole object
*/
#define XMALLOCFZ(type, member, n) ((type*)memset(xmalloc(offsetof(type, member) + sizeof(((type*)0)->member) * (n)), 0, offsetof(type, member) + sizeof(((type*)0)->member) * (n)))
#define XMALLOCFZ(type, member, n) ((type*)memset(XMALLOCF(type, member, (n)), 0, offsetof(type, member) + sizeof(*((type*)0)->member) * (n)))
/**
* Allocate n objects of a certain type on the stack
......@@ -89,6 +89,38 @@ char *xstrdup(const char *str);
*/
#define ALLOCANZ(type, n) ((type*)memset((type*)alloca(sizeof(type) * (n)), 0, sizeof(type) * (n)))
/**
* Allocate n objects of a certain type on the given obstack
*/
#define OALLOCN(obst, type, n) ((type*)obstack_alloc((obst), sizeof(type) * (n)))
/**
* Allocate n objects of a certain type on the given obstack and zero them
*/
#define OALLOCNZ(obst, type, n) ((type*)memset(OALLOCN((obst), type, (n)), 0, sizeof(type) * (n)))
/**
* Allocate one object of a certain type on the given obstack
*/
#define OALLOC(obst, type) OALLOCN(obst, type, 1)
/**
* Allocate one object of a certain type on the given obstack and zero it
*/
#define OALLOCZ(obst, type) OALLOCNZ(obst, type, 1)
/**
* Allocate an object with n elements of a flexible array member on the given
* obstck
*/
#define OALLOCF(obst, type, member, n) ((type*)obstack_alloc((obst), offsetof(type, member) + sizeof(*((type*)0)->member) * (n)))
/**
* Allocate an object with n elements of a flexible array member on the given
* obstack and zero the whole object
*/
#define OALLOCFZ(obst, type, member, n) ((type*)memset(OALLOCF((obst), type, member, (n)), 0, offsetof(type, member) + sizeof(*((type*)0)->member) * (n)))
/* Includes for alloca() */
#ifdef _WIN32
......
......@@ -57,12 +57,6 @@ struct _hungarian_problem_t {
DEBUG_ONLY(firm_dbg_module_t *dbg);
};
static inline void *get_init_mem(struct obstack *obst, size_t sz) {
void *p = obstack_alloc(obst, sz);
memset(p, 0, sz);
return p;
}
static void hungarian_dump_f(FILE *f, int **C, int rows, int cols, int width) {
int i, j;
......@@ -116,9 +110,9 @@ hungarian_problem_t *hungarian_new(int rows, int cols, int match_type) {
}
/* allocate space for cost matrix */
p->cost = (int **)get_init_mem(&p->obst, rows * sizeof(p->cost[0]));
p->cost = OALLOCNZ(&p->obst, int*, rows);
for (i = 0; i < p->num_rows; i++)
p->cost[i] = (int *)get_init_mem(&p->obst, cols * sizeof(p->cost[0][0]));
p->cost[i] = OALLOCNZ(&p->obst, int, cols);
return p;
}
......
......@@ -48,7 +48,7 @@ static plist_element_t *allocate_element(plist_t* list) {
new_element->next = NULL;
}
else {
new_element = obstack_alloc(list->obst, sizeof(*new_element));
new_element = OALLOC(list->obst, plist_element_t);
}
return new_element;
......@@ -69,7 +69,7 @@ plist_t *plist_new(void) {
}
plist_t *plist_obstack_new(struct obstack *obst) {
plist_t *list = obstack_alloc(obst, sizeof(*list));
plist_t *list = OALLOC(obst, plist_t);
list->obst = obst;
list->foreign_obstack = 1;
......
......@@ -206,10 +206,7 @@ SET *
/* Make segments */
for (i = 0; i < nslots; ++i) {
table->dir[i] = (Segment *)obstack_alloc (&table->obst,
sizeof (Segment) * SEGMENT_SIZE);
memset(table->dir[i], 0, sizeof (Segment) * SEGMENT_SIZE);
table->dir[i] = OALLOCNZ(&table->obst, Segment, SEGMENT_SIZE);
table->nseg++;
}
......@@ -357,10 +354,7 @@ expand_table (SET *table)
NewSegmentDir = NewAddress >> SEGMENT_SIZE_SHIFT;
NewSegmentIndex = NewAddress & (SEGMENT_SIZE-1);
if (NewSegmentIndex == 0) {
table->dir[NewSegmentDir] =
(Segment *)obstack_alloc (&table->obst,
sizeof(Segment) * SEGMENT_SIZE);
memset(table->dir[NewSegmentDir], 0, sizeof(Segment) * SEGMENT_SIZE);
table->dir[NewSegmentDir] = OALLOCNZ(&table->obst, Segment, SEGMENT_SIZE);
table->nseg++;
}
NewSegment = table->dir[NewSegmentDir];
......@@ -443,7 +437,7 @@ MANGLE(_,_search) (SET *table,
q = table->free_list;
table->free_list = table->free_list->chain;
} else {
q = obstack_alloc (&table->obst, sizeof (Element));
q = OALLOC(&table->obst, Element);
}
q->entry.dptr = (void *)key;
#else
......
......@@ -249,7 +249,7 @@ static void ana_Call(ir_node *n, void *env) {
ARR_APP1(ir_node *, arr, n);
found->call_list = arr;
} else { /* New node, add Call node and init nesting. */
found = (cg_callee_entry *)obstack_alloc(irg->obst, sizeof(*found));
found = OALLOC(irg->obst, cg_callee_entry);
found->irg = callee;
found->call_list = NEW_ARR_F(ir_node *, 1);
found->call_list[0] = n;
......@@ -428,9 +428,7 @@ typedef struct scc_info {
* allocates a new scc_info on the obstack
*/
static inline scc_info *new_scc_info(struct obstack *obst) {
scc_info *info = obstack_alloc(obst, sizeof(*info));
memset(info, 0, sizeof(*info));
return info;
return OALLOCZ(obst, scc_info);
}
/**
......
......@@ -64,7 +64,7 @@ static void add_cdep(ir_node *node, ir_node *dep_on) {
#endif
if (dep == NULL) {
ir_cdep *newdep = obstack_alloc(&cdep_data->obst, sizeof(*newdep));
ir_cdep *newdep = OALLOC(&cdep_data->obst, ir_cdep);
newdep->node = dep_on;
newdep->next = NULL;
......@@ -77,7 +77,7 @@ static void add_cdep(ir_node *node, ir_node *dep_on) {
if (dep->next == NULL) break;
dep = dep->next;
}
newdep = obstack_alloc(&cdep_data->obst, sizeof(*newdep));
newdep = OALLOC(&cdep_data->obst, ir_cdep);
newdep->node = dep_on;
newdep->next = NULL;
dep->next = newdep;
......
......@@ -77,9 +77,7 @@ typedef struct scc_info {
/** Allocate a new scc_info on the given obstack */
static inline scc_info *new_scc_info(struct obstack *obst) {
scc_info *info = obstack_alloc(obst, sizeof(*info));
memset(info, 0, sizeof(*info));
return info;
return OALLOCZ(obst, scc_info);
}
/**
......
......@@ -52,7 +52,7 @@ int (is_ir_extbb)(const void *thing) {
* allocate a new extended block header.
*/
static void allocate_extblk(ir_node *block, env_t *env) {
ir_extblk *extblk = obstack_alloc(env->obst, sizeof(*extblk));
ir_extblk *extblk = OALLOC(env->obst, ir_extblk);
extblk->kind = k_ir_extblk;
extblk->visited = 1;
......
......@@ -50,7 +50,7 @@ typedef struct _env {
*/
static ir_extblk *allocate_extblk(ir_node *block, env_t *env)
{
ir_extblk *extblk = obstack_alloc(env->obst, sizeof(*extblk));
ir_extblk *extblk = OALLOC(env->obst, ir_extblk);
extblk->kind = k_ir_extblk;
extblk->visited = 1;
......
......@@ -260,8 +260,7 @@ lv_chk_t *lv_chk_new(ir_graph *irg, const dfs_t *dfs)
res->n_blocks = dfs_get_n_nodes(res->dfs);
res->back_edge_src = bitset_obstack_alloc(obst, res->n_blocks);
res->back_edge_tgt = bitset_obstack_alloc(obst, res->n_blocks);
res->map = obstack_alloc(obst, res->n_blocks * sizeof(res->map[0]));
memset(res->map, 0, res->n_blocks * sizeof(res->map[0]));
res->map = OALLOCNZ(obst, bl_info_t*, res->n_blocks);
#if 0
{
......
......@@ -220,8 +220,7 @@ ir_loop *(get_irg_loop)(const ir_graph *irg) {
ir_loop *alloc_loop(ir_loop *father, struct obstack *obst) {
ir_loop *son;
son = obstack_alloc(obst, sizeof(*son));
memset(son, 0, sizeof(*son));
son = OALLOCZ(obst, ir_loop);
son->kind = k_ir_loop;
son->children = NEW_ARR_F(loop_element, 0);
son->n_nodes = 0;
......
......@@ -88,9 +88,7 @@ typedef struct scc_info {
* Allocates a new SCC info on the given obstack.
*/
static inline scc_info *new_scc_info(struct obstack *obst) {
scc_info *info = obstack_alloc(obst, sizeof(*info));
memset(info, 0, sizeof(*info));
return info;
return OALLOCZ(obst, scc_info);
}
/**
......
......@@ -228,7 +228,7 @@ static void wrap_BasicBlocks(ir_node *block, void *ctx) {
ir_region *reg;
/* Allocate a Block wrapper */
reg = obstack_alloc(env->obst, sizeof(*reg));
reg = OALLOC(env->obst, ir_region);
reg->kind = k_ir_region;
reg->type = ir_rk_BasicBlock;
reg->parent = NULL;
......@@ -281,7 +281,7 @@ static void update_BasicBlock_regions(ir_node *blk, void *ctx) {
/** Allocate a new region on an obstack */
#define ALLOC_REG(obst, reg, tp) \
do { \
(reg) = obstack_alloc((obst), sizeof(*(reg))); \
(reg) = OALLOC((obst), ir_region); \
(reg)->kind = k_ir_region; \
(reg)->type = tp; \
(reg)->parent = NULL; \
......
......@@ -1260,7 +1260,7 @@ static ir_type *compute_arg_type(be_abi_irg_t *env, be_abi_call_t *call,
ident *id = get_entity_ident(get_irg_entity(env->birg->irg));
ir_entity **map;
*param_map = map = obstack_alloc(&env->obst, n * sizeof(ir_entity *));
*param_map = map = OALLOCN(&env->obst, ir_entity*, n);
res = new_type_struct(id_mangle_u(id, new_id_from_chars("arg_type", 8)));
for (i = 0; i < n; ++i, curr += inc) {
ir_type *param_type = get_method_param_type(method_type, curr);
......@@ -1315,7 +1315,7 @@ static reg_node_map_t *reg_map_to_arr(struct obstack *obst, pmap *reg_map)
pmap_entry *ent;
int n = pmap_count(reg_map);
int i = 0;
reg_node_map_t *res = obstack_alloc(obst, n * sizeof(res[0]));
reg_node_map_t *res = OALLOCN(obst, reg_node_map_t, n);
foreach_pmap(reg_map, ent) {
res[i].reg = ent->key;
......@@ -1410,7 +1410,7 @@ static ir_node *create_be_return(be_abi_irg_t *env, ir_node *irn, ir_node *bl,
ir_node **in;
ir_node *stack;
const arch_register_t **regs;
pmap_entry *ent ;
pmap_entry *ent;
/*
get the valid stack node in this block.
......@@ -1453,8 +1453,8 @@ static ir_node *create_be_return(be_abi_irg_t *env, ir_node *irn, ir_node *bl,
*/
in_max = pmap_count(reg_map) + n_res + 2;
in = obstack_alloc(&env->obst, in_max * sizeof(in[0]));
regs = obstack_alloc(&env->obst, in_max * sizeof(regs[0]));
in = OALLOCN(&env->obst, ir_node*, in_max);
regs = OALLOCN(&env->obst, arch_register_t const*, in_max);
in[0] = mem;
in[1] = be_abi_reg_map_get(reg_map, arch_env->sp);
......@@ -1892,8 +1892,7 @@ static void modify_irg(be_abi_irg_t *env)
env->regs = pmap_create();
n_params = get_method_n_params(method_type);
args = obstack_alloc(&env->obst, n_params * sizeof(args[0]));
memset(args, 0, n_params * sizeof(args[0]));
args = OALLOCNZ(&env->obst, ir_node*, n_params);
/*
* for inner function we must now fix access to outer frame entities.
......@@ -2314,8 +2313,7 @@ be_abi_irg_t *be_abi_introduce(be_irg_t *birg)
env->dce_survivor = new_survive_dce();
env->birg = birg;
sp_req = obstack_alloc(&env->obst, sizeof(*sp_req));
memset(sp_req, 0, sizeof(*sp_req));
sp_req = OALLOCZ(&env->obst, arch_register_req_t);
env->sp_req = sp_req;
sp_req->type = arch_register_req_type_limited
......
......@@ -145,8 +145,7 @@ static void collect_egde_frequency(ir_node *block, void *data)
memset(&edge, 0, sizeof(edge));
entry = obstack_alloc(env->obst, sizeof(entry[0]));
memset(entry, 0, sizeof(*entry));
entry = OALLOCZ(env->obst, blocksched_entry_t);
entry->block = block;
set_irn_link(block, entry);
......@@ -585,7 +584,7 @@ static void collect_egde_frequency_ilp(ir_node *block, void *data)
snprintf(name, sizeof(name), "block_out_constr_%ld", get_irn_node_nr(block));
out_count = get_irn_n_edges_kind(block, EDGE_KIND_BLOCK);
entry = obstack_alloc(env->env.obst, sizeof(entry[0]));
entry = OALLOC(env->env.obst, blocksched_ilp_entry_t);
entry->block = block;
entry->next = NULL;
entry->prev = NULL;
......
......@@ -127,11 +127,10 @@ static inline border_t *border_add(be_chordal_env_t *env, struct list_head *head
if (!is_def) {
border_t *def;
b = obstack_alloc(env->obst, sizeof(*b));
b = OALLOC(env->obst, border_t);
/* also allocate the def and tie it to the use. */
def = obstack_alloc(env->obst, sizeof(*def));
memset(def, 0, sizeof(*def));
def = OALLOCZ(env->obst, border_t);
b->other_end = def;
def->other_end = b;
......@@ -630,7 +629,7 @@ static void pressure(ir_node *block, void *env_ptr)
bitset_clear_all(live);
/* Set up the border list in the block info */
head = obstack_alloc(env->obst, sizeof(*head));
head = OALLOC(env->obst, struct list_head);
INIT_LIST_HEAD(head);
assert(pmap_get(env->border_heads, block) == NULL);
pmap_insert(env->border_heads, block, head);
......
......@@ -263,10 +263,9 @@ static void block_dims_walker(ir_node *block, void *data)
draw_chordal_env_t *env = data;
struct list_head *head = get_block_border_head(env->chordal_env, block);
const draw_chordal_opts_t *opts = env->opts;
struct block_dims *dims = obstack_alloc(&env->obst, sizeof(*dims));
struct block_dims *dims = OALLOCZ(&env->obst, struct block_dims);
border_t *b;
memset(dims, 0, sizeof(*dims));
dims->min_step = INT_MAX;
list_for_each_entry_reverse(border_t, b, head, list) {
......
......@@ -986,18 +986,13 @@ static void process_cloud(co2_cloud_t *cloud)
int n_childs = ci->mst_n_childs;
int j;
ci->col_costs = obstack_alloc(&cloud->obst, n_regs * sizeof(ci->col_costs[0]));
ci->tmp_coloring = obstack_alloc(&cloud->obst, n_regs * sizeof(ci->tmp_coloring[0]));
ci->fronts = obstack_alloc(&cloud->obst, n_regs * n_childs * sizeof(ci->fronts[0]));
ci->color_badness = obstack_alloc(&cloud->obst, n_regs * sizeof(ci->fronts[0]));
memset(ci->color_badness, 0, n_regs * sizeof(ci->color_badness[0]));
memset(ci->col_costs, 0, n_regs * sizeof(ci->col_costs[0]));
memset(ci->tmp_coloring, 0, n_regs * sizeof(ci->tmp_coloring[0]));
memset(ci->fronts, 0, n_regs * n_childs * sizeof(ci->fronts[0]));
ci->col_costs = OALLOCNZ(&cloud->obst, int, n_regs);
ci->tmp_coloring = OALLOCNZ(&cloud->obst, col_cost_pair_t, n_regs);
ci->fronts = OALLOCNZ(&cloud->obst, int, n_regs * n_childs);
ci->color_badness = OALLOCNZ(&cloud->obst, int, n_regs);
for(j = 0; j < env->n_regs; j++)
ci->col_costs[j] = INT_MAX;
}
determine_color_badness(cloud->mst_root, 0);
......
......@@ -144,7 +144,7 @@ void sr_remove(size_red_t *sr) {
if (!arch_register_req_is(req, limited) && !sr_is_removed(sr, irn) && !co_gs_is_optimizable(sr->co, irn)) {
if (sr_is_simplicial(sr, irn)) {
coloring_suffix_t *cs = obstack_alloc(&sr->ob, sizeof(*cs));
coloring_suffix_t *cs = OALLOC(&sr->ob, coloring_suffix_t);
cs->irn = irn;
cs->next = sr->col_suff;
......
......@@ -752,7 +752,7 @@ static void add_edge(copy_opt_t *co, ir_node *n1, ir_node *n2, int costs) {
/* if we did not find n2 in n1's neighbourhood insert it */
if (allocnew) {
nbr = obstack_alloc(&co->obst, sizeof(*nbr));
nbr = OALLOC(&co->obst, neighb_t);
nbr->irn = n2;
nbr->costs = 0;
nbr->next = node->neighbours;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment