143abf8a3SMichael Lotz/*
243abf8a3SMichael Lotz * Copyright 2005, Haiku.
343abf8a3SMichael Lotz * Distributed under the terms of the MIT License.
443abf8a3SMichael Lotz *
543abf8a3SMichael Lotz * Authors:
643abf8a3SMichael Lotz *		Michael Lotz <mmlr@mlotz.ch>
743abf8a3SMichael Lotz */
843abf8a3SMichael Lotz
943abf8a3SMichael Lotz/* A BMallocIO similar structure but with less overhead */
1043abf8a3SMichael Lotz
1143abf8a3SMichael Lotz#ifndef _SIMPLE_MALLOC_IO_H_
1243abf8a3SMichael Lotz#define _SIMPLE_MALLOC_IO_H_
1343abf8a3SMichael Lotz
1443abf8a3SMichael Lotz#include <malloc.h>
1543abf8a3SMichael Lotz
1643abf8a3SMichael Lotznamespace BPrivate {
1743abf8a3SMichael Lotz
1843abf8a3SMichael Lotzclass BSimpleMallocIO {
1943abf8a3SMichael Lotzpublic:
2043abf8a3SMichael Lotz						BSimpleMallocIO(size_t size)
2143abf8a3SMichael Lotz							:	fSize(size)
2243abf8a3SMichael Lotz						{
2343abf8a3SMichael Lotz							fBuffer = (char *)malloc(size);
2443abf8a3SMichael Lotz						}
2543abf8a3SMichael Lotz
2643abf8a3SMichael Lotz						~BSimpleMallocIO()
2743abf8a3SMichael Lotz						{
2843abf8a3SMichael Lotz							free(fBuffer);
2943abf8a3SMichael Lotz						}
3043abf8a3SMichael Lotz
3143abf8a3SMichael Lotz		void			Read(void *buffer)
3243abf8a3SMichael Lotz						{
3343abf8a3SMichael Lotz							memcpy(buffer, fBuffer, fSize);
3443abf8a3SMichael Lotz						}
3543abf8a3SMichael Lotz
3643abf8a3SMichael Lotz		void			Read(void *buffer, size_t size)
3743abf8a3SMichael Lotz						{
3843abf8a3SMichael Lotz							memcpy(buffer, fBuffer, size);
3943abf8a3SMichael Lotz						}
4043abf8a3SMichael Lotz
4143abf8a3SMichael Lotz		void			ReadAt(off_t pos, void *buffer, size_t size)
4243abf8a3SMichael Lotz						{
4343abf8a3SMichael Lotz							memcpy(buffer, fBuffer + pos, size);
4443abf8a3SMichael Lotz						}
4543abf8a3SMichael Lotz
4643abf8a3SMichael Lotz		void			Write(const void *buffer)
4743abf8a3SMichael Lotz						{
4843abf8a3SMichael Lotz							memcpy(fBuffer, buffer, fSize);
4943abf8a3SMichael Lotz						}
5043abf8a3SMichael Lotz
5143abf8a3SMichael Lotz		void			Write(const void *buffer, size_t size)
5243abf8a3SMichael Lotz						{
5343abf8a3SMichael Lotz							memcpy(fBuffer, buffer, size);
5443abf8a3SMichael Lotz						}
5543abf8a3SMichael Lotz
5643abf8a3SMichael Lotz		void			WriteAt(off_t pos, const void *buffer, size_t size)
5743abf8a3SMichael Lotz						{
5843abf8a3SMichael Lotz							memcpy(fBuffer + pos, buffer, size);
5943abf8a3SMichael Lotz						}
6043abf8a3SMichael Lotz
6143abf8a3SMichael Lotz		status_t		SetSize(off_t size)
6243abf8a3SMichael Lotz						{
6343abf8a3SMichael Lotz							fBuffer = (char *)realloc(fBuffer, size);
6443abf8a3SMichael Lotz							if (!fBuffer)
6543abf8a3SMichael Lotz								return B_NO_MEMORY;
6643abf8a3SMichael Lotz
6743abf8a3SMichael Lotz							fSize = size;
6843abf8a3SMichael Lotz							return B_OK;
6943abf8a3SMichael Lotz						}
7043abf8a3SMichael Lotz
7143abf8a3SMichael Lotz		char			*Buffer()
7243abf8a3SMichael Lotz						{
7343abf8a3SMichael Lotz							return fBuffer;
7443abf8a3SMichael Lotz						}
7543abf8a3SMichael Lotz
7643abf8a3SMichael Lotz		size_t			BufferLength()
7743abf8a3SMichael Lotz						{
7843abf8a3SMichael Lotz							return fSize;
7943abf8a3SMichael Lotz						}
8043abf8a3SMichael Lotz
8143abf8a3SMichael Lotzprivate:
8243abf8a3SMichael Lotz		char			*fBuffer;
8343abf8a3SMichael Lotz		size_t			fSize;
8443abf8a3SMichael Lotz};
8543abf8a3SMichael Lotz
8643abf8a3SMichael Lotz} // namespace BPivate
8743abf8a3SMichael Lotz
8843abf8a3SMichael Lotz#endif // _SIMPLE_MALLOC_IO_H_