xref: /haiku/src/add-ons/kernel/file_systems/ramfs/DataContainer.h (revision 125b262675217084e0c59014b4a98f724f1c4fb3)
1 /*
2  * Copyright 2007, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Copyright 2019, Haiku, Inc.
4  * All rights reserved. Distributed under the terms of the MIT license.
5  */
6 #ifndef DATA_CONTAINER_H
7 #define DATA_CONTAINER_H
8 
9 #include <OS.h>
10 
11 struct vm_page;
12 class VMCache;
13 class AllocationInfo;
14 class Volume;
15 
16 class DataContainer {
17 public:
18 	DataContainer(Volume *volume);
19 	virtual ~DataContainer();
20 
21 	status_t InitCheck() const;
22 
23 	Volume *GetVolume() const	{ return fVolume; }
24 
25 	status_t Resize(off_t newSize);
26 	off_t GetSize() const { return fSize; }
27 
28 	VMCache* GetCache();
29 
30 	virtual status_t ReadAt(off_t offset, void *buffer, size_t size,
31 							size_t *bytesRead);
32 	virtual status_t WriteAt(off_t offset, const void *buffer, size_t size,
33 							 size_t *bytesWritten);
34 
35 	// debugging
36 	void GetAllocationInfo(AllocationInfo &info);
37 
38 private:
39 	inline bool _RequiresCacheMode(size_t size);
40 	inline bool _IsCacheMode() const;
41 	status_t _SwitchToCacheMode();
42 	void _GetPages(off_t offset, off_t length, bool isWrite, vm_page** pages);
43 	void _PutPages(off_t offset, off_t length, vm_page** pages, bool success);
44 	status_t _DoCacheIO(const off_t offset, uint8* buffer, ssize_t length,
45 		size_t* bytesProcessed, bool isWrite);
46 
47 	inline int32 _CountBlocks() const;
48 
49 private:
50 	Volume				*fVolume;
51 	off_t				fSize;
52 	VMCache*			fCache;
53 
54 	uint8*				fSmallBuffer;
55 	off_t				fSmallBufferSize;
56 };
57 
58 #endif	// DATA_CONTAINER_H
59