From 23eb8c4250d9dc763978e5fbddc1c7e784839078 Mon Sep 17 00:00:00 2001 From: lloyd Date: Tue, 7 Sep 2010 22:58:45 +0000 Subject: 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. --- src/alloc/secmem.h | 50 ++++++++++---------------------------------------- 1 file changed, 10 insertions(+), 40 deletions(-) (limited to 'src/alloc/secmem.h') 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. @@ -161,16 +161,6 @@ class MemoryRegion void append(const MemoryRegion& other) { 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. */ @@ -182,21 +172,13 @@ 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. */ @@ -240,30 +222,18 @@ class MemoryRegion }; /* -* Create a new buffer +* Change the size of the buffer */ template void MemoryRegion::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 -void MemoryRegion::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); -- cgit v1.2.3