int TSIStackAllocator::end_batch_alloc(const bool rollback)
 {
   int err = OB_SUCCESS;
   StackAllocator* allocator = NULL;
   if (NULL == (allocator = get()))
   {
     err = OB_MEM_OVERFLOW;
     TBSYS_LOG(ERROR, "get_tsi_allocator() failed");
   }
   else
   {
     err = allocator->end_batch_alloc(rollback);
   }
   return err;
 }
 int TSIStackAllocator::start_batch_alloc()
 {
   int err = OB_SUCCESS;
   StackAllocator* allocator = NULL;
   if (NULL == (allocator = get()))
   {
     err = OB_MEM_OVERFLOW;
     TBSYS_LOG(ERROR, "get_tsi_allocator() failed");
   }
   else
   {
     err = allocator->start_batch_alloc();
   }
   return err;
 }
 int TSIStackAllocator::reserve(const int64_t size)
 {
   int err = OB_SUCCESS;
   StackAllocator* allocator = NULL;
   if (NULL == (allocator = get()))
   {
     err = OB_MEM_OVERFLOW;
     TBSYS_LOG(ERROR, "get_tsi_allocator() failed");
   }
   else
   {
     err = allocator->reserve(size);
   }
   return err;
 }
 void* TSIStackAllocator::alloc(const int64_t size)
 {
   int err = OB_SUCCESS;
   StackAllocator* allocator = NULL;
   void* p = NULL;
   if (NULL == (allocator = get()))
   {
     err = OB_MEM_OVERFLOW;
     TBSYS_LOG(ERROR, "get_tsi_allocator() failed");
   }
   else if (NULL == (p = allocator->alloc(size)))
   {
     err = OB_MEM_OVERFLOW;
     TBSYS_LOG(ERROR, "allocator->alloc(%ld)=>NULL", size);
   }
   return p;
 }
Exemplo n.º 5
0
TEST(StackAllocator, marker) {
  StackAllocator allocator;
  void *ptr1 = allocator.Malloc(10);
  void *ptr2 = allocator.Malloc(20);
  StackAllocator::Marker makrer_a = allocator.GetMarker();
  int remain_size_1 = allocator.remain_size();
  void *ptr3 = allocator.Malloc(30);
  void *ptr4 = allocator.Malloc(400);
  allocator.FreeToMarker(makrer_a);
  EXPECT_EQ(remain_size_1, allocator.remain_size());
}
Exemplo n.º 6
0
TEST(StackAllocator, basic_malloc_free) {
  StackAllocator allocator;
  EXPECT_EQ(allocator.stack_size(), allocator.remain_size());
  const int header_size = allocator.GetAllocHeaderSize();

  void *ptr1 = allocator.Malloc(10);
  EXPECT_EQ(allocator.stack_size() - (10 + header_size), allocator.remain_size());
  
  void *ptr2 = allocator.Malloc(20);
  EXPECT_EQ(allocator.stack_size() - (20 + 10 + header_size * 2), allocator.remain_size());

  EXPECT_NE(20, (uchar*)ptr2 - (uchar*)ptr1);

  allocator.Free(ptr2);
  EXPECT_EQ(allocator.stack_size() - (10 + header_size), allocator.remain_size());

  allocator.Free(ptr1);
  EXPECT_EQ(allocator.stack_size(), allocator.remain_size());
}
Exemplo n.º 7
0
int OOBase::Environment::get_block(const env_table_t& tabEnv, ScopedArrayPtr<wchar_t>& ptr)
{
	if (tabEnv.empty())
		return 0;

	// Copy and widen to UNICODE
	typedef SharedPtr<wchar_t> temp_wchar_t;

	// Sort environment block - UNICODE, no-locale, case-insensitive (from MSDN)
	struct env_sort
	{
		bool operator ()(const OOBase::SharedPtr<wchar_t>& s1, const OOBase::SharedPtr<wchar_t>& s2) const
		{
			return (_wcsicmp(s1.get(),s2.get()) < 0);
		}
	};

	StackAllocator<1024> allocator;
	Table<temp_wchar_t,temp_wchar_t,env_sort,AllocatorInstance> wenv(env_sort(),allocator);

	size_t total_size = 0;
	for (env_table_t::const_iterator i=tabEnv.begin();i;++i)
	{
		int err = Win32::utf8_to_wchar_t(i->first.c_str(),ptr);
		if (!err)
		{
			// Include \0 and optionally '=' length
			size_t len = wcslen(ptr.get());
			total_size += len + 1;

			temp_wchar_t key = make_shared(static_cast<wchar_t*>(allocator.allocate(len+1,alignment_of<wchar_t>::value)),allocator);
			if (!key)
				return ERROR_OUTOFMEMORY;

			wcscpy(key.get(),ptr.get());

			err = Win32::utf8_to_wchar_t(i->second.c_str(),ptr);
			if (!err)
			{
				temp_wchar_t value;

				len = wcslen(ptr.get());
				if (len)
				{
					total_size += len + 1;

					value = make_shared(static_cast<wchar_t*>(allocator.allocate(len+1,alignment_of<wchar_t>::value)),allocator);
					if (!value)
						return ERROR_OUTOFMEMORY;

					wcscpy(value.get(),ptr.get());
				}

				if (!wenv.insert(key,value))
					return ERROR_OUTOFMEMORY;
			}
		}
		
		if (err)
			return err;
	}

	// And now copy into one giant block
	if (!ptr.resize(total_size + 2))
		return ERROR_OUTOFMEMORY;

	wchar_t* pout = ptr.get();
	for (Table<temp_wchar_t,temp_wchar_t,env_sort,AllocatorInstance>::iterator i=wenv.begin();i;++i)
	{
		const wchar_t* p = i->first.get();

		while (*p != L'\0')
			*pout++ = *p++;

		p = i->second.get();
		if (p && *p != L'\0')
		{
			*pout++ = L'=';

			while (*p != L'\0')
				*pout++ = *p++;
		}

		*pout++ = L'\0';
	}

	// Terminate with \0
	*pout++ = L'\0';
	*pout++ = L'\0';

	return 0;
}
Exemplo n.º 8
0
 ~stack_tuple()
 {
     stack_alloc.deallocate( stack_ctx);
 }
Exemplo n.º 9
0
 stack_tuple( StackAllocator const& stack_alloc_, std::size_t size) :
     stack_ctx(),
     stack_alloc( stack_alloc_)
 {
     stack_alloc.allocate( stack_ctx, size);
 }