extern void * realloc(void *ptr, size_t size) { pthread_mutex_lock(&mutex); increaseReallocCalls(); // Allocate new object void * newptr = allocateObject( size ); // Copy old object only if ptr != 0 if ( ptr != 0 ) { // copy only the minimum number of bytes size_t sizeToCopy = objectSize( ptr ); if ( sizeToCopy > size ) { sizeToCopy = size; } memcpy( newptr, ptr, sizeToCopy ); // Free old object freeObject( ptr ); } return newptr; }
listPo removeListEl(heapPo H, listPo list, integer px) { basePo base = C_BASE(list->base); int root = gcAddRoot(H, (ptrPo) (&base)); gcAddRoot(H, (ptrPo) (&list)); integer delta = base->length / 8; integer newLen = list->length + delta; basePo nb = (basePo) allocateObject(H, baseClass, BaseCellCount(newLen)); integer ocount = list->length; assert(ocount >= 0); integer extra = newLen - ocount; nb->min = extra / 2; nb->max = nb->min + ocount-1; nb->length = newLen; for (integer ix = 0; ix < px; ix++) { nb->els[nb->min + ix] = base->els[list->start + ix]; } for (integer ix = px + 1; ix < ocount; ix++) { nb->els[nb->min + ix] = base->els[list->start + ix]; } gcAddRoot(H, (ptrPo) (&nb)); listPo slice = (listPo) newSlice(H, nb, nb->min, list->length - 1); assert(saneList(H,slice)); gcReleaseRoot(H, root); releaseHeapLock(H); return slice; }
/* initialize double */ Object * initDouble(double v) { Object * o = allocateObject(); o->type = DOUBLE; o->data.Double.v = v; return o; }
/* initialize integer */ Object * initInteger(int v) { Object * o = allocateObject(); o->type = INTEGER; o->data.Integer.v = v; return o; }
methodPo defineMtd(heapPo H, insPo ins, integer insCount, integer lclCount, integer stackDelta, labelPo lbl, normalPo pool, normalPo locals, normalPo lines) { int root = gcAddRoot(H, (ptrPo) &lbl); gcAddRoot(H, (ptrPo) &pool); gcAddRoot(H, (ptrPo) &locals); gcAddRoot(H, (ptrPo) &lines); methodPo mtd = (methodPo) allocateObject(H, methodClass, MtdCellCount(insCount)); for (integer ix = 0; ix < insCount; ix++) mtd->code[ix] = ins[ix]; mtd->codeSize = insCount; mtd->jit = Null; mtd->arity = lbl->arity; mtd->lclcnt = lclCount; mtd->pool = pool; mtd->locals = locals; mtd->lines = lines; mtd->stackDelta = stackDelta; lbl->mtd = mtd; gcReleaseRoot(H, root); return mtd; }
cellPo newCell(heapPo H, termPo content) { int root = gcAddRoot(H, (ptrPo) (&content)); cellPo cell = (cellPo) allocateObject(H, cellClass, CellCellCount); cell->content = content; gcReleaseRoot(H, root); return cell; }
extern void * malloc(size_t size) { pthread_mutex_lock(&mutex); increaseMallocCalls(); return allocateObject( size ); }
/* cons */ Object * cons(Object * car, Object * cdr) { Object * o = allocateObject(); o->type = PAIR; o->data.Pair.car = car; o->data.Pair.cdr = cdr; return o; }
static termPo newSlice(heapPo H, basePo base, integer start, integer length) { int root = gcAddRoot(H, (ptrPo) &base); listPo slice = (listPo) allocateObject(H, listClass, ListCellCount); slice->base = (termPo) base; slice->start = start; slice->length = length; gcReleaseRoot(H, root); return (termPo) slice; }
termPo allocateBase(heapPo H, integer length, logical safeMode) { basePo base = (basePo) allocateObject(H, baseClass, BaseCellCount(length)); base->min = length / 2; base->max = length / 2; base->length = length; if (safeMode) { for (integer ix = 0; ix < length; ix++) base->els[ix] = voidEnum; } return (termPo) base; }
/* initialize string */ Object * initString(char * v) { Object * o = allocateObject(); o->type = STRING; o->data.String.v = malloc(sizeof(char)*(strlen(v) + 1)); if(o->data.String.v == NULL) { printf("ERROR:Out of memory\n"); exit(1); } strcpy(o->data.String.v, v); return o; }
listPo createList(heapPo H, integer capacity) { listPo list = (listPo) allocateObject(H, listClass, ListCellCount); int root = gcAddRoot(H, (ptrPo) &list); integer extra = maximum(capacity / 8, 1); list->start = extra / 2; list->length = 0; list->base = voidEnum; basePo base = (basePo) allocateBase(H, capacity + extra, False); base->min = base->max = list->start; list->base = (termPo) base; gcReleaseRoot(H, root); return list; }
basePo copyBase(heapPo H, basePo ob, integer from, integer count, integer delta) { int root = gcAddRoot(H, (ptrPo) (&ob)); integer newLen = count + delta; basePo base = (basePo) allocateObject(H, baseClass, BaseCellCount(newLen)); base->min = delta / 2; base->max = base->min + count; for (integer ix = 0; ix < count; ix++) { base->els[base->min + ix] = ob->els[from + ix]; } base->length = newLen; gcReleaseRoot(H, root); return base; }
listPo allocateList(heapPo H, integer length, logical safeMode) { listPo list = (listPo) allocateObject(H, listClass, ListCellCount); int root = gcAddRoot(H, (ptrPo) &list); list->start = 0; list->length = length; list->base = voidEnum; integer extra = maximum(length / 8, 1); basePo base = (basePo) allocateBase(H, length + extra, safeMode); base->min = extra / 2; base->max = base->min + length; list->base = (termPo) base; list->start = base->min; gcReleaseRoot(H, root); return list; }
listPo insertListEl(heapPo H, listPo list, integer px, termPo vl) { basePo base = C_BASE(list->base); if (px <= 0) return prependToList(H, list, vl); else if (px >= listSize(list)) return appendToList(H, list, vl); else { int root = gcAddRoot(H, (ptrPo) (&base)); gcAddRoot(H, (ptrPo) (&list)); gcAddRoot(H, (ptrPo) (&vl)); integer delta = base->length / 8; integer newLen = base->length + delta + 1; basePo nb = (basePo) allocateObject(H, baseClass, BaseCellCount(newLen)); integer ocount = list->length; assert(ocount >= 0); integer extra = newLen - ocount; nb->min = extra / 2 - 1; nb->max = nb->min + ocount + 1; nb->length = newLen; for (integer ix = 0; ix < px; ix++) { nb->els[nb->min + ix] = base->els[base->min + ix]; } nb->els[nb->min + px] = vl; for (integer ix = px; ix < ocount; ix++) { nb->els[nb->min + ix + 1] = base->els[base->min + ix]; } gcAddRoot(H, (ptrPo) (&nb)); listPo slice = (listPo) newSlice(H, nb, nb->min, ocount + 1); gcReleaseRoot(H, root); releaseHeapLock(H); assert(saneList(H, slice)); return slice; } }
extern void * calloc(size_t nelem, size_t elsize) { pthread_mutex_lock(&mutex); increaseCallocCalls(); // calloc allocates and initializes size_t size = nelem * elsize; void * ptr = allocateObject( size ); if ( ptr ) { // No error // Initialize chunk with 0s memset( ptr, 0, size ); } return ptr; }
void* MM_SegregatedAllocationInterface::allocateArray(MM_EnvironmentBase *env, MM_AllocateDescription *allocateDescription, MM_MemorySpace *memorySpace, bool shouldCollectOnFailure) { void *result = NULL; #if defined(OMR_GC_ARRAYLETS) MM_MemorySubSpace *subSpace = memorySpace->getDefaultMemorySubSpace(); result = subSpace->allocateObject(env, allocateDescription, NULL, NULL, shouldCollectOnFailure); if ((NULL != result) && !allocateDescription->isCompletedFromTlh()) { _stats._allocationBytes += allocateDescription->getContiguousBytes(); ++_stats._allocationCount; } #else /* OMR_GC_ARRAYLETS */ result = allocateObject(env, allocateDescription, memorySpace, shouldCollectOnFailure); #endif /* OMR_GC_ARRAYLETS */ return result; }
termPo allocateObject(heapPo H, clssPo clss, size_t amnt) { if ((((ptrPo) currHeap->curr) + amnt) < ((ptrPo) (currHeap->limit))) { termPo t = currHeap->curr; H->curr = H->curr + amnt; t->clss = clss; #ifdef TRACEMEM if (traceMemory) { numAllocated++; totalAllocated += amnt; } if (validateMemory) { verifyHeap(H); } #endif return t; } else if (gcCollect(H, amnt) == Ok) return allocateObject(H, clss, amnt); else return Null; }
int main(int argc, char *argv[]) { // expects no arguments because we are using a particular class file if (argc != 1) { fprintf(stderr, "Usage: heapTest\n"); exit(-1); } // initialize: need to use a class with a user class that has at least // four fields initializeVM(10000, "userClass.class"); // okay, begin the testing... Class A = getIthClass(4); Reference objectRef = allocateObject(getObjectClass()); Reference integerRef = allocateObject(getIntegerClass()); Reference aRef = allocateObject(A); Reference stringRef = allocateString("abcdefghijklmnopqrstuvwxyz"); if (getObjectClass() != getClass(objectRef)) { fprintf(stderr, "FAIL: getClass from objectRef\n"); } if (getIntegerClass() != getClass(integerRef)) { fprintf(stderr, "FAIL: getClass from integerRef\n"); } if (getStringClass() != getClass(stringRef)) { fprintf(stderr, "FAIL: getClass from stringRef\n"); } if (A != getClass(aRef)) { fprintf(stderr, "FAIL: getClass from aRef\n"); } if (strcmp(getStringValue(stringRef), "abcdefghijklmnopqrstuvwxyz")) { fprintf(stderr, "FAIL: getStringValue\n"); } putIntegerValue(integerRef, 1066); if (getIntegerValue(integerRef) != 1066) { fprintf(stderr, "FAIL: getIntegerValue\n"); } putField(aRef, 0, integerRef); if (getField(aRef, 0) != integerRef) { fprintf(stderr, "FAIL: getField 0\n"); } putField(aRef, 3, objectRef); if (getField(aRef, 3) != objectRef) { fprintf(stderr, "FAIL: getField 3\n"); } printf("testing complete.\n"); return 0; }
normalPo allocateStruct(heapPo H, labelPo lbl) { return (normalPo) allocateObject(H, (clssPo) lbl, NormalCellCount(labelArity(lbl))); }