/* * HEAP - Heap functions * * (c)Copyright 1993-2014, Matthew Dillon, All Rights Reserved. See the * COPYRIGHT file at the base of the distribution. */ #include "defs.h" /* * heapAlloc() - Allocate an object on the heap, load into fat pointer. * The related refstor is lock+ref. * * NOTE: (type) is the object type, NOT a ptr-to-object type. * * XXX for now assume that the target pointer is in naturally locked * storage. XXX broken. */ void * heapAlloc(PointerStor *ps, Type *type) { RefStor *rs; rs = zalloc(sizeof(*rs)); rs->rs_Op = RSOP_ZALLOC; rs->rs_Count = 1; rs->rs_Refs = 1; #ifdef LIBRUNTIME /* * Assume returned data is content-locked. */ RuneRunTime_RSLock(rs); #else rs->rs_Lock.rl_Count = RLCOUNT_INCR; #endif if ((type->ty_SQFlags & SF_NOZERO) && (type->ty_Flags & TF_HASLVPTR) == 0) { rs->rs_Base = znalloc(type->ty_Bytes); } else { rs->rs_Base = zalloc(type->ty_Bytes); } rs->rs_Type = type; ps->s_Addr = rs->rs_Base; ps->s_Beg = ps->s_Addr; ps->s_End = ps->s_Addr + type->ty_Bytes; ps->s_RefStor = rs; ps->s_Type = type; return(ps->s_Addr); }