Skip to content

Commit

Permalink
tempmem: final
Browse files Browse the repository at this point in the history
  • Loading branch information
Megatokio committed Nov 17, 2019
1 parent 981e491 commit bbbdb1a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 36 deletions.
58 changes: 27 additions & 31 deletions cstrings/tempmem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#define PTHREADS 1
#elif defined(TEMPMEM_USE_THREAD_LOCAL)
#define PTHREADS 0
elif __cplusplus < 201101
#elif __cplusplus < 201101
#define PTHREADS 1
#else
#define PTHREADS 0
Expand All @@ -34,8 +34,8 @@ elif __cplusplus < 201101
#endif

#define ALIGNMENT_MASK (sizeof(ptr)-1u)
#define MAX_REQUEST_SIZE 500
#define DATA_BLOCK_SIZE (8000)
#define MAX_REQUEST_SIZE 1000
#define DATA_BLOCK_SIZE 8000


#if PTHREADS
Expand Down Expand Up @@ -241,22 +241,18 @@ str dupstr (cstr s) noexcept
{
// Create copy of string in tempmem

if (!s||!*s) return emptystr;
size_t n = strlen(s);
str c = tempstr(uint(n));
memcpy(c,s,n);
return c;
if (unlikely(!s||!*s)) return emptystr;
str dest = temp<char>(uint(strlen(s))+1);
return strcpy(dest,s);
}

str xdupstr (cstr s) noexcept
{
// Create copy of string in the outer tempmem pool

if (!s||!*s) return emptystr;
size_t n = strlen(s);
str c = xtempstr(uint(n));
memcpy(c,s,n);
return c;
if (unlikely(!s||!*s)) return emptystr;
str dest = xtemp<char>(uint(strlen(s))+1);
return strcpy(dest,s);
}

str newstr (uint n) noexcept
Expand All @@ -276,32 +272,32 @@ str newcopy (cstr s) noexcept
// deallocate with delete[]
// returns NULL if source string is NULL

str c = nullptr;
if (s)
{
c = newstr(uint(strlen(s)));
strcpy(c,s);
}
return c;
if (s) return strcpy(new char[strlen(s)+1],s);
else return nullptr;
}

#ifndef NDEBUG
namespace TempMemTest
{
static_assert((sizeof(TempMemData)&(ALIGNMENT_MASK)) == 0, "");

static struct T
ON_INIT([]
{
T()
{
// check assumptions:
ptr p1 = new char[17], p2 = new char[15];
assert( (uintptr_t(p1)&ALIGNMENT_MASK)==0 );
assert( (uintptr_t(p2)&ALIGNMENT_MASK)==0 );
delete[] p1;
delete[] p2;
}
} dummy;
// check assumptions:
ptr p1 = new char[17], p2 = new char[15];
assert( (uintptr_t(p1)&ALIGNMENT_MASK)==0 );
assert( (uintptr_t(p2)&ALIGNMENT_MASK)==0 );
delete[] p1;
delete[] p2;

char s[] = {1,2,3,4};
p1 = strcpy(s,"abc");
assert(p1 == s); // must return dest addr
assert(s[3] == 0); // must copy final '\0'
p2 = newcopy(p1);
assert(p1!=p2&&eq(p2,p1));
delete[] p2;
});
}
#endif

Expand Down
17 changes: 12 additions & 5 deletions cstrings/tempmem.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@
Accessing Pools Directly
------------------------
• if you have created a local pool, then you can use the member functions allocStr(), allocMem()
and purge() directly with this instance.
• if you have created a local pool, then you can use the member functions alloc(), allocStr(),
allocMem() and purge() directly with this instance.
• TempMemPool::getPool() retrieves and may create the current pool, it never returns NULL.
Expand Down Expand Up @@ -99,8 +99,8 @@ class TempMemPool
TempMemPool () noexcept;
~TempMemPool () noexcept;

void purge () noexcept;
char* alloc (uint size) noexcept; // unaligned, uncleared
void purge () noexcept;
char* alloc (uint size) noexcept; // unaligned, uncleared

char* allocStr (uint len) noexcept
{
Expand Down Expand Up @@ -129,7 +129,7 @@ class TempMemPool
// get 'count' elements of type T aligned to sizeof(T)
// memory contents are not cleared

this->size &= ~(sizeof(T)-1);
this->size &= ~(sizeof(T)-1); // align
return reinterpret_cast<T*>(alloc(count*sizeof(T)));
}

Expand Down Expand Up @@ -163,6 +163,13 @@ template<typename T> inline T* temp (uint count) noexcept
return TempMemPool::getPool()->alloc<T>(count);
}

template<typename T> inline T* xtemp (uint count) noexcept
{
// allocate 'count' elements of type T in outer pool
// aligned to sizeof(T), not cleared

return TempMemPool::getXPool()->alloc<T>(count);
}

inline void __attribute((deprecated)) purgeTempMem () noexcept
{
Expand Down

0 comments on commit bbbdb1a

Please sign in to comment.