1*43abf8a3SMichael Lotz /* 2*43abf8a3SMichael Lotz * Copyright 2005, Haiku. 3*43abf8a3SMichael Lotz * Distributed under the terms of the MIT License. 4*43abf8a3SMichael Lotz * 5*43abf8a3SMichael Lotz * Authors: 6*43abf8a3SMichael Lotz * Michael Lotz <mmlr@mlotz.ch> 7*43abf8a3SMichael Lotz */ 8*43abf8a3SMichael Lotz 9*43abf8a3SMichael Lotz /* A BMallocIO similar structure but with less overhead */ 10*43abf8a3SMichael Lotz 11*43abf8a3SMichael Lotz #ifndef _SIMPLE_MALLOC_IO_H_ 12*43abf8a3SMichael Lotz #define _SIMPLE_MALLOC_IO_H_ 13*43abf8a3SMichael Lotz 14*43abf8a3SMichael Lotz #include <malloc.h> 15*43abf8a3SMichael Lotz 16*43abf8a3SMichael Lotz namespace BPrivate { 17*43abf8a3SMichael Lotz 18*43abf8a3SMichael Lotz class BSimpleMallocIO { 19*43abf8a3SMichael Lotz public: BSimpleMallocIO(size_t size)20*43abf8a3SMichael Lotz BSimpleMallocIO(size_t size) 21*43abf8a3SMichael Lotz : fSize(size) 22*43abf8a3SMichael Lotz { 23*43abf8a3SMichael Lotz fBuffer = (char *)malloc(size); 24*43abf8a3SMichael Lotz } 25*43abf8a3SMichael Lotz ~BSimpleMallocIO()26*43abf8a3SMichael Lotz ~BSimpleMallocIO() 27*43abf8a3SMichael Lotz { 28*43abf8a3SMichael Lotz free(fBuffer); 29*43abf8a3SMichael Lotz } 30*43abf8a3SMichael Lotz Read(void * buffer)31*43abf8a3SMichael Lotz void Read(void *buffer) 32*43abf8a3SMichael Lotz { 33*43abf8a3SMichael Lotz memcpy(buffer, fBuffer, fSize); 34*43abf8a3SMichael Lotz } 35*43abf8a3SMichael Lotz Read(void * buffer,size_t size)36*43abf8a3SMichael Lotz void Read(void *buffer, size_t size) 37*43abf8a3SMichael Lotz { 38*43abf8a3SMichael Lotz memcpy(buffer, fBuffer, size); 39*43abf8a3SMichael Lotz } 40*43abf8a3SMichael Lotz ReadAt(off_t pos,void * buffer,size_t size)41*43abf8a3SMichael Lotz void ReadAt(off_t pos, void *buffer, size_t size) 42*43abf8a3SMichael Lotz { 43*43abf8a3SMichael Lotz memcpy(buffer, fBuffer + pos, size); 44*43abf8a3SMichael Lotz } 45*43abf8a3SMichael Lotz Write(const void * buffer)46*43abf8a3SMichael Lotz void Write(const void *buffer) 47*43abf8a3SMichael Lotz { 48*43abf8a3SMichael Lotz memcpy(fBuffer, buffer, fSize); 49*43abf8a3SMichael Lotz } 50*43abf8a3SMichael Lotz Write(const void * buffer,size_t size)51*43abf8a3SMichael Lotz void Write(const void *buffer, size_t size) 52*43abf8a3SMichael Lotz { 53*43abf8a3SMichael Lotz memcpy(fBuffer, buffer, size); 54*43abf8a3SMichael Lotz } 55*43abf8a3SMichael Lotz WriteAt(off_t pos,const void * buffer,size_t size)56*43abf8a3SMichael Lotz void WriteAt(off_t pos, const void *buffer, size_t size) 57*43abf8a3SMichael Lotz { 58*43abf8a3SMichael Lotz memcpy(fBuffer + pos, buffer, size); 59*43abf8a3SMichael Lotz } 60*43abf8a3SMichael Lotz SetSize(off_t size)61*43abf8a3SMichael Lotz status_t SetSize(off_t size) 62*43abf8a3SMichael Lotz { 63*43abf8a3SMichael Lotz fBuffer = (char *)realloc(fBuffer, size); 64*43abf8a3SMichael Lotz if (!fBuffer) 65*43abf8a3SMichael Lotz return B_NO_MEMORY; 66*43abf8a3SMichael Lotz 67*43abf8a3SMichael Lotz fSize = size; 68*43abf8a3SMichael Lotz return B_OK; 69*43abf8a3SMichael Lotz } 70*43abf8a3SMichael Lotz Buffer()71*43abf8a3SMichael Lotz char *Buffer() 72*43abf8a3SMichael Lotz { 73*43abf8a3SMichael Lotz return fBuffer; 74*43abf8a3SMichael Lotz } 75*43abf8a3SMichael Lotz BufferLength()76*43abf8a3SMichael Lotz size_t BufferLength() 77*43abf8a3SMichael Lotz { 78*43abf8a3SMichael Lotz return fSize; 79*43abf8a3SMichael Lotz } 80*43abf8a3SMichael Lotz 81*43abf8a3SMichael Lotz private: 82*43abf8a3SMichael Lotz char *fBuffer; 83*43abf8a3SMichael Lotz size_t fSize; 84*43abf8a3SMichael Lotz }; 85*43abf8a3SMichael Lotz 86*43abf8a3SMichael Lotz } // namespace BPivate 87*43abf8a3SMichael Lotz 88*43abf8a3SMichael Lotz #endif // _SIMPLE_MALLOC_IO_H_ 89