Sign in to follow this  

adt buffer

This topic is 4813 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi I have coded an adt_buffer where you can store any datatype in. This buffer contains one byte buffer and two pointer insert_offset and get_offet. insert_offset stores the address where the buffer will store data the next store function call and get_offset is where you are fetching data on the next get function call. Both these pointers move along the buffer. I’m thinking for making the buffer circular that will remove any move data operations. Please comment my buffer, will this buffer work on all CPU platforms? If one inserts a 32 bit value in the buffer should the buffer insert it at a 32 bit aligns?
#include "adt_buffer.h"

int main(void)
{	
	adt::buffer b(4096);

	int a = 1;
	b.add_string(L"hello world");
	b.add_string("hello");
	b.add(a);
	
	UNICODE *str = b.get_string<UNICODE>();
	MessageBoxW(0,str,str,MB_OK);
	char *str2 = b.get_string<char>();
	MessageBox(0,str2,str2,MB_OK);
	int c = b.get_<int>();

	return 0;
}

*/
#ifndef adt_buffer_h
#define adt_buffer_h


typedef int BOOL;

#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif

namespace adt
{
	class buffer
	{
	public:
		/*! add data
		*/
		template <typename TYPE>
		buffer &add(TYPE item)
		{
			int size = sizeof(TYPE);
			memcpy(m_insert_offset,&item,size);
			m_insert_offset+=size;
			return *this;
		}

		template <typename TYPE>
		buffer &add_string(TYPE *item)
		{
			TYPE *p = item;
			int size = sizeof(TYPE);
			while(*p)
			{++p;}
			
			size+= ((int)(p - item))*sizeof(TYPE);
			memcpy(m_insert_offset,item,size);
			m_insert_offset+=size;
			return *this;
		}


		template <typename TYPE>
		TYPE get_()
		{
			char *p = m_get_offset;
			m_get_offset+= sizeof(TYPE);
			return *(TYPE *)p;
		}
		template <typename TYPE>
		TYPE *get_string()
		{
			int tsize = sizeof(TYPE);
			TYPE *p = (TYPE *)m_get_offset;
			int size = tsize;
			while(*p && p != m_end_offset)
			{++p;}
			size+=((int)((char *)p - m_get_offset)*tsize);
			p  = (TYPE *)m_get_offset;
			m_get_offset+=size;
			return p;
		}

		buffer &add(void *p,int size);
		void addsize(int size);
		void *insert_offset();
		/*! returns pointer to the begining of the buffer
		*/
		void *data();
		/*! returns current data capacity
		*/
		int sizeleft();
		int datasize();

		/*! returns total buffer capacity
		*/
		int totalsize();
		buffer(char *provide_buffer,int size);
		/*! allocated memory
		*/
		buffer(int size);
		~buffer();

		//protected:
		BOOL alloc;
		char *m_buf;
		char *m_end_offset;
		char *m_insert_offset;
		char *m_get_offset;
		int m_tsize;
	};

}
#endif




#include "adt_buffer.h"


#include <memory.h>

namespace adt
{

	buffer &buffer::add(void *p,int size)
	{
		memcpy(m_insert_offset,p,size);
		m_insert_offset+=size;
		return *this;
	}
	
	void buffer::addsize(int size)
	{m_insert_offset+=size;}

	void *buffer::insert_offset()
	{return m_insert_offset;}

	void *buffer::data()
	{return (void *)m_buf;}

	int buffer::sizeleft()
	{return (m_tsize-((int)(m_insert_offset-m_buf)));}

	int buffer::datasize()
	{return (int)(m_insert_offset-m_buf);}

	int buffer::totalsize()
	{return m_tsize;}

	buffer::buffer(char *provide_buffer,int size):alloc(FALSE),m_buf(provide_buffer),m_insert_offset(m_buf),m_get_offset(m_insert_offset),m_tsize(size)
	{
		m_end_offset = (m_buf+size);
	}

	buffer::buffer(int size):alloc(TRUE)
	{
		m_buf = new char[size];
		m_get_offset = m_insert_offset = m_buf;
		m_end_offset = (m_buf+size);
		m_tsize = size;
	}

	buffer::~buffer()
	{
		if(alloc) delete m_buf;
	}
}//end namespace adt


Share this post


Link to post
Share on other sites
Yes, you must make sure that you access 32-bit aligned addresses when working with 32-bits of data. i.e. the lowest two bits of the memory address must be zero. Otherwise your program WILL crash on some platforms.

Share this post


Link to post
Share on other sites

This topic is 4813 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this