Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: ngscopeclient/scopehal
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 1461e1858629
Choose a base ref
...
head repository: ngscopeclient/scopehal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 9edea105d887
Choose a head ref
  • 3 commits
  • 2 files changed
  • 1 contributor

Commits on Aug 8, 2020

  1. Copy the full SHA
    0cd0a99 View commit details
  2. Copy the full SHA
    28fdea0 View commit details
  3. Copy the full SHA
    9edea10 View commit details
Showing with 146 additions and 3 deletions.
  1. +136 −0 scopehal/AlignedAllocator.h
  2. +10 −3 scopehal/Waveform.h
136 changes: 136 additions & 0 deletions scopehal/AlignedAllocator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/***********************************************************************************************************************
* *
* ANTIKERNEL v0.1 *
* *
* Copyright (c) 2012-2020 Andrew D. Zonenberg *
* All rights reserved. *
* *
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the *
* following conditions are met: *
* *
* * Redistributions of source code must retain the above copyright notice, this list of conditions, and the *
* following disclaimer. *
* *
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the *
* following disclaimer in the documentation and/or other materials provided with the distribution. *
* *
* * Neither the name of the author nor the names of any contributors may be used to endorse or promote products *
* derived from this software without specific prior written permission. *
* *
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL *
* THE AUTHORS BE HELD LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR *
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
* POSSIBILITY OF SUCH DAMAGE. *
* *
***********************************************************************************************************************/

/**
@file
@author Andrew D. Zonenberg
@brief Declaration of AlignedAllocator
*/

#ifndef AlignedAllocator_h
#define AlignedAllocator_h

/**
@brief Aligned memory allocator for STL containers
Based on https://devblogs.microsoft.com/cppblog/the-mallocator/
*/
template <class T, size_t alignment>
class AlignedAllocator
{
public:

//Standard typedefs
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef T value_type;
typedef size_t size_type;
typedef ptrdiff_t difference_type;

//Overloads in case somebody overloaded the unary operator&()
//(which is pretty weird but the spec allows it)
T* address(T& rhs)
{ return &rhs; }

const T* address(T& rhs) const
{ return &rhs; }

size_t max_size() const
{ return (static_cast<size_t>(0) - static_cast<size_t>(1)) / sizeof(T); }

//RTTI and construction helpers
template<typename U>
struct rebind
{
typedef AlignedAllocator<U, alignment> other;
};

bool operator!=(const AlignedAllocator& other) const
{ return !(*this == other); }

//Look at that, a placement new! First time I've ever used one.
void construct(T* const p, const T& t) const
{ new( static_cast<void*>(p) ) T(t); }

void destroy(T* const p) const
{ p->~T(); }

//Check if this allocator is functionally equivalent to another
//We have no member variables, so all objects of the same type are equivalent
bool operator== (const AlignedAllocator& /* unused */) const
{ return true; }

//Default ctors, do nothing
AlignedAllocator()
{}

AlignedAllocator(const AlignedAllocator& /* unused */)
{}

template<typename U>
AlignedAllocator(const AlignedAllocator<U, alignment>&)
{}

~AlignedAllocator()
{}

//Now for the fun part
T* allocate(const size_t n) const
{
//Fail if we got an invalid size
if(n == 0)
return NULL;
if(n > max_size())
throw std::length_error("AlignedAllocator<T>::allocate(): requested size is too large, integer overflow?");

//Do the actual allocation
T* ret = static_cast<T*>(aligned_alloc(alignment, n*sizeof(T)));

//Error check
if(ret == NULL)
throw std::bad_alloc();

return ret;
}

void deallocate(T* const p, const size_t /*unused*/) const
{ free(p); }

//Not quite sure what this is for but apparently we need it?
template<typename U>
T* allocate(const size_t n, const U* /* const hint */ const)
{ return allocate(n); }

//Disallow assignment
AlignedAllocator& operator=(const AlignedAllocator&) = delete;
};

#endif
13 changes: 10 additions & 3 deletions scopehal/Waveform.h
Original file line number Diff line number Diff line change
@@ -37,6 +37,7 @@
#define Waveform_h

#include <vector>
#include <AlignedAllocator.h>

/**
@brief Wrapper around a primitive data type that has an empty default constructor.
@@ -113,10 +114,16 @@ class WaveformBase
double m_triggerPhase;

///@brief Start timestamps of each sample
std::vector<EmptyConstructorWrapper<int64_t>> m_offsets;
std::vector<
EmptyConstructorWrapper<int64_t>,
AlignedAllocator< EmptyConstructorWrapper<int64_t>, 64 >
> m_offsets;

///@brief Durations of each sample
std::vector<EmptyConstructorWrapper<int64_t>> m_durations;
std::vector<
EmptyConstructorWrapper<int64_t>,
AlignedAllocator< EmptyConstructorWrapper<int64_t>, 64 >
> m_durations;

virtual void clear()
{
@@ -152,7 +159,7 @@ class Waveform : public WaveformBase
public:

///@brief Sample data
std::vector<S> m_samples;
std::vector< S, AlignedAllocator<S, 64> > m_samples;

virtual void Resize(size_t size)
{