private SegmentInformation AllocateSegment(int size) { byte *start = (byte *)Marshal.AllocHGlobal(size).ToPointer(); byte *end = start + size; var segment = new SegmentInformation { Start = start, Current = start, End = end, CanDispose = true }; _wholeSegments.Add(segment); return(segment); }
private void AllocateExternalSegment(int size) { byte *start = (byte *)Marshal.AllocHGlobal(size).ToPointer(); byte *end = start + size; _externalCurrent = new SegmentInformation { Start = start, Current = start, End = end, CanDispose = true }; _externalAlignedSize = (sizeof(ByteStringStorage) + (sizeof(long) - sizeof(ByteStringStorage) % sizeof(long))); _externalCurrentLeft = (int)(_externalCurrent.End - _externalCurrent.Start) / _externalAlignedSize; _wholeSegments.Add(_externalCurrent); }
public static Texture CreateDummyHeightTextureDependentOnPixelPosition(SegmentInformation segmentInformation, float offset, float stepMultiplier) { var tex = new Texture2D(240, 240, TextureFormat.RFloat, false); float[] rawTextureData = new float[tex.width * tex.height]; for (int x = 0; x < tex.width; x++) { for (int y = 0; y < tex.height; y++) { rawTextureData[x + y * tex.width] = offset + ((x + y * 4) / 400f) * stepMultiplier; } } tex.LoadRawTextureData(CastUtils.ConvertFloatArrayToByte(rawTextureData)); tex.Apply(); return(tex); }
public ByteStringContext(int allocationBlockSize = DefaultAllocationBlockSizeInBytes) { if (allocationBlockSize < MinBlockSizeInBytes) { throw new ArgumentException($"It is not a good idea to allocate chunks of less than the {nameof(MinBlockSizeInBytes)} value of {MinBlockSizeInBytes}"); } this._allocationBlockSize = allocationBlockSize; this._wholeSegments = new List <SegmentInformation>(); this._internalReadyToUseMemorySegments = new List <SegmentInformation>(); this._internalReusableStringPool = new Stack <IntPtr> [LogMinBlockSize]; this._internalReusableStringPoolCount = new int[LogMinBlockSize]; this._internalCurrent = AllocateSegment(allocationBlockSize); this._externalCurrent = AllocateSegment(allocationBlockSize); this._externalStringPool = new Stack <IntPtr>(64); PrepareForValidation(); }
public ActionResult <SegmentInformation> Get(string City1, string City2, string parcelType, int parcelWeight, bool signed) { City1 = City1.ToLower(); City2 = City2.ToLower(); parcelType = parcelType.ToLower(); DatabaseLookupController databaseLookup = new DatabaseLookupController(); List <Edge> allEdges = databaseLookup.queryForMap().Value; List <string[]> prices = databaseLookup.queryForPricingData().Value; SegmentInformation newSeg = new SegmentInformation(); List <string> formattedPrices = new List <string>(); foreach (string[] price in prices) { string formattedPrice = Convert.ToString(price.GetValue(1)); formattedPrice.Replace(",", "."); formattedPrices.Add(formattedPrice); } foreach (Edge edge in allEdges) { string startCity = edge.fromCity.name.ToLower(); string endCity = edge.toCity.name.ToLower(); if (startCity.Equals(City1) || endCity.Equals(City1)) { if (startCity.Equals(City2) || endCity.Equals(City2)) { double priceOfDelivery = 0; Edge edgeForDelivery = edge; int nodesEdgeForDelivery = Convert.ToInt32(edgeForDelivery.node); if (parcelType.Equals("standard")) { priceOfDelivery = Convert.ToDouble(formattedPrices[0]) * nodesEdgeForDelivery; } else if (parcelType.Equals("weapons")) { priceOfDelivery = Convert.ToDouble(formattedPrices[0]) * Convert.ToDouble(formattedPrices[2]) * nodesEdgeForDelivery; } else if (parcelType.Equals("live animals")) { priceOfDelivery = Convert.ToDouble(formattedPrices[0]) * Convert.ToDouble(formattedPrices[3]) * nodesEdgeForDelivery; } else if (parcelType.Equals("cautious parcels")) { priceOfDelivery = Convert.ToDouble(formattedPrices[0]) * Convert.ToDouble(formattedPrices[4]) * nodesEdgeForDelivery; } else if (parcelType.Equals("refrigerated goods")) { priceOfDelivery = Convert.ToDouble(formattedPrices[0]) * Convert.ToDouble(formattedPrices[5]) * nodesEdgeForDelivery; } if (parcelWeight > 40) { priceOfDelivery = 0; } int timeOfDelivery = 4 * nodesEdgeForDelivery; newSeg.price = priceOfDelivery; newSeg.time = timeOfDelivery; if (newSeg.price == 0) { newSeg.available = false; } else { newSeg.available = true; } } } } if (newSeg == null) { return(NotFound()); } return(newSeg); }
public void Release(ref ByteString value) { Debug.Assert(value._pointer != null, "Pointer cannot be null. You have a defect in your code."); if (value._pointer == null) { return; } // We are releasing, therefore we should validate among other things if an immutable string changed and if we are the owners. ValidateAndUnregister(value); if (value.IsExternal) { // We release the pointer in the appropriate reuse pool. if (this._externalFastPoolCount < ExternalFastPoolSize) { // Release in the fast pool. this._externalFastPool[this._externalFastPoolCount++] = new IntPtr(value._pointer); } else { this._externalStringPool.Push(new IntPtr(value._pointer)); } } else { int reusablePoolIndex = GetPoolIndexForReuse(value._pointer->Size); if (value._pointer->Size <= MinBlockSizeInBytes) { Stack <IntPtr> pool = this._internalReusableStringPool[reusablePoolIndex]; if (pool == null) { pool = new Stack <IntPtr>(); this._internalReusableStringPool[reusablePoolIndex] = pool; } pool.Push(new IntPtr(value._pointer)); this._internalReusableStringPoolCount[reusablePoolIndex]++; } else // The released memory is big enough, we will just release it as a new segment. { byte *start = (byte *)value._pointer; byte *end = start + value._pointer->Size; var segment = new SegmentInformation { Start = start, Current = start, End = end, CanDispose = false }; _internalReadyToUseMemorySegments.Add(segment); } } #if VALIDATE // Setting the null key ensures that in between we can validate that no further deallocation // happens on this memory segment. value._pointer->Key = ByteStringStorage.NullKey; // Setting the length to zero ensures that the hash returns 0 and do not // fail with an AccessViolationException because there is garbage stored here. value._pointer->Length = 0; #endif // WE WANT it to happen, no matter what. value._pointer = null; }
private ByteString AllocateInternal(int length, ByteStringType type) { Debug.Assert((type & ByteStringType.External) == 0, "This allocation routine is only for use with internal storage byte strings."); type &= ~ByteStringType.External; // We are allocating internal, so we will force it (even if we are checking for it in debug). int allocationSize = length + sizeof(ByteStringStorage); // This is even bigger than the configured allocation block size. There is no reason why we shouldn't // allocate it directly. When released (if released) this will be reused as a segment, ensuring that the context // could handle that. if (allocationSize > _allocationBlockSize) { return(AllocateWholeSegment(length, type)); // We will pass the length because this is a whole allocated segment able to hold a length size ByteString. } int reusablePoolIndex = GetPoolIndexForReuse(allocationSize); int allocationUnit = Bits.NextPowerOf2(allocationSize); // The allocation unit is bigger than MinBlockSize (therefore it wont be 2^n aligned). // Then we will 64bits align the allocation. if (allocationUnit > MinBlockSizeInBytes) { allocationUnit += sizeof(long) - allocationUnit % sizeof(long); } // All allocation units are 32 bits aligned. If not we will have a performance issue. Debug.Assert(allocationUnit % sizeof(int) == 0); // If we can reuse... we retrieve those. if (allocationSize <= MinBlockSizeInBytes && _internalReusableStringPoolCount[reusablePoolIndex] != 0) { // This is a stack because hotter memory will be on top. Stack <IntPtr> pool = _internalReusableStringPool[reusablePoolIndex]; _internalReusableStringPoolCount[reusablePoolIndex]--; void *ptr = pool.Pop().ToPointer(); return(Create(ptr, length, allocationUnit, type)); } else { int currentSizeLeft = _internalCurrent.SizeLeft; if (allocationUnit > currentSizeLeft) // This shouldn't happen that much, if it does you should increase your default allocation block. { SegmentInformation segment = null; // We will try to find a hot segment with enough space if available. // Older (colder) segments are at the front of the list. That's why we would start scanning backwards. for (int i = _internalReadyToUseMemorySegments.Count - 1; i >= 0; i--) { var segmentValue = _internalReadyToUseMemorySegments[i]; if (segmentValue.SizeLeft >= allocationUnit) { // Put the last where this one is (if it is the same, this is a no-op) and remove it from the list. _internalReadyToUseMemorySegments[i] = _internalReadyToUseMemorySegments[_internalReadyToUseMemorySegments.Count - 1]; _internalReadyToUseMemorySegments.RemoveAt(_internalReadyToUseMemorySegments.Count - 1); segment = segmentValue; break; } } // If the size left is bigger than MinBlockSize, we release current as a reusable segment if (currentSizeLeft > MinBlockSizeInBytes) { byte *start = _internalCurrent.Current; byte *end = start + currentSizeLeft; _internalReadyToUseMemorySegments.Add(new SegmentInformation { Start = start, Current = start, End = end, CanDispose = false }); } else if (currentSizeLeft > sizeof(ByteStringType) + MinReusableBlockSizeInBytes) { // The memory chunk left is big enough to make sense to reuse it. reusablePoolIndex = GetPoolIndexForReservation(currentSizeLeft); Stack <IntPtr> pool = this._internalReusableStringPool[reusablePoolIndex]; if (pool == null) { pool = new Stack <IntPtr>(); this._internalReusableStringPool[reusablePoolIndex] = pool; } pool.Push(new IntPtr(_internalCurrent.Current)); this._internalReusableStringPoolCount[reusablePoolIndex]++; } // Use the segment and if there is no segment available that matches the request, just get a new one. this._internalCurrent = segment ?? AllocateSegment(_allocationBlockSize); } var byteString = Create(_internalCurrent.Current, length, allocationUnit, type); _internalCurrent.Current += byteString._pointer->Size; return(byteString); } }
public void SegmentStateChange(SegmentInformation segmentInfo) { }
public void RemoveSegment(SegmentInformation segmentInfo) { }
public void AddSegment(SegmentInformation segmentInfo) { var token = new SegmentGenerationProcessToken(SegmentGenerationProcessSituation.BeforeStartOfCreation, RequiredSegmentSituation.Filled); _executor.ExecuteSegmentAction(token, segmentInfo.SegmentAlignedPosition); }