static inline uint64_t alignup(uint64_t sz, uint32_t align) { uint64_t s = alignsize(sz, align); if (s == sz) return s + align; /* align up */ else return s; }
type *TypeAArray::toCtype() { type *t; if (ctype) return ctype; if (0 && global.params.symdebug) { /* An associative array is represented by: * struct AArray { size_t length; void* ptr; } */ static Symbol *s; if (!s) { s = symbol_calloc("_AArray"); s->Sclass = SCstruct; s->Sstruct = struct_calloc(); s->Sstruct->Sflags |= 0; s->Sstruct->Salignsize = alignsize(); s->Sstruct->Sstructalign = global.structalign; s->Sstruct->Sstructsize = size(0); slist_add(s); Symbol *s1 = symbol_name("length", SCmember, Type::tsize_t->toCtype()); list_append(&s->Sstruct->Sfldlst, s1); Symbol *s2 = symbol_name("data", SCmember, Type::tvoidptr->toCtype()); s2->Smemoff = Type::tsize_t->size(); list_append(&s->Sstruct->Sfldlst, s2); } t = type_alloc(TYstruct); t->Ttag = (Classsym *)s; // structure tag name t->Tcount++; s->Stype = t; } else { if (global.params.symdebug == 1) { /* Generate D symbolic debug info, rather than C * Tnext: element type * Tkey: key type */ t = type_allocn(TYaarray, next->toCtype()); t->Tkey = index->toCtype(); t->Tkey->Tcount++; } else t = type_fake(TYaarray); } t->Tcount++; ctype = t; return t; }
type *TypeDelegate::toCtype() { type *t; if (ctype) return ctype; if (0 && global.params.symdebug) { /* A delegate consists of: * _Delegate { void* frameptr; Function *funcptr; } */ static Symbol *s; if (!s) { s = symbol_calloc("_Delegate"); s->Sclass = SCstruct; s->Sstruct = struct_calloc(); s->Sstruct->Sflags |= 0; s->Sstruct->Salignsize = alignsize(); s->Sstruct->Sstructalign = global.structalign; s->Sstruct->Sstructsize = size(0); slist_add(s); Symbol *s1 = symbol_name("frameptr", SCmember, Type::tvoidptr->toCtype()); list_append(&s->Sstruct->Sfldlst, s1); Symbol *s2 = symbol_name("funcptr", SCmember, Type::tvoidptr->toCtype()); s2->Smemoff = Type::tvoidptr->size(); list_append(&s->Sstruct->Sfldlst, s2); } t = type_alloc(TYstruct); t->Ttag = (Classsym *)s; // structure tag name t->Tcount++; s->Stype = t; } else { if (global.params.symdebug == 1) { // Generate D symbolic debug info, rather than C t = type_allocn(TYdelegate, next->toCtype()); } else t = type_fake(TYdelegate); } t->Tcount++; ctype = t; return t; }
type *TypeDArray::toCtype() { type *t; if (ctype) return ctype; if (0 && global.params.symdebug) { /* Create a C type out of: * struct _Array_T { size_t length; T* data; } */ Symbol *s; char *id; assert(next->deco); id = (char *) alloca(7 + strlen(next->deco) + 1); sprintf(id, "_Array_%s", next->deco); s = symbol_calloc(id); s->Sclass = SCstruct; s->Sstruct = struct_calloc(); s->Sstruct->Sflags |= 0; s->Sstruct->Salignsize = alignsize(); s->Sstruct->Sstructalign = global.structalign; s->Sstruct->Sstructsize = size(0); slist_add(s); Symbol *s1 = symbol_name("length", SCmember, Type::tsize_t->toCtype()); list_append(&s->Sstruct->Sfldlst, s1); Symbol *s2 = symbol_name("data", SCmember, next->pointerTo()->toCtype()); s2->Smemoff = Type::tsize_t->size(); list_append(&s->Sstruct->Sfldlst, s2); t = type_alloc(TYstruct); t->Ttag = (Classsym *)s; // structure tag name t->Tcount++; s->Stype = t; } else { if (global.params.symdebug == 1) { // Generate D symbolic debug info, rather than C t = type_allocn(TYdarray, next->toCtype()); } else t = type_fake(TYdarray); } t->Tcount++; ctype = t; return t; }
void *local_pool_alloc(struct allocator *_lp,int32_t size) { assert(_lp); struct local_pool *lp = (struct local_pool *)_lp; uint32_t alloc_size; if(size <= 0) alloc_size = 1; alloc_size = alignsize(alloc_size); if(lp->free_size < alloc_size) return 0; void *ret = (void*)lp->free_ptr; lp->free_size -= alloc_size; lp->free_ptr += alloc_size; return ret; }
/* * malloc */ void *malloc (size_t size) { // checkheap(1); // Let's make sure the heap is ok! size_t newsize; /* Adjusted block size */ char *bp; /* Ignore spurious requests */ if (size == 0) return NULL; newsize=alignsize(size+8); /* Search the free list for a fit */ if ((bp = find_fit(newsize)) != NULL) { place(bp, newsize); return (void *)((uint32_t *)bp+1); } /* No fit found. Get more memory and place the block */ if ((bp = extend_heap(newsize)) == NULL) return NULL; alloc_block(bp,newsize); return (void *)((uint32_t *)bp+1); }