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