CuAllocUserDataPtr_t CuRealloc(void *old, CuSize_t n){
    assert (old); // keep assert and if for DBG/RLS
    if (old==NULL){
        return CuAlloc(n);
    }else{
        register CuAlloc_t* ptr = CuAlloc_getHeaderAddr(old);
        register CuAllocUserDataPtr_t oldPtr = old;

        CuAlloc_t *ptr2;

        switch (CuAlloc_getBufferValidity(oldPtr)){
         case true:
            ptr2 = (CuAlloc_t*)malloc(CuAlloc_calculateTotalSize(n));
            // @todo (mc78#1#09/22/18): May cause dangling pointer if realloc fails!!!
            if (ptr2){
                void *data = CuAlloc_getDataAddr(ptr2);
                memcpy(ptr2, ptr, CuAlloc_calculateTotalSize(n));

                memset(ptr, 0x55, CuAlloc_getTotalSize(oldPtr)); // Fill memory from previous alloc
                free(ptr); // free memory from previous alloc
                ++realloccount;
                CuAlloc_initHeader(ptr2, n);
                return data;
            }
         default: return NULL;
        }
    }
}
示例#2
0
文件: CuData.c 项目: millenomi/cumin
CuData* CuDataMakeNoCopy(const void* bytes, size_t size) {
	struct CuData* c = CuAlloc(&CuDataKindInfo);
	c->Bytes = (void*) bytes;
	c->Size = size;
	c->OwnsBuffer = false;
	
	return CuReleaseLater(c);
}
示例#3
0
文件: CuData.c 项目: millenomi/cumin
CuData* CuDataMake(const void* bytes, size_t size) {
	struct CuData* c = CuAlloc(&CuDataKindInfo);
	c->Bytes = malloc(size);
	memcpy(c->Bytes, bytes, size);
	c->Size = size;
	c->OwnsBuffer = true;
	
	return CuReleaseLater(c);
}
void testCuAlloc(CuTest_t* tc){
    enum {constTestValInt = 10054};
    unsigned int alloccount = CuAlloc_getAllocCount();

    int *iVar = (int*)CuAlloc(sizeof(int));
    CuAssertIntEquals(tc, sizeof(*iVar), CuAlloc_getDataSize(iVar));
    CuAssertIntEquals(tc, sizeof(*iVar)+sizeof(CuAlloc_BufHeader_t), CuAlloc_getTotalSize(iVar));
    CuAssertTrue(tc, CuAlloc_getBufferValidity(iVar));
    CuAssertIntEquals(tc, alloccount+1, CuAlloc_getAllocCount());
    iVar[0] = constTestValInt;
    CuAssertIntEquals(tc, iVar[0], constTestValInt);

    CuFree(iVar);
}
void testCuFree(CuTest_t* tc){
    int *iVar = (int*)CuAlloc(sizeof(int));
    CuSize_t freecount = CuAlloc_getFreeCount();
    CuSize_t alloccount = CuAlloc_getAllocCount();
    bool freeResult = CuFree(iVar);


   // CuAssertTrue(tc, !CuAlloc_getBufferValidity(iVar));
    CuAssertIntEquals(tc, true, freeResult);
    CuAssertIntEquals(tc, alloccount, CuAlloc_getAllocCount());
    CuAssertIntEquals(tc, freecount+1, CuAlloc_getFreeCount());

    freeResult = CuFree(iVar);
    //CuAssertTrue(tc, !CuAlloc_getBufferValidity(iVar));
    CuAssertTrue(tc, !freeResult);

}