xref: /haiku/src/system/libroot/os/extended_system_info.cpp (revision 68ea01249e1e2088933cb12f9c28d4e5c5d1c9ef)
1 /*
2  * Copyright 2010, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include <extended_system_info.h>
8 #include <extended_system_info_defs.h>
9 
10 #include <syscalls.h>
11 #include <AutoDeleter.h>
12 
13 #include <util/KMessage.h>
14 
15 
16 namespace BPrivate {
17 
18 
19 status_t
20 get_extended_team_info(team_id teamID, uint32 flags, KMessage& info)
21 {
22 	// initial buffer size
23 	size_t bufferSize = 4096;
24 		// TODO: Pick it depending on the set flags.
25 
26 	while (true) {
27 		// allocate the buffer
28 		void* buffer = malloc(bufferSize);
29 		if (buffer == NULL)
30 			return B_NO_MEMORY;
31 		MemoryDeleter bufferDeleter(buffer);
32 
33 		// get the info
34 		size_t sizeNeeded;
35 		status_t error = _kern_get_extended_team_info(teamID, flags, buffer,
36 			bufferSize, &sizeNeeded);
37 		if (error == B_OK) {
38 			return info.SetTo((const void*)buffer, sizeNeeded,
39 				KMessage::KMESSAGE_CLONE_BUFFER);
40 				// TODO: Just transfer our buffer, if it isn't much larger.
41 		}
42 
43 		if (error != B_BUFFER_OVERFLOW)
44 			return error;
45 
46 		// The buffer was too small. Try again with a larger one.
47 		bufferSize = (sizeNeeded + 1023) / 1024 * 1024;
48 	}
49 }
50 
51 
52 }	// namespace BPrivate
53