diff options
author | lloyd <[email protected]> | 2010-09-07 22:58:45 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2010-09-07 22:58:45 +0000 |
commit | 23eb8c4250d9dc763978e5fbddc1c7e784839078 (patch) | |
tree | 707c83ac6e03fe8f35a2b2cbee944430166d280b /src/alloc | |
parent | 3c43fb5878bcf585dd32b1a74ae4dd733a89ac05 (diff) |
Realization while thinking about the recently added truncate: in a STL
container like vector, truncate is simply resize, but what
MemoryRegion called resize will zap the entire contents, and then what
was resize was called grow_to. This is really problematic in terms of
the goal of replacing MemoryRegion with a vector with a custom
allocator.
In this checkin:
- Remove MemoryRegion::grow_to and MemoryRegion::truncate
- Change the semantics of MemoryRegion::resize to change the size
while keeping any current contents intact (up to the new size),
zero initializing any new values.
Unrelated, just noticed the lack while I was in there, add a version
of CryptoBox::decrypt taking a std::string for the input.
Diffstat (limited to 'src/alloc')
-rw-r--r-- | src/alloc/secmem.h | 50 |
1 files changed, 10 insertions, 40 deletions
diff --git a/src/alloc/secmem.h b/src/alloc/secmem.h index f14e2a9db..aae1634d3 100644 --- a/src/alloc/secmem.h +++ b/src/alloc/secmem.h @@ -146,7 +146,7 @@ class MemoryRegion * @param n the size of the array data */ void append(const T data[], u32bit n) - { grow_to(size()+n); copy(size() - n, data, n); } + { resize(size()+n); copy(size() - n, data, n); } /** * Append a single element. @@ -162,16 +162,6 @@ class MemoryRegion { append(other.begin(), other.size()); } /** - * Truncate the buffer to at most n elements - * @param n the length of the resulting buffer - */ - void truncate(u32bit n) - { - if(n < used) - used = n; - } - - /** * Zeroise the bytes of this buffer. The length remains unchanged. */ void clear() { clear_mem(buf, allocated); } @@ -182,22 +172,14 @@ class MemoryRegion void destroy() { resize(0); } /** - * Reset this buffer to a buffer of specified length. The content will be - * initialized to zero bytes. - * @param n the new length of the buffer + * Inserts or erases elements at the end such that the size + * becomes n, leaving elements in the range 0...n unmodified if + * set or otherwise zero-initialized + * @param n length of the new buffer */ void resize(u32bit n); /** - * Change the size to n elements. If n is >= size(), preexisting - * elements remain unchanged, with later elements - * zero-initialized. If n < size(), then the last (size() - N) - * elements are removed. - * @param n the new size - */ - void grow_to(u32bit n); - - /** * Swap this buffer with another object. */ void swap(MemoryRegion<T>& other); @@ -240,30 +222,18 @@ class MemoryRegion }; /* -* Create a new buffer +* Change the size of the buffer */ template<typename T> void MemoryRegion<T>::resize(u32bit n) { - if(n <= allocated) { clear(); used = n; return; } - deallocate(buf, allocated); - buf = allocate(n); - allocated = used = n; - } - -/* -* Increase the size of the buffer -*/ -template<typename T> -void MemoryRegion<T>::grow_to(u32bit n) - { - if(n > used && n <= allocated) + if(n <= allocated) { - clear_mem(buf + used, n - used); + u32bit zap = std::min(used, n); + clear_mem(buf + zap, allocated - zap); used = n; - return; } - else if(n > allocated) + else { T* new_buf = allocate(n); copy_mem(new_buf, buf, used); |