Ejemplo n.º 1
0
void* THRealloc(void *ptr, long size)
{
  if(!ptr)
    return(THAlloc(size));
  
  if(size == 0)
  {
    THFree(ptr);
    return NULL;
  }

  if(size < 0)
    THError("$ Torch: invalid memory size -- maybe an overflow?");

  THHeapUpdate(-getAllocSize(ptr));
  void *newptr = realloc(ptr, size);

  if(!newptr && torchGCFunction) {
    torchGCFunction(torchGCData);
    newptr = realloc(ptr, size);
  }
  THHeapUpdate(getAllocSize(newptr ? newptr : ptr));

  if(!newptr)
    THError("$ Torch: not enough memory: you tried to reallocate %dGB. Buy new RAM!", size/1073741824);

  return newptr;
}
Ejemplo n.º 2
0
    void append(const V& value)
    {
        if (topElement >= allDataCount)
        {
            V* newAllData = new V[getAllocSize()];

            if (!newAllData)
            {
            	throw std::bad_alloc();
            }

            for (size_t i = 0; i < getAllocSize(); i++)
            {
                newAllData[i] = nullptr;

                if (i < allDataCount)
                {
                    newAllData[i] = allData[i];
                    allData[i].reset();
                }
            }

            delete[] allData;

            allData = newAllData;

            allDataCount = getAllocSize();
        }

        allData[topElement] = value;
        topElement++;
    }
Ejemplo n.º 3
0
void* THRealloc(void *ptr, ptrdiff_t size)
{
  if(!ptr)
    return(THAlloc(size));

  if(size == 0)
  {
    THFree(ptr);
    return NULL;
  }

  if(size < 0)
    THError("$ Torch: invalid memory size -- maybe an overflow?");

  ptrdiff_t oldSize = -getAllocSize(ptr);
  void *newptr = realloc(ptr, size);

  if(!newptr && torchGCFunction) {
    torchGCFunction(torchGCData);
    newptr = realloc(ptr, size);
  }

  if(!newptr)
    THError("$ Torch: not enough memory: you tried to reallocate %dGB. Buy new RAM!", size/1073741824);

  // update heapSize only after successfully reallocated
  THHeapUpdate(oldSize + getAllocSize(newptr));

  return newptr;
}
Ejemplo n.º 4
0
    VkBool32 insert(const size_t index, const V& value)
    {
        if (index > topElement)
        {
            return VK_FALSE;
        }

        if (index == topElement)
        {
            append(value);

            return VK_TRUE;
        }

        if (topElement + 1 >= allDataCount)
        {
            V* newAllData = new V[getAllocSize()];

            if (!newAllData)
            {
            	throw std::bad_alloc();
            }

            for (size_t i = 0; i < getAllocSize(); i++)
            {
                newAllData[i] = nullptr;

                if (i < allDataCount)
                {
                    newAllData[i] = allData[i];
                    allData[i].reset();
                }
            }

            delete[] allData;

            allData = newAllData;

            allDataCount = getAllocSize();
        }

        for (size_t copyIndex = topElement; copyIndex >= index + 1; copyIndex--)
        {
            allData[copyIndex] = allData[copyIndex - 1];
        }

        allData[index] = value;
        topElement++;

        return VK_TRUE;
    }
Ejemplo n.º 5
0
static void* THAllocInternal(ptrdiff_t size)
{
  void *ptr;

  if (size > 5120)
  {
#if (defined(__unix) || defined(__APPLE__)) && (!defined(DISABLE_POSIX_MEMALIGN))
    if (posix_memalign(&ptr, 64, size) != 0)
      ptr = NULL;
/*
#elif defined(_WIN32)
    ptr = _aligned_malloc(size, 64);
*/
#else
    ptr = malloc(size);
#endif
  }
  else
  {
    ptr = malloc(size);
  }

  THHeapUpdate(getAllocSize(ptr));
  return ptr;
}
Ejemplo n.º 6
0
void THFree(void *ptr)
{
    if (ptr) {
        THHeapUpdate(-getAllocSize(ptr));
        free(ptr);
    }
    else {
#ifdef __DEBUG
        printf("THFree called with ptr==0\n");
#endif
    }
}
Ejemplo n.º 7
0
void OpaqueImpl::setDatasImpl( size_t count, uint8_t const *src, size_t srcStride, uint8_t *dst, size_t dstStride ) const
{
    size_t allocSize = getAllocSize();

    FABRIC_ASSERT( src );
    FABRIC_ASSERT( dst );
    uint8_t * const dstEnd = dst + count * dstStride;

    while ( dst != dstEnd )
    {
        memcpy( dst, src, allocSize );
        src += srcStride;
        dst += dstStride;
    }
}
Ejemplo n.º 8
0
Value *
SimpleAllocatorInfo::getOrCreateAllocSize(Value * AllocSite) const {
  return getAllocSize (AllocSite);
}
Ejemplo n.º 9
0
static bool OCParseArray(OCRepPayload* out, const char* name, CborValue* container)
{
    OCRepPayloadPropType type;
    size_t dimensions[MAX_REP_ARRAY_DEPTH];
    bool err = OCParseArrayFindDimensionsAndType(container, dimensions, &type);

    if (err)
    {
        OC_LOG(ERROR, TAG, "Array details weren't clear");
        return err;
    }

    if (type == OCREP_PROP_NULL)
    {
        err = err || OCRepPayloadSetNull(out, name);
        err = err || cbor_value_advance(container);
        return err;
    }

    size_t dimTotal = calcDimTotal(dimensions);
    size_t allocSize = getAllocSize(type);
    void* arr = OICCalloc(dimTotal, allocSize);

    if (!arr)
    {
        OC_LOG(ERROR, TAG, "Array Parse allocation failed");
        return true;
    }

    err = err || OCParseArrayFillArray(container, dimensions, type, arr);

    switch (type)
    {
    case OCREP_PROP_INT:
        if (err || !OCRepPayloadSetIntArrayAsOwner(out, name, (int64_t*)arr, dimensions))
        {
            OICFree(arr);
            err = true;
        }
        break;
    case OCREP_PROP_DOUBLE:
        if (err || !OCRepPayloadSetDoubleArrayAsOwner(out, name, (double*)arr, dimensions))
        {
            OICFree(arr);
            err = true;
        }
        break;
    case OCREP_PROP_BOOL:
        if (err || !OCRepPayloadSetBoolArrayAsOwner(out, name, (bool*)arr, dimensions))
        {
            OICFree(arr);
            err = true;
        }
        break;
    case OCREP_PROP_STRING:
        if (err || !OCRepPayloadSetStringArrayAsOwner(out, name, (char**)arr, dimensions))
        {
            for(size_t i = 0; i < dimTotal; ++i)
            {
                OICFree(((char**)arr)[i]);
            }
            OICFree(arr);
            err = true;
        }
        break;
    case OCREP_PROP_OBJECT:
        if (err || !OCRepPayloadSetPropObjectArrayAsOwner(out, name, (OCRepPayload**)arr, dimensions))
        {
            for(size_t i = 0; i < dimTotal; ++i)
            {
                OCRepPayloadDestroy(((OCRepPayload**)arr)[i]);
            }
            OICFree(arr);
            err = true;
        }
        break;
    default:
        OC_LOG(ERROR, TAG, "Invalid Array type in Parse Array");
        err = true;
        break;
    }

    return err;
}
Ejemplo n.º 10
0
bool OpaqueImpl::equalsData( void const *lhs, void const *rhs ) const
{
    return memcmp( lhs, rhs, getAllocSize() ) == 0;
}
Ejemplo n.º 11
0
std::string OpaqueImpl::descData( void const *src ) const
{
    return "Opaque<" + Util::hexBuf( getAllocSize(), src ) + ">";
}
Ejemplo n.º 12
0
void OpaqueImpl::encodeJSON( void const *data, JSON::Encoder &encoder ) const
{
    encoder.makeBoolean( memcmp( data, m_defaultData, getAllocSize() ) != 0 );
}
Ejemplo n.º 13
0
 void OpaqueImpl::decodeJSON( JSON::Entity const &entity, void *dst ) const
 {
   entity.requireNull();
   memset( dst, 0, getAllocSize() );
 }
Ejemplo n.º 14
0
 void OpaqueImpl::setData( void const *src, void *dst ) const
 {
   memcpy( dst, src, getAllocSize() );
 }
Ejemplo n.º 15
0
void Vector<T>::setSize(unsigned newSize) {
    if (!sameAllocSize(size(), newSize)) {
        _buf = (T *) realloc(buf(), getAllocSize(newSize) * sizeof(T));
    }
    _size = (newSize << 4) | (_size & 0xf);
}
Ejemplo n.º 16
0
bool OpaqueImpl::isEquivalentTo( RC::ConstHandle<Impl> const &impl ) const
{
    if ( !isOpaque( impl->getType() ) )
        return false;
    return getAllocSize() == impl->getAllocSize();
}
Ejemplo n.º 17
0
void THFree(void *ptr)
{
  THHeapUpdate(-getAllocSize(ptr));
  free(ptr);
}
Ejemplo n.º 18
0
static CborError OCParseArrayFillArray(const CborValue *parent,
        size_t dimensions[MAX_REP_ARRAY_DEPTH], OCRepPayloadPropType type, void *targetArray)
{
    CborValue insideArray;

    size_t i = 0;
    char *tempStr = NULL;
    OCByteString ocByteStr = { .bytes = NULL, .len = 0};
    size_t tempLen = 0;
    OCRepPayload *tempPl = NULL;

    size_t newdim[MAX_REP_ARRAY_DEPTH];
    newdim[0] = dimensions[1];
    newdim[1] = dimensions[2];
    newdim[2] = 0;

    CborError err = cbor_value_enter_container(parent, &insideArray);
    VERIFY_CBOR_SUCCESS(TAG, err, "Failed to enter container");

    while (!err && i < dimensions[0] && cbor_value_is_valid(&insideArray))
    {
        bool noAdvance = false;
        if (cbor_value_get_type(&insideArray) != CborNullType)
        {
            switch (type)
            {
                case OCREP_PROP_INT:
                    if (dimensions[1] == 0)
                    {
                        err = cbor_value_get_int64(&insideArray, &(((int64_t*)targetArray)[i]));
                    }
                    else
                    {
                        err = OCParseArrayFillArray(&insideArray, newdim, type,
                            &(((int64_t*)targetArray)[arrayStep(dimensions, i)]));
                    }
                    break;
                case OCREP_PROP_DOUBLE:
                    if (dimensions[1] == 0)
                    {
                        err = cbor_value_get_double(&insideArray, &(((double*)targetArray)[i]));
                    }
                    else
                    {
                        err = OCParseArrayFillArray(&insideArray, newdim, type,
                            &(((double*)targetArray)[arrayStep(dimensions, i)]));
                    }
                    break;
                case OCREP_PROP_BOOL:
                    if (dimensions[1] == 0)
                    {
                        err = cbor_value_get_boolean(&insideArray, &(((bool*)targetArray)[i]));
                    }
                    else
                    {
                        err = OCParseArrayFillArray(&insideArray, newdim, type,
                            &(((bool*)targetArray)[arrayStep(dimensions, i)]));
                    }
                    break;
                case OCREP_PROP_STRING:
                    if (dimensions[1] == 0)
                    {
                        err = cbor_value_dup_text_string(&insideArray, &tempStr, &tempLen, NULL);
                        ((char**)targetArray)[i] = tempStr;
                        tempStr = NULL;
                    }
                    else
                    {
                        err = OCParseArrayFillArray(&insideArray, newdim, type,
                            &(((char**)targetArray)[arrayStep(dimensions, i)]));
                    }
                    break;
                case OCREP_PROP_BYTE_STRING:
                    if (dimensions[1] == 0)
                    {
                        err = cbor_value_dup_byte_string(&insideArray, &(ocByteStr.bytes),
                                &(ocByteStr.len), NULL);
                        ((OCByteString*)targetArray)[i] = ocByteStr;
                    }
                    else
                    {
                        err = OCParseArrayFillArray(&insideArray, newdim, type,
                                &(((OCByteString*)targetArray)[arrayStep(dimensions, i)]));
                    }
                    break;
                case OCREP_PROP_OBJECT:
                    if (dimensions[1] == 0)
                    {
                        err = OCParseSingleRepPayload(&tempPl, &insideArray, false);
                        ((OCRepPayload**)targetArray)[i] = tempPl;
                        tempPl = NULL;
                        noAdvance = true;
                    }
                    else
                    {
                        err = OCParseArrayFillArray(&insideArray, newdim, type,
                            &(((OCRepPayload**)targetArray)[arrayStep(dimensions, i)]));
                    }
                    break;
                default:
                    OIC_LOG(ERROR, TAG, "Invalid Array type in Parse Array");
                    err = CborErrorUnknownType;
                    break;
            }
            VERIFY_CBOR_SUCCESS(TAG, err, "Failed setting repPayload");
        }
        ++i;
        if (!noAdvance && cbor_value_is_valid(&insideArray))
        {
            err = cbor_value_advance(&insideArray);
            VERIFY_CBOR_SUCCESS(TAG, err, "Failed advnce insideArray");
        }
    }

exit:
    return err;
}

static CborError OCParseArray(OCRepPayload *out, const char *name, CborValue *container)
{
    void *arr = NULL;
    OCRepPayloadPropType type;
    size_t dimensions[MAX_REP_ARRAY_DEPTH];
    size_t dimTotal;
    size_t allocSize;
    bool res = true;
    CborError err = OCParseArrayFindDimensionsAndType(container, dimensions, &type);
    VERIFY_CBOR_SUCCESS(TAG, err, "Array details weren't clear");

    if (type == OCREP_PROP_NULL)
    {
        res = OCRepPayloadSetNull(out, name);
        err = (CborError) !res;
        VERIFY_CBOR_SUCCESS(TAG, err, "Failed setting value");
        err = cbor_value_advance(container);
        VERIFY_CBOR_SUCCESS(TAG, err, "Failed advancing container");
        return err;
    }

    dimTotal = calcDimTotal(dimensions);
    allocSize = getAllocSize(type);
    arr = OICCalloc(dimTotal, allocSize);
    VERIFY_PARAM_NON_NULL(TAG, arr, "Array Parse allocation failed");

    res = OCParseArrayFillArray(container, dimensions, type, arr);
    VERIFY_CBOR_SUCCESS(TAG, err, "Failed parse array");

    switch (type)
    {
        case OCREP_PROP_INT:
            res = OCRepPayloadSetIntArrayAsOwner(out, name, (int64_t *)arr, dimensions);
            break;
        case OCREP_PROP_DOUBLE:
            res = OCRepPayloadSetDoubleArrayAsOwner(out, name, (double *)arr, dimensions);
            break;
        case OCREP_PROP_BOOL:
            res = OCRepPayloadSetBoolArrayAsOwner(out, name, (bool *)arr, dimensions);
            break;
        case OCREP_PROP_STRING:
            res = OCRepPayloadSetStringArrayAsOwner(out, name, (char **)arr, dimensions);
            break;
        case OCREP_PROP_BYTE_STRING:
            res = OCRepPayloadSetByteStringArrayAsOwner(out, name, (OCByteString *)arr, dimensions);
            break;
        case OCREP_PROP_OBJECT:
            res = OCRepPayloadSetPropObjectArrayAsOwner(out, name, (OCRepPayload**)arr, dimensions);
            break;
        default:
            OIC_LOG(ERROR, TAG, "Invalid Array type in Parse Array");
            break;
    }
    err = (CborError) !res;
    VERIFY_CBOR_SUCCESS(TAG, err, "Failed setting array parameter");
    return CborNoError;
exit:
    if (type == OCREP_PROP_STRING)
    {
        for(size_t i = 0; i < dimTotal; ++i)
        {
            OICFree(((char**)arr)[i]);
        }
    }
    if (type == OCREP_PROP_BYTE_STRING)
    {
        for(size_t i = 0; i < dimTotal; ++i)
        {
            OICFree(((OCByteString*)arr)[i].bytes);
        }
    }
    if (type == OCREP_PROP_OBJECT)
    {
        for(size_t i = 0; i < dimTotal; ++i)
        {
            OCRepPayloadDestroy(((OCRepPayload**)arr)[i]);
        }
    }
    OICFree(arr);
    return err;
}