void count_simul_efun_extra_refs (struct pointer_table *ptable) /* DEBUG support: count the extra refs for structures of this module. */ { if (simul_efun_vector) { simul_efun_vector->extra_ref++; if (NULL != register_pointer(ptable, simul_efun_vector) ) count_extra_ref_in_vector( simul_efun_vector->item, VEC_SIZE(simul_efun_vector) ); } if (simul_efun_program) { simul_efun_program->extra_ref++; if (NULL == register_pointer(ptable, simul_efun_program)) return; simul_efun_program->extra_ref = 1; count_inherits(simul_efun_program); } } /* count_simul_efun_extra_refs() */
std::pair<uint8_t *, size_t> memory_manager::__allocate_consecutive(size_t upper_bound, size_t granularity) { log_flusher lf; size_t high=available()/granularity; if (upper_bound != 0) high=std::min(upper_bound/granularity, high); size_t low=1; uint8_t * res; //first check quickly if we can get "high" bytes of memory //directly. try { res = new uint8_t[high*granularity]; m_used.fetch_add(high*granularity); #ifndef TPIE_NDEBUG register_pointer(res, high*granularity, typeid(uint8_t) ); #endif return std::make_pair(res, high*granularity); } catch (std::bad_alloc) { lf.buf << "Failed to get " << (high*granularity)/(1024*1024) << " megabytes of memory. " << "Performing binary search to find largest amount " << "of memory available. This might take a few moments.\n"; } //perform a binary search in [low,high] for highest possible //memory allocation size_t best=0; do { size_t mid = static_cast<size_t>((static_cast<uint64_t>(low)+high)/2); lf.buf << "Search area is [" << low*granularity << "," << high*granularity << "]" << " query amount is: " << mid*granularity << ":\n"; //try to allocate "mid" bytes of memory //std::new throws an exception if memory allocation fails try { uint8_t* mem = new uint8_t[mid*granularity]; low = mid+1; best=mid*granularity; delete[] mem; } catch (std::bad_alloc) { high = mid-1; lf.buf << " failed.\n"; } } while (high >= low); lf.buf << "- - - - - - - END MEMORY SEARCH - - - - - -\n"; res = new uint8_t[best]; m_used.fetch_add(best); #ifndef TPIE_NDEBUG register_pointer(res, best, typeid(uint8_t) ); #endif return std::make_pair(res, best); }
void link_repository_rep::insert_locus (string id, tree t) { observer obs= tree_pointer (t, true); register_pointer (id, obs); attach_observer (t, obs); ids= list<string> (id, ids); loci= list<observer> (obs, loci); }
void *osdep_alloc_aligned( int bytes ) { byte *p, *q; again: p = (byte*)malloc( bytes+4096 ); if (p == 0) { memfail( MF_MALLOC, "Failed to allocate %d bytes heap memory.", bytes ); goto again; } q = (byte*)roundup( (word)p, 4096 ); fragmentation += 4096; register_pointer( q, p ); return q; }
/*-------------------------------------------------------------------------*/ static size_t svalue_size (svalue_t *v, mp_int * pTotal) /* Compute the memory usage of *<v> (modified to reflect data sharing), * calling svalue_size() recursively if necessary, and return it. * The size of *v itself is not included. * *<pUnshared> and *<pShared> are set to the total unshared and shared * datasize. */ { mp_int i, composite, total, overhead; assert_stack_gap(); *pTotal = 0; total = overhead = composite = 0; switch(v->type) { case T_OBJECT: case T_NUMBER: case T_FLOAT: return 0; case T_STRING: case T_SYMBOL: // If ref==0 the string is probably shared a lot, but we can't estimate // the correct number, so we return 0 as memory consumption for the // string. if (v->u.str->info.ref) { *pTotal = mstr_mem_size(v->u.str); return *pTotal / v->u.str->info.ref; } else return 0; case T_MAPPING: { struct svalue_size_locals locals; if (NULL == register_pointer(ptable, v->u.map) ) return 0; if (v->u.map->ref) { overhead = (mp_uint)mapping_overhead(v->u.map); locals.total = 0; locals.composite = 0; locals.num_values = v->u.map->num_values; walk_mapping(v->u.map, svalue_size_map_filter, &locals); *pTotal = locals.total + overhead; return (overhead + locals.composite) / v->u.map->ref; } else return 0; } case T_POINTER: case T_QUOTED_ARRAY: { if (v->u.vec == &null_vector) return 0; if (NULL == register_pointer(ptable, v->u.vec) ) return 0; if (v->u.vec->ref) { overhead = sizeof *v->u.vec - sizeof v->u.vec->item + sizeof(svalue_t) * v->u.vec->size + sizeof(char *); for (i=0; i < (mp_int)VEC_SIZE(v->u.vec); i++) { composite += svalue_size(&v->u.vec->item[i], &total); *pTotal += total; } *pTotal += overhead; return (overhead + composite) / v->u.vec->ref; } else return 0; } case T_STRUCT: { struct_t *st = v->u.strct; if (NULL == register_pointer(ptable, st) ) return 0; if (st->ref) { overhead = sizeof *st - sizeof st->member + sizeof(svalue_t) * struct_size(st); for (i=0; i < (mp_int)struct_size(st); i++) { composite += svalue_size(&st->member[i], &total); *pTotal += total; } *pTotal += overhead; return (overhead + composite) / st->ref; } else return 0; } case T_CLOSURE: { int num_values; svalue_t *svp; lambda_t *l; if (!CLOSURE_MALLOCED(v->x.closure_type)) return 0; if (!CLOSURE_REFERENCES_CODE(v->x.closure_type)) { if (v->x.closure_type == CLOSURE_LFUN) composite = SIZEOF_LAMBDA(v->u.lambda->function.lfun.context_size); else /* CLOSURE_IDENTIFIER || CLOSURE_PRELIMINARY */ composite = sizeof *v->u.lambda; composite += sizeof(char *); *pTotal = composite; return composite / v->u.lambda->ref; } /* CLOSURE_LAMBDA */ composite = overhead = 0; l = v->u.lambda; if (v->x.closure_type == CLOSURE_BOUND_LAMBDA) { total = sizeof *l - sizeof l->function + sizeof l->function.lambda; *pTotal += total; composite += total / l->ref; l = l->function.lambda; } num_values = l->function.code.num_values; svp = (svalue_t *)l - num_values; if (NULL == register_pointer(ptable, svp)) return 0; overhead = sizeof(svalue_t) * num_values + sizeof (char *); { bytecode_p p = l->function.code.program; do { switch(GET_CODE(p++)) { case F_RETURN: case F_RETURN0: break; default: continue; } break; } while (1); overhead += (p - (bytecode_p)l + (sizeof(bytecode_p) - 1)) & ~(sizeof(bytecode_p) - 1); } while (--num_values >= 0) { composite += svalue_size(svp++, &total); *pTotal += total; } *pTotal += overhead; if (l->ref) return (overhead + composite) / l->ref; else return 0; } default: fatal("Illegal type: %d\n", v->type); } /*NOTREACHED*/ return 0; }