[dali_1.9.31] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / integration-api / lockless-buffer.cpp
index b704dbc..156ba6b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 namespace Dali
 {
-
 namespace Integration
 {
-
-LocklessBuffer::LocklessBuffer( size_t size )
-: mState( R0W1 ),
-  mSize( size )
+LocklessBuffer::LocklessBuffer(uint32_t size)
+: mState(R0W1),
+  mSize(size)
 {
   // allocate memory to speed up operation
-  mBuffer[0] = new unsigned char[size];
-  mBuffer[1] = new unsigned char[size];
+  mBuffer[0] = new uint8_t[size];
+  mBuffer[1] = new uint8_t[size];
 
-  memset (mBuffer[0], 0, size );
-  memset (mBuffer[1], 0, size );
+  memset(mBuffer[0], 0, size);
+  memset(mBuffer[1], 0, size);
 }
 
 LocklessBuffer::~LocklessBuffer()
@@ -45,40 +43,40 @@ LocklessBuffer::~LocklessBuffer()
   delete[] mBuffer[1];
 }
 
-void LocklessBuffer::Write( const unsigned char *src, size_t size )
+void LocklessBuffer::Write(const uint8_t* src, uint32_t size)
 {
-  DALI_ASSERT_ALWAYS( size <= mSize );
+  DALI_ASSERT_ALWAYS(size <= mSize);
 
   // set WRITING bit
-  BufferState currentState( __sync_fetch_and_or( &mState, WRITING ) );
-  DALI_ASSERT_DEBUG( !(currentState & WRITING_MASK) ); // WRITING bit should never be set when we get here
+  BufferState currentState(__sync_fetch_and_or(&mState, WRITING));
+  DALI_ASSERT_DEBUG(!(currentState & WRITING_MASK)); // WRITING bit should never be set when we get here
 
   // copy data to current write buffer, negate state to get actual index (recap: R0W1 = 0, R1W0 = 1)
   const unsigned int index = (currentState & WRITE_BUFFER_MASK);
-  memcpy( mBuffer[index], src, size );
+  memcpy(mBuffer[index], src, size);
 
   // unset WRITING bit, set UPDATED bit
-  BufferState checkState = __sync_val_compare_and_swap( &mState,
-                                                        static_cast<BufferState>(currentState | WRITING),
-                                                        static_cast<BufferState>(index | UPDATED) );
+  BufferState checkState = __sync_val_compare_and_swap(&mState,
+                                                       static_cast<BufferState>(currentState | WRITING),
+                                                       static_cast<BufferState>(index | UPDATED));
 
-  DALI_ASSERT_DEBUG( checkState & WRITING );
+  DALI_ASSERT_DEBUG(checkState & WRITING);
   (void)checkState; // Avoid unused variable warning
 }
 
-const unsigned char* LocklessBuffer::Read()
+const uint8_t* LocklessBuffer::Read()
 {
   // current state (only to avoid multiple memory reads with volatile variable)
-  BufferState currentState( mState );
-  BufferState currentWriteBuf( static_cast<BufferState>( currentState & WRITE_BUFFER_MASK ) );
+  BufferState currentState(mState);
+  BufferState currentWriteBuf(static_cast<BufferState>(currentState & WRITE_BUFFER_MASK));
 
-  if( currentState & UPDATED_MASK )
+  if(currentState & UPDATED_MASK)
   {
     // Try to swap buffers.
     // This will set mState to 1 if readbuffer 0 was updated, 0 if readbuffer 1 was updated and fail if WRITING is set
-    if( __sync_bool_compare_and_swap( &mState,
-                                      static_cast<BufferState>(currentWriteBuf | UPDATED),
-                                      static_cast<BufferState>(!currentWriteBuf) )  )
+    if(__sync_bool_compare_and_swap(&mState,
+                                    static_cast<BufferState>(currentWriteBuf | UPDATED),
+                                    static_cast<BufferState>(!currentWriteBuf)))
     {
       // swap successful
       return mBuffer[currentWriteBuf];
@@ -90,11 +88,11 @@ const unsigned char* LocklessBuffer::Read()
   return mBuffer[!currentWriteBuf];
 }
 
-unsigned int LocklessBuffer::GetSize() const
+uint32_t LocklessBuffer::GetSize() const
 {
   return static_cast<unsigned int>(mSize);
 }
 
-} // Internal
+} // namespace Integration
 
-} // Dali
+} // namespace Dali