diff --git a/UnitTests/Basics/vector-test.cpp b/UnitTests/Basics/vector-test.cpp index 5031c238ba..eeec312c45 100644 --- a/UnitTests/Basics/vector-test.cpp +++ b/UnitTests/Basics/vector-test.cpp @@ -538,7 +538,7 @@ BOOST_AUTO_TEST_CASE (tst_copy) { BOOST_CHECK_EQUAL(4, *(int*) TRI_AtVector(v2, 3)); BOOST_CHECK_EQUAL(r, TRI_AtVector(v2, 4)); - TRI_FreeVector(v2->_memoryZone, v2); + TRI_FreeVector(TRI_CORE_MEM_ZONE, v2); } //////////////////////////////////////////////////////////////////////////////// @@ -583,7 +583,7 @@ BOOST_AUTO_TEST_CASE (tst_value_modifications) { BOOST_CHECK_EQUAL(3, *(int*) TRI_AtVector(v2, 2)); BOOST_CHECK_EQUAL(4, *(int*) TRI_AtVector(v2, 3)); - TRI_FreeVector(v2->_memoryZone, v2); + TRI_FreeVector(TRI_CORE_MEM_ZONE, v2); } //////////////////////////////////////////////////////////////////////////////// diff --git a/lib/Basics/memory.cpp b/lib/Basics/memory.cpp index b7a0978319..e946f8f3e2 100644 --- a/lib/Basics/memory.cpp +++ b/lib/Basics/memory.cpp @@ -286,7 +286,38 @@ TRI_memory_zone_t* TRI_UNKNOWN_MEM_ZONE = &TriUnknownMemZone; // ----------------------------------------------------------------------------- //////////////////////////////////////////////////////////////////////////////// -/// @brief generates an error message +/// @brief returns the memory zone for a zone id +//////////////////////////////////////////////////////////////////////////////// + +TRI_memory_zone_t* TRI_MemoryZone (TRI_memory_zone_id_t zid) { + if (zid == 0) { + return TRI_CORE_MEM_ZONE; + } + else if (zid != 1) { + TRI_ASSERT(false); + // fall-through for non-debug builds + } + return TRI_UNKNOWN_MEM_ZONE; +} + +//////////////////////////////////////////////////////////////////////////////// +/// @brief returns the memory zone id for a zone +//////////////////////////////////////////////////////////////////////////////// + +TRI_memory_zone_id_t TRI_MemoryZoneId (TRI_memory_zone_t const* zone) { + if (zone == TRI_CORE_MEM_ZONE) { + return TRI_CORE_MEM_ZONE->_zid; + } + else if (zone != TRI_UNKNOWN_MEM_ZONE) { + TRI_ASSERT(false); + // fall-through for non-debug builds + } + + return TRI_UNKNOWN_MEM_ZONE->_zid; +} + +//////////////////////////////////////////////////////////////////////////////// +/// @brief returns the unknown memory zone //////////////////////////////////////////////////////////////////////////////// #ifdef TRI_ENABLE_MAINTAINER_MODE diff --git a/lib/Basics/memory.h b/lib/Basics/memory.h index ac13600784..ad1cd18f21 100644 --- a/lib/Basics/memory.h +++ b/lib/Basics/memory.h @@ -38,15 +38,20 @@ // --SECTION-- public types // ----------------------------------------------------------------------------- +//////////////////////////////////////////////////////////////////////////////// +/// @brief memory zone id +//////////////////////////////////////////////////////////////////////////////// + +typedef uint32_t TRI_memory_zone_id_t; + //////////////////////////////////////////////////////////////////////////////// /// @brief memory zone //////////////////////////////////////////////////////////////////////////////// typedef struct TRI_memory_zone_s { - uint32_t _zid; - bool _failed; - bool _failable; - void* _impl; + TRI_memory_zone_id_t _zid; + bool _failed; + bool _failable; } TRI_memory_zone_t; @@ -60,6 +65,18 @@ TRI_memory_zone_t; extern TRI_memory_zone_t* TRI_CORE_MEM_ZONE; +//////////////////////////////////////////////////////////////////////////////// +/// @brief returns the memory zone for a zone id +//////////////////////////////////////////////////////////////////////////////// + +TRI_memory_zone_t* TRI_MemoryZone (TRI_memory_zone_id_t); + +//////////////////////////////////////////////////////////////////////////////// +/// @brief returns the memory zone id for a zone +//////////////////////////////////////////////////////////////////////////////// + +TRI_memory_zone_id_t TRI_MemoryZoneId (TRI_memory_zone_t const*); + //////////////////////////////////////////////////////////////////////////////// /// @brief unknown memory zone //////////////////////////////////////////////////////////////////////////////// diff --git a/lib/Basics/vector.cpp b/lib/Basics/vector.cpp index 3bd4a2bd6e..0d4ba01772 100644 --- a/lib/Basics/vector.cpp +++ b/lib/Basics/vector.cpp @@ -54,8 +54,8 @@ //////////////////////////////////////////////////////////////////////////////// void TRI_InitVector (TRI_vector_t* vector, TRI_memory_zone_t* zone, size_t elementSize) { - vector->_memoryZone = zone; vector->_buffer = nullptr; + vector->_memoryZoneX = TRI_MemoryZoneId(zone); vector->_lengthX = 0; vector->_capacityX = 0; vector->_elementSizeX = static_cast(elementSize); @@ -73,7 +73,7 @@ int TRI_InitVector2 (TRI_vector_t* vector, TRI_InitVector(vector, zone, elementSize); if (initialCapacity != 0) { - vector->_buffer = static_cast(TRI_Allocate(vector->_memoryZone, (initialCapacity * static_cast(vector->_elementSizeX)), false)); + vector->_buffer = static_cast(TRI_Allocate(TRI_MemoryZone(vector->_memoryZoneX), (initialCapacity * static_cast(vector->_elementSizeX)), false)); if (vector->_buffer == nullptr) { return TRI_ERROR_OUT_OF_MEMORY; @@ -91,7 +91,7 @@ int TRI_InitVector2 (TRI_vector_t* vector, void TRI_DestroyVector (TRI_vector_t* vector) { if (vector->_buffer != nullptr) { - TRI_Free(vector->_memoryZone, vector->_buffer); + TRI_Free(TRI_MemoryZone(vector->_memoryZoneX), vector->_buffer); } } @@ -126,7 +126,7 @@ int TRI_ReserveVector (TRI_vector_t* vector, newSize = (size_t) (1 + GROW_FACTOR * newSize); } - auto newBuffer = static_cast(TRI_Reallocate(vector->_memoryZone, vector->_buffer, newSize * static_cast(vector->_elementSizeX))); + auto newBuffer = static_cast(TRI_Reallocate(TRI_MemoryZone(vector->_memoryZoneX), vector->_buffer, newSize * static_cast(vector->_elementSizeX))); if (newBuffer == nullptr) { return TRI_ERROR_OUT_OF_MEMORY; @@ -151,12 +151,12 @@ TRI_vector_t* TRI_CopyVector (TRI_memory_zone_t* zone, } copy->_elementSizeX = vector->_elementSizeX; - copy->_memoryZone = zone; + copy->_memoryZoneX = TRI_MemoryZoneId(zone); if (vector->_capacityX == 0) { - copy->_buffer = nullptr; - copy->_lengthX = 0; - copy->_capacityX = 0; + copy->_buffer = nullptr; + copy->_lengthX = 0; + copy->_capacityX = 0; } else { copy->_buffer = static_cast(TRI_Allocate(zone, static_cast(vector->_lengthX * vector->_elementSizeX), false)); @@ -186,7 +186,7 @@ int TRI_CopyDataVector (TRI_vector_t* dst, } if (dst->_buffer != nullptr) { - TRI_Free(dst->_memoryZone, dst->_buffer); + TRI_Free(TRI_MemoryZone(dst->_memoryZoneX), dst->_buffer); dst->_buffer = nullptr; } @@ -194,7 +194,7 @@ int TRI_CopyDataVector (TRI_vector_t* dst, dst->_lengthX = 0; if (source->_lengthX > 0) { - dst->_buffer = static_cast(TRI_Allocate(dst->_memoryZone, static_cast(source->_lengthX * source->_elementSizeX), false)); + dst->_buffer = static_cast(TRI_Allocate(TRI_MemoryZone(dst->_memoryZoneX), static_cast(source->_lengthX * source->_elementSizeX), false)); if (dst->_buffer == nullptr) { return TRI_ERROR_OUT_OF_MEMORY; @@ -244,7 +244,7 @@ int TRI_ResizeVector (TRI_vector_t* vector, if (static_cast(vector->_capacityX) < n) { size_t newSize = n; - auto newBuffer = static_cast(TRI_Reallocate(vector->_memoryZone, vector->_buffer, newSize * static_cast(vector->_elementSizeX))); + auto newBuffer = static_cast(TRI_Reallocate(TRI_MemoryZone(vector->_memoryZoneX), vector->_buffer, newSize * static_cast(vector->_elementSizeX))); if (newBuffer == nullptr) { return TRI_ERROR_OUT_OF_MEMORY; @@ -268,7 +268,7 @@ int TRI_PushBackVector (TRI_vector_t* vector, void const* element) { if (vector->_lengthX == vector->_capacityX) { size_t newSize = (size_t) (1 + (GROW_FACTOR * static_cast(vector->_capacityX))); - auto newBuffer = static_cast(TRI_Reallocate(vector->_memoryZone, vector->_buffer, newSize * elementSize)); + auto newBuffer = static_cast(TRI_Reallocate(TRI_MemoryZone(vector->_memoryZoneX), vector->_buffer, newSize * elementSize)); if (newBuffer == nullptr) { return TRI_ERROR_OUT_OF_MEMORY; @@ -371,7 +371,7 @@ int TRI_InsertVector (TRI_vector_t* vector, void const* element, size_t n) { TRI_ASSERT(newSize > n); - auto newBuffer = static_cast(TRI_Allocate(vector->_memoryZone, newSize * elementSize, false)); + auto newBuffer = static_cast(TRI_Allocate(TRI_MemoryZone(vector->_memoryZoneX), newSize * elementSize, false)); if (newBuffer == nullptr) { return TRI_ERROR_OUT_OF_MEMORY; @@ -381,7 +381,7 @@ int TRI_InsertVector (TRI_vector_t* vector, void const* element, size_t n) { if (vector->_buffer != nullptr) { memcpy(newBuffer, vector->_buffer, static_cast(vector->_lengthX) * elementSize); - TRI_Free(vector->_memoryZone, vector->_buffer); + TRI_Free(TRI_MemoryZone(vector->_memoryZoneX), vector->_buffer); } vector->_buffer = newBuffer; diff --git a/lib/Basics/vector.h b/lib/Basics/vector.h index 4d5b479331..24b010aed9 100644 --- a/lib/Basics/vector.h +++ b/lib/Basics/vector.h @@ -45,14 +45,18 @@ //////////////////////////////////////////////////////////////////////////////// typedef struct TRI_vector_s { - char* _buffer; - TRI_memory_zone_t* _memoryZone; - uint32_t _lengthX; // private. do not access from outside! - uint32_t _capacityX; // private. do not access from outside! - uint32_t _elementSizeX; // private. do not access from outside! + char* _buffer; + TRI_memory_zone_id_t _memoryZoneX; // private. do not access from outside + uint32_t _lengthX; // private. do not access from outside! + uint32_t _capacityX; // private. do not access from outside! + uint32_t _elementSizeX; // private. do not access from outside! } TRI_vector_t; +static_assert(sizeof(TRI_memory_zone_id_t) == 4, "invalid size for TRI_memory_zone_id_t"); + +static_assert(sizeof(TRI_vector_t) == sizeof(char*) + 4 * sizeof(uint32_t), "invalid size for TRI_vector_t"); + // ----------------------------------------------------------------------------- // --SECTION-- constructors and destructors // -----------------------------------------------------------------------------